Alovestocode commited on
Commit
8a5142a
·
verified ·
1 Parent(s): d48b78c

Fix: Add routes after Blocks context when router is ready

Browse files
Files changed (1) hide show
  1. app.py +23 -6
app.py CHANGED
@@ -548,12 +548,15 @@ with gr.Blocks(
548
  html_content = interactive_ui()
549
  return HTMLResponse(content=html_content)
550
 
551
- # Insert routes at the beginning of Gradio's router to ensure they're processed first
552
- # This prevents Gradio's catch-all routes from intercepting our API endpoints
553
- gradio_app.app.router.routes.insert(0, Route("/health", health_handler, methods=["GET"]))
554
- gradio_app.app.router.routes.insert(0, Route("/v1/generate", generate_handler, methods=["POST"]))
555
- gradio_app.app.router.routes.insert(0, Route("/console", console_handler, methods=["GET"]))
556
- print(f"FastAPI routes mounted successfully on Gradio app. Total routes: {len(gradio_app.app.router.routes)}")
 
 
 
557
  except Exception as e:
558
  print(f"Warning: Could not mount FastAPI routes: {e}")
559
  import traceback
@@ -562,6 +565,20 @@ with gr.Blocks(
562
  # Enable queued execution so ZeroGPU can schedule GPU work reliably
563
  gradio_app.queue(max_size=8)
564
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
565
  # Set app to Gradio for Spaces compatibility (sdk: gradio requires Gradio app)
566
  # Spaces will handle running the server automatically
567
  app = gradio_app
 
548
  html_content = interactive_ui()
549
  return HTMLResponse(content=html_content)
550
 
551
+ # Store handlers to add routes after Blocks context (router is ready then)
552
+ # Store in a list that will be accessible after the Blocks context
553
+ if not hasattr(gradio_app, '_api_handlers'):
554
+ gradio_app._api_handlers = []
555
+ gradio_app._api_handlers.extend([
556
+ ("/health", health_handler, ["GET"]),
557
+ ("/v1/generate", generate_handler, ["POST"]),
558
+ ("/console", console_handler, ["GET"]),
559
+ ])
560
  except Exception as e:
561
  print(f"Warning: Could not mount FastAPI routes: {e}")
562
  import traceback
 
565
  # Enable queued execution so ZeroGPU can schedule GPU work reliably
566
  gradio_app.queue(max_size=8)
567
 
568
+ # Add routes after Blocks context when router is definitely ready
569
+ try:
570
+ if hasattr(gradio_app, '_api_handlers'):
571
+ from starlette.routing import Route
572
+ for path, handler, methods in gradio_app._api_handlers:
573
+ gradio_app.app.router.routes.insert(0, Route(path, handler, methods=methods))
574
+ print(f"FastAPI routes added after Blocks context. Total routes: {len(gradio_app.app.router.routes)}")
575
+ # Clean up
576
+ del gradio_app._api_handlers
577
+ except Exception as e:
578
+ print(f"Warning: Could not add routes after Blocks context: {e}")
579
+ import traceback
580
+ traceback.print_exc()
581
+
582
  # Set app to Gradio for Spaces compatibility (sdk: gradio requires Gradio app)
583
  # Spaces will handle running the server automatically
584
  app = gradio_app