Sborole-Final-Assignment / tools /ReadVideoTool.py
Sborole's picture
Update tools/ReadVideoTool.py
29eed7b verified
from smolagents import Tool
from google import genai
import requests
import os
class ReadVideoTool(Tool):
name = "video_inspector"
description = "Analyze video content to answer questions."
inputs = {
"video_url": {"type": "string", "description": "URL of video."},
"user_query": {"type": "string", "description": "Question about video."}
}
output_type = "string"
MODEL_PRIORITY = ["gemini-2.5-flash", "gemini-2.5-flash-other-project"]
def forward(self, video_url: str, user_query: str) -> str:
for model_name in self.MODEL_PRIORITY:
# 2. Build the request with the current model
req = {
'model': f'models/{model_name}',
# ... rest of your contents part
}
# 3. Define the URL for the current model
url = f'https://generativelanguage.googleapis.com/v1beta/models/{model_name}:generateContent?key={os.getenv("GOOGLE_API_KEY")}'
res = requests.post(url, json=req, headers={'Content-Type': 'application/json'})
if res.status_code != 200:
return f"Video error {res.status_code}: {res.text}"
if res.status_code == 429:
# HTTP 429: Too Many Requests (Rate limit/Quota reached)
return f"Tool unavailable: Gemini API Quota Limit Reached (Status 429). Cannot process video analysis right now."
if res.status_code == 503:
# HTTP 503: Service Unavailable (Can sometimes indicate a temporary capacity issue)
return f"Tool unavailable: Gemini API Service Unavailable (Status 503). Try again later."
parts = res.json()['candidates'][0]['content']['parts']
return "".join([p.get('text', '') for p in parts])