Spaces:
Running
on
Zero
Running
on
Zero
Use Gradio load event to mount FastAPI routes
Browse files
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
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 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
|