Call Gemini API with video and text prompt. Args: video_bytes: Video file content as bytes prompt: Text prompt for the model api_key: Gemini API key fps: FPS for video sampling (default: 5) retry_times: Number of retry attempts client: Op
(
video_bytes: bytes,
prompt: str,
api_key: str,
fps: int = 5,
retry_times: int = 3,
client=None
)
| 19 | |
| 20 | |
| 21 | def call_gemini_api( |
| 22 | video_bytes: bytes, |
| 23 | prompt: str, |
| 24 | api_key: str, |
| 25 | fps: int = 5, |
| 26 | retry_times: int = 3, |
| 27 | client=None |
| 28 | ) -> str: |
| 29 | """ |
| 30 | Call Gemini API with video and text prompt. |
| 31 | |
| 32 | Args: |
| 33 | video_bytes: Video file content as bytes |
| 34 | prompt: Text prompt for the model |
| 35 | api_key: Gemini API key |
| 36 | fps: FPS for video sampling (default: 5) |
| 37 | retry_times: Number of retry attempts |
| 38 | client: Optional pre-initialized Gemini client |
| 39 | |
| 40 | Returns: |
| 41 | Model response text |
| 42 | |
| 43 | Raises: |
| 44 | RuntimeError: If all retry attempts fail |
| 45 | """ |
| 46 | if not GEMINI_AVAILABLE: |
| 47 | raise ImportError("google-genai package is required but not installed") |
| 48 | |
| 49 | if not api_key: |
| 50 | raise ValueError("API key is required") |
| 51 | |
| 52 | client = client or genai.Client(api_key=api_key) |
| 53 | text = prompt.strip() |
| 54 | |
| 55 | for i in range(retry_times): |
| 56 | try: |
| 57 | response = client.models.generate_content( |
| 58 | model="gemini-2.5-flash", |
| 59 | contents=types.Content( |
| 60 | role="user", |
| 61 | parts=[ |
| 62 | types.Part(text=text), |
| 63 | types.Part( |
| 64 | inline_data=types.Blob(data=video_bytes, mime_type="video/mp4"), |
| 65 | video_metadata=types.VideoMetadata(fps=fps) |
| 66 | ) |
| 67 | ], |
| 68 | ), |
| 69 | ) |
| 70 | if response.text: |
| 71 | return response.text |
| 72 | except Exception as e: |
| 73 | print(f"Attempt {i+1} failed: {e}") |
| 74 | if i < retry_times - 1: |
| 75 | time.sleep(2 ** i) # Exponential backoff |
| 76 | |
| 77 | raise RuntimeError(f"Gemini API failed after {retry_times} attempts") |
no outgoing calls
no test coverage detected