cazyundee commited on
Commit
e926870
·
verified ·
1 Parent(s): b1fcc10

Patch demo.launch to inject FastAPI routes into Gradio server

Browse files
Files changed (1) hide show
  1. app.py +35 -21
app.py CHANGED
@@ -105,6 +105,23 @@ def _get_runtime_specs():
105
  }
106
 
107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  # ---------------------------------------------------------------------------
109
  # Model cache
110
  # ---------------------------------------------------------------------------
@@ -156,23 +173,6 @@ def generate_audio(prompt, duration, steps, cfg_scale, seed, model_name):
156
  return output_path
157
 
158
 
159
- # ---------------------------------------------------------------------------
160
- # FastAPI application
161
- # ---------------------------------------------------------------------------
162
-
163
- api = FastAPI(title=API_SPECS["name"], version=API_SPECS["version"])
164
-
165
-
166
- @api.get("/api/resources")
167
- def resources():
168
- return JSONResponse({"resources": API_RESOURCES})
169
-
170
-
171
- @api.get("/api/specs")
172
- def specs():
173
- return JSONResponse({**API_SPECS, "runtime": _get_runtime_specs()})
174
-
175
-
176
  # ---------------------------------------------------------------------------
177
  # Gradio UI
178
  # ---------------------------------------------------------------------------
@@ -217,9 +217,23 @@ with gr.Blocks(title="Respite API") as demo:
217
  outputs=audio_output,
218
  )
219
 
 
 
 
 
 
 
 
 
 
220
  demo.queue(max_size=4, default_concurrency_limit=1)
221
 
222
- # Mount Gradio UI under the FastAPI app.
223
- # Routes are defined on `api` first, then Gradio is mounted at "/".
224
- # FastAPI evaluates routes top-down, so /api/* routes are served directly.
225
- app = gr.mount_gradio_app(api, demo, path="/")
 
 
 
 
 
 
105
  }
106
 
107
 
108
+ # ---------------------------------------------------------------------------
109
+ # FastAPI application with discovery routes
110
+ # ---------------------------------------------------------------------------
111
+
112
+ api = FastAPI(title=API_SPECS["name"], version=API_SPECS["version"])
113
+
114
+
115
+ @api.get("/api/resources")
116
+ def resources():
117
+ return JSONResponse({"resources": API_RESOURCES})
118
+
119
+
120
+ @api.get("/api/specs")
121
+ def specs():
122
+ return JSONResponse({**API_SPECS, "runtime": _get_runtime_specs()})
123
+
124
+
125
  # ---------------------------------------------------------------------------
126
  # Model cache
127
  # ---------------------------------------------------------------------------
 
173
  return output_path
174
 
175
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176
  # ---------------------------------------------------------------------------
177
  # Gradio UI
178
  # ---------------------------------------------------------------------------
 
217
  outputs=audio_output,
218
  )
219
 
220
+
221
+ # ---------------------------------------------------------------------------
222
+ # Patch launch() so the HF Spaces runner picks up our FastAPI routes.
223
+ #
224
+ # The Gradio SDK runner calls demo.launch() directly, which normally creates
225
+ # its own FastAPI app. By intercepting that call we inject our routes so
226
+ # /api/resources and /api/specs are served alongside the Gradio UI.
227
+ # ---------------------------------------------------------------------------
228
+
229
  demo.queue(max_size=4, default_concurrency_limit=1)
230
 
231
+ _original_launch = demo.launch
232
+
233
+
234
+ def _patched_launch(*args, **kwargs):
235
+ kwargs.setdefault("app_kwargs", {})["app"] = api
236
+ return _original_launch(*args, **kwargs)
237
+
238
+
239
+ demo.launch = _patched_launch