Spaces:
Running on Zero
Running on Zero
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import torch
|
| 3 |
import torchaudio
|
| 4 |
import gradio as gr
|
|
@@ -42,6 +43,13 @@ def load_model(model_name):
|
|
| 42 |
return MODEL_CACHE[model_name]
|
| 43 |
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
def generate_audio(prompt, duration, steps, cfg_scale, seed, model_name):
|
| 46 |
print(f"Generating with {model_name}: prompt='{prompt}', duration={duration}s, steps={steps}, cfg={cfg_scale}, seed={seed}")
|
| 47 |
|
|
@@ -60,7 +68,7 @@ def generate_audio(prompt, duration, steps, cfg_scale, seed, model_name):
|
|
| 60 |
audio = rearrange(audio, "b d n -> d (b n)")
|
| 61 |
audio = audio.to(torch.float32).clamp(-1, 1).mul(32767).to(torch.int16).cpu()
|
| 62 |
|
| 63 |
-
output_path = "
|
| 64 |
torchaudio.save(output_path, audio, 44100)
|
| 65 |
print("Generation complete!")
|
| 66 |
return output_path
|
|
|
|
| 1 |
import os
|
| 2 |
+
import tempfile
|
| 3 |
import torch
|
| 4 |
import torchaudio
|
| 5 |
import gradio as gr
|
|
|
|
| 43 |
return MODEL_CACHE[model_name]
|
| 44 |
|
| 45 |
|
| 46 |
+
# Pre-warm both models at startup so the first user request is fast
|
| 47 |
+
print("Pre-warming models...")
|
| 48 |
+
for _name in ["small-music", "small-sfx"]:
|
| 49 |
+
load_model(_name)
|
| 50 |
+
print("All models ready!")
|
| 51 |
+
|
| 52 |
+
|
| 53 |
def generate_audio(prompt, duration, steps, cfg_scale, seed, model_name):
|
| 54 |
print(f"Generating with {model_name}: prompt='{prompt}', duration={duration}s, steps={steps}, cfg={cfg_scale}, seed={seed}")
|
| 55 |
|
|
|
|
| 68 |
audio = rearrange(audio, "b d n -> d (b n)")
|
| 69 |
audio = audio.to(torch.float32).clamp(-1, 1).mul(32767).to(torch.int16).cpu()
|
| 70 |
|
| 71 |
+
output_path = os.path.join(tempfile.gettempdir(), f"stable_audio_{seed}_{hash(prompt) & 0xFFFFFFFF:08x}.wav")
|
| 72 |
torchaudio.save(output_path, audio, 44100)
|
| 73 |
print("Generation complete!")
|
| 74 |
return output_path
|