File size: 1,845 Bytes
58998e1
29eed7b
 
 
 
58998e1
29eed7b
 
8273c18
29eed7b
 
8273c18
58998e1
 
29eed7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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])