-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvlm_utils.py
More file actions
77 lines (65 loc) · 2.17 KB
/
Copy pathvlm_utils.py
File metadata and controls
77 lines (65 loc) · 2.17 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
VLM Utilities for MBench evaluation.
Provides video preprocessing and Gemini API calling utilities.
"""
import os
import time
from pathlib import Path
from typing import Optional
try:
from google import genai
from google.genai import types
GEMINI_AVAILABLE = True
except ImportError:
GEMINI_AVAILABLE = False
print("Warning: google-genai not available. Install it with: pip install google-genai")
def call_gemini_api(
video_bytes: bytes,
prompt: str,
api_key: str,
fps: int = 5,
retry_times: int = 3,
client=None
) -> str:
"""
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: Optional pre-initialized Gemini client
Returns:
Model response text
Raises:
RuntimeError: If all retry attempts fail
"""
if not GEMINI_AVAILABLE:
raise ImportError("google-genai package is required but not installed")
if not api_key:
raise ValueError("API key is required")
client = client or genai.Client(api_key=api_key)
text = prompt.strip()
for i in range(retry_times):
try:
response = client.models.generate_content(
model="gemini-2.5-flash",
contents=types.Content(
role="user",
parts=[
types.Part(text=text),
types.Part(
inline_data=types.Blob(data=video_bytes, mime_type="video/mp4"),
video_metadata=types.VideoMetadata(fps=fps)
)
],
),
)
if response.text:
return response.text
except Exception as e:
print(f"Attempt {i+1} failed: {e}")
if i < retry_times - 1:
time.sleep(2 ** i) # Exponential backoff
raise RuntimeError(f"Gemini API failed after {retry_times} attempts")