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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from diffusers import AudioLDMPipeline
|
4 |
+
from share_btn import community_icon_html, loading_icon_html, share_js
|
5 |
+
|
6 |
+
from transformers import AutoProcessor, ClapModel
|
7 |
+
|
8 |
+
|
9 |
+
# make Space compatible with CPU duplicates
|
10 |
+
if torch.cuda.is_available():
|
11 |
+
device = "cuda"
|
12 |
+
torch_dtype = torch.float16
|
13 |
+
else:
|
14 |
+
device = "cpu"
|
15 |
+
torch_dtype = torch.float32
|
16 |
+
|
17 |
+
# load the diffusers pipeline
|
18 |
+
repo_id = "cvssp/audioldm-m-full"
|
19 |
+
pipe = AudioLDMPipeline.from_pretrained(repo_id, torch_dtype=torch_dtype).to(device)
|
20 |
+
pipe.unet = torch.compile(pipe.unet)
|
21 |
+
|
22 |
+
# CLAP model (only required for automatic scoring)
|
23 |
+
clap_model = ClapModel.from_pretrained("sanchit-gandhi/clap-htsat-unfused-m-full").to(device)
|
24 |
+
processor = AutoProcessor.from_pretrained("sanchit-gandhi/clap-htsat-unfused-m-full")
|
25 |
+
|
26 |
+
generator = torch.Generator(device)
|
27 |
+
|
28 |
+
|
29 |
+
def text2audio(text, negative_prompt, duration, guidance_scale, random_seed, n_candidates):
|
30 |
+
if text is None:
|
31 |
+
raise gr.Error("Please provide a text input.")
|
32 |
+
|
33 |
+
waveforms = pipe(
|
34 |
+
text,
|
35 |
+
audio_length_in_s=duration,
|
36 |
+
guidance_scale=guidance_scale,
|
37 |
+
negative_prompt=negative_prompt,
|
38 |
+
num_waveforms_per_prompt=n_candidates if n_candidates else 1,
|
39 |
+
generator=generator.manual_seed(int(random_seed)),
|
40 |
+
)["audios"]
|
41 |
+
|
42 |
+
if waveforms.shape[0] > 1:
|
43 |
+
waveform = score_waveforms(text, waveforms)
|
44 |
+
else:
|
45 |
+
waveform = waveforms[0]
|
46 |
+
|
47 |
+
return gr.make_waveform((16000, waveform), bg_image="bg.png")
|
48 |
+
|
49 |
+
|
50 |
+
def score_waveforms(text, waveforms):
|
51 |
+
inputs = processor(text=text, audios=list(waveforms), return_tensors="pt", padding=True)
|
52 |
+
inputs = {key: inputs[key].to(device) for key in inputs}
|
53 |
+
with torch.no_grad():
|
54 |
+
logits_per_text = clap_model(**inputs).logits_per_text # this is the audio-text similarity score
|
55 |
+
probs = logits_per_text.softmax(dim=-1) # we can take the softmax to get the label probabilities
|
56 |
+
most_probable = torch.argmax(probs) # and now select the most likely audio waveform
|
57 |
+
waveform = waveforms[most_probable]
|
58 |
+
return waveform
|
59 |
+
|
60 |
+
|
61 |
+
css = """
|
62 |
+
a {
|
63 |
+
color: inherit; text-decoration: underline;
|
64 |
+
} .gradio-container {
|
65 |
+
font-family: 'IBM Plex Sans', sans-serif;
|
66 |
+
} .gr-button {
|
67 |
+
color: white; border-color: #000000; background: #000000;
|
68 |
+
} input[type='range'] {
|
69 |
+
accent-color: #000000;
|
70 |
+
} .dark input[type='range'] {
|
71 |
+
accent-color: #dfdfdf;
|
72 |
+
} .container {
|
73 |
+
max-width: 730px; margin: auto; padding-top: 1.5rem;
|
74 |
+
} #gallery {
|
75 |
+
min-height: 22rem; margin-bottom: 15px; margin-left: auto; margin-right: auto; border-bottom-right-radius:
|
76 |
+
.5rem !important; border-bottom-left-radius: .5rem !important;
|
77 |
+
} #gallery>div>.h-full {
|
78 |
+
min-height: 20rem;
|
79 |
+
} .details:hover {
|
80 |
+
text-decoration: underline;
|
81 |
+
} .gr-button {
|
82 |
+
white-space: nowrap;
|
83 |
+
} .gr-button:focus {
|
84 |
+
border-color: rgb(147 197 253 / var(--tw-border-opacity)); outline: none; box-shadow:
|
85 |
+
var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000); --tw-border-opacity: 1;
|
86 |
+
--tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width)
|
87 |
+
var(--tw-ring-offset-color); --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px
|
88 |
+
var(--tw-ring-offset-width)) var(--tw-ring-color); --tw-ring-color: rgb(191 219 254 /
|
89 |
+
var(--tw-ring-opacity)); --tw-ring-opacity: .5;
|
90 |
+
} #advanced-btn {
|
91 |
+
font-size: .7rem !important; line-height: 19px; margin-top: 12px; margin-bottom: 12px; padding: 2px 8px;
|
92 |
+
border-radius: 14px !important;
|
93 |
+
} #advanced-options {
|
94 |
+
margin-bottom: 20px;
|
95 |
+
} .footer {
|
96 |
+
margin-bottom: 45px; margin-top: 35px; text-align: center; border-bottom: 1px solid #e5e5e5;
|
97 |
+
} .footer>p {
|
98 |
+
font-size: .8rem; display: inline-block; padding: 0 10px; transform: translateY(10px); background: white;
|
99 |
+
} .dark .footer {
|
100 |
+
border-color: #303030;
|
101 |
+
} .dark .footer>p {
|
102 |
+
background: #0b0f19;
|
103 |
+
} .acknowledgments h4{
|
104 |
+
margin: 1.25em 0 .25em 0; font-weight: bold; font-size: 115%;
|
105 |
+
} #container-advanced-btns{
|
106 |
+
display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center;
|
107 |
+
} .animate-spin {
|
108 |
+
animation: spin 1s linear infinite;
|
109 |
+
} @keyframes spin {
|
110 |
+
from {
|
111 |
+
transform: rotate(0deg);
|
112 |
+
} to {
|
113 |
+
transform: rotate(360deg);
|
114 |
+
}
|
115 |
+
} #share-btn-container {
|
116 |
+
display: flex; padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color:
|
117 |
+
#000000; justify-content: center; align-items: center; border-radius: 9999px !important; width: 13rem;
|
118 |
+
margin-top: 10px; margin-left: auto;
|
119 |
+
} #share-btn {
|
120 |
+
all: initial; color: #ffffff;font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif;
|
121 |
+
margin-left: 0.5rem !important; padding-top: 0.25rem !important; padding-bottom: 0.25rem
|
122 |
+
!important;right:0;
|
123 |
+
} #share-btn * {
|
124 |
+
all: unset;
|
125 |
+
} #share-btn-container div:nth-child(-n+2){
|
126 |
+
width: auto !important; min-height: 0px !important;
|
127 |
+
} #share-btn-container .wrap {
|
128 |
+
display: none !important;
|
129 |
+
} .gr-form{
|
130 |
+
flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
|
131 |
+
} #prompt-container{
|
132 |
+
gap: 0;
|
133 |
+
} #generated_id{
|
134 |
+
min-height: 700px
|
135 |
+
} #setting_id{
|
136 |
+
margin-bottom: 12px; text-align: center; font-weight: 900;
|
137 |
+
}
|
138 |
+
"""
|
139 |
+
iface = gr.Blocks(css=css)
|
140 |
+
|
141 |
+
with iface:
|
142 |
+
gr.HTML(
|
143 |
+
"""
|
144 |
+
<div style="text-align: center; max-width: 700px; margin: 0 auto;">
|
145 |
+
<div
|
146 |
+
style="
|
147 |
+
display: inline-flex; align-items: center; gap: 0.8rem; font-size: 1.75rem;
|
148 |
+
"
|
149 |
+
>
|
150 |
+
<h1 style="font-weight: 900; margin-bottom: 7px; line-height: normal;">
|
151 |
+
AudioLDM: Text-to-Audio Generation with Latent Diffusion Models
|
152 |
+
</h1>
|
153 |
+
</div>
|
154 |
+
</div>
|
155 |
+
"""
|
156 |
+
)
|
157 |
+
|
158 |
+
with gr.Group():
|
159 |
+
with gr.Box():
|
160 |
+
textbox = gr.Textbox(
|
161 |
+
value="A hammer is hitting a wooden surface",
|
162 |
+
max_lines=1,
|
163 |
+
label="Input text",
|
164 |
+
info="Your text is important for the audio quality. Please ensure it is descriptive by using more adjectives.",
|
165 |
+
elem_id="prompt-in",
|
166 |
+
)
|
167 |
+
negative_textbox = gr.Textbox(
|
168 |
+
value="low quality, average quality",
|
169 |
+
max_lines=1,
|
170 |
+
label="Negative prompt",
|
171 |
+
info="Enter a negative prompt not to guide the audio generation. Selecting appropriate negative prompts can improve the audio quality significantly.",
|
172 |
+
elem_id="prompt-in",
|
173 |
+
)
|
174 |
+
|
175 |
+
with gr.Accordion("Click to modify detailed configurations", open=False):
|
176 |
+
seed = gr.Number(
|
177 |
+
value=45,
|
178 |
+
label="Seed",
|
179 |
+
info="Change this value (any integer number) will lead to a different generation result.",
|
180 |
+
)
|
181 |
+
duration = gr.Slider(2.5, 10, value=5, step=2.5, label="Duration (seconds)")
|
182 |
+
guidance_scale = gr.Slider(
|
183 |
+
0,
|
184 |
+
4,
|
185 |
+
value=2.5,
|
186 |
+
step=0.5,
|
187 |
+
label="Guidance scale",
|
188 |
+
info="Large => better quality and relevancy to text; Small => better diversity",
|
189 |
+
)
|
190 |
+
n_candidates = gr.Slider(
|
191 |
+
1,
|
192 |
+
3,
|
193 |
+
value=3,
|
194 |
+
step=1,
|
195 |
+
label="Number waveforms to generate",
|
196 |
+
info="Automatic quality control. This number control the number of candidates (e.g., generate three audios and choose the best to show you). A Larger value usually lead to better quality with heavier computation",
|
197 |
+
)
|
198 |
+
|
199 |
+
outputs = gr.Video(label="Output", elem_id="output-video")
|
200 |
+
btn = gr.Button("Submit").style(full_width=True)
|
201 |
+
|
202 |
+
with gr.Group(elem_id="share-btn-container", visible=False):
|
203 |
+
community_icon = gr.HTML(community_icon_html)
|
204 |
+
loading_icon = gr.HTML(loading_icon_html)
|
205 |
+
share_button = gr.Button("Share to community", elem_id="share-btn")
|
206 |
+
|
207 |
+
btn.click(
|
208 |
+
text2audio,
|
209 |
+
inputs=[textbox, negative_textbox, duration, guidance_scale, seed, n_candidates],
|
210 |
+
outputs=[outputs],
|
211 |
+
)
|
212 |
+
|
213 |
+
share_button.click(None, [], [], _js=share_js)
|
214 |
+
gr.HTML(
|
215 |
+
"""
|
216 |
+
<div class="footer" style="text-align: center; max-width: 700px; margin: 0 auto;">
|
217 |
+
<p>Follow the latest update of AudioLDM on our<a href="https://github.com/soulabi"
|
218 |
+
style="text-decoration: underline;" target="_blank"> Github repo</a> </p> <br>
|
219 |
+
</div>
|
220 |
+
"""
|
221 |
+
)
|
222 |
+
gr.Examples(
|
223 |
+
[
|
224 |
+
["A hammer is hitting a wooden surface", "low quality, average quality", 5, 2.5, 45, 3],
|
225 |
+
["Peaceful and calming ambient music with singing bowl and other instruments.", "low quality, average quality", 5, 2.5, 45, 3],
|
226 |
+
["A man is speaking in a small room.", "low quality, average quality", 5, 2.5, 45, 3],
|
227 |
+
["A female is speaking followed by footstep sound", "low quality, average quality", 5, 2.5, 45, 3],
|
228 |
+
["Wooden table tapping sound followed by water pouring sound.", "low quality, average quality", 5, 2.5, 45, 3],
|
229 |
+
],
|
230 |
+
fn=text2audio,
|
231 |
+
inputs=[textbox, negative_textbox, duration, guidance_scale, seed, n_candidates],
|
232 |
+
outputs=[outputs],
|
233 |
+
cache_examples=True,
|
234 |
+
)
|
235 |
+
gr.HTML(
|
236 |
+
"""
|
237 |
+
<div class="acknowledgements"> <p>Essential Tricks for Enhancing the Quality of Your Generated
|
238 |
+
Audio</p> <p>1. Try to use more adjectives to describe your sound. For example: "A man is speaking
|
239 |
+
clearly and slowly in a large room" is better than "A man is speaking". This can make sure AudioLDM
|
240 |
+
understands what you want.</p> <p>2. Try to use different random seeds, which can affect the generation
|
241 |
+
quality significantly sometimes.</p> <p>3. It's better to use general terms like 'man' or 'woman'
|
242 |
+
instead of specific names for individuals or abstract objects that humans may not be familiar with,
|
243 |
+
such as 'mummy'.</p> <p>4. Using a negative prompt to not guide the diffusion process can improve the
|
244 |
+
audio quality significantly. Try using negative prompts like 'low quality'.</p> </div>
|
245 |
+
"""
|
246 |
+
)
|
247 |
+
with gr.Accordion("Additional information", open=False):
|
248 |
+
gr.HTML(
|
249 |
+
"""
|
250 |
+
<div class="acknowledgments">
|
251 |
+
<p> We build the model with data from <a href="http://research.google.com/audioset/">AudioSet</a>,
|
252 |
+
<a href="https://freesound.org/">Freesound</a> and <a
|
253 |
+
href="https://sound-effects.bbcrewind.co.uk/">BBC Sound Effect library</a>. We share this demo
|
254 |
+
based on the <a
|
255 |
+
href="https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/375954/Research.pdf">UK
|
256 |
+
copyright exception</a> of data for academic research. </p>
|
257 |
+
</div>
|
258 |
+
"""
|
259 |
+
)
|
260 |
+
# <p>This demo is strictly for research demo purpose only. For commercial use please <a href="[email protected]">contact us</a>.</p>
|
261 |
+
|
262 |
+
iface.queue(max_size=10).launch(debug=True)
|