Alovestocode commited on
Commit
d90f2a7
·
verified ·
1 Parent(s): 037dd46

Fix: Remove HTML/console endpoints, use APIRouter for clean API integration

Browse files
Files changed (1) hide show
  1. app.py +22 -36
app.py CHANGED
@@ -294,6 +294,7 @@ def _generate_with_gpu(
294
 
295
 
296
  def healthcheck() -> dict[str, str]:
 
297
  return {
298
  "status": "ok",
299
  "model": MODEL_ID,
@@ -307,6 +308,7 @@ def warm_start() -> None:
307
 
308
 
309
  def generate_endpoint(payload: GeneratePayload) -> GenerateResponse:
 
310
  try:
311
  text = _generate_with_gpu(
312
  prompt=payload.prompt,
@@ -480,43 +482,27 @@ with gr.Blocks(
480
  fn=lambda: ("", "", STATUS_IDLE),
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
 
522
  # Enable queued execution so ZeroGPU can schedule GPU work reliably
 
294
 
295
 
296
  def healthcheck() -> dict[str, str]:
297
+ """Health check endpoint handler."""
298
  return {
299
  "status": "ok",
300
  "model": MODEL_ID,
 
308
 
309
 
310
  def generate_endpoint(payload: GeneratePayload) -> GenerateResponse:
311
+ """Generate endpoint handler."""
312
  try:
313
  text = _generate_with_gpu(
314
  prompt=payload.prompt,
 
482
  fn=lambda: ("", "", STATUS_IDLE),
483
  outputs=[prompt_input, output, status_display],
484
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
485
 
486
+ # Attach API routes directly onto Gradio's FastAPI instance using APIRouter
487
+ api_router = APIRouter()
488
+
489
+
490
+ @api_router.get("/health")
491
+ def api_health() -> dict[str, str]:
492
+ """API health check endpoint."""
493
+ return healthcheck()
494
+
495
+
496
+ @api_router.post("/v1/generate", response_model=GenerateResponse)
497
+ def api_generate(payload: GeneratePayload) -> GenerateResponse:
498
+ """API generate endpoint."""
499
+ return generate_endpoint(payload)
500
+
501
+
502
+ # Include the router in Gradio's FastAPI app
503
+ gradio_app.app.include_router(api_router)
504
+
505
+ # Call warm start
506
  warm_start()
507
 
508
  # Enable queued execution so ZeroGPU can schedule GPU work reliably