Spaces:
Running
on
Zero
Running
on
Zero
Fix: Add API routes after queue setup, avoid interfering with Gradio static assets
Browse files
app.py
CHANGED
|
@@ -463,14 +463,18 @@ with gr.Blocks(
|
|
| 463 |
outputs=[prompt_input, output],
|
| 464 |
)
|
| 465 |
|
| 466 |
-
#
|
| 467 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 468 |
try:
|
| 469 |
from fastapi.responses import JSONResponse
|
| 470 |
from starlette.routing import Route
|
| 471 |
from starlette.requests import Request
|
| 472 |
|
| 473 |
-
# Create route handlers
|
| 474 |
async def health_handler(request: Request):
|
| 475 |
"""Handle GET /health requests."""
|
| 476 |
result = healthcheck()
|
|
@@ -481,44 +485,24 @@ with gr.Blocks(
|
|
| 481 |
try:
|
| 482 |
data = await request.json()
|
| 483 |
payload = GeneratePayload(**data)
|
| 484 |
-
# Call the FastAPI endpoint function directly
|
| 485 |
result = generate_endpoint(payload)
|
| 486 |
return JSONResponse(content={"text": result.text})
|
| 487 |
except Exception as exc:
|
| 488 |
from fastapi import HTTPException
|
| 489 |
raise HTTPException(status_code=500, detail=str(exc))
|
| 490 |
|
| 491 |
-
#
|
| 492 |
-
#
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
gradio_app.
|
| 496 |
-
("/health", health_handler, ["GET"]),
|
| 497 |
-
("/v1/generate", generate_handler, ["POST"]),
|
| 498 |
-
])
|
| 499 |
except Exception as e:
|
| 500 |
-
print(f"Warning: Could not
|
| 501 |
import traceback
|
| 502 |
traceback.print_exc()
|
| 503 |
|
| 504 |
-
#
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
# Add routes after Blocks context when router is definitely ready
|
| 508 |
-
# Append routes instead of inserting to avoid interfering with Gradio's static asset routes
|
| 509 |
-
try:
|
| 510 |
-
if hasattr(gradio_app, '_api_handlers'):
|
| 511 |
-
from starlette.routing import Route
|
| 512 |
-
for path, handler, methods in gradio_app._api_handlers:
|
| 513 |
-
# Append routes so Gradio's routes (especially /_app/*) are checked first
|
| 514 |
-
gradio_app.app.router.routes.append(Route(path, handler, methods=methods))
|
| 515 |
-
print(f"FastAPI routes added after Blocks context. Total routes: {len(gradio_app.app.router.routes)}")
|
| 516 |
-
# Clean up
|
| 517 |
-
del gradio_app._api_handlers
|
| 518 |
-
except Exception as e:
|
| 519 |
-
print(f"Warning: Could not add routes after Blocks context: {e}")
|
| 520 |
-
import traceback
|
| 521 |
-
traceback.print_exc()
|
| 522 |
|
| 523 |
# Set app to Gradio for Spaces compatibility (sdk: gradio requires Gradio app)
|
| 524 |
# Spaces will handle running the server automatically
|
|
|
|
| 463 |
outputs=[prompt_input, output],
|
| 464 |
)
|
| 465 |
|
| 466 |
+
# Note: API routes will be added after Blocks context to avoid interfering with Gradio's static assets
|
| 467 |
+
|
| 468 |
+
# Add API routes after Blocks context and queue setup
|
| 469 |
+
# This ensures Gradio's static assets are fully initialized first
|
| 470 |
+
def add_api_routes_safely():
|
| 471 |
+
"""Add API routes without interfering with Gradio's static asset serving."""
|
| 472 |
try:
|
| 473 |
from fastapi.responses import JSONResponse
|
| 474 |
from starlette.routing import Route
|
| 475 |
from starlette.requests import Request
|
| 476 |
|
| 477 |
+
# Create route handlers
|
| 478 |
async def health_handler(request: Request):
|
| 479 |
"""Handle GET /health requests."""
|
| 480 |
result = healthcheck()
|
|
|
|
| 485 |
try:
|
| 486 |
data = await request.json()
|
| 487 |
payload = GeneratePayload(**data)
|
|
|
|
| 488 |
result = generate_endpoint(payload)
|
| 489 |
return JSONResponse(content={"text": result.text})
|
| 490 |
except Exception as exc:
|
| 491 |
from fastapi import HTTPException
|
| 492 |
raise HTTPException(status_code=500, detail=str(exc))
|
| 493 |
|
| 494 |
+
# Add routes directly - Starlette will match them in order
|
| 495 |
+
# Gradio's routes (like /_app/*) should already be registered
|
| 496 |
+
gradio_app.app.router.routes.append(Route("/health", health_handler, methods=["GET"]))
|
| 497 |
+
gradio_app.app.router.routes.append(Route("/v1/generate", generate_handler, methods=["POST"]))
|
| 498 |
+
print(f"API routes added. Total routes: {len(gradio_app.app.router.routes)}")
|
|
|
|
|
|
|
|
|
|
| 499 |
except Exception as e:
|
| 500 |
+
print(f"Warning: Could not add API routes: {e}")
|
| 501 |
import traceback
|
| 502 |
traceback.print_exc()
|
| 503 |
|
| 504 |
+
# Call after queue setup
|
| 505 |
+
add_api_routes_safely()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 506 |
|
| 507 |
# Set app to Gradio for Spaces compatibility (sdk: gradio requires Gradio app)
|
| 508 |
# Spaces will handle running the server automatically
|