Spaces:
Runtime error
Runtime error
import spaces | |
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 | |
import os | |
# Load model config from stable-audio-tools | |
model, model_config = get_pretrained_model( | |
"stabilityai/stable-audio-open-1.0", config_filename="model_config.json" | |
) | |
sample_rate = model_config["sample_rate"] | |
sample_size = model_config["sample_size"] | |
# Load the model using the transformers library | |
token = os.environ.get("TOKEN") | |
model = AutoModelForAudioClassification.from_pretrained( | |
"stabilityai/stable-audio-open-1.0", use_auth_token=token, cache_dir=None | |
) | |
device = "cuda" if torch.cuda.is_available() else "cpu" | |
model = model.to(device) | |
# --- Gradio App --- | |
def generate_music(prompt, seconds_total, bpm, genre): | |
"""Generates music from a prompt using Stable Diffusion.""" | |
# Set up text and timing conditioning | |
conditioning = [{ | |
"prompt": f"{bpm} BPM {genre} {prompt}", | |
"seconds_start": 0, | |
"seconds_total": seconds_total | |
}] | |
# 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 output | |
def generate_music_and_save(prompt, seconds_total, bpm, genre): | |
"""Generates music, saves it to a file, and returns the file path.""" | |
output = generate_music(prompt, seconds_total, bpm, genre) | |
filename = "output.wav" | |
torchaudio.save(filename, output, sample_rate) | |
return filename | |
# Create Gradio interface | |
iface = spaces.Interface( | |
generate_music_and_save, | |
inputs=[ | |
spaces.Textbox(label="Prompt (e.g., 'upbeat drum loop')", lines=1), | |
spaces.Slider(label="Duration (seconds)", minimum=1, maximum=60, step=1), | |
spaces.Slider(label="BPM", minimum=60, maximum=200, step=1), | |
spaces.Dropdown(label="Genre", choices=["pop", "rock", "hip hop", "electronic", "classical"], value="pop") | |
], | |
outputs=[ | |
spaces.Audio(label="Generated Music") | |
], | |
title="Stable Audio Open", | |
description="Generate music from text prompts using Stable Audio." | |
) | |
iface.launch(share=True) |