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

Use /respite/ prefix with working Gradio mount structure

Browse files
Files changed (1) hide show
  1. app.py +26 -42
app.py CHANGED
@@ -7,6 +7,7 @@ import torch
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
@@ -70,11 +71,11 @@ API_SPECS = {
70
  "name": "Respite API",
71
  "version": "1.0.0",
72
  "description": "General-purpose AI API server with audio generation capabilities.",
73
- "base_path": "/api",
74
  "authentication": "none",
75
  "content_types": ["application/json", "audio/wav"],
76
- "resources_endpoint": "/api/resources",
77
- "specs_endpoint": "/api/specs",
78
  "limits": {
79
  "max_concurrent_requests": 1,
80
  "max_queue_size": 4,
@@ -133,6 +134,23 @@ def generate_audio(prompt, duration, steps, cfg_scale, seed, model_name):
133
  return output_path
134
 
135
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
  # ---------------------------------------------------------------------------
137
  # Gradio UI
138
  # ---------------------------------------------------------------------------
@@ -171,43 +189,9 @@ with gr.Blocks(title="Respite API") as demo:
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)
 
 
 
 
 
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
 
71
  "name": "Respite API",
72
  "version": "1.0.0",
73
  "description": "General-purpose AI API server with audio generation capabilities.",
74
+ "base_path": "/respite",
75
  "authentication": "none",
76
  "content_types": ["application/json", "audio/wav"],
77
+ "resources_endpoint": "/respite/resources",
78
+ "specs_endpoint": "/respite/specs",
79
  "limits": {
80
  "max_concurrent_requests": 1,
81
  "max_queue_size": 4,
 
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
  )
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()