jomasego commited on
Commit
b62deb9
·
1 Parent(s): 996f027

Revert: Restore /analyze_video to handle both URL and file upload

Browse files
Files changed (1) hide show
  1. modal_whisper_app.py +16 -6
modal_whisper_app.py CHANGED
@@ -443,7 +443,7 @@ async def analyze_video_comprehensive(video_bytes: bytes) -> Dict[str, Any]:
443
 
444
  # --- Pydantic model for FastAPI request ---
445
  class VideoAnalysisRequestPayload(BaseModel):
446
- video_url: Optional[str] = None
447
 
448
 
449
  # === FastAPI Endpoint for Comprehensive Analysis ===
@@ -456,30 +456,40 @@ async def process_video_for_analysis(
456
  video_bytes_content: Optional[bytes] = None
457
  video_source_description: str = "Unknown"
458
 
459
- if video_file:
460
- print(f"[FastAPI Endpoint] Processing uploaded video file: {video_file.filename}, size: {video_file.size} bytes.")
461
- video_bytes_content = await video_file.read() # Use await for async read
 
 
 
 
462
  video_source_description = f"direct file upload: {video_file.filename}"
 
 
463
  elif payload and payload.video_url:
464
  video_url = str(payload.video_url) # Ensure it's a string
465
  print(f"[FastAPI Endpoint] Processing video_url: {video_url}")
466
  video_source_description = f"URL: {video_url}"
467
  try:
468
  async with httpx.AsyncClient() as client:
469
- response = await client.get(video_url, follow_redirects=True, timeout=60.0)
470
  response.raise_for_status()
471
  video_bytes_content = await response.aread()
472
  if not video_bytes_content:
473
  print(f"[FastAPI Endpoint] Download failed: content was empty for URL: {video_url}")
474
  return JSONResponse(status_code=400, content={"error": f"Failed to download video from URL: {video_url}. Content was empty."})
475
  print(f"[FastAPI Endpoint] Successfully downloaded {len(video_bytes_content)} bytes from {video_url}")
 
 
 
476
  except httpx.RequestError as e:
477
  print(f"[FastAPI Endpoint] httpx.RequestError downloading video: {e}")
478
  return JSONResponse(status_code=400, content={"error": f"Error downloading video from URL: {video_url}. Details: {str(e)}"})
479
- except Exception as e:
480
  print(f"[FastAPI Endpoint] Unexpected Exception downloading video: {e}")
481
  return JSONResponse(status_code=500, content={"error": f"Unexpected error downloading video. Details: {str(e)}"})
482
  else:
 
483
  print("[FastAPI Endpoint] No video_url in payload and no video_file uploaded.")
484
  return JSONResponse(status_code=400, content={"error": "Either 'video_url' in JSON payload or a 'video_file' in form-data must be provided."})
485
 
 
443
 
444
  # --- Pydantic model for FastAPI request ---
445
  class VideoAnalysisRequestPayload(BaseModel):
446
+ video_url: Optional[str] = None # Reverted to optional
447
 
448
 
449
  # === FastAPI Endpoint for Comprehensive Analysis ===
 
456
  video_bytes_content: Optional[bytes] = None
457
  video_source_description: str = "Unknown"
458
 
459
+ if video_file: # Prioritize file upload
460
+ print(f"[FastAPI Endpoint] Processing uploaded video file: {video_file.filename}")
461
+ video_bytes_content = await video_file.read()
462
+ await video_file.close() # Important to close the file
463
+ if not video_bytes_content:
464
+ print(f"[FastAPI Endpoint] Uploaded video file {video_file.filename} is empty.")
465
+ return JSONResponse(status_code=400, content={"error": "Uploaded video file is empty."})
466
  video_source_description = f"direct file upload: {video_file.filename}"
467
+ print(f"[FastAPI Endpoint] Read {len(video_bytes_content)} bytes from uploaded file {video_file.filename}.")
468
+
469
  elif payload and payload.video_url:
470
  video_url = str(payload.video_url) # Ensure it's a string
471
  print(f"[FastAPI Endpoint] Processing video_url: {video_url}")
472
  video_source_description = f"URL: {video_url}"
473
  try:
474
  async with httpx.AsyncClient() as client:
475
+ response = await client.get(video_url, follow_redirects=True, timeout=60.0) # Download timeout
476
  response.raise_for_status()
477
  video_bytes_content = await response.aread()
478
  if not video_bytes_content:
479
  print(f"[FastAPI Endpoint] Download failed: content was empty for URL: {video_url}")
480
  return JSONResponse(status_code=400, content={"error": f"Failed to download video from URL: {video_url}. Content was empty."})
481
  print(f"[FastAPI Endpoint] Successfully downloaded {len(video_bytes_content)} bytes from {video_url}")
482
+ except httpx.TimeoutException: # Specific timeout exception
483
+ print(f"[FastAPI Endpoint] Timeout downloading video from URL: {video_url}")
484
+ return JSONResponse(status_code=408, content={"error": f"Timeout downloading video from URL: {video_url}."})
485
  except httpx.RequestError as e:
486
  print(f"[FastAPI Endpoint] httpx.RequestError downloading video: {e}")
487
  return JSONResponse(status_code=400, content={"error": f"Error downloading video from URL: {video_url}. Details: {str(e)}"})
488
+ except Exception as e: # Catch any other unexpected error during download
489
  print(f"[FastAPI Endpoint] Unexpected Exception downloading video: {e}")
490
  return JSONResponse(status_code=500, content={"error": f"Unexpected error downloading video. Details: {str(e)}"})
491
  else:
492
+ # Neither file nor URL provided
493
  print("[FastAPI Endpoint] No video_url in payload and no video_file uploaded.")
494
  return JSONResponse(status_code=400, content={"error": "Either 'video_url' in JSON payload or a 'video_file' in form-data must be provided."})
495