Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -22,18 +22,6 @@ model = Gemma3ForConditionalGeneration.from_pretrained(
|
|
22 |
|
23 |
MAX_NUM_IMAGES = int(os.getenv("MAX_NUM_IMAGES", "5"))
|
24 |
|
25 |
-
css = '''h1 {
|
26 |
-
text-align: center;
|
27 |
-
display: block;
|
28 |
-
}
|
29 |
-
|
30 |
-
#logo {
|
31 |
-
display: block;
|
32 |
-
margin: 0 auto;
|
33 |
-
width: 40%;
|
34 |
-
object-fit: contain;
|
35 |
-
}
|
36 |
-
'''
|
37 |
|
38 |
def count_files_in_new_message(paths: list[str]) -> tuple[int, int]:
|
39 |
image_count = 0
|
@@ -80,22 +68,27 @@ def validate_media_constraints(message: dict, history: list[dict]) -> bool:
|
|
80 |
return False
|
81 |
return True
|
82 |
|
83 |
-
def downsample_video(video_path):
|
84 |
vidcap = cv2.VideoCapture(video_path)
|
85 |
-
total_frames = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
|
86 |
fps = vidcap.get(cv2.CAP_PROP_FPS)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
frames = []
|
88 |
-
|
89 |
-
frame_indices = np.linspace(0, total_frames - 1, 10, dtype=int)
|
90 |
-
for i in frame_indices:
|
91 |
vidcap.set(cv2.CAP_PROP_POS_FRAMES, i)
|
92 |
success, image = vidcap.read()
|
93 |
if success:
|
94 |
-
# Convert from BGR to RGB and then to PIL Image.
|
95 |
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
96 |
pil_image = Image.fromarray(image)
|
97 |
timestamp = round(i / fps, 2)
|
98 |
frames.append((pil_image, timestamp))
|
|
|
99 |
vidcap.release()
|
100 |
return frames
|
101 |
|
@@ -163,7 +156,7 @@ def process_history(history: list[dict]) -> list[dict]:
|
|
163 |
current_user_content.append({"type": "image", "url": content[0]})
|
164 |
return messages
|
165 |
|
166 |
-
@spaces.GPU(duration=
|
167 |
def run(message: dict, history: list[dict], system_prompt: str = "", max_new_tokens: int = 512) -> Iterator[str]:
|
168 |
if not validate_media_constraints(message, history):
|
169 |
yield ""
|
@@ -249,11 +242,11 @@ examples = [
|
|
249 |
{
|
250 |
"text": "Create a short story based on the sequence of images.",
|
251 |
"files": [
|
252 |
-
"
|
253 |
-
"
|
254 |
-
"
|
255 |
-
"
|
256 |
-
"
|
257 |
],
|
258 |
}
|
259 |
],
|
@@ -342,7 +335,7 @@ demo = gr.ChatInterface(
|
|
342 |
examples=examples,
|
343 |
run_examples_on_click=False,
|
344 |
cache_examples=False,
|
345 |
-
|
346 |
delete_cache=(1800, 1800),
|
347 |
)
|
348 |
|
|
|
22 |
|
23 |
MAX_NUM_IMAGES = int(os.getenv("MAX_NUM_IMAGES", "5"))
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
def count_files_in_new_message(paths: list[str]) -> tuple[int, int]:
|
27 |
image_count = 0
|
|
|
68 |
return False
|
69 |
return True
|
70 |
|
71 |
+
def downsample_video(video_path: str) -> list[tuple[Image.Image, float]]:
|
72 |
vidcap = cv2.VideoCapture(video_path)
|
|
|
73 |
fps = vidcap.get(cv2.CAP_PROP_FPS)
|
74 |
+
total_frames = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
|
75 |
+
|
76 |
+
max_frames = 5 # Limit to 5 frames to prevent memory overload
|
77 |
+
if total_frames <= max_frames:
|
78 |
+
indices = list(range(total_frames))
|
79 |
+
else:
|
80 |
+
indices = [int(i * (total_frames - 1) / (max_frames - 1)) for i in range(max_frames)]
|
81 |
+
|
82 |
frames = []
|
83 |
+
for i in indices:
|
|
|
|
|
84 |
vidcap.set(cv2.CAP_PROP_POS_FRAMES, i)
|
85 |
success, image = vidcap.read()
|
86 |
if success:
|
|
|
87 |
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
88 |
pil_image = Image.fromarray(image)
|
89 |
timestamp = round(i / fps, 2)
|
90 |
frames.append((pil_image, timestamp))
|
91 |
+
|
92 |
vidcap.release()
|
93 |
return frames
|
94 |
|
|
|
156 |
current_user_content.append({"type": "image", "url": content[0]})
|
157 |
return messages
|
158 |
|
159 |
+
@spaces.GPU(duration=120)
|
160 |
def run(message: dict, history: list[dict], system_prompt: str = "", max_new_tokens: int = 512) -> Iterator[str]:
|
161 |
if not validate_media_constraints(message, history):
|
162 |
yield ""
|
|
|
242 |
{
|
243 |
"text": "Create a short story based on the sequence of images.",
|
244 |
"files": [
|
245 |
+
"assets/sample-images/09-1.png",
|
246 |
+
"assets/sample-images/09-2.png",
|
247 |
+
"assets/sample-images/09-3.png",
|
248 |
+
"assets/sample-images/09-4.png",
|
249 |
+
"assets/sample-images/09-5.png",
|
250 |
],
|
251 |
}
|
252 |
],
|
|
|
335 |
examples=examples,
|
336 |
run_examples_on_click=False,
|
337 |
cache_examples=False,
|
338 |
+
css_paths="style.css",
|
339 |
delete_cache=(1800, 1800),
|
340 |
)
|
341 |
|