Spaces:
Running on Zero
Running on Zero
Clean Respite API: FastAPI app with Gradio mounted, no GPU touch
Browse files
app.py
CHANGED
|
@@ -7,7 +7,6 @@ import traceback
|
|
| 7 |
import torch
|
| 8 |
import torchaudio
|
| 9 |
import gradio as gr
|
| 10 |
-
import spaces
|
| 11 |
from fastapi import FastAPI
|
| 12 |
from fastapi.responses import JSONResponse
|
| 13 |
from einops import rearrange
|
|
@@ -22,14 +21,6 @@ hf_token = os.environ.get("HF_TOKEN")
|
|
| 22 |
if hf_token:
|
| 23 |
login(token=hf_token)
|
| 24 |
|
| 25 |
-
try:
|
| 26 |
-
@spaces.GPU(duration=1)
|
| 27 |
-
def _gpu_startup_touch():
|
| 28 |
-
return "ok"
|
| 29 |
-
_gpu_startup_touch()
|
| 30 |
-
except Exception as e:
|
| 31 |
-
print(f"Warning: ZeroGPU touch failed (running CPU-only): {e}", flush=True)
|
| 32 |
-
|
| 33 |
|
| 34 |
def _log(message):
|
| 35 |
print(message.encode("ascii", "backslashreplace").decode("ascii"), flush=True)
|
|
@@ -54,7 +45,7 @@ API_RESOURCES = {
|
|
| 54 |
"audio_generation": {
|
| 55 |
"name": "Audio generation",
|
| 56 |
"description": "Generate music or sound effects from a text prompt.",
|
| 57 |
-
"endpoint": "/
|
| 58 |
"method": "POST",
|
| 59 |
"input": {
|
| 60 |
"prompt": "string",
|
|
@@ -136,86 +127,66 @@ def generate_audio(prompt, duration, steps, cfg_scale, seed, model_name):
|
|
| 136 |
|
| 137 |
|
| 138 |
# ---------------------------------------------------------------------------
|
| 139 |
-
# Gradio UI
|
| 140 |
# ---------------------------------------------------------------------------
|
| 141 |
|
| 142 |
-
|
| 143 |
-
gr.
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
with gr.
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
|
| 176 |
# ---------------------------------------------------------------------------
|
| 177 |
-
#
|
| 178 |
-
#
|
| 179 |
-
# The HF Spaces Gradio runner calls demo.launch() directly, so our own
|
| 180 |
-
# FastAPI app is never served. We monkey-patch Blocks.launch so that
|
| 181 |
-
# AFTER Gradio creates its server, we inject routes onto that server.
|
| 182 |
# ---------------------------------------------------------------------------
|
| 183 |
|
| 184 |
-
|
| 185 |
|
| 186 |
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
return JSONResponse({"resources": API_RESOURCES})
|
| 204 |
-
|
| 205 |
-
@fa_app.get("/respite/specs")
|
| 206 |
-
def _specs():
|
| 207 |
-
return JSONResponse({**API_SPECS, "runtime": _get_runtime_specs()})
|
| 208 |
-
|
| 209 |
-
_log("Respite API routes injected successfully")
|
| 210 |
-
else:
|
| 211 |
-
_log("Warning: demo.app is None after launch")
|
| 212 |
-
except Exception:
|
| 213 |
-
_log("ERROR injecting routes:")
|
| 214 |
-
_log(traceback.format_exc())
|
| 215 |
-
|
| 216 |
-
return result
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
gr.Blocks.launch = _patched_blocks_launch
|
| 220 |
-
|
| 221 |
-
demo.queue(max_size=4, default_concurrency_limit=1)
|
|
|
|
| 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
|
|
|
|
| 21 |
if hf_token:
|
| 22 |
login(token=hf_token)
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
def _log(message):
|
| 26 |
print(message.encode("ascii", "backslashreplace").decode("ascii"), flush=True)
|
|
|
|
| 45 |
"audio_generation": {
|
| 46 |
"name": "Audio generation",
|
| 47 |
"description": "Generate music or sound effects from a text prompt.",
|
| 48 |
+
"endpoint": "/respite/audio/generate",
|
| 49 |
"method": "POST",
|
| 50 |
"input": {
|
| 51 |
"prompt": "string",
|
|
|
|
| 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")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|