DRBAPH
commited on
Commit
β’
e06cbbd
1
Parent(s):
ddc5a5a
app.py
Browse files
app.py
ADDED
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
from pydub import AudioSegment
|
7 |
+
import re
|
8 |
+
import os
|
9 |
+
from datetime import datetime
|
10 |
+
import gradio as gr
|
11 |
+
|
12 |
+
# Define the function to generate audio based on a prompt
|
13 |
+
def generate_audio(prompt, steps, cfg_scale, sigma_min, sigma_max, generation_time, seed, sampler_type, model_half):
|
14 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
15 |
+
|
16 |
+
# Download model
|
17 |
+
model, model_config = get_pretrained_model("audo/stable-audio-open-1.0")
|
18 |
+
sample_rate = model_config["sample_rate"]
|
19 |
+
sample_size = model_config["sample_size"]
|
20 |
+
|
21 |
+
model = model.to(device)
|
22 |
+
|
23 |
+
# Print model data type before conversion
|
24 |
+
print("Model data type before conversion:", next(model.parameters()).dtype)
|
25 |
+
|
26 |
+
# Convert model to float16 if model_half is True
|
27 |
+
if model_half:
|
28 |
+
model = model.to(torch.float16)
|
29 |
+
|
30 |
+
# Print model data type after conversion
|
31 |
+
print("Model data type after conversion:", next(model.parameters()).dtype)
|
32 |
+
|
33 |
+
# Set up text and timing conditioning
|
34 |
+
conditioning = [{
|
35 |
+
"prompt": prompt,
|
36 |
+
"seconds_start": 0,
|
37 |
+
"seconds_total": generation_time
|
38 |
+
}]
|
39 |
+
|
40 |
+
# Generate stereo audio
|
41 |
+
output = generate_diffusion_cond(
|
42 |
+
model,
|
43 |
+
steps=steps,
|
44 |
+
cfg_scale=cfg_scale,
|
45 |
+
conditioning=conditioning,
|
46 |
+
sample_size=sample_size,
|
47 |
+
sigma_min=sigma_min,
|
48 |
+
sigma_max=sigma_max,
|
49 |
+
sampler_type=sampler_type,
|
50 |
+
device=device,
|
51 |
+
seed=seed
|
52 |
+
)
|
53 |
+
|
54 |
+
# Print output data type
|
55 |
+
print("Output data type:", output.dtype)
|
56 |
+
|
57 |
+
# Rearrange audio batch to a single sequence
|
58 |
+
output = rearrange(output, "b d n -> d (b n)")
|
59 |
+
|
60 |
+
# Peak normalize, clip, and convert to int16 directly if model_half is used
|
61 |
+
output = output.div(torch.max(torch.abs(output))).clamp(-1, 1).mul(32767)
|
62 |
+
if model_half:
|
63 |
+
output = output.to(torch.int16).cpu()
|
64 |
+
else:
|
65 |
+
output = output.to(torch.float32).to(torch.int16).cpu()
|
66 |
+
|
67 |
+
torchaudio.save("temp_output.wav", output, sample_rate)
|
68 |
+
|
69 |
+
# Convert to MP3 format using pydub
|
70 |
+
audio = AudioSegment.from_wav("temp_output.wav")
|
71 |
+
|
72 |
+
# Create Output folder and dated subfolder if they do not exist
|
73 |
+
output_folder = "Output"
|
74 |
+
date_folder = datetime.now().strftime("%Y-%m-%d")
|
75 |
+
save_path = os.path.join(output_folder, date_folder)
|
76 |
+
os.makedirs(save_path, exist_ok=True)
|
77 |
+
|
78 |
+
# Generate a filename based on the prompt
|
79 |
+
filename = re.sub(r'\W+', '_', prompt) + ".mp3" # Replace non-alphanumeric characters with underscores
|
80 |
+
full_path = os.path.join(save_path, filename)
|
81 |
+
|
82 |
+
# Ensure the filename is unique by appending a number if the file already exists
|
83 |
+
base_filename = filename
|
84 |
+
counter = 1
|
85 |
+
while os.path.exists(full_path):
|
86 |
+
filename = f"{base_filename[:-4]}_{counter}.mp3"
|
87 |
+
full_path = os.path.join(save_path, filename)
|
88 |
+
counter += 1
|
89 |
+
|
90 |
+
# Export the audio to MP3 format
|
91 |
+
audio.export(full_path, format="mp3")
|
92 |
+
|
93 |
+
return full_path
|
94 |
+
|
95 |
+
def audio_generator(prompt, sampler_type, steps, cfg_scale, sigma_min, sigma_max, generation_time, seed, model_half):
|
96 |
+
try:
|
97 |
+
print("Generating audio with parameters:")
|
98 |
+
print("Prompt:", prompt)
|
99 |
+
print("Sampler Type:", sampler_type)
|
100 |
+
print("Steps:", steps)
|
101 |
+
print("CFG Scale:", cfg_scale)
|
102 |
+
print("Sigma Min:", sigma_min)
|
103 |
+
print("Sigma Max:", sigma_max)
|
104 |
+
print("Generation Time:", generation_time)
|
105 |
+
print("Seed:", seed)
|
106 |
+
print("Model Half Precision:", model_half)
|
107 |
+
|
108 |
+
filename = generate_audio(prompt, steps, cfg_scale, sigma_min, sigma_max, generation_time, seed, sampler_type, model_half)
|
109 |
+
return gr.Audio(filename), f"Generated: {filename}"
|
110 |
+
except Exception as e:
|
111 |
+
return str(e)
|
112 |
+
|
113 |
+
# Create Gradio interface
|
114 |
+
prompt_textbox = gr.Textbox(lines=5, label="Prompt")
|
115 |
+
sampler_dropdown = gr.Dropdown(
|
116 |
+
label="Sampler Type",
|
117 |
+
choices=[
|
118 |
+
"dpmpp-3m-sde",
|
119 |
+
"dpmpp-2m-sde",
|
120 |
+
"k-heun",
|
121 |
+
"k-lms",
|
122 |
+
"k-dpmpp-2s-ancestral",
|
123 |
+
"k-dpm-2",
|
124 |
+
"k-dpm-fast"
|
125 |
+
],
|
126 |
+
value="dpmpp-3m-sde"
|
127 |
+
)
|
128 |
+
steps_slider = gr.Slider(minimum=0, maximum=200, label="Steps", step=1, value=100)
|
129 |
+
cfg_scale_slider = gr.Slider(minimum=0, maximum=15, label="CFG Scale", step=0.1, value=7)
|
130 |
+
sigma_min_slider = gr.Slider(minimum=0, maximum=50, label="Sigma Min", step=0.1, value=0.3)
|
131 |
+
sigma_max_slider = gr.Slider(minimum=0, maximum=1000, label="Sigma Max", step=0.1, value=500)
|
132 |
+
generation_time_slider = gr.Slider(minimum=0, maximum=47, label="Generation Time (seconds)", step=1, value=47)
|
133 |
+
seed_slider = gr.Slider(minimum=-1, maximum=999999, label="Seed", step=1, value=123456)
|
134 |
+
model_half_checkbox = gr.Checkbox(label="Low VRAM (float16)", value=False)
|
135 |
+
|
136 |
+
output_textbox = gr.Textbox(label="Output")
|
137 |
+
|
138 |
+
title = "ππ StableAudioWebUI ππ"
|
139 |
+
description = "[Github Repository](https://github.com/Saganaki22/StableAudioWebUI)"
|
140 |
+
|
141 |
+
gr.Interface(
|
142 |
+
audio_generator,
|
143 |
+
[prompt_textbox, sampler_dropdown, steps_slider, cfg_scale_slider, sigma_min_slider, sigma_max_slider, generation_time_slider, seed_slider, model_half_checkbox],
|
144 |
+
[gr.Audio(), output_textbox],
|
145 |
+
title=title,
|
146 |
+
description=description
|
147 |
+
).launch()
|