Spaces:
Running
on
Zero
Running
on
Zero
Fix API routes: Use add_api_route to properly register FastAPI endpoints in Gradio
Browse files
app.py
CHANGED
|
@@ -511,15 +511,13 @@ with gr.Blocks(
|
|
| 511 |
outputs=[prompt_input, output],
|
| 512 |
)
|
| 513 |
|
| 514 |
-
# Add FastAPI routes directly to Gradio's
|
| 515 |
-
# This
|
| 516 |
try:
|
| 517 |
from fastapi.responses import JSONResponse
|
| 518 |
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
"""FastAPI endpoint mounted in Gradio app."""
|
| 522 |
-
from fastapi import Request
|
| 523 |
try:
|
| 524 |
data = await request.json()
|
| 525 |
payload = GeneratePayload(**data)
|
|
@@ -534,22 +532,30 @@ with gr.Blocks(
|
|
| 534 |
from fastapi import HTTPException
|
| 535 |
raise HTTPException(status_code=500, detail=str(exc))
|
| 536 |
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
return HTMLResponse(interactive_ui())
|
| 541 |
-
|
| 542 |
-
@gradio_app.app.get("/")
|
| 543 |
-
async def fastapi_healthcheck():
|
| 544 |
-
"""FastAPI healthcheck endpoint."""
|
| 545 |
-
return {
|
| 546 |
"status": "ok",
|
| 547 |
"model": MODEL_ID,
|
| 548 |
"strategy": ACTIVE_STRATEGY or "pending",
|
| 549 |
-
}
|
| 550 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 551 |
except Exception as e:
|
| 552 |
print(f"Warning: Could not add FastAPI routes: {e}")
|
|
|
|
|
|
|
| 553 |
|
| 554 |
# Set app to Gradio Blocks for Spaces - ZeroGPU requires Gradio SDK
|
| 555 |
app = gradio_app
|
|
|
|
| 511 |
outputs=[prompt_input, output],
|
| 512 |
)
|
| 513 |
|
| 514 |
+
# Add FastAPI routes directly to Gradio's router
|
| 515 |
+
# This ensures they're registered and accessible alongside Gradio routes
|
| 516 |
try:
|
| 517 |
from fastapi.responses import JSONResponse
|
| 518 |
|
| 519 |
+
async def generate_handler(request):
|
| 520 |
+
"""Handle POST /v1/generate requests."""
|
|
|
|
|
|
|
| 521 |
try:
|
| 522 |
data = await request.json()
|
| 523 |
payload = GeneratePayload(**data)
|
|
|
|
| 532 |
from fastapi import HTTPException
|
| 533 |
raise HTTPException(status_code=500, detail=str(exc))
|
| 534 |
|
| 535 |
+
async def healthcheck_handler(request):
|
| 536 |
+
"""Handle GET /api/health requests."""
|
| 537 |
+
return JSONResponse(content={
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 538 |
"status": "ok",
|
| 539 |
"model": MODEL_ID,
|
| 540 |
"strategy": ACTIVE_STRATEGY or "pending",
|
| 541 |
+
})
|
| 542 |
+
|
| 543 |
+
async def gradio_ui_handler(request):
|
| 544 |
+
"""Handle GET /api/gradio requests."""
|
| 545 |
+
return HTMLResponse(interactive_ui())
|
| 546 |
+
|
| 547 |
+
# Add routes to Gradio's router
|
| 548 |
+
# Use /api prefix to avoid conflicts with Gradio's routes
|
| 549 |
+
gradio_app.app.add_api_route("/v1/generate", generate_handler, methods=["POST"])
|
| 550 |
+
gradio_app.app.add_api_route("/api/health", healthcheck_handler, methods=["GET"])
|
| 551 |
+
gradio_app.app.add_api_route("/api/gradio", gradio_ui_handler, methods=["GET"])
|
| 552 |
+
# Also add /gradio for backward compatibility
|
| 553 |
+
gradio_app.app.add_api_route("/gradio", gradio_ui_handler, methods=["GET"])
|
| 554 |
+
print("FastAPI routes added successfully to Gradio router")
|
| 555 |
except Exception as e:
|
| 556 |
print(f"Warning: Could not add FastAPI routes: {e}")
|
| 557 |
+
import traceback
|
| 558 |
+
traceback.print_exc()
|
| 559 |
|
| 560 |
# Set app to Gradio Blocks for Spaces - ZeroGPU requires Gradio SDK
|
| 561 |
app = gradio_app
|