Untitled

 avatar
unknown
plain_text
2 months ago
2.1 kB
2
Indexable
import google.generativeai as genai
import pathlib
import os
from dotenv import load_dotenv
import mimetypes
from google.generativeai.types import GenerationConfig

# Load environment variables from .env file
load_dotenv()

# Configure the API key
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
genai.configure(api_key=GOOGLE_API_KEY)

def upload_and_analyze_video(video_path, prompt):
    """
    Uploads a video to Gemini 1.5 Flash and gets a response based on the prompt.

    Args:
        video_path: The local path to the video file.
        prompt: The text prompt to send to the model along with the video.

    Returns:
        The response from the Gemini model or an error message.
    """

    if not pathlib.Path(video_path).exists():
        return f"Error: Video file not found at {video_path}"

    # Use mimetypes to guess the MIME type
    mime_type, _ = mimetypes.guess_type(video_path)
    if mime_type is None or not mime_type.startswith('video/'):
        mime_type = 'video/mp4'  # Default to MP4 if unsure

    video_file = pathlib.Path(video_path)
    video_data = {
        "mime_type": mime_type,
        "data": video_file.read_bytes()
    }

    # --- Generation Configuration ---
    generation_config = GenerationConfig(
        candidate_count=1,
        stop_sequences=["<end>"],
        max_output_tokens=500,
        temperature=0.7,
        top_p=0.8,
        top_k=40
    )

    # --- Initialize the Model (without safety_settings) ---
    model = genai.GenerativeModel(
        model_name="gemini-1.5-flash",
        generation_config=generation_config
    )

    try:
        # --- Generate Content ---
        response = model.generate_content(
            [prompt, video_data]
        )
        return response.text
    except Exception as e:
        return f"Error during inference: {e}"

if __name__ == "__main__":
    video_path = "/path/to/your/video.mp4"  # Replace with your video path
    prompt = "Describe the content of this video and identify any objects. Stop the description when you see the <end> tag."

    result = upload_and_analyze_video(video_path, prompt)
    print(result)


Leave a Comment