Spaces:
Sleeping
Sleeping
from typing import Optional | |
from google import genai | |
from google.genai import types | |
import os | |
def analyze_video(video_url: str, analysis_prompt: Optional[str] = None) -> str: | |
""" | |
Analyze a YouTube video using Google Gemini API. Creates summary of the video content. | |
Args: | |
video_url (str): Url path to a YouTube video to anlyze | |
analysis_prompt (Optional[str]): Optional prompt for specific analysis focus | |
Returns: | |
str: Text containing analysis results | |
""" | |
try: | |
# Initialize Google Gen client | |
gemini_llm = genai.Client(api_key=os.getenv("GOOGLE_API_KEY")) | |
print(f"Analyzing video from URL {video_url}") | |
text=analysis_prompt or "Provide a detailed description of this video." | |
# Get response from Gemini Flash 2.0 Vision | |
response = gemini_llm.models.generate_content( | |
model='models/gemini-2.0-flash', | |
contents=types.Content( | |
parts=[ | |
types.Part( | |
file_data=types.FileData(file_uri=video_url) | |
), | |
types.Part(text=text) | |
] | |
) | |
) | |
print(response.text) | |
return response.text | |
except Exception as e: | |
return {"error": f"Error analyzing video: {str(e)}"} |