Spaces:
Runtime error

AxionLabsAI commited on
Commit
0c68577
Β·
1 Parent(s): a8d4641

Expose API routes through ZeroGPU Gradio launcher

Browse files
Files changed (1) hide show
  1. app.py +15 -16
app.py CHANGED
@@ -5,8 +5,8 @@ import uuid
5
  import asyncio
6
  import threading
7
  import queue as queue_mod
8
- import uvicorn
9
  import gradio as gr
 
10
  from fastapi import FastAPI, Request
11
  from fastapi.responses import StreamingResponse, JSONResponse
12
  from huggingface_hub import hf_hub_download
@@ -127,6 +127,12 @@ def _load_model():
127
  threading.Thread(target=_load_model, daemon=True).start()
128
 
129
 
 
 
 
 
 
 
130
  # ── Gradio chat helpers ───────────────────────────────────────────────────────
131
 
132
  def user_submit(message, history):
@@ -333,9 +339,10 @@ with gr.Blocks(title="Lumen \u2014 Axion Labs") as demo:
333
 
334
 
335
  # ── FastAPI routes ────────────────────────────────────────────────────────────
336
- # Gradio 6 serves Blocks through an outer app, so routes attached to demo.app
337
- # are not reachable from the public Space URL. Register the API on a parent
338
- # FastAPI app, then mount the Gradio UI after the API routes are defined.
 
339
 
340
  api_app = FastAPI()
341
 
@@ -449,17 +456,9 @@ async def chat_completions(request: Request):
449
 
450
  # ── Launch ───────────────────────────────────────────────────────────────────
451
 
452
- app = gr.mount_gradio_app(
453
- api_app,
454
- demo,
455
- path="/",
456
- theme=THEME,
457
- css=CSS,
458
- )
459
-
460
  if __name__ == "__main__":
461
- uvicorn.run(
462
- app,
463
- host="0.0.0.0",
464
- port=int(os.environ.get("PORT", "7860")),
465
  )
 
5
  import asyncio
6
  import threading
7
  import queue as queue_mod
 
8
  import gradio as gr
9
+ import spaces
10
  from fastapi import FastAPI, Request
11
  from fastapi.responses import StreamingResponse, JSONResponse
12
  from huggingface_hub import hf_hub_download
 
127
  threading.Thread(target=_load_model, daemon=True).start()
128
 
129
 
130
+ # ZeroGPU requires one decorated function even though inference stays on CPU.
131
+ @spaces.GPU
132
+ def _zerogpu_compatibility_marker():
133
+ return True
134
+
135
+
136
  # ── Gradio chat helpers ───────────────────────────────────────────────────────
137
 
138
  def user_submit(message, history):
 
339
 
340
 
341
  # ── FastAPI routes ────────────────────────────────────────────────────────────
342
+ # Gradio 6 recreates its FastAPI app during demo.launch(), and its catch-all
343
+ # routes shadow custom routes attached directly to demo.app. Build the API
344
+ # routes separately and pass them into the new app at construction time so
345
+ # they are registered before Gradio's catch-all routes.
346
 
347
  api_app = FastAPI()
348
 
 
456
 
457
  # ── Launch ───────────────────────────────────────────────────────────────────
458
 
 
 
 
 
 
 
 
 
459
  if __name__ == "__main__":
460
+ demo.launch(
461
+ theme=THEME,
462
+ css=CSS,
463
+ app_kwargs={"routes": api_app.router.routes},
464
  )