cazyundee commited on
Commit
bd1297a
·
verified ·
1 Parent(s): 4d82070

Add API routes directly to Gradio's FastAPI app - no monkey-patch needed

Browse files
Files changed (1) hide show
  1. app.py +41 -51
app.py CHANGED
@@ -3,11 +3,9 @@ import platform
3
  import sys
4
  import shutil
5
  import tempfile
6
- import traceback
7
  import torch
8
  import torchaudio
9
  import gradio as gr
10
- from fastapi import FastAPI
11
  from fastapi.responses import JSONResponse
12
  from einops import rearrange
13
  from huggingface_hub import login
@@ -127,66 +125,58 @@ def generate_audio(prompt, duration, steps, cfg_scale, seed, model_name):
127
 
128
 
129
  # ---------------------------------------------------------------------------
130
- # Gradio UI (built inside a function to keep it out of global scope)
131
  # ---------------------------------------------------------------------------
132
 
133
- def _build_ui():
134
- with gr.Blocks(title="Respite API") as ui:
135
- gr.Markdown("# Respite API - Music & SFX Generation")
136
- gr.Markdown(
137
- "Generate music and sound effects using "
138
- "Stability AI's Stable Audio 3 Small models."
139
- )
140
-
141
- with gr.Row():
142
- with gr.Column():
143
- model_name = gr.Dropdown(
144
- choices=["small-music", "small-sfx"],
145
- value="small-music", label="Model",
146
- )
147
- prompt = gr.Textbox(
148
- label="Prompt",
149
- placeholder="Describe the music or sound effect you want to generate...",
150
- lines=2,
151
- )
152
- duration = gr.Slider(minimum=1, maximum=120, value=30, step=1, label="Duration (seconds)")
153
- steps = gr.Slider(minimum=1, maximum=50, value=8, step=1, label="Steps")
154
- cfg_scale = gr.Slider(minimum=0.0, maximum=10.0, value=1.0, step=0.1, label="CFG Scale")
155
- seed = gr.Number(value=-1, label="Seed (-1 for random)")
156
- btn = gr.Button("Generate", variant="primary")
157
-
158
- with gr.Column():
159
- audio_output = gr.Audio(label="Generated Audio", type="filepath")
160
-
161
- btn.click(
162
- fn=generate_audio,
163
- inputs=[prompt, duration, steps, cfg_scale, seed, model_name],
164
- outputs=audio_output,
165
- )
166
-
167
- return ui
168
 
169
 
170
  # ---------------------------------------------------------------------------
171
- # FastAPI application with discovery routes
 
172
  # ---------------------------------------------------------------------------
173
 
174
- app = FastAPI(title="Respite API")
175
-
176
-
177
- @app.get("/respite/resources")
178
- def resources():
179
  return JSONResponse({"resources": API_RESOURCES})
180
 
181
 
182
- @app.get("/respite/specs")
183
- def specs():
184
  return JSONResponse({**API_SPECS, "runtime": _get_runtime_specs()})
185
 
186
 
187
- # Mount the Gradio UI onto the FastAPI app.
188
- # The HF Spaces Gradio runner should detect `app` (FastAPI) as the
189
- # entrypoint and serve it directly.
190
- app = gr.mount_gradio_app(app, _build_ui(), path="/")
191
 
192
- _log("Respite API application initialized")
 
3
  import sys
4
  import shutil
5
  import tempfile
 
6
  import torch
7
  import torchaudio
8
  import gradio as gr
 
9
  from fastapi.responses import JSONResponse
10
  from einops import rearrange
11
  from huggingface_hub import login
 
125
 
126
 
127
  # ---------------------------------------------------------------------------
128
+ # Gradio UI
129
  # ---------------------------------------------------------------------------
130
 
131
+ with gr.Blocks(title="Respite API") as demo:
132
+ gr.Markdown("# Respite API - Music & SFX Generation")
133
+ gr.Markdown(
134
+ "Generate music and sound effects using "
135
+ "Stability AI's Stable Audio 3 Small models."
136
+ )
137
+
138
+ with gr.Row():
139
+ with gr.Column():
140
+ model_name = gr.Dropdown(
141
+ choices=["small-music", "small-sfx"],
142
+ value="small-music", label="Model",
143
+ )
144
+ prompt = gr.Textbox(
145
+ label="Prompt",
146
+ placeholder="Describe the music or sound effect you want to generate...",
147
+ lines=2,
148
+ )
149
+ duration = gr.Slider(minimum=1, maximum=120, value=30, step=1, label="Duration (seconds)")
150
+ steps = gr.Slider(minimum=1, maximum=50, value=8, step=1, label="Steps")
151
+ cfg_scale = gr.Slider(minimum=0.0, maximum=10.0, value=1.0, step=0.1, label="CFG Scale")
152
+ seed = gr.Number(value=-1, label="Seed (-1 for random)")
153
+ btn = gr.Button("Generate", variant="primary")
154
+
155
+ with gr.Column():
156
+ audio_output = gr.Audio(label="Generated Audio", type="filepath")
157
+
158
+ btn.click(
159
+ fn=generate_audio,
160
+ inputs=[prompt, duration, steps, cfg_scale, seed, model_name],
161
+ outputs=audio_output,
162
+ )
 
 
 
163
 
164
 
165
  # ---------------------------------------------------------------------------
166
+ # Add discovery routes directly to Gradio's internal FastAPI app.
167
+ # demo.app exists as a FastAPI-compatible app after the Blocks context exits.
168
  # ---------------------------------------------------------------------------
169
 
170
+ @demo.app.get("/respite/resources")
171
+ def _resources():
 
 
 
172
  return JSONResponse({"resources": API_RESOURCES})
173
 
174
 
175
+ @demo.app.get("/respite/specs")
176
+ def _specs():
177
  return JSONResponse({**API_SPECS, "runtime": _get_runtime_specs()})
178
 
179
 
180
+ _log("Respite API routes registered")
 
 
 
181
 
182
+ demo.queue(max_size=4, default_concurrency_limit=1)