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

Use Gradio load event to mount FastAPI routes

Browse files
Files changed (1) hide show
  1. app.py +23 -25
app.py CHANGED
@@ -411,31 +411,29 @@ 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 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
 
411
  inputs=[prompt_input, max_tokens_input, temp_input, top_p_input],
412
  outputs=output,
413
  )
414
+
415
+ # Mount FastAPI routes using Gradio's load event
416
+ def mount_fastapi_routes():
417
+ """Mount FastAPI routes when Gradio app loads."""
418
+ try:
419
+ # Access Gradio's underlying FastAPI app
420
+ gradio_app.app.mount("/v1", fastapi_app)
421
+ gradio_app.app.mount("/gradio", fastapi_app)
422
+ print("FastAPI routes mounted successfully")
423
+ except (AttributeError, Exception) as e:
424
+ print(f"FastAPI routes mounting note: {e}")
425
+ # Try alternative mounting approach
426
+ try:
427
+ import starlette
428
+ # Mount using Starlette's mount
429
+ if hasattr(gradio_app, 'app') and hasattr(gradio_app.app, 'mount'):
430
+ gradio_app.app.mount("/v1", fastapi_app)
431
+ gradio_app.app.mount("/gradio", fastapi_app)
432
+ except Exception as e2:
433
+ print(f"Alternative mounting also failed: {e2}")
434
+
435
+ # Use load event to mount routes when app is ready
436
+ gradio_app.load(mount_fastapi_routes)
 
 
437
 
438
  # Set app to Gradio Blocks for Spaces - ZeroGPU requires Gradio SDK
439
  app = gradio_app