Spaces:
Sleeping
Sleeping
Fix: Implement /analyze_topic endpoint to handle calls from Gradio UI
Browse files- modal_whisper_app.py +30 -40
modal_whisper_app.py
CHANGED
|
@@ -489,46 +489,36 @@ async def analyze_topic_endpoint(topic: str = Query(..., min_length=3, descripti
|
|
| 489 |
max_videos: Optional[int] = Query(3, ge=1, le=10, description="Maximum number of videos to find and analyze.")):
|
| 490 |
"""Endpoint to find videos for a topic, analyze them, and return aggregated results."""
|
| 491 |
print(f"[FastAPI /analyze_topic] Received request for topic: '{topic}', max_videos: {max_videos}")
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
# This endpoint is the entry point for the USER's request to Cascade.
|
| 523 |
-
# Cascade will then perform the sequence of operations.
|
| 524 |
-
# So, this function body is more of a declaration for the endpoint.
|
| 525 |
-
# We expect Cascade to handle the full orchestration when this endpoint is invoked.
|
| 526 |
-
# For the purpose of defining the Modal app structure, this is sufficient.
|
| 527 |
-
# The `analyze_videos_by_topic` function is what Modal will ultimately run with the list of URLs provided by Cascade.
|
| 528 |
-
|
| 529 |
-
# Return a message indicating that the process is initiated by Cascade.
|
| 530 |
-
# This response won't typically be seen if Cascade directly calls the .remote() of the target function.
|
| 531 |
-
return JSONResponse(status_code=202, content={"message": "Topic analysis process initiated. Cascade will orchestrate the search and analysis."})
|
| 532 |
|
| 533 |
|
| 534 |
|
|
|
|
| 489 |
max_videos: Optional[int] = Query(3, ge=1, le=10, description="Maximum number of videos to find and analyze.")):
|
| 490 |
"""Endpoint to find videos for a topic, analyze them, and return aggregated results."""
|
| 491 |
print(f"[FastAPI /analyze_topic] Received request for topic: '{topic}', max_videos: {max_videos}")
|
| 492 |
+
try:
|
| 493 |
+
print(f"[FastAPI /analyze_topic] Calling find_video_urls_for_topic for topic: '{topic}', max_videos: {max_videos}")
|
| 494 |
+
# find_video_urls_for_topic is a Modal function that internally calls search_web and extracts URLs
|
| 495 |
+
video_urls_dict = await find_video_urls_for_topic.call(topic=topic, max_results=max_videos)
|
| 496 |
+
|
| 497 |
+
if "error" in video_urls_dict:
|
| 498 |
+
print(f"[FastAPI /analyze_topic] Error from find_video_urls_for_topic: {video_urls_dict}")
|
| 499 |
+
# Use status_code from the error dict if available, otherwise default (e.g., 500)
|
| 500 |
+
return JSONResponse(status_code=video_urls_dict.get("status_code", 500), content=video_urls_dict)
|
| 501 |
+
|
| 502 |
+
video_urls = video_urls_dict.get("video_urls", [])
|
| 503 |
+
|
| 504 |
+
if not video_urls:
|
| 505 |
+
print(f"[FastAPI /analyze_topic] No video URLs found by find_video_urls_for_topic for topic: '{topic}'")
|
| 506 |
+
return JSONResponse(status_code=404, content={"error": "No relevant video URLs found for the topic after search.", "details": video_urls_dict.get("search_provider_log", "No additional search details.")})
|
| 507 |
+
|
| 508 |
+
print(f"[FastAPI /analyze_topic] Found {len(video_urls)} URLs: {video_urls}. Calling analyze_videos_by_topic.")
|
| 509 |
+
# analyze_videos_by_topic is another Modal function
|
| 510 |
+
analysis_results = await analyze_videos_by_topic.call(video_urls=video_urls, topic=topic)
|
| 511 |
+
print(f"[FastAPI /analyze_topic] Analysis complete for topic: '{topic}'.")
|
| 512 |
+
return JSONResponse(status_code=200, content=analysis_results)
|
| 513 |
+
|
| 514 |
+
except modal.exception.ModalError as e:
|
| 515 |
+
print(f"[FastAPI /analyze_topic] ModalError during topic analysis: {e}")
|
| 516 |
+
return JSONResponse(status_code=500, content={"error": f"Modal processing error during topic analysis: {str(e)}"})
|
| 517 |
+
except Exception as e:
|
| 518 |
+
print(f"[FastAPI /analyze_topic] Unexpected Exception during topic analysis: {e}")
|
| 519 |
+
# import traceback # For server-side debugging
|
| 520 |
+
# traceback.print_exc() # For server-side debugging
|
| 521 |
+
return JSONResponse(status_code=500, content={"error": f"Unexpected server error during topic analysis: {str(e)}"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 522 |
|
| 523 |
|
| 524 |
|