Alovestocode commited on
Commit
bfb9eb9
·
verified ·
1 Parent(s): 91a200a

Fix FastAPI route mounting with proper callback

Browse files
Files changed (1) hide show
  1. app.py +25 -4
app.py CHANGED
@@ -411,10 +411,31 @@ with gr.Blocks(title="Router Model API - ZeroGPU") as gradio_app:
411
  inputs=[prompt_input, max_tokens_input, temp_input, top_p_input],
412
  outputs=output,
413
  )
414
-
415
- # Mount FastAPI routes to Gradio's underlying FastAPI app
416
- gradio_app.app.mount("/v1", fastapi_app)
417
- gradio_app.app.mount("/gradio", fastapi_app)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
418
 
419
  # Set app to Gradio Blocks for Spaces - ZeroGPU requires Gradio SDK
420
  app = gradio_app
 
411
  inputs=[prompt_input, max_tokens_input, temp_input, top_p_input],
412
  outputs=output,
413
  )
414
+
415
+ # Mount FastAPI routes to Gradio's underlying FastAPI app after Blocks context
416
+ # Use a callback to mount routes when the app is ready
417
+ def mount_fastapi_on_load():
418
+ """Mount FastAPI routes when Gradio app loads."""
419
+ try:
420
+ # Access Gradio's FastAPI app and mount our routes
421
+ gradio_app.app.mount("/v1", fastapi_app)
422
+ gradio_app.app.mount("/gradio", fastapi_app)
423
+ except (AttributeError, Exception) as e:
424
+ print(f"Note: FastAPI routes mounting deferred: {e}")
425
+
426
+ # Try to mount immediately, and also set up for when app launches
427
+ try:
428
+ mount_fastapi_on_load()
429
+ except:
430
+ # If mounting fails now, it will be handled during launch
431
+ pass
432
+
433
+ # Override launch to mount routes
434
+ original_launch = gradio_app.launch
435
+ def launch_with_mount(*args, **kwargs):
436
+ mount_fastapi_on_load()
437
+ return original_launch(*args, **kwargs)
438
+ gradio_app.launch = launch_with_mount
439
 
440
  # Set app to Gradio Blocks for Spaces - ZeroGPU requires Gradio SDK
441
  app = gradio_app