Mariano Mollo commited on
Commit
11e7e3c
1 Parent(s): 64d2f8a

Add application file

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchaudio
3
+ from einops import rearrange
4
+ from stable_audio_tools import get_pretrained_model
5
+ from stable_audio_tools.inference.generation import generate_diffusion_cond
6
+
7
+ device = "cuda" if torch.cuda.is_available() else "cpu"
8
+
9
+ # Download model
10
+ model, model_config = get_pretrained_model("stabilityai/stable-audio-open-1.0")
11
+ sample_rate = model_config["sample_rate"]
12
+ sample_size = model_config["sample_size"]
13
+
14
+ model = model.to(device)
15
+
16
+ # Set up text and timing conditioning
17
+ conditioning = [{
18
+ "prompt": "128 BPM tech house drum loop",
19
+ "seconds_start": 0,
20
+ "seconds_total": 30
21
+ }]
22
+
23
+ # Generate stereo audio
24
+ output = generate_diffusion_cond(
25
+ model,
26
+ steps=100,
27
+ cfg_scale=7,
28
+ conditioning=conditioning,
29
+ sample_size=sample_size,
30
+ sigma_min=0.3,
31
+ sigma_max=500,
32
+ sampler_type="dpmpp-3m-sde",
33
+ device=device
34
+ )
35
+
36
+ # Rearrange audio batch to a single sequence
37
+ output = rearrange(output, "b d n -> d (b n)")
38
+
39
+ # Peak normalize, clip, convert to int16, and save to file
40
+ output = output.to(torch.float32).div(torch.max(torch.abs(output))).clamp(-1, 1).mul(32767).to(torch.int16).cpu()
41
+ torchaudio.save("output.wav", output, sample_rate)
42
+