Spaces:
Building
on
A10G
Building
on
A10G
deepanwayx
commited on
Commit
•
2c55b55
1
Parent(s):
09e9063
update app
Browse files- .ipynb_checkpoints/app-checkpoint.py +283 -0
- app.py +16 -25
.ipynb_checkpoints/app-checkpoint.py
ADDED
@@ -0,0 +1,283 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import json
|
3 |
+
import torch
|
4 |
+
import wavio
|
5 |
+
import numpy as np
|
6 |
+
from tqdm import tqdm
|
7 |
+
from huggingface_hub import snapshot_download
|
8 |
+
|
9 |
+
from audioldm.audio.stft import TacotronSTFT
|
10 |
+
from audioldm.variational_autoencoder import AutoencoderKL
|
11 |
+
|
12 |
+
from transformers import AutoTokenizer, T5ForConditionalGeneration
|
13 |
+
from modelling_deberta_v2 import DebertaV2ForTokenClassificationRegression
|
14 |
+
|
15 |
+
import sys
|
16 |
+
sys.path.insert(0, "diffusers/src")
|
17 |
+
|
18 |
+
from diffusers import DDPMScheduler
|
19 |
+
from models import MusicAudioDiffusion
|
20 |
+
|
21 |
+
from gradio import Markdown
|
22 |
+
|
23 |
+
class MusicFeaturePredictor:
|
24 |
+
def __init__(self, path, device="cuda:0", cache_dir=None, local_files_only=False):
|
25 |
+
self.beats_tokenizer = AutoTokenizer.from_pretrained(
|
26 |
+
"microsoft/deberta-v3-large",
|
27 |
+
use_fast=False,
|
28 |
+
cache_dir=cache_dir,
|
29 |
+
local_files_only=local_files_only,
|
30 |
+
)
|
31 |
+
self.beats_model = DebertaV2ForTokenClassificationRegression.from_pretrained(
|
32 |
+
"microsoft/deberta-v3-large",
|
33 |
+
cache_dir=cache_dir,
|
34 |
+
local_files_only=local_files_only,
|
35 |
+
)
|
36 |
+
self.beats_model.eval()
|
37 |
+
self.beats_model.to(device)
|
38 |
+
|
39 |
+
beats_ckpt = f"{path}/beats/microsoft-deberta-v3-large.pt"
|
40 |
+
beats_weight = torch.load(beats_ckpt, map_location="cpu")
|
41 |
+
self.beats_model.load_state_dict(beats_weight)
|
42 |
+
|
43 |
+
self.chords_tokenizer = AutoTokenizer.from_pretrained(
|
44 |
+
"google/flan-t5-large",
|
45 |
+
cache_dir=cache_dir,
|
46 |
+
local_files_only=local_files_only,
|
47 |
+
)
|
48 |
+
self.chords_model = T5ForConditionalGeneration.from_pretrained(
|
49 |
+
"google/flan-t5-large",
|
50 |
+
cache_dir=cache_dir,
|
51 |
+
local_files_only=local_files_only,
|
52 |
+
)
|
53 |
+
self.chords_model.eval()
|
54 |
+
self.chords_model.to(device)
|
55 |
+
|
56 |
+
chords_ckpt = f"{path}/chords/flan-t5-large.bin"
|
57 |
+
chords_weight = torch.load(chords_ckpt, map_location="cpu")
|
58 |
+
self.chords_model.load_state_dict(chords_weight)
|
59 |
+
|
60 |
+
def generate_beats(self, prompt):
|
61 |
+
tokenized = self.beats_tokenizer(
|
62 |
+
prompt, max_length=512, padding=True, truncation=True, return_tensors="pt"
|
63 |
+
)
|
64 |
+
tokenized = {k: v.to(self.beats_model.device) for k, v in tokenized.items()}
|
65 |
+
|
66 |
+
with torch.no_grad():
|
67 |
+
out = self.beats_model(**tokenized)
|
68 |
+
|
69 |
+
max_beat = (
|
70 |
+
1 + torch.argmax(out["logits"][:, 0, :], -1).detach().cpu().numpy()
|
71 |
+
).tolist()[0]
|
72 |
+
intervals = (
|
73 |
+
out["values"][:, :, 0]
|
74 |
+
.detach()
|
75 |
+
.cpu()
|
76 |
+
.numpy()
|
77 |
+
.astype("float32")
|
78 |
+
.round(4)
|
79 |
+
.tolist()
|
80 |
+
)
|
81 |
+
|
82 |
+
intervals = np.cumsum(intervals)
|
83 |
+
predicted_beats_times = []
|
84 |
+
for t in intervals:
|
85 |
+
if t < 10:
|
86 |
+
predicted_beats_times.append(round(t, 2))
|
87 |
+
else:
|
88 |
+
break
|
89 |
+
predicted_beats_times = list(np.array(predicted_beats_times)[:50])
|
90 |
+
|
91 |
+
if len(predicted_beats_times) == 0:
|
92 |
+
predicted_beats = [[], []]
|
93 |
+
else:
|
94 |
+
beat_counts = []
|
95 |
+
for i in range(len(predicted_beats_times)):
|
96 |
+
beat_counts.append(float(1.0 + np.mod(i, max_beat)))
|
97 |
+
predicted_beats = [[predicted_beats_times, beat_counts]]
|
98 |
+
|
99 |
+
return max_beat, predicted_beats_times, predicted_beats
|
100 |
+
|
101 |
+
def generate(self, prompt):
|
102 |
+
max_beat, predicted_beats_times, predicted_beats = self.generate_beats(prompt)
|
103 |
+
|
104 |
+
chords_prompt = "Caption: {} \\n Timestamps: {} \\n Max Beat: {}".format(
|
105 |
+
prompt,
|
106 |
+
" , ".join([str(round(t, 2)) for t in predicted_beats_times]),
|
107 |
+
max_beat,
|
108 |
+
)
|
109 |
+
|
110 |
+
tokenized = self.chords_tokenizer(
|
111 |
+
chords_prompt,
|
112 |
+
max_length=512,
|
113 |
+
padding=True,
|
114 |
+
truncation=True,
|
115 |
+
return_tensors="pt",
|
116 |
+
)
|
117 |
+
tokenized = {k: v.to(self.chords_model.device) for k, v in tokenized.items()}
|
118 |
+
|
119 |
+
generated_chords = self.chords_model.generate(
|
120 |
+
input_ids=tokenized["input_ids"],
|
121 |
+
attention_mask=tokenized["attention_mask"],
|
122 |
+
min_length=8,
|
123 |
+
max_length=128,
|
124 |
+
num_beams=5,
|
125 |
+
early_stopping=True,
|
126 |
+
num_return_sequences=1,
|
127 |
+
)
|
128 |
+
|
129 |
+
generated_chords = self.chords_tokenizer.decode(
|
130 |
+
generated_chords[0],
|
131 |
+
skip_special_tokens=True,
|
132 |
+
clean_up_tokenization_spaces=True,
|
133 |
+
).split(" n ")
|
134 |
+
|
135 |
+
predicted_chords, predicted_chords_times = [], []
|
136 |
+
for item in generated_chords:
|
137 |
+
c, ct = item.split(" at ")
|
138 |
+
predicted_chords.append(c)
|
139 |
+
predicted_chords_times.append(float(ct))
|
140 |
+
|
141 |
+
return predicted_beats, predicted_chords, predicted_chords_times
|
142 |
+
|
143 |
+
|
144 |
+
class Mustango:
|
145 |
+
def __init__(
|
146 |
+
self,
|
147 |
+
name="declare-lab/mustango",
|
148 |
+
device="cuda:0",
|
149 |
+
cache_dir=None,
|
150 |
+
local_files_only=False,
|
151 |
+
):
|
152 |
+
path = snapshot_download(repo_id=name, cache_dir=cache_dir)
|
153 |
+
|
154 |
+
self.music_model = MusicFeaturePredictor(
|
155 |
+
path, device, cache_dir=cache_dir, local_files_only=local_files_only
|
156 |
+
)
|
157 |
+
|
158 |
+
vae_config = json.load(open(f"{path}/configs/vae_config.json"))
|
159 |
+
stft_config = json.load(open(f"{path}/configs/stft_config.json"))
|
160 |
+
main_config = json.load(open(f"{path}/configs/main_config.json"))
|
161 |
+
|
162 |
+
self.vae = AutoencoderKL(**vae_config).to(device)
|
163 |
+
self.stft = TacotronSTFT(**stft_config).to(device)
|
164 |
+
self.model = MusicAudioDiffusion(
|
165 |
+
main_config["text_encoder_name"],
|
166 |
+
main_config["scheduler_name"],
|
167 |
+
unet_model_config_path=f"{path}/configs/music_diffusion_model_config.json",
|
168 |
+
).to(device)
|
169 |
+
self.model.device = device
|
170 |
+
|
171 |
+
vae_weights = torch.load(
|
172 |
+
f"{path}/vae/pytorch_model_vae.bin", map_location=device
|
173 |
+
)
|
174 |
+
stft_weights = torch.load(
|
175 |
+
f"{path}/stft/pytorch_model_stft.bin", map_location=device
|
176 |
+
)
|
177 |
+
main_weights = torch.load(
|
178 |
+
f"{path}/ldm/pytorch_model_ldm.bin", map_location=device
|
179 |
+
)
|
180 |
+
|
181 |
+
self.vae.load_state_dict(vae_weights)
|
182 |
+
self.stft.load_state_dict(stft_weights)
|
183 |
+
self.model.load_state_dict(main_weights)
|
184 |
+
|
185 |
+
print("Successfully loaded checkpoint from:", name)
|
186 |
+
|
187 |
+
self.vae.eval()
|
188 |
+
self.stft.eval()
|
189 |
+
self.model.eval()
|
190 |
+
|
191 |
+
self.scheduler = DDPMScheduler.from_pretrained(
|
192 |
+
main_config["scheduler_name"], subfolder="scheduler"
|
193 |
+
)
|
194 |
+
|
195 |
+
def generate(self, prompt, steps=100, guidance=3, samples=1, disable_progress=True):
|
196 |
+
"""Genrate music for a single prompt string."""
|
197 |
+
|
198 |
+
with torch.no_grad():
|
199 |
+
beats, chords, chords_times = self.music_model.generate(prompt)
|
200 |
+
latents = self.model.inference(
|
201 |
+
[prompt],
|
202 |
+
beats,
|
203 |
+
[chords],
|
204 |
+
[chords_times],
|
205 |
+
self.scheduler,
|
206 |
+
steps,
|
207 |
+
guidance,
|
208 |
+
samples,
|
209 |
+
disable_progress,
|
210 |
+
)
|
211 |
+
mel = self.vae.decode_first_stage(latents)
|
212 |
+
wave = self.vae.decode_to_waveform(mel)
|
213 |
+
|
214 |
+
return wave[0]
|
215 |
+
|
216 |
+
|
217 |
+
# Initialize Mustango
|
218 |
+
if torch.cuda.is_available():
|
219 |
+
mustango = Mustango()
|
220 |
+
else:
|
221 |
+
mustango = Mustango(device="cpu")
|
222 |
+
|
223 |
+
# output_wave = mustango.generate("This techno song features a synth lead playing the main melody.", 5, 3, disable_progress=False)
|
224 |
+
|
225 |
+
def gradio_generate(prompt, steps, guidance):
|
226 |
+
output_wave = mustango.generate(prompt, steps, guidance)
|
227 |
+
# output_filename = f"{prompt.replace(' ', '_')}_{steps}_{guidance}"[:250] + ".wav"
|
228 |
+
output_filename = "temp.wav"
|
229 |
+
wavio.write(output_filename, output_wave, rate=16000, sampwidth=2)
|
230 |
+
|
231 |
+
return output_filename
|
232 |
+
|
233 |
+
|
234 |
+
title="Mustango: Toward Controllable Text-to-Music Generation"
|
235 |
+
description_text = """
|
236 |
+
<p><a href="https://huggingface.co/spaces/declare-lab/mustango/blob/main/app.py?duplicate=true"> <img style="margin-top: 0em; margin-bottom: 0em" src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a> For faster inference without waiting in queue, you may duplicate the space and upgrade to a GPU in the settings. <br/><br/>
|
237 |
+
Generate music using Mustango by providing a text prompt.
|
238 |
+
<br/><br/> This is the demo for Mustango for controllable text to music generation: <a href="https://arxiv.org/abs/2311.08355">Read our paper.</a>
|
239 |
+
<p/>
|
240 |
+
"""
|
241 |
+
#description_text = ""
|
242 |
+
# Gradio input and output components
|
243 |
+
input_text = gr.Textbox(lines=2, label="Prompt")
|
244 |
+
output_audio = gr.Audio(label="Generated Music", type="filepath")
|
245 |
+
denoising_steps = gr.Slider(minimum=100, maximum=200, value=100, step=1, label="Steps", interactive=True)
|
246 |
+
guidance_scale = gr.Slider(minimum=1, maximum=10, value=3, step=0.1, label="Guidance Scale", interactive=True)
|
247 |
+
|
248 |
+
# CSS styling for the Duplicate button
|
249 |
+
css = '''
|
250 |
+
#duplicate-button {
|
251 |
+
margin: auto;
|
252 |
+
color: white;
|
253 |
+
background: #1565c0;
|
254 |
+
border-radius: 100vh;
|
255 |
+
}
|
256 |
+
'''
|
257 |
+
|
258 |
+
# Gradio interface
|
259 |
+
gr_interface = gr.Interface(
|
260 |
+
fn=gradio_generate,
|
261 |
+
inputs=[input_text, denoising_steps, guidance_scale],
|
262 |
+
outputs=[output_audio],
|
263 |
+
description=description_text,
|
264 |
+
allow_flagging=False,
|
265 |
+
examples=[
|
266 |
+
["This techno song features a synth lead playing the main melody. This is accompanied by programmed percussion playing a simple kick focused beat. The hi-hat is accented in an open position on the 3-and count of every bar. The synth plays the bass part with a voicing that sounds like a cello. This techno song can be played in a club. The chord sequence is Gm, A7, Eb, Bb, C, F, Gm. The beat counts to 2. The tempo of this song is 128.0 beats per minute. The key of this song is G minor.", 100, 3],
|
267 |
+
["This is a new age piece. There is a flute playing the main melody with a lot of staccato notes. The rhythmic background consists of a medium tempo electronic drum beat with percussive elements all over the spectrum. There is a playful atmosphere to the piece. This piece can be used in the soundtrack of a children's TV show or an advertisement jingle.", 100, 3],
|
268 |
+
["The song is an instrumental. The song is in medium tempo with a classical guitar playing a lilting melody in accompaniment style. The song is emotional and romantic. The song is a romantic instrumental song. The chord sequence is Gm, F6, Ebm. The time signature is 4/4. This song is in Adagio. The key of this song is G minor.", 100, 3],
|
269 |
+
["This folk song features a female voice singing the main melody. This is accompanied by a tabla playing the percussion. A guitar strums chords. For most parts of the song, only one chord is played. At the last bar, a different chord is played. This song has minimal instruments. This song has a story-telling mood. This song can be played in a village scene in an Indian movie. The chord sequence is Bbm, Ab. The beat is 3. The tempo of this song is Allegro. The key of this song is Bb minor.", 100, 3],
|
270 |
+
["This is a live performance of a classical music piece. There is an orchestra performing the piece with a violin lead playing the main melody. The atmosphere is sentimental and heart-touching. This piece could be playing in the background at a classy restaurant. The chord progression in this song is Am7, Gm, Dm, A7, Dm. The beat is 3. This song is in Largo. The key of this song is D minor.", 100, 3],
|
271 |
+
["This is a techno piece with drums and beats and a leading melody. A synth plays chords. The music kicks off with a powerful and relentless drumbeat. Over the pounding beats, a leading melody emerges. In the middle of the song, a flock of seagulls flies over the venue and make loud bird sounds. It has strong danceability and can be played in a club. The tempo is 120 bpm. The chords played by the synth are Am, Cm, Dm, Gm.", 100, 3],
|
272 |
+
],
|
273 |
+
cache_examples=True,
|
274 |
+
)
|
275 |
+
|
276 |
+
with gr.Blocks(css=css) as demo:
|
277 |
+
title=gr.HTML(f"<h1><center>{title}</center></h1>")
|
278 |
+
dupe = gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
|
279 |
+
gr_interface.render()
|
280 |
+
|
281 |
+
|
282 |
+
# Launch Gradio app
|
283 |
+
demo.queue().launch()
|
app.py
CHANGED
@@ -144,13 +144,12 @@ class MusicFeaturePredictor:
|
|
144 |
class Mustango:
|
145 |
def __init__(
|
146 |
self,
|
|
|
147 |
device="cuda:0",
|
148 |
cache_dir=None,
|
149 |
local_files_only=False,
|
150 |
):
|
151 |
-
|
152 |
-
path = snapshot_download(repo_id="declare-lab/mustango", cache_dir=cache_dir)
|
153 |
-
pretrained_path = snapshot_download(repo_id="declare-lab/mustango-pretrained", cache_dir=cache_dir)
|
154 |
|
155 |
self.music_model = MusicFeaturePredictor(
|
156 |
path, device, cache_dir=cache_dir, local_files_only=local_files_only
|
@@ -175,16 +174,15 @@ class Mustango:
|
|
175 |
stft_weights = torch.load(
|
176 |
f"{path}/stft/pytorch_model_stft.bin", map_location=device
|
177 |
)
|
178 |
-
|
179 |
f"{path}/ldm/pytorch_model_ldm.bin", map_location=device
|
180 |
)
|
181 |
-
|
182 |
-
self.main_weights_pretrained = torch.load(
|
183 |
-
f"{pretrained_path}/ldm/pytorch_model_ldm.bin", map_location=device
|
184 |
-
)
|
185 |
|
186 |
self.vae.load_state_dict(vae_weights)
|
187 |
self.stft.load_state_dict(stft_weights)
|
|
|
|
|
|
|
188 |
|
189 |
self.vae.eval()
|
190 |
self.stft.eval()
|
@@ -194,13 +192,8 @@ class Mustango:
|
|
194 |
main_config["scheduler_name"], subfolder="scheduler"
|
195 |
)
|
196 |
|
197 |
-
def generate(self,
|
198 |
"""Genrate music for a single prompt string."""
|
199 |
-
|
200 |
-
if model_name == "declare-lab/mustango":
|
201 |
-
self.model.load_state_dict(self.main_weights)
|
202 |
-
else:
|
203 |
-
self.model.load_state_dict(self.main_weights_pretrained)
|
204 |
|
205 |
with torch.no_grad():
|
206 |
beats, chords, chords_times = self.music_model.generate(prompt)
|
@@ -229,8 +222,8 @@ else:
|
|
229 |
|
230 |
# output_wave = mustango.generate("This techno song features a synth lead playing the main melody.", 5, 3, disable_progress=False)
|
231 |
|
232 |
-
def gradio_generate(
|
233 |
-
output_wave = mustango.generate(
|
234 |
# output_filename = f"{prompt.replace(' ', '_')}_{steps}_{guidance}"[:250] + ".wav"
|
235 |
output_filename = "temp.wav"
|
236 |
wavio.write(output_filename, output_wave, rate=16000, sampwidth=2)
|
@@ -251,8 +244,6 @@ input_text = gr.Textbox(lines=2, label="Prompt")
|
|
251 |
output_audio = gr.Audio(label="Generated Music", type="filepath")
|
252 |
denoising_steps = gr.Slider(minimum=100, maximum=200, value=100, step=1, label="Steps", interactive=True)
|
253 |
guidance_scale = gr.Slider(minimum=1, maximum=10, value=3, step=0.1, label="Guidance Scale", interactive=True)
|
254 |
-
model_name = gr.Radio(["declare-lab/mustango", "declare-lab/mustango-pretrained",], label="Choose a model type", value="declare-lab/mustango", type="value")
|
255 |
-
|
256 |
|
257 |
# CSS styling for the Duplicate button
|
258 |
css = '''
|
@@ -267,17 +258,17 @@ css = '''
|
|
267 |
# Gradio interface
|
268 |
gr_interface = gr.Interface(
|
269 |
fn=gradio_generate,
|
270 |
-
inputs=[
|
271 |
outputs=[output_audio],
|
272 |
description=description_text,
|
273 |
allow_flagging=False,
|
274 |
examples=[
|
275 |
-
["
|
276 |
-
["
|
277 |
-
["
|
278 |
-
["
|
279 |
-
["
|
280 |
-
["
|
281 |
],
|
282 |
cache_examples=True,
|
283 |
)
|
|
|
144 |
class Mustango:
|
145 |
def __init__(
|
146 |
self,
|
147 |
+
name="declare-lab/mustango",
|
148 |
device="cuda:0",
|
149 |
cache_dir=None,
|
150 |
local_files_only=False,
|
151 |
):
|
152 |
+
path = snapshot_download(repo_id=name, cache_dir=cache_dir)
|
|
|
|
|
153 |
|
154 |
self.music_model = MusicFeaturePredictor(
|
155 |
path, device, cache_dir=cache_dir, local_files_only=local_files_only
|
|
|
174 |
stft_weights = torch.load(
|
175 |
f"{path}/stft/pytorch_model_stft.bin", map_location=device
|
176 |
)
|
177 |
+
main_weights = torch.load(
|
178 |
f"{path}/ldm/pytorch_model_ldm.bin", map_location=device
|
179 |
)
|
|
|
|
|
|
|
|
|
180 |
|
181 |
self.vae.load_state_dict(vae_weights)
|
182 |
self.stft.load_state_dict(stft_weights)
|
183 |
+
self.model.load_state_dict(main_weights)
|
184 |
+
|
185 |
+
print("Successfully loaded checkpoint from:", name)
|
186 |
|
187 |
self.vae.eval()
|
188 |
self.stft.eval()
|
|
|
192 |
main_config["scheduler_name"], subfolder="scheduler"
|
193 |
)
|
194 |
|
195 |
+
def generate(self, prompt, steps=100, guidance=3, samples=1, disable_progress=True):
|
196 |
"""Genrate music for a single prompt string."""
|
|
|
|
|
|
|
|
|
|
|
197 |
|
198 |
with torch.no_grad():
|
199 |
beats, chords, chords_times = self.music_model.generate(prompt)
|
|
|
222 |
|
223 |
# output_wave = mustango.generate("This techno song features a synth lead playing the main melody.", 5, 3, disable_progress=False)
|
224 |
|
225 |
+
def gradio_generate(prompt, steps, guidance):
|
226 |
+
output_wave = mustango.generate(prompt, steps, guidance)
|
227 |
# output_filename = f"{prompt.replace(' ', '_')}_{steps}_{guidance}"[:250] + ".wav"
|
228 |
output_filename = "temp.wav"
|
229 |
wavio.write(output_filename, output_wave, rate=16000, sampwidth=2)
|
|
|
244 |
output_audio = gr.Audio(label="Generated Music", type="filepath")
|
245 |
denoising_steps = gr.Slider(minimum=100, maximum=200, value=100, step=1, label="Steps", interactive=True)
|
246 |
guidance_scale = gr.Slider(minimum=1, maximum=10, value=3, step=0.1, label="Guidance Scale", interactive=True)
|
|
|
|
|
247 |
|
248 |
# CSS styling for the Duplicate button
|
249 |
css = '''
|
|
|
258 |
# Gradio interface
|
259 |
gr_interface = gr.Interface(
|
260 |
fn=gradio_generate,
|
261 |
+
inputs=[input_text, denoising_steps, guidance_scale],
|
262 |
outputs=[output_audio],
|
263 |
description=description_text,
|
264 |
allow_flagging=False,
|
265 |
examples=[
|
266 |
+
["This techno song features a synth lead playing the main melody. This is accompanied by programmed percussion playing a simple kick focused beat. The hi-hat is accented in an open position on the 3-and count of every bar. The synth plays the bass part with a voicing that sounds like a cello. This techno song can be played in a club. The chord sequence is Gm, A7, Eb, Bb, C, F, Gm. The beat counts to 2. The tempo of this song is 128.0 beats per minute. The key of this song is G minor.", 100, 3],
|
267 |
+
["This is a new age piece. There is a flute playing the main melody with a lot of staccato notes. The rhythmic background consists of a medium tempo electronic drum beat with percussive elements all over the spectrum. There is a playful atmosphere to the piece. This piece can be used in the soundtrack of a children's TV show or an advertisement jingle.", 100, 3],
|
268 |
+
["The song is an instrumental. The song is in medium tempo with a classical guitar playing a lilting melody in accompaniment style. The song is emotional and romantic. The song is a romantic instrumental song. The chord sequence is Gm, F6, Ebm. The time signature is 4/4. This song is in Adagio. The key of this song is G minor.", 100, 3],
|
269 |
+
["This folk song features a female voice singing the main melody. This is accompanied by a tabla playing the percussion. A guitar strums chords. For most parts of the song, only one chord is played. At the last bar, a different chord is played. This song has minimal instruments. This song has a story-telling mood. This song can be played in a village scene in an Indian movie. The chord sequence is Bbm, Ab. The beat is 3. The tempo of this song is Allegro. The key of this song is Bb minor.", 100, 3],
|
270 |
+
["This is a live performance of a classical music piece. There is an orchestra performing the piece with a violin lead playing the main melody. The atmosphere is sentimental and heart-touching. This piece could be playing in the background at a classy restaurant. The chord progression in this song is Am7, Gm, Dm, A7, Dm. The beat is 3. This song is in Largo. The key of this song is D minor.", 100, 3],
|
271 |
+
["This is a techno piece with drums and beats and a leading melody. A synth plays chords. The music kicks off with a powerful and relentless drumbeat. Over the pounding beats, a leading melody emerges. In the middle of the song, a flock of seagulls flies over the venue and make loud bird sounds. It has strong danceability and can be played in a club. The tempo is 120 bpm. The chords played by the synth are Am, Cm, Dm, Gm.", 100, 3],
|
272 |
],
|
273 |
cache_examples=True,
|
274 |
)
|