Alovestocode commited on
Commit
92775c9
·
verified ·
1 Parent(s): eb7b063

Fix Content-Length: Remove response_class, let FastAPI handle headers automatically, add defensive checks

Browse files
Files changed (1) hide show
  1. app.py +24 -6
app.py CHANGED
@@ -485,23 +485,41 @@ with gr.Blocks(
485
 
486
  # Add API routes directly to Gradio's FastAPI app
487
  # These routes are added after Gradio Blocks context but before queue/launch
488
- from fastapi.responses import JSONResponse
489
  from fastapi import Request
490
 
491
- @gradio_app.app.get("/health", response_class=JSONResponse)
492
  def api_health():
493
- """API health check endpoint."""
494
- return healthcheck()
 
 
 
 
 
 
 
 
495
 
496
- @gradio_app.app.post("/v1/generate", response_class=JSONResponse)
497
  async def api_generate(request: Request):
498
- """API generate endpoint."""
 
 
 
 
499
  try:
500
  data = await request.json()
501
  payload = GeneratePayload(**data)
502
  result = generate_endpoint(payload)
 
 
503
  return {"text": result.text}
 
 
 
504
  except Exception as exc:
 
505
  raise HTTPException(status_code=500, detail=str(exc))
506
 
507
  # Call warm start
 
485
 
486
  # Add API routes directly to Gradio's FastAPI app
487
  # These routes are added after Gradio Blocks context but before queue/launch
488
+ # Let FastAPI handle Content-Length automatically - return dicts, not Response objects
489
  from fastapi import Request
490
 
491
+ @gradio_app.app.get("/health")
492
  def api_health():
493
+ """
494
+ API health check endpoint.
495
+ Returns JSON dict - FastAPI will serialize and set Content-Length correctly.
496
+ Never returns 204/304 - always returns 200 with JSON body.
497
+ """
498
+ result = healthcheck()
499
+ # Ensure we always return a dict (never empty/None) to avoid 204 issues
500
+ if not result:
501
+ result = {"status": "ok"}
502
+ return result
503
 
504
+ @gradio_app.app.post("/v1/generate")
505
  async def api_generate(request: Request):
506
+ """
507
+ API generate endpoint.
508
+ Returns JSON dict - FastAPI will serialize and set Content-Length correctly.
509
+ Never returns 204/304 - always returns 200 with JSON body or 500 error.
510
+ """
511
  try:
512
  data = await request.json()
513
  payload = GeneratePayload(**data)
514
  result = generate_endpoint(payload)
515
+ # Return dict directly - FastAPI will serialize and set Content-Length correctly
516
+ # Ensure we always return a dict with content (never empty)
517
  return {"text": result.text}
518
+ except HTTPException:
519
+ # Re-raise HTTPException as-is - FastAPI handles Content-Length correctly
520
+ raise
521
  except Exception as exc:
522
+ # HTTPException will be handled by FastAPI with proper Content-Length
523
  raise HTTPException(status_code=500, detail=str(exc))
524
 
525
  # Call warm start