victor HF Staff commited on
Commit
797f91d
·
1 Parent(s): 646e0c7

Fix: Use demo.launch() for ZeroGPU, insert route at position 0 for priority

Browse files

- Revert to demo.launch() which is required for ZeroGPU detection
- Insert custom route at position 0 in demo.app.routes to take priority over Gradio's catch-all
- Remove unused FastAPI app creation

Files changed (1) hide show
  1. app.py +13 -11
app.py CHANGED
@@ -11,7 +11,7 @@ import re
11
  import sys
12
  import warnings
13
 
14
- from fastapi import FastAPI, Request
15
  from fastapi.responses import StreamingResponse
16
 
17
  from diffusers import AutoencoderKL, FlowMatchEulerDiscreteScheduler
@@ -586,12 +586,7 @@ with gr.Blocks(title="Z-Image Generation MCP") as demo:
586
  api_name="generate_image", # nice, stable name for tool clients
587
  )
588
 
589
- # ==================== FastAPI App with Custom Routes ====================
590
- # Create our own FastAPI app so custom routes take precedence over Gradio's catch-all
591
- app = FastAPI()
592
-
593
-
594
- @app.get("/generate/{prompt_path:path}")
595
  def http_generate_png(
596
  prompt_path: str,
597
  request: Request,
@@ -691,9 +686,16 @@ def http_generate_png(
691
  return StreamingResponse(io.BytesIO(png_bytes), media_type="image/png")
692
 
693
 
694
- # Mount Gradio app onto our FastAPI app
695
- app = gr.mount_gradio_app(app, demo, path="/")
 
 
 
 
 
 
 
 
696
 
697
  if __name__ == "__main__":
698
- import uvicorn
699
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
11
  import sys
12
  import warnings
13
 
14
+ from fastapi import Request
15
  from fastapi.responses import StreamingResponse
16
 
17
  from diffusers import AutoencoderKL, FlowMatchEulerDiscreteScheduler
 
586
  api_name="generate_image", # nice, stable name for tool clients
587
  )
588
 
589
+ # ==================== /generate/<prompt>.png Endpoint ====================
 
 
 
 
 
590
  def http_generate_png(
591
  prompt_path: str,
592
  request: Request,
 
686
  return StreamingResponse(io.BytesIO(png_bytes), media_type="image/png")
687
 
688
 
689
+ # Add custom route to Gradio's FastAPI app (insert at position 0 for priority)
690
+ from fastapi.routing import APIRoute
691
+ demo.app.routes.insert(
692
+ 0,
693
+ APIRoute(
694
+ "/generate/{prompt_path:path}",
695
+ http_generate_png,
696
+ methods=["GET"],
697
+ ),
698
+ )
699
 
700
  if __name__ == "__main__":
701
+ demo.launch(mcp_server=True)