cazyundee commited on
Commit
38ffac8
·
verified ·
1 Parent(s): d5b7b54

Debug monkey-patch with error logging

Browse files
Files changed (1) hide show
  1. app.py +46 -22
app.py CHANGED
@@ -3,6 +3,7 @@ import platform
3
  import sys
4
  import shutil
5
  import tempfile
 
6
  import torch
7
  import torchaudio
8
  import gradio as gr
@@ -134,23 +135,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("/respite/resources")
145
- def resources():
146
- return JSONResponse({"resources": API_RESOURCES})
147
-
148
-
149
- @api.get("/respite/specs")
150
- def specs():
151
- return JSONResponse({**API_SPECS, "runtime": _get_runtime_specs()})
152
-
153
-
154
  # ---------------------------------------------------------------------------
155
  # Gradio UI
156
  # ---------------------------------------------------------------------------
@@ -189,9 +173,49 @@ with gr.Blocks(title="Respite API") as demo:
189
  )
190
 
191
 
192
- # Mount Gradio under the FastAPI app.
193
- demo.queue(max_size=4, default_concurrency_limit=1)
194
- app = gr.mount_gradio_app(api, demo, path="/")
 
 
 
 
195
 
196
- if __name__ == "__main__":
197
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  import sys
4
  import shutil
5
  import tempfile
6
+ import traceback
7
  import torch
8
  import torchaudio
9
  import gradio as gr
 
135
  return output_path
136
 
137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  # ---------------------------------------------------------------------------
139
  # Gradio UI
140
  # ---------------------------------------------------------------------------
 
173
  )
174
 
175
 
176
+ # ---------------------------------------------------------------------------
177
+ # Inject discovery routes into Gradio's internal FastAPI server.
178
+ #
179
+ # The HF Spaces Gradio runner calls demo.launch() directly, so our own
180
+ # FastAPI app is never served. We monkey-patch Blocks.launch so that
181
+ # AFTER Gradio creates its server, we inject routes onto that server.
182
+ # ---------------------------------------------------------------------------
183
 
184
+ _original_blocks_launch = gr.Blocks.launch
185
+
186
+
187
+ def _patched_blocks_launch(self, *args, **kwargs):
188
+ _log("Intercepted Blocks.launch() - calling original...")
189
+ try:
190
+ result = _original_blocks_launch(self, *args, **kwargs)
191
+ except Exception:
192
+ _log("ERROR in original launch:")
193
+ _log(traceback.format_exc())
194
+ raise
195
+
196
+ if self is demo:
197
+ try:
198
+ fa_app = getattr(self, "app", None)
199
+ _log(f"demo.app is: {fa_app}")
200
+ if fa_app is not None:
201
+ @fa_app.get("/respite/resources")
202
+ def _resources():
203
+ return JSONResponse({"resources": API_RESOURCES})
204
+
205
+ @fa_app.get("/respite/specs")
206
+ def _specs():
207
+ return JSONResponse({**API_SPECS, "runtime": _get_runtime_specs()})
208
+
209
+ _log("Respite API routes injected successfully")
210
+ else:
211
+ _log("Warning: demo.app is None after launch")
212
+ except Exception:
213
+ _log("ERROR injecting routes:")
214
+ _log(traceback.format_exc())
215
+
216
+ return result
217
+
218
+
219
+ gr.Blocks.launch = _patched_blocks_launch
220
+
221
+ demo.queue(max_size=4, default_concurrency_limit=1)