Spaces:
Running on Zero
Running on Zero
Add API routes directly to Gradio's FastAPI app - no monkey-patch needed
Browse files
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
|
| 131 |
# ---------------------------------------------------------------------------
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
with gr.
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
)
|
| 166 |
-
|
| 167 |
-
return ui
|
| 168 |
|
| 169 |
|
| 170 |
# ---------------------------------------------------------------------------
|
| 171 |
-
#
|
|
|
|
| 172 |
# ---------------------------------------------------------------------------
|
| 173 |
|
| 174 |
-
app
|
| 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
|
| 184 |
return JSONResponse({**API_SPECS, "runtime": _get_runtime_specs()})
|
| 185 |
|
| 186 |
|
| 187 |
-
|
| 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 |
-
|
|
|
|
| 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)
|