artificialguybr commited on
Commit
456ed62
1 Parent(s): 5e1963c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchaudio
3
+ from einops import rearrange
4
+
5
+ import gradio as gr
6
+ import spaces
7
+
8
+ # Importing the model-related functions
9
+ from stable_audio_tools import get_pretrained_model
10
+ from stable_audio_tools.inference.generation import generate_diffusion_cond
11
+
12
+ # Function to set up, generate, and process the audio
13
+ @spaces.GPU(duration=120) # Allocate GPU only when this function is called
14
+ def generate_audio(prompt, seconds_total=30, steps=100, cfg_scale=7):
15
+ device = "cuda" if torch.cuda.is_available() else "cpu"
16
+
17
+ # Download and set up the model
18
+ model, model_config = get_pretrained_model("stabilityai/stable-audio-open-1.0")
19
+ sample_rate = model_config["sample_rate"]
20
+ sample_size = model_config["sample_size"]
21
+
22
+ model = model.to(device)
23
+
24
+ # Set up text and timing conditioning
25
+ conditioning = [{
26
+ "prompt": prompt,
27
+ "seconds_start": 0,
28
+ "seconds_total": seconds_total
29
+ }]
30
+
31
+ # Generate stereo audio
32
+ output = generate_diffusion_cond(
33
+ model,
34
+ steps=steps,
35
+ cfg_scale=cfg_scale,
36
+ conditioning=conditioning,
37
+ sample_size=sample_size,
38
+ sigma_min=0.3,
39
+ sigma_max=500,
40
+ sampler_type="dpmpp-3m-sde",
41
+ device=device
42
+ )
43
+
44
+ # Rearrange audio batch to a single sequence
45
+ output = rearrange(output, "b d n -> d (b n)")
46
+
47
+ # Peak normalize, clip, convert to int16
48
+ output = output.to(torch.float32).div(torch.max(torch.abs(output))).clamp(-1, 1).mul(32767).to(torch.int16).cpu()
49
+
50
+ # Save to file
51
+ torchaudio.save("output.wav", output, sample_rate)
52
+
53
+ # Return the path to the generated audio file
54
+ return "output.wav"
55
+
56
+ # Setting up the Gradio Interface
57
+ interface = gr.Interface(
58
+ fn=generate_audio,
59
+ inputs=[
60
+ gr.Textbox(label="Prompt", placeholder="Enter your text prompt here"),
61
+ gr.Slider(0, 47, value=30, label="Duration in Seconds"),
62
+ gr.Slider(10, 300, value=100, step=10, label="Number of Diffusion Steps"),
63
+ gr.Slider(1, 15, value=7, step=0.1, label="CFG Scale")
64
+ ],
65
+ outputs=gr.Audio(type="filepath", label="Generated Audio"),
66
+ title="Stable Audio Generator",
67
+ description="Generate variable-length stereo audio at 44.1kHz from text prompts using Stable Audio Open 1.0."
68
+ )
69
+
70
+ # Launch the Interface
71
+ interface.launch()