File size: 2,046 Bytes
0124826
 
4ca91f3
0124826
 
 
 
 
 
 
 
 
 
 
bb23a85
0124826
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import torch
import torchaudio
import spaces
from einops import rearrange
from stable_audio_tools import get_pretrained_model
from stable_audio_tools.inference.generation import generate_diffusion_cond
import gradio as gr

# Define the function to generate audio
@spaces.GPU()
def generate_audio(prompt, bpm, seconds_total):
    device = "cuda" if torch.cuda.is_available() else "cpu"

    # Download model
    model, model_config = get_pretrained_model("stabilityai/stable-audio-open-1.0",token=os.environ.get('HF_TOKEN'))
    sample_rate = model_config["sample_rate"]
    sample_size = model_config["sample_size"]

    model = model.to(device)

    # Set up text and timing conditioning
    conditioning = [{
        "prompt": f"{bpm} BPM {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()
    
    output_path = "output.wav"
    torchaudio.save(output_path, output, sample_rate)
    
    return output_path

# Define the Gradio interface
iface = gr.Interface(
    fn=generate_audio,
    inputs=[
        gr.Textbox(label="Prompt", placeholder="Enter the description of the audio (e.g., tech house drum loop)"),
        gr.Number(label="BPM", value=128),
        gr.Number(label="Duration (seconds)", value=30)
    ],
    outputs=gr.Audio(label="Generated Audio"),
    title="Stable Audio Generation",
    description="Generate audio based on a text prompt using stable audio tools.",
)

# Launch the interface
iface.launch()