|
import gradio as gr |
|
import torch |
|
import os |
|
import uuid |
|
import torchaudio |
|
from einops import rearrange |
|
from stable_audio_tools import get_pretrained_model |
|
from stable_audio_tools.inference.generation import generate_diffusion_cond |
|
|
|
def gen_music(description): |
|
device = "cuda" if torch.cuda.is_available() else "cpu" |
|
|
|
|
|
hf_token = os.getenv('HF_TOKEN') |
|
print(f"Hugging Face token: {hf_token}") |
|
|
|
|
|
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) |
|
|
|
|
|
conditioning = [{ |
|
"prompt": f"{description}", |
|
"seconds_start": 0, |
|
"seconds_total": 30 |
|
}] |
|
|
|
|
|
output = generate_diffusion_cond( |
|
model, |
|
conditioning=conditioning, |
|
sample_size=sample_size, |
|
device=device |
|
) |
|
|
|
|
|
output = rearrange(output, "b d n -> d (b n)") |
|
|
|
|
|
output = output.to(torch.float32).div(torch.max(torch.abs(output))).clamp(-1, 1).mul(32767).to(torch.int16).cpu() |
|
|
|
|
|
unique_filename = f"output_{uuid.uuid4().hex}.wav" |
|
print(f"Saving audio to file: {unique_filename}") |
|
|
|
|
|
torchaudio.save(unique_filename, output, sample_rate) |
|
print(f"Audio saved: {unique_filename}") |
|
|
|
|
|
return unique_filename |
|
|
|
|
|
|
|
description = gr.Textbox(label="Description", placeholder="128 BPM tech house drum loop") |
|
output_path = gr.Audio(label="Generated Music", type="filepath") |
|
|
|
gr.Interface( |
|
fn=gen_music, |
|
inputs=[description], |
|
outputs=output_path, |
|
title="StableAudio Music Generation Demo", |
|
).launch() |
|
|