akhaliq HF Staff commited on
Commit
9db959b
·
1 Parent(s): 77cc6e5

feat: implement fallback to video loading if image processing fails in file handling logic

Browse files
Files changed (1) hide show
  1. app.py +17 -16
app.py CHANGED
@@ -224,7 +224,13 @@ def predict(
224
  h_content = []
225
  if turn_files:
226
  for f_path in turn_files:
227
- h_content.append({"type": "image", "image": Image.open(f_path).convert("RGB")})
 
 
 
 
 
 
228
 
229
  if user_text:
230
  h_content.append({"type": "text", "text": user_text})
@@ -238,22 +244,17 @@ def predict(
238
  if files:
239
  for f in files:
240
  file_path = f["path"]
241
- # Detect if video or image
242
- is_video = False
243
  try:
244
- # Basic extension check instead of opening every file
245
- ext = os.path.splitext(file_path)[1].lower()
246
- if ext in ('.mp4', '.mkv', '.mov', '.avi', '.webm'):
247
- is_video = True
248
- except:
249
- is_video = False
250
-
251
- if is_video:
252
- frames = load_video(file_path, max_frames=max_frames)
253
- if frames:
254
- content.append({"type": "video", "video": frames})
255
- else:
256
- content.append({"type": "image", "image": Image.open(file_path).convert("RGB")})
257
 
258
  if message:
259
  content.append({"type": "text", "text": message})
 
224
  h_content = []
225
  if turn_files:
226
  for f_path in turn_files:
227
+ try:
228
+ img = Image.open(f_path).convert("RGB")
229
+ h_content.append({"type": "image", "image": img})
230
+ except Exception:
231
+ v_frames = load_video(f_path, max_frames=max_frames)
232
+ if v_frames:
233
+ h_content.append({"type": "video", "video": v_frames})
234
 
235
  if user_text:
236
  h_content.append({"type": "text", "text": user_text})
 
244
  if files:
245
  for f in files:
246
  file_path = f["path"]
 
 
247
  try:
248
+ # Try opening as image first
249
+ img = Image.open(file_path).convert("RGB")
250
+ content.append({"type": "image", "image": img})
251
+ except Exception:
252
+ # If image fails, try as video
253
+ v_frames = load_video(file_path, max_frames=max_frames)
254
+ if v_frames:
255
+ content.append({"type": "video", "video": v_frames})
256
+ else:
257
+ print(f"Failed to identify file type for: {file_path}")
 
 
 
258
 
259
  if message:
260
  content.append({"type": "text", "text": message})