Text-To-Beats / app.py
tushar27's picture
app.py
ca4e4f6
raw
history blame
1.73 kB
import gradio as gr
import torch
import torchaudio
from einops import rearrange
from stable_audio_tools import get_pretrained_model
from stable_audio_tools.inference.generation import generate_diffusion_cond
device = "cuda" if torch.cuda.is_available() else "cpu"
# Download model
model, model_config = get_pretrained_model("stabilityai/stable-audio-open-1.0")
sample_rate = model_config["sample_rate"]
sample_size = model_config["sample_size"]
model = model.to(device)
def generate_audio(prompt, bpm, duration):
# Set up text and timing conditioning
conditioning = [{
"prompt": f"{bpm} BPM {prompt}",
"seconds_start": 0,
"seconds_total": duration
}]
# Generate stereo audio
output = generate_diffusion_cond(
model,
steps=100,
cfg_scale=7,
conditioning=conditioning,
sample_size=sample_size,
sigma_min=0.3,
sigma_max=500,
sampler_type="dpmpp-3m-sde",
device=device
)
# Rearrange audio batch to a single sequence
output = rearrange(output, "b d n -> d (b n)")
# Peak normalize, clip, convert to int16, and save to file
output = output.to(torch.float32).div(torch.max(torch.abs(output))).clamp(-1, 1).mul(32767).to(torch.int16).cpu()
return sample_rate, output
inputs = [
gr.inputs.Textbox(label="Prompt"),
gr.inputs.Number(label="BPM", default=128),
gr.inputs.Number(label="Duration (seconds)", default=30)
]
output = gr.outputs.Audio(type="numpy", label="Generated Audio")
gr.Interface(
fn=generate_audio,
inputs=inputs,
outputs=output,
title="Stable Audio Generation",
description="Generate audio using Stable Audio Open 1.0"
).launch()