Spaces:
Running
on
Zero
Running
on
Zero
ameerazam08
commited on
Commit
•
0124826
1
Parent(s):
bc2c808
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torchaudio
|
3 |
+
from einops import rearrange
|
4 |
+
from stable_audio_tools import get_pretrained_model
|
5 |
+
from stable_audio_tools.inference.generation import generate_diffusion_cond
|
6 |
+
import gradio as gr
|
7 |
+
import spaces
|
8 |
+
|
9 |
+
# Define the function to generate audio
|
10 |
+
@spaces.GPU()
|
11 |
+
def generate_audio(prompt, bpm, seconds_total):
|
12 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
+
|
14 |
+
# Download model
|
15 |
+
model, model_config = get_pretrained_model("stabilityai/stable-audio-open-1.0")
|
16 |
+
sample_rate = model_config["sample_rate"]
|
17 |
+
sample_size = model_config["sample_size"]
|
18 |
+
|
19 |
+
model = model.to(device)
|
20 |
+
|
21 |
+
# Set up text and timing conditioning
|
22 |
+
conditioning = [{
|
23 |
+
"prompt": f"{bpm} BPM {prompt}",
|
24 |
+
"seconds_start": 0,
|
25 |
+
"seconds_total": seconds_total
|
26 |
+
}]
|
27 |
+
|
28 |
+
# Generate stereo audio
|
29 |
+
output = generate_diffusion_cond(
|
30 |
+
model,
|
31 |
+
steps=100,
|
32 |
+
cfg_scale=7,
|
33 |
+
conditioning=conditioning,
|
34 |
+
sample_size=sample_size,
|
35 |
+
sigma_min=0.3,
|
36 |
+
sigma_max=500,
|
37 |
+
sampler_type="dpmpp-3m-sde",
|
38 |
+
device=device
|
39 |
+
)
|
40 |
+
|
41 |
+
# Rearrange audio batch to a single sequence
|
42 |
+
output = rearrange(output, "b d n -> d (b n)")
|
43 |
+
|
44 |
+
# Peak normalize, clip, convert to int16, and save to file
|
45 |
+
output = output.to(torch.float32).div(torch.max(torch.abs(output))).clamp(-1, 1).mul(32767).to(torch.int16).cpu()
|
46 |
+
|
47 |
+
output_path = "output.wav"
|
48 |
+
torchaudio.save(output_path, output, sample_rate)
|
49 |
+
|
50 |
+
return output_path
|
51 |
+
|
52 |
+
# Define the Gradio interface
|
53 |
+
iface = gr.Interface(
|
54 |
+
fn=generate_audio,
|
55 |
+
inputs=[
|
56 |
+
gr.Textbox(label="Prompt", placeholder="Enter the description of the audio (e.g., tech house drum loop)"),
|
57 |
+
gr.Number(label="BPM", value=128),
|
58 |
+
gr.Number(label="Duration (seconds)", value=30)
|
59 |
+
],
|
60 |
+
outputs=gr.Audio(label="Generated Audio"),
|
61 |
+
title="Stable Audio Generation",
|
62 |
+
description="Generate audio based on a text prompt using stable audio tools.",
|
63 |
+
)
|
64 |
+
|
65 |
+
# Launch the interface
|
66 |
+
iface.launch()
|