cazyundee commited on
Commit
97b549d
·
verified ·
1 Parent(s): 21e68df

Restore working structure with FastAPI + Gradio mount

Browse files
Files changed (1) hide show
  1. app.py +41 -6
app.py CHANGED
@@ -7,16 +7,16 @@ import torch
7
  import torchaudio
8
  import gradio as gr
9
  import spaces
10
-
11
- for _stream in (sys.stdout, sys.stderr):
12
- if hasattr(_stream, "reconfigure"):
13
- _stream.reconfigure(encoding="utf-8", errors="backslashreplace")
14
-
15
  from fastapi.responses import JSONResponse
16
  from einops import rearrange
17
  from huggingface_hub import login
18
  from stable_audio_3 import StableAudioModel
19
 
 
 
 
 
20
  hf_token = os.environ.get("HF_TOKEN")
21
  if hf_token:
22
  login(token=hf_token)
@@ -45,6 +45,10 @@ def _get_ram_bytes():
45
  return None
46
 
47
 
 
 
 
 
48
  API_RESOURCES = {
49
  "audio_generation": {
50
  "name": "Audio generation",
@@ -93,6 +97,10 @@ def _get_runtime_specs():
93
  }
94
 
95
 
 
 
 
 
96
  MODEL_CACHE = {}
97
 
98
 
@@ -126,6 +134,27 @@ def generate_audio(prompt, duration, steps, cfg_scale, seed, model_name):
126
  return output_path
127
 
128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
  with gr.Blocks(title="Respite API") as demo:
130
  gr.Markdown("# Respite API - Music & SFX Generation")
131
  gr.Markdown(
@@ -160,5 +189,11 @@ with gr.Blocks(title="Respite API") as demo:
160
  )
161
 
162
 
163
- # This is the only thing that matters for the HF Gradio runner.
 
 
164
  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
14
  from stable_audio_3 import StableAudioModel
15
 
16
+ for _stream in (sys.stdout, sys.stderr):
17
+ if hasattr(_stream, "reconfigure"):
18
+ _stream.reconfigure(encoding="utf-8", errors="backslashreplace")
19
+
20
  hf_token = os.environ.get("HF_TOKEN")
21
  if hf_token:
22
  login(token=hf_token)
 
45
  return None
46
 
47
 
48
+ # ---------------------------------------------------------------------------
49
+ # API metadata
50
+ # ---------------------------------------------------------------------------
51
+
52
  API_RESOURCES = {
53
  "audio_generation": {
54
  "name": "Audio generation",
 
97
  }
98
 
99
 
100
+ # ---------------------------------------------------------------------------
101
+ # Model cache
102
+ # ---------------------------------------------------------------------------
103
+
104
  MODEL_CACHE = {}
105
 
106
 
 
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("/api/resources")
145
+ def resources():
146
+ return JSONResponse({"resources": API_RESOURCES})
147
+
148
+
149
+ @api.get("/api/specs")
150
+ def specs():
151
+ return JSONResponse({**API_SPECS, "runtime": _get_runtime_specs()})
152
+
153
+
154
+ # ---------------------------------------------------------------------------
155
+ # Gradio UI
156
+ # ---------------------------------------------------------------------------
157
+
158
  with gr.Blocks(title="Respite API") as demo:
159
  gr.Markdown("# Respite API - Music & SFX Generation")
160
  gr.Markdown(
 
189
  )
190
 
191
 
192
+ # Mount Gradio under the FastAPI app and export as `app`.
193
+ # HF Spaces Gradio runner finds the Blocks `demo` and calls demo.launch().
194
+ # The `app` variable is here for direct uvicorn execution.
195
  demo.queue(max_size=4, default_concurrency_limit=1)
196
+ app = gr.mount_gradio_app(api, demo, path="/")
197
+
198
+ if __name__ == "__main__":
199
+ demo.launch()