Spaces:
Running on Zero
Running on Zero
Patch App.create_app to inject routes into served app
Browse files
app.py
CHANGED
|
@@ -3,8 +3,8 @@ import platform
|
|
| 3 |
import sys
|
| 4 |
import shutil
|
| 5 |
import tempfile
|
| 6 |
-
import spaces # must come before torch to avoid CUDA init error
|
| 7 |
import torch
|
|
|
|
| 8 |
import torchaudio
|
| 9 |
import gradio as gr
|
| 10 |
from fastapi.responses import JSONResponse
|
|
@@ -177,21 +177,34 @@ with gr.Blocks(title="Respite API") as demo:
|
|
| 177 |
|
| 178 |
|
| 179 |
# ---------------------------------------------------------------------------
|
| 180 |
-
#
|
| 181 |
-
#
|
|
|
|
|
|
|
|
|
|
| 182 |
# ---------------------------------------------------------------------------
|
| 183 |
|
| 184 |
-
|
| 185 |
-
def _resources():
|
| 186 |
-
return JSONResponse({"resources": API_RESOURCES})
|
| 187 |
|
|
|
|
| 188 |
|
| 189 |
-
@demo.app.get("/respite/specs")
|
| 190 |
-
def _specs():
|
| 191 |
-
return JSONResponse({**API_SPECS, "runtime": _get_runtime_specs()})
|
| 192 |
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
demo.queue(max_size=4, default_concurrency_limit=1)
|
| 197 |
-
demo.launch(ssr_mode=False)
|
|
|
|
| 3 |
import sys
|
| 4 |
import shutil
|
| 5 |
import tempfile
|
|
|
|
| 6 |
import torch
|
| 7 |
+
import spaces
|
| 8 |
import torchaudio
|
| 9 |
import gradio as gr
|
| 10 |
from fastapi.responses import JSONResponse
|
|
|
|
| 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():
|
| 198 |
+
return JSONResponse({"resources": API_RESOURCES})
|
| 199 |
+
|
| 200 |
+
@fa_app.get("/respite/specs")
|
| 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)
|
|
|