Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,46 @@
|
|
1 |
import os
|
2 |
from huggingface_hub import login
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
|
|
4 |
token = os.getenv("HUGGINGFACE_TOKEN")
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
from huggingface_hub import login
|
3 |
+
import torch
|
4 |
+
import torchaudio
|
5 |
+
from einops import rearrange
|
6 |
+
import gradio as gr
|
7 |
+
from stable_audio_tools import get_pretrained_model
|
8 |
+
from stable_audio_tools.inference.generation import generate_diffusion_cond
|
9 |
|
10 |
+
# Authenticate using token from Secrets
|
11 |
token = os.getenv("HUGGINGFACE_TOKEN")
|
12 |
+
if not token:
|
13 |
+
raise RuntimeError("HUGGINGFACE_TOKEN not set")
|
14 |
+
login(token=token, add_to_git_credential=False)
|
15 |
|
16 |
+
# Set device + load model
|
17 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
18 |
+
model, config = get_pretrained_model("stabilityai/stable-audio-open-small")
|
19 |
+
model = model.to(device)
|
20 |
+
sample_rate = config["sample_rate"]
|
21 |
+
sample_size = config["sample_size"]
|
22 |
|
23 |
+
# Inference function
|
24 |
+
def generate_audio(prompt):
|
25 |
+
conditioning = [{"prompt": prompt, "seconds_total": 11}]
|
26 |
+
with torch.no_grad():
|
27 |
+
output = generate_diffusion_cond(
|
28 |
+
model,
|
29 |
+
steps=8,
|
30 |
+
conditioning=conditioning,
|
31 |
+
sample_size=sample_size,
|
32 |
+
device=device
|
33 |
+
)
|
34 |
+
output = rearrange(output, "b d n -> d (b n)")
|
35 |
+
output = output.to(torch.float32).div(torch.max(torch.abs(output))).clamp(-1, 1).mul(32767).to(torch.int16).cpu()
|
36 |
+
path = "output.wav"
|
37 |
+
torchaudio.save(path, output, sample_rate)
|
38 |
+
return path
|
39 |
+
|
40 |
+
# Launch UI
|
41 |
+
gr.Interface(
|
42 |
+
fn=generate_audio,
|
43 |
+
inputs=gr.Textbox(label="Enter your sound prompt"),
|
44 |
+
outputs=gr.Audio(type="filepath"),
|
45 |
+
title="Stable Audio Generator"
|
46 |
+
).launch()
|