prithivMLmods commited on
Commit
4707b9a
·
verified ·
1 Parent(s): 7340c37

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -12
app.py CHANGED
@@ -80,27 +80,22 @@ def validate_media_constraints(message: dict, history: list[dict]) -> bool:
80
  return False
81
  return True
82
 
83
- def downsample_video(video_path: str) -> list[tuple[Image.Image, float]]:
84
  vidcap = cv2.VideoCapture(video_path)
85
- fps = vidcap.get(cv2.CAP_PROP_FPS)
86
  total_frames = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
87
-
88
- max_frames = 5
89
- if total_frames <= max_frames:
90
- indices = list(range(total_frames))
91
- else:
92
- indices = [int(i * (total_frames - 1) / (max_frames - 1)) for i in range(max_frames)]
93
-
94
  frames = []
95
- for i in indices:
 
 
96
  vidcap.set(cv2.CAP_PROP_POS_FRAMES, i)
97
  success, image = vidcap.read()
98
  if success:
 
99
  image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
100
  pil_image = Image.fromarray(image)
101
  timestamp = round(i / fps, 2)
102
  frames.append((pil_image, timestamp))
103
-
104
  vidcap.release()
105
  return frames
106
 
@@ -168,7 +163,7 @@ def process_history(history: list[dict]) -> list[dict]:
168
  current_user_content.append({"type": "image", "url": content[0]})
169
  return messages
170
 
171
- @spaces.GPU(duration=40)
172
  def run(message: dict, history: list[dict], system_prompt: str = "", max_new_tokens: int = 512) -> Iterator[str]:
173
  if not validate_media_constraints(message, history):
174
  yield ""
 
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
+ # Sample 10 evenly spaced frames.
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
  current_user_content.append({"type": "image", "url": content[0]})
164
  return messages
165
 
166
+ @spaces.GPU(duration=60)
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 ""