cazyundee commited on
Commit
8be6d5e
·
verified ·
1 Parent(s): 011b234

Add Respite API discovery metadata

Browse files
Files changed (1) hide show
  1. app.py +103 -1
app.py CHANGED
@@ -1,9 +1,13 @@
1
  import os
 
 
2
  import tempfile
3
  import torch
4
  import torchaudio
5
  import gradio as gr
6
  import spaces
 
 
7
  from einops import rearrange
8
  from huggingface_hub import login
9
  from stable_audio_3 import StableAudioModel
@@ -27,6 +31,89 @@ except Exception as e:
27
  print(f"Warning: ZeroGPU touch failed (running CPU-only): {e}")
28
 
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  # Model cache
31
  MODEL_CACHE = {}
32
 
@@ -81,6 +168,19 @@ def generate_audio(prompt, duration, steps, cfg_scale, seed, model_name):
81
  return output_path
82
 
83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  with gr.Blocks(title="Stable Audio 3 Small") as demo:
85
  gr.Markdown("# 🎵 Stable Audio 3 Small - Music & SFX Generation")
86
  gr.Markdown("Generate music and sound effects using Stability AI's Stable Audio 3 Small models. Runs on CPU.")
@@ -127,4 +227,6 @@ with gr.Blocks(title="Stable Audio 3 Small") as demo:
127
  )
128
 
129
 
130
- demo.queue(max_size=4, default_concurrency_limit=1).launch()
 
 
 
1
  import os
2
+ import platform
3
+ import shutil
4
  import tempfile
5
  import torch
6
  import torchaudio
7
  import gradio as gr
8
  import spaces
9
+ from fastapi import FastAPI
10
+ from fastapi.responses import JSONResponse
11
  from einops import rearrange
12
  from huggingface_hub import login
13
  from stable_audio_3 import StableAudioModel
 
31
  print(f"Warning: ZeroGPU touch failed (running CPU-only): {e}")
32
 
33
 
34
+ # API metadata
35
+ API_RESOURCES = {
36
+ "audio_generation": {
37
+ "name": "Audio generation",
38
+ "description": "Generate music or sound effects from a text prompt.",
39
+ "endpoint": "/api/audio/generate",
40
+ "method": "POST",
41
+ "input": {
42
+ "prompt": "string",
43
+ "duration": "number (1-120 seconds)",
44
+ "steps": "integer (1-50)",
45
+ "cfg_scale": "number (0-10)",
46
+ "seed": "integer (-1 for random)",
47
+ "model": "small-music | small-sfx"
48
+ },
49
+ "output": "WAV audio file"
50
+ }
51
+ }
52
+
53
+ def get_server_specs():
54
+ storage = shutil.disk_usage(os.getcwd())
55
+ return {
56
+ "name": "Respite API",
57
+ "version": "1.0.0",
58
+ "description": "General-purpose AI API server with audio generation capabilities.",
59
+ "base_path": "/api",
60
+ "authentication": "none",
61
+ "content_types": ["application/json", "audio/wav"],
62
+ "resources_endpoint": "/api/resources",
63
+ "specs_endpoint": "/api/specs",
64
+ "runtime": {
65
+ "platform": platform.platform(),
66
+ "python_version": platform.python_version(),
67
+ "cpu_cores": os.cpu_count(),
68
+ "ram_bytes": _get_ram_bytes(),
69
+ "storage_total_bytes": storage.total,
70
+ "storage_used_bytes": storage.used,
71
+ "storage_free_bytes": storage.free
72
+ },
73
+ "limits": {
74
+ "max_concurrent_requests": 1,
75
+ "max_queue_size": 4,
76
+ "audio_max_duration_seconds": 120
77
+ }
78
+ }
79
+
80
+
81
+ def _get_ram_bytes():
82
+ try:
83
+ with open("/proc/meminfo", "r", encoding="utf-8") as meminfo:
84
+ for line in meminfo:
85
+ if line.startswith("MemTotal:"):
86
+ return int(line.split()[1]) * 1024
87
+ except (FileNotFoundError, OSError, ValueError):
88
+ pass
89
+ return None
90
+
91
+
92
+ API_SPECS = {
93
+ "name": "Respite API",
94
+ "version": "1.0.0",
95
+ "description": "General-purpose AI API server with audio generation capabilities.",
96
+ "base_path": "/api",
97
+ "authentication": "none",
98
+ "content_types": ["application/json", "audio/wav"],
99
+ "resources_endpoint": "/api/resources",
100
+ "specs_endpoint": "/api/specs",
101
+ "limits": {
102
+ "max_concurrent_requests": 1,
103
+ "max_queue_size": 4,
104
+ "audio_max_duration_seconds": 120
105
+ }
106
+ }
107
+
108
+
109
+ def get_resources():
110
+ return {"resources": API_RESOURCES}
111
+
112
+
113
+ def get_specs():
114
+ return {**API_SPECS, "runtime": get_server_specs()["runtime"]}
115
+
116
+
117
  # Model cache
118
  MODEL_CACHE = {}
119
 
 
168
  return output_path
169
 
170
 
171
+ api = FastAPI(title=API_SPECS["name"], version=API_SPECS["version"])
172
+
173
+
174
+ @api.get("/api/resources")
175
+ def resources():
176
+ return JSONResponse(get_resources())
177
+
178
+
179
+ @api.get("/api/specs")
180
+ def specs():
181
+ return JSONResponse(get_specs())
182
+
183
+
184
  with gr.Blocks(title="Stable Audio 3 Small") as demo:
185
  gr.Markdown("# 🎵 Stable Audio 3 Small - Music & SFX Generation")
186
  gr.Markdown("Generate music and sound effects using Stability AI's Stable Audio 3 Small models. Runs on CPU.")
 
227
  )
228
 
229
 
230
+ demo.queue(max_size=4, default_concurrency_limit=1).launch(
231
+ app_kwargs={"app": api}
232
+ )