Spaces:
Running
on
Zero
Running
on
Zero
Fix: Add routes directly in Blocks context instead of using load event
Browse files
app.py
CHANGED
|
@@ -518,51 +518,46 @@ with gr.Blocks(
|
|
| 518 |
outputs=[prompt_input, output],
|
| 519 |
)
|
| 520 |
|
| 521 |
-
# Mount FastAPI routes onto Gradio's app
|
| 522 |
-
#
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
import traceback
|
| 562 |
-
traceback.print_exc()
|
| 563 |
-
|
| 564 |
-
# Mount routes when Gradio app loads (must be inside Blocks context)
|
| 565 |
-
gradio_app.load(mount_fastapi_routes)
|
| 566 |
|
| 567 |
# Enable queued execution so ZeroGPU can schedule GPU work reliably
|
| 568 |
gradio_app.queue(max_size=8)
|
|
|
|
| 518 |
outputs=[prompt_input, output],
|
| 519 |
)
|
| 520 |
|
| 521 |
+
# Mount FastAPI routes directly onto Gradio's app (inside Blocks context)
|
| 522 |
+
# Add routes immediately so they're registered before Gradio processes requests
|
| 523 |
+
try:
|
| 524 |
+
from fastapi.responses import JSONResponse
|
| 525 |
+
from starlette.routing import Route
|
| 526 |
+
from starlette.requests import Request
|
| 527 |
+
|
| 528 |
+
# Create route handlers that call the FastAPI endpoint functions
|
| 529 |
+
async def health_handler(request: Request):
|
| 530 |
+
"""Handle GET /health requests."""
|
| 531 |
+
result = healthcheck()
|
| 532 |
+
return JSONResponse(content=result)
|
| 533 |
+
|
| 534 |
+
async def generate_handler(request: Request):
|
| 535 |
+
"""Handle POST /v1/generate requests."""
|
| 536 |
+
try:
|
| 537 |
+
data = await request.json()
|
| 538 |
+
payload = GeneratePayload(**data)
|
| 539 |
+
# Call the FastAPI endpoint function directly
|
| 540 |
+
result = generate_endpoint(payload)
|
| 541 |
+
return JSONResponse(content={"text": result.text})
|
| 542 |
+
except Exception as exc:
|
| 543 |
+
from fastapi import HTTPException
|
| 544 |
+
raise HTTPException(status_code=500, detail=str(exc))
|
| 545 |
+
|
| 546 |
+
async def console_handler(request: Request):
|
| 547 |
+
"""Handle GET /console requests."""
|
| 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
|
| 560 |
+
traceback.print_exc()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 561 |
|
| 562 |
# Enable queued execution so ZeroGPU can schedule GPU work reliably
|
| 563 |
gradio_app.queue(max_size=8)
|