Spaces:
Running on Zero
Running on Zero
Switch to Docker SDK for full ASGI control
Browse files
app.py
CHANGED
|
@@ -7,6 +7,11 @@ import torch
|
|
| 7 |
import torchaudio
|
| 8 |
import gradio as gr
|
| 9 |
import spaces
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Hugging Face Spaces should use UTF-8; explicitly configure streams so model
|
| 12 |
# libraries cannot inherit a platform-specific charmap encoding.
|
|
@@ -14,11 +19,6 @@ for _stream in (sys.stdout, sys.stderr):
|
|
| 14 |
if hasattr(_stream, "reconfigure"):
|
| 15 |
_stream.reconfigure(encoding="utf-8", errors="backslashreplace")
|
| 16 |
|
| 17 |
-
from fastapi.responses import JSONResponse
|
| 18 |
-
from einops import rearrange
|
| 19 |
-
from huggingface_hub import login
|
| 20 |
-
from stable_audio_3 import StableAudioModel
|
| 21 |
-
|
| 22 |
# Authenticate with gated model when HF_TOKEN secret is present
|
| 23 |
hf_token = os.environ.get("HF_TOKEN")
|
| 24 |
if hf_token:
|
|
@@ -156,6 +156,23 @@ def generate_audio(prompt, duration, steps, cfg_scale, seed, model_name):
|
|
| 156 |
return output_path
|
| 157 |
|
| 158 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
# ---------------------------------------------------------------------------
|
| 160 |
# Gradio UI
|
| 161 |
# ---------------------------------------------------------------------------
|
|
@@ -200,20 +217,9 @@ with gr.Blocks(title="Respite API") as demo:
|
|
| 200 |
outputs=audio_output,
|
| 201 |
)
|
| 202 |
|
| 203 |
-
|
| 204 |
-
# ---------------------------------------------------------------------------
|
| 205 |
-
# Mount API discovery routes directly onto Gradio's FastAPI app.
|
| 206 |
-
# This avoids conflicts with Gradio's internal /api proxy.
|
| 207 |
-
# ---------------------------------------------------------------------------
|
| 208 |
-
|
| 209 |
demo.queue(max_size=4, default_concurrency_limit=1)
|
| 210 |
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
@demo.app.get("/api/specs")
|
| 218 |
-
def specs():
|
| 219 |
-
return JSONResponse({**API_SPECS, "runtime": _get_runtime_specs()})
|
|
|
|
| 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
|
| 14 |
+
from stable_audio_3 import StableAudioModel
|
| 15 |
|
| 16 |
# Hugging Face Spaces should use UTF-8; explicitly configure streams so model
|
| 17 |
# libraries cannot inherit a platform-specific charmap encoding.
|
|
|
|
| 19 |
if hasattr(_stream, "reconfigure"):
|
| 20 |
_stream.reconfigure(encoding="utf-8", errors="backslashreplace")
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# Authenticate with gated model when HF_TOKEN secret is present
|
| 23 |
hf_token = os.environ.get("HF_TOKEN")
|
| 24 |
if hf_token:
|
|
|
|
| 156 |
return output_path
|
| 157 |
|
| 158 |
|
| 159 |
+
# ---------------------------------------------------------------------------
|
| 160 |
+
# FastAPI application
|
| 161 |
+
# ---------------------------------------------------------------------------
|
| 162 |
+
|
| 163 |
+
api = FastAPI(title=API_SPECS["name"], version=API_SPECS["version"])
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
@api.get("/api/resources")
|
| 167 |
+
def resources():
|
| 168 |
+
return JSONResponse({"resources": API_RESOURCES})
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
@api.get("/api/specs")
|
| 172 |
+
def specs():
|
| 173 |
+
return JSONResponse({**API_SPECS, "runtime": _get_runtime_specs()})
|
| 174 |
+
|
| 175 |
+
|
| 176 |
# ---------------------------------------------------------------------------
|
| 177 |
# Gradio UI
|
| 178 |
# ---------------------------------------------------------------------------
|
|
|
|
| 217 |
outputs=audio_output,
|
| 218 |
)
|
| 219 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
demo.queue(max_size=4, default_concurrency_limit=1)
|
| 221 |
|
| 222 |
+
# Mount Gradio UI under the FastAPI app.
|
| 223 |
+
# Routes are defined on `api` first, then Gradio is mounted at "/".
|
| 224 |
+
# FastAPI evaluates routes top-down, so /api/* routes are served directly.
|
| 225 |
+
app = gr.mount_gradio_app(api, demo, path="/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|