Spaces:
Running on Zero
Running on Zero
Make Space startup logging ASCII safe
Browse files
app.py
CHANGED
|
@@ -28,7 +28,7 @@ try:
|
|
| 28 |
return "ok"
|
| 29 |
_gpu_startup_touch()
|
| 30 |
except Exception as e:
|
| 31 |
-
|
| 32 |
|
| 33 |
|
| 34 |
# API metadata
|
|
@@ -114,6 +114,10 @@ def get_specs():
|
|
| 114 |
return {**API_SPECS, "runtime": get_server_specs()["runtime"]}
|
| 115 |
|
| 116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
# Model cache
|
| 118 |
MODEL_CACHE = {}
|
| 119 |
|
|
@@ -121,13 +125,13 @@ MODEL_CACHE = {}
|
|
| 121 |
def load_model(model_name):
|
| 122 |
"""Load model on demand and cache it."""
|
| 123 |
if model_name not in MODEL_CACHE:
|
| 124 |
-
|
| 125 |
model = StableAudioModel.from_pretrained(
|
| 126 |
model_name,
|
| 127 |
device="cpu"
|
| 128 |
)
|
| 129 |
MODEL_CACHE[model_name] = model
|
| 130 |
-
|
| 131 |
return MODEL_CACHE[model_name]
|
| 132 |
|
| 133 |
|
|
@@ -136,16 +140,16 @@ def load_model(model_name):
|
|
| 136 |
import threading
|
| 137 |
|
| 138 |
def _prewarm():
|
| 139 |
-
|
| 140 |
for _name in ["small-music", "small-sfx"]:
|
| 141 |
load_model(_name)
|
| 142 |
-
|
| 143 |
|
| 144 |
threading.Thread(target=_prewarm, daemon=True).start()
|
| 145 |
|
| 146 |
|
| 147 |
def generate_audio(prompt, duration, steps, cfg_scale, seed, model_name):
|
| 148 |
-
|
| 149 |
|
| 150 |
model = load_model(model_name)
|
| 151 |
|
|
@@ -164,7 +168,7 @@ def generate_audio(prompt, duration, steps, cfg_scale, seed, model_name):
|
|
| 164 |
|
| 165 |
output_path = os.path.join(tempfile.gettempdir(), f"stable_audio_{seed}_{hash(prompt) & 0xFFFFFFFF:08x}.wav")
|
| 166 |
torchaudio.save(output_path, audio, 44100)
|
| 167 |
-
|
| 168 |
return output_path
|
| 169 |
|
| 170 |
|
|
|
|
| 28 |
return "ok"
|
| 29 |
_gpu_startup_touch()
|
| 30 |
except Exception as e:
|
| 31 |
+
_log(f"Warning: ZeroGPU touch failed (running CPU-only): {e}")
|
| 32 |
|
| 33 |
|
| 34 |
# API metadata
|
|
|
|
| 114 |
return {**API_SPECS, "runtime": get_server_specs()["runtime"]}
|
| 115 |
|
| 116 |
|
| 117 |
+
def _log(message):
|
| 118 |
+
print(message.encode("ascii", "backslashreplace").decode("ascii"), flush=True)
|
| 119 |
+
|
| 120 |
+
|
| 121 |
# Model cache
|
| 122 |
MODEL_CACHE = {}
|
| 123 |
|
|
|
|
| 125 |
def load_model(model_name):
|
| 126 |
"""Load model on demand and cache it."""
|
| 127 |
if model_name not in MODEL_CACHE:
|
| 128 |
+
_log(f"Loading {model_name} model...")
|
| 129 |
model = StableAudioModel.from_pretrained(
|
| 130 |
model_name,
|
| 131 |
device="cpu"
|
| 132 |
)
|
| 133 |
MODEL_CACHE[model_name] = model
|
| 134 |
+
_log(f"{model_name} loaded successfully!")
|
| 135 |
return MODEL_CACHE[model_name]
|
| 136 |
|
| 137 |
|
|
|
|
| 140 |
import threading
|
| 141 |
|
| 142 |
def _prewarm():
|
| 143 |
+
_log("Pre-warming models...")
|
| 144 |
for _name in ["small-music", "small-sfx"]:
|
| 145 |
load_model(_name)
|
| 146 |
+
_log("All models ready!")
|
| 147 |
|
| 148 |
threading.Thread(target=_prewarm, daemon=True).start()
|
| 149 |
|
| 150 |
|
| 151 |
def generate_audio(prompt, duration, steps, cfg_scale, seed, model_name):
|
| 152 |
+
_log(f"Generating with {model_name}: prompt='{prompt}', duration={duration}s, steps={steps}, cfg={cfg_scale}, seed={seed}")
|
| 153 |
|
| 154 |
model = load_model(model_name)
|
| 155 |
|
|
|
|
| 168 |
|
| 169 |
output_path = os.path.join(tempfile.gettempdir(), f"stable_audio_{seed}_{hash(prompt) & 0xFFFFFFFF:08x}.wav")
|
| 170 |
torchaudio.save(output_path, audio, 44100)
|
| 171 |
+
_log("Generation complete!")
|
| 172 |
return output_path
|
| 173 |
|
| 174 |
|