Spaces:
Running
on
Zero
Running
on
Zero
Fix: Use gradio_app.load() event to add routes after initialization
Browse files
app.py
CHANGED
|
@@ -511,54 +511,63 @@ with gr.Blocks(
|
|
| 511 |
outputs=[prompt_input, output],
|
| 512 |
)
|
| 513 |
|
| 514 |
-
# Add FastAPI routes
|
| 515 |
-
# This ensures
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 562 |
|
| 563 |
# Set app to Gradio Blocks for Spaces - ZeroGPU requires Gradio SDK
|
| 564 |
app = gradio_app
|
|
|
|
| 511 |
outputs=[prompt_input, output],
|
| 512 |
)
|
| 513 |
|
| 514 |
+
# Add FastAPI routes using Gradio's load event
|
| 515 |
+
# This ensures routes are added after Gradio is fully initialized
|
| 516 |
+
def add_api_routes():
|
| 517 |
+
"""Add API routes after Gradio app is loaded."""
|
| 518 |
+
try:
|
| 519 |
+
from fastapi.responses import JSONResponse
|
| 520 |
+
from starlette.routing import Route
|
| 521 |
+
|
| 522 |
+
async def generate_handler(request):
|
| 523 |
+
"""Handle POST /v1/generate requests."""
|
| 524 |
+
try:
|
| 525 |
+
data = await request.json()
|
| 526 |
+
payload = GeneratePayload(**data)
|
| 527 |
+
text = _generate_with_gpu(
|
| 528 |
+
prompt=payload.prompt,
|
| 529 |
+
max_new_tokens=payload.max_new_tokens or MAX_NEW_TOKENS,
|
| 530 |
+
temperature=payload.temperature or DEFAULT_TEMPERATURE,
|
| 531 |
+
top_p=payload.top_p or DEFAULT_TOP_P,
|
| 532 |
+
)
|
| 533 |
+
return JSONResponse(content={"text": text})
|
| 534 |
+
except Exception as exc:
|
| 535 |
+
from fastapi import HTTPException
|
| 536 |
+
raise HTTPException(status_code=500, detail=str(exc))
|
| 537 |
+
|
| 538 |
+
async def healthcheck_handler(request):
|
| 539 |
+
"""Handle GET /api/health requests."""
|
| 540 |
+
return JSONResponse(content={
|
| 541 |
+
"status": "ok",
|
| 542 |
+
"model": MODEL_ID,
|
| 543 |
+
"strategy": ACTIVE_STRATEGY or "pending",
|
| 544 |
+
})
|
| 545 |
+
|
| 546 |
+
async def gradio_ui_handler(request):
|
| 547 |
+
"""Handle GET /api/gradio requests."""
|
| 548 |
+
return HTMLResponse(interactive_ui())
|
| 549 |
+
|
| 550 |
+
# Add routes using Route objects
|
| 551 |
+
gradio_app.app.router.routes.append(
|
| 552 |
+
Route("/v1/generate", generate_handler, methods=["POST"])
|
| 553 |
+
)
|
| 554 |
+
gradio_app.app.router.routes.append(
|
| 555 |
+
Route("/api/health", healthcheck_handler, methods=["GET"])
|
| 556 |
+
)
|
| 557 |
+
gradio_app.app.router.routes.append(
|
| 558 |
+
Route("/api/gradio", gradio_ui_handler, methods=["GET"])
|
| 559 |
+
)
|
| 560 |
+
gradio_app.app.router.routes.append(
|
| 561 |
+
Route("/gradio", gradio_ui_handler, methods=["GET"])
|
| 562 |
+
)
|
| 563 |
+
print("FastAPI routes added successfully via load event")
|
| 564 |
+
except Exception as e:
|
| 565 |
+
print(f"Warning: Could not add FastAPI routes: {e}")
|
| 566 |
+
import traceback
|
| 567 |
+
traceback.print_exc()
|
| 568 |
+
|
| 569 |
+
# Use load event to add routes after app initialization
|
| 570 |
+
gradio_app.load(add_api_routes)
|
| 571 |
|
| 572 |
# Set app to Gradio Blocks for Spaces - ZeroGPU requires Gradio SDK
|
| 573 |
app = gradio_app
|