Spaces:
Running
on
Zero
Running
on
Zero
Fix: Use load event to add routes, insert at position 0 for priority
Browse files
app.py
CHANGED
|
@@ -481,56 +481,41 @@ with gr.Blocks(
|
|
| 481 |
outputs=[prompt_input, output, status_display],
|
| 482 |
)
|
| 483 |
|
| 484 |
-
#
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
# Use include_router which is the proper FastAPI way to add routes
|
| 488 |
-
api_router = APIRouter()
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
@api_router.get("/health")
|
| 492 |
-
def api_health() -> dict[str, str]:
|
| 493 |
-
"""Health check endpoint."""
|
| 494 |
-
return healthcheck()
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
@api_router.post("/v1/generate", response_model=GenerateResponse)
|
| 498 |
-
def api_generate(payload: GeneratePayload) -> GenerateResponse:
|
| 499 |
-
"""Generate endpoint."""
|
| 500 |
-
return generate_endpoint(payload)
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
# Include router on Gradio's FastAPI app
|
| 504 |
-
try:
|
| 505 |
-
gradio_app.app.include_router(api_router)
|
| 506 |
-
print(f"API router included successfully. Total routes: {len(gradio_app.app.routes)}")
|
| 507 |
-
except AttributeError:
|
| 508 |
-
# Fallback: add routes directly if include_router not available
|
| 509 |
-
print("include_router not available, adding routes directly...")
|
| 510 |
-
from fastapi.responses import JSONResponse
|
| 511 |
-
from starlette.routing import Route
|
| 512 |
-
from starlette.requests import Request
|
| 513 |
-
|
| 514 |
-
async def health_handler(request: Request):
|
| 515 |
-
return JSONResponse(content=healthcheck())
|
| 516 |
-
|
| 517 |
-
async def generate_handler(request: Request):
|
| 518 |
try:
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 526 |
|
| 527 |
-
|
| 528 |
-
gradio_app.
|
| 529 |
-
print(f"Routes added directly. Total routes: {len(gradio_app.app.routes)}")
|
| 530 |
-
except Exception as e:
|
| 531 |
-
print(f"Warning: Could not add API routes: {e}")
|
| 532 |
-
import traceback
|
| 533 |
-
traceback.print_exc()
|
| 534 |
|
| 535 |
warm_start()
|
| 536 |
|
|
|
|
| 481 |
outputs=[prompt_input, output, status_display],
|
| 482 |
)
|
| 483 |
|
| 484 |
+
# Add API routes using Gradio's load event to ensure they're registered correctly
|
| 485 |
+
def add_api_routes():
|
| 486 |
+
"""Add API routes after Gradio is fully initialized."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 487 |
try:
|
| 488 |
+
from fastapi.responses import JSONResponse
|
| 489 |
+
from starlette.routing import Route
|
| 490 |
+
from starlette.requests import Request
|
| 491 |
+
|
| 492 |
+
async def health_handler(request: Request):
|
| 493 |
+
"""Handle GET /health requests."""
|
| 494 |
+
return JSONResponse(content=healthcheck())
|
| 495 |
+
|
| 496 |
+
async def generate_handler(request: Request):
|
| 497 |
+
"""Handle POST /v1/generate requests."""
|
| 498 |
+
try:
|
| 499 |
+
data = await request.json()
|
| 500 |
+
payload = GeneratePayload(**data)
|
| 501 |
+
result = generate_endpoint(payload)
|
| 502 |
+
return JSONResponse(content={"text": result.text})
|
| 503 |
+
except Exception as exc:
|
| 504 |
+
from fastapi import HTTPException
|
| 505 |
+
raise HTTPException(status_code=500, detail=str(exc))
|
| 506 |
+
|
| 507 |
+
# Add routes directly to Gradio's router
|
| 508 |
+
# Insert at position 0 to ensure they're checked before Gradio's catch-all routes
|
| 509 |
+
gradio_app.app.router.routes.insert(0, Route("/health", health_handler, methods=["GET"]))
|
| 510 |
+
gradio_app.app.router.routes.insert(0, Route("/v1/generate", generate_handler, methods=["POST"]))
|
| 511 |
+
print(f"API routes added via load event. Total routes: {len(gradio_app.app.routes)}")
|
| 512 |
+
except Exception as e:
|
| 513 |
+
print(f"Warning: Could not add API routes: {e}")
|
| 514 |
+
import traceback
|
| 515 |
+
traceback.print_exc()
|
| 516 |
|
| 517 |
+
# Register the load event handler
|
| 518 |
+
gradio_app.load(add_api_routes)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 519 |
|
| 520 |
warm_start()
|
| 521 |
|