cazyundee commited on
Commit
6d16737
·
verified ·
1 Parent(s): 618d7e8

Fix staticmethod patching via __func__

Browse files
Files changed (1) hide show
  1. app.py +11 -10
app.py CHANGED
@@ -11,6 +11,7 @@ from fastapi.responses import JSONResponse
11
  from einops import rearrange
12
  from huggingface_hub import login
13
  from stable_audio_3 import StableAudioModel
 
14
 
15
  for _stream in (sys.stdout, sys.stderr):
16
  if hasattr(_stream, "reconfigure"):
@@ -177,21 +178,21 @@ with gr.Blocks(title="Respite API") as demo:
177
 
178
 
179
  # ---------------------------------------------------------------------------
180
- # Monkey-patch App.create_app to inject discovery routes.
181
  #
182
- # Gradio's Blocks.launch() calls App.create_app() which creates a brand-new
183
- # FastAPI app, discarding anything we put on demo.app earlier. By wrapping
184
- # create_app we can add our routes to the app that actually gets served.
 
185
  # ---------------------------------------------------------------------------
186
 
187
  from gradio.routes import App as GradioApp
188
 
189
- _original_create_app = GradioApp.create_app
190
 
191
 
192
- @staticmethod
193
- def _patched_create_app(blocks=None, **kwargs):
194
- fa_app = _original_create_app(blocks=blocks, **kwargs)
195
 
196
  @fa_app.get("/respite/resources")
197
  def _resources():
@@ -201,10 +202,10 @@ def _patched_create_app(blocks=None, **kwargs):
201
  def _specs():
202
  return JSONResponse({**API_SPECS, "runtime": _get_runtime_specs()})
203
 
204
- _log("Respite API discovery routes injected into Gradio app")
205
  return fa_app
206
 
207
 
208
- GradioApp.create_app = _patched_create_app
209
 
210
  demo.queue(max_size=4, default_concurrency_limit=1)
 
11
  from einops import rearrange
12
  from huggingface_hub import login
13
  from stable_audio_3 import StableAudioModel
14
+ from contextlib import asynccontextmanager
15
 
16
  for _stream in (sys.stdout, sys.stderr):
17
  if hasattr(_stream, "reconfigure"):
 
178
 
179
 
180
  # ---------------------------------------------------------------------------
181
+ # Inject discovery routes into the Gradio FastAPI app.
182
  #
183
+ # Gradio's Blocks.launch() creates a new FastAPI app via App.create_app()
184
+ # and any routes on demo.app before launch() are lost. We intercept the
185
+ # app creation by wrapping the FastAPI app returned by the Gradio server's
186
+ # configure_app / create_app lifecycle, then add our routes at startup.
187
  # ---------------------------------------------------------------------------
188
 
189
  from gradio.routes import App as GradioApp
190
 
191
+ _orig_create_app = GradioApp.create_app.__func__
192
 
193
 
194
+ def _patched_create_app(blocks, **kwargs):
195
+ fa_app = _orig_create_app(blocks, **kwargs)
 
196
 
197
  @fa_app.get("/respite/resources")
198
  def _resources():
 
202
  def _specs():
203
  return JSONResponse({**API_SPECS, "runtime": _get_runtime_specs()})
204
 
205
+ _log("Respite API discovery routes injected")
206
  return fa_app
207
 
208
 
209
+ GradioApp.create_app = staticmethod(_patched_create_app)
210
 
211
  demo.queue(max_size=4, default_concurrency_limit=1)