jomasego commited on
Commit
8432008
·
1 Parent(s): c5c4fe4

Fix: Implement /analyze_topic endpoint to handle calls from Gradio UI

Browse files
Files changed (1) hide show
  1. 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
- # This endpoint is orchestrated by Cascade. Cascade will:
494
- # 1. Call its `search_web` tool with the `topic`.
495
- # 2. Call the local Python helper `extract_video_urls_from_search` with the search results.
496
- # 3. Call the Modal function `analyze_videos_by_topic.remote()` with the extracted URLs and topic.
497
- # The actual implementation of these steps happens in Cascade's execution flow, not directly in this FastAPI code.
498
- # This FastAPI endpoint definition tells Modal to expect such a route and parameters.
499
- # The body of this function in the Python file is a placeholder for Cascade's orchestration.
500
-
501
- # Placeholder: The actual call to analyze_videos_by_topic.remote() will be made by Cascade
502
- # after it performs the search and URL extraction.
503
- # For standalone Modal testing, one might simulate this:
504
- # if modal.is_local():
505
- # # Simulate search and extraction
506
- # simulated_search_results = [{"link": "https://www.youtube.com/watch?v=dQw4w9WgXcQ"}]
507
- # video_urls = extract_video_urls_from_search(simulated_search_results, max_videos)
508
- # if not video_urls:
509
- # return JSONResponse(status_code=404, content={"error": "No relevant video URLs found for the topic after search."})
510
- # try:
511
- # result = await analyze_videos_by_topic.coro(video_urls=video_urls, topic=topic)
512
- # return result
513
- # except Exception as e:
514
- # return JSONResponse(status_code=500, content={"error": f"Error during topic analysis: {str(e)}"})
515
- # else:
516
- # # In deployed Modal, Cascade handles the call chain.
517
- # # This function body might not even be executed directly if Cascade calls .remote() on analyze_videos_by_topic directly.
518
- # # However, having a defined endpoint is good practice for discoverability and potential direct calls.
519
- pass
520
-
521
- # The actual logic is: Cascade calls search_web -> Cascade calls extract_video_urls_from_search -> Cascade calls analyze_videos_by_topic.remote().
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