cazyundee commited on
Commit
8759bb2
·
verified ·
1 Parent(s): 97b549d

Inject Respite routes into Gradio server via launch monkey-patch

Browse files
Files changed (1) hide show
  1. app.py +39 -25
app.py CHANGED
@@ -7,7 +7,6 @@ import torch
7
  import torchaudio
8
  import gradio as gr
9
  import spaces
10
- from fastapi import FastAPI
11
  from fastapi.responses import JSONResponse
12
  from einops import rearrange
13
  from huggingface_hub import login
@@ -134,23 +133,6 @@ def generate_audio(prompt, duration, steps, cfg_scale, seed, model_name):
134
  return output_path
135
 
136
 
137
- # ---------------------------------------------------------------------------
138
- # FastAPI application with discovery routes
139
- # ---------------------------------------------------------------------------
140
-
141
- api = FastAPI(title=API_SPECS["name"], version=API_SPECS["version"])
142
-
143
-
144
- @api.get("/api/resources")
145
- def resources():
146
- return JSONResponse({"resources": API_RESOURCES})
147
-
148
-
149
- @api.get("/api/specs")
150
- def specs():
151
- return JSONResponse({**API_SPECS, "runtime": _get_runtime_specs()})
152
-
153
-
154
  # ---------------------------------------------------------------------------
155
  # Gradio UI
156
  # ---------------------------------------------------------------------------
@@ -189,11 +171,43 @@ with gr.Blocks(title="Respite API") as demo:
189
  )
190
 
191
 
192
- # Mount Gradio under the FastAPI app and export as `app`.
193
- # HF Spaces Gradio runner finds the Blocks `demo` and calls demo.launch().
194
- # The `app` variable is here for direct uvicorn execution.
195
- demo.queue(max_size=4, default_concurrency_limit=1)
196
- app = gr.mount_gradio_app(api, demo, path="/")
 
 
 
197
 
198
- if __name__ == "__main__":
199
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  import torchaudio
8
  import gradio as gr
9
  import spaces
 
10
  from fastapi.responses import JSONResponse
11
  from einops import rearrange
12
  from huggingface_hub import login
 
133
  return output_path
134
 
135
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
  # ---------------------------------------------------------------------------
137
  # Gradio UI
138
  # ---------------------------------------------------------------------------
 
171
  )
172
 
173
 
174
+ # ---------------------------------------------------------------------------
175
+ # Inject discovery routes into Gradio's own FastAPI server.
176
+ #
177
+ # The HF Spaces Gradio runner calls demo.launch() directly. Our FastAPI
178
+ # routes must be registered on Gradio's internal FastAPI app AFTER the
179
+ # server is created. We monkey-patch Blocks.launch to intercept that call
180
+ # and inject the routes before the server begins accepting traffic.
181
+ # ---------------------------------------------------------------------------
182
 
183
+ _original_launch = gr.Blocks.launch
184
+
185
+
186
+ def _inject_respite_routes(block_demo):
187
+ """Add Respite API discovery routes to the running Gradio FastAPI app."""
188
+ fa_app = getattr(block_demo, "app", None)
189
+ if fa_app is None:
190
+ _log("Warning: could not access Gradio FastAPI app for route injection")
191
+ return
192
+
193
+ @fa_app.get("/api/resources")
194
+ def _resources():
195
+ return JSONResponse({"resources": API_RESOURCES})
196
+
197
+ @fa_app.get("/api/specs")
198
+ def _specs():
199
+ return JSONResponse({**API_SPECS, "runtime": _get_runtime_specs()})
200
+
201
+ _log("Respite API routes injected into Gradio server")
202
+
203
+
204
+ def _patched_launch(self, *args, **kwargs):
205
+ result = _original_launch(self, *args, **kwargs)
206
+ if self is demo:
207
+ _inject_respite_routes(self)
208
+ return result
209
+
210
+
211
+ gr.Blocks.launch = _patched_launch
212
+
213
+ demo.queue(max_size=4, default_concurrency_limit=1)