Spaces:
Runtime error
Runtime error
File size: 14,575 Bytes
3ae65e0 8e10a53 3ae65e0 daf3ca1 3ae65e0 925b7f8 3ae65e0 daf3ca1 17e0c31 3ae65e0 daf3ca1 82e1128 daf3ca1 3ae65e0 daf3ca1 98b68dc 88c405d 3ae65e0 88c405d 82e1128 3ae65e0 daf3ca1 3ae65e0 88c405d 3ae65e0 23fe483 daf3ca1 3ae65e0 82e1128 000b8ee 82e1128 3ae65e0 5238467 8e10a53 daf3ca1 82e1128 000b8ee 82e1128 daf3ca1 8e10a53 82e1128 88c405d 925b7f8 88c405d 8e10a53 88c405d 45e1876 925b7f8 88c405d daf3ca1 88c405d 8e10a53 daf3ca1 88c405d daf3ca1 8e10a53 daf3ca1 88c405d daf3ca1 88c405d 82e1128 8e10a53 daf3ca1 82e1128 daf3ca1 82e1128 daf3ca1 17e0c31 daf3ca1 82e1128 8e10a53 98b68dc 8e10a53 98b68dc 8e10a53 98b68dc 8e10a53 98b68dc 8e10a53 17e0c31 98b68dc daf3ca1 8e10a53 23fe483 8e10a53 23fe483 8e10a53 23fe483 8e10a53 23fe483 8e10a53 5238467 8e10a53 daf3ca1 8e10a53 daf3ca1 8e10a53 daf3ca1 8e10a53 daf3ca1 8e10a53 daf3ca1 8e10a53 daf3ca1 8e10a53 23fe483 8e10a53 daf3ca1 8e10a53 cfe7e3e daf3ca1 5238467 8e10a53 daf3ca1 3ae65e0 8e10a53 daf3ca1 3ae65e0 8e10a53 daf3ca1 8e10a53 cfe7e3e daf3ca1 8e10a53 daf3ca1 8e10a53 3ae65e0 8e10a53 daf3ca1 23fe483 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
"""
Copyright (c) Meta Platforms, Inc. and affiliates.
All rights reserved.
This source code is licensed under the license found in the
LICENSE file in the root directory of this source tree.
"""
from tempfile import NamedTemporaryFile
import argparse
import torch
import torchaudio
import gradio as gr
import os
from audiocraft.models import MusicGen
from audiocraft.data.audio import audio_write
from share_btn import community_icon_html, loading_icon_html, share_js, css
MODEL = None
def load_model(version):
print("Loading model", version)
return MusicGen.get_pretrained(version)
def predict(
text,
melody_input,
duration=30,
continuation_start=0,
continuation_end=30,
topk=250,
topp=0,
temperature=1,
cfg_coef=3,
):
global MODEL
topk = int(topk)
if MODEL is None:
MODEL = load_model("melody")
if melody_input is None:
raise gr.Error("Please upload a melody to continue!")
if duration > MODEL.lm.cfg.dataset.segment_duration:
raise gr.Error("MusicGen currently supports durations of up to 30 seconds!")
if continuation_end < continuation_start:
raise gr.Error("The end time must be greater than the start time!")
MODEL.set_generation_params(
use_sampling=True,
top_k=topk,
top_p=topp,
temperature=temperature,
cfg_coef=cfg_coef,
duration=duration,
)
if melody_input:
melody, sr = torchaudio.load(melody_input)
# sr, melody = melody_input[0], torch.from_numpy(melody_input[1]).to(MODEL.device).float().t().unsqueeze(0)
if melody.dim() == 2:
melody = melody[None]
print("\nGenerating continuation\n")
melody_wavform = melody[
..., int(sr * continuation_start) : int(sr * continuation_end)
]
melody_duration = melody_wavform.shape[-1] / sr
if duration + melody_duration > MODEL.lm.cfg.dataset.segment_duration:
raise gr.Error("Duration + continuation duration must be <= 30 seconds")
output = MODEL.generate_continuation(
prompt=melody_wavform,
prompt_sample_rate=sr,
descriptions=[text],
progress=True,
)
output = output.detach().cpu().float()[0]
with NamedTemporaryFile("wb", suffix=".wav", delete=False) as file:
audio_write(
file.name,
output,
MODEL.sample_rate,
strategy="loudness",
loudness_headroom_db=16,
loudness_compressor=True,
add_suffix=False,
)
waveform_video = gr.make_waveform(file.name)
return (
waveform_video,
(sr, melody_wavform.unsqueeze(0).numpy()) if melody_input else None,
)
def ui(**kwargs):
def toggle(choice):
if choice == "mic":
return gr.update(source="microphone", value=None, label="Microphone")
else:
return gr.update(source="upload", value=None, label="File")
def check_melody_length(melody_input):
if not melody_input:
return gr.update(maximum=0, value=0), gr.update(maximum=0, value=0)
melody, sr = torchaudio.load(melody_input)
audio_length = melody.shape[-1] / sr
if melody.dim() == 2:
melody = melody[None]
return gr.update(maximum=audio_length, value=0), gr.update(
maximum=audio_length, value=audio_length
)
def preview_melody_cut(melody_input, continuation_start, continuation_end):
if not melody_input:
return gr.update(maximum=0, value=0), gr.update(maximum=0, value=0)
melody, sr = torchaudio.load(melody_input)
audio_length = melody.shape[-1] / sr
if melody.dim() == 2:
melody = melody[None]
if continuation_end < continuation_start:
raise gr.Error("The end time must be greater than the start time!")
if continuation_start < 0 or continuation_end > audio_length:
raise gr.Error("The continuation settings must be within the audio length!")
print("cutting", int(sr * continuation_start), int(sr * continuation_end))
prompt_waveform = melody[
..., int(sr * continuation_start) : int(sr * continuation_end)
]
return (sr, prompt_waveform.unsqueeze(0).numpy())
with gr.Blocks(css=css) as interface:
gr.Markdown(
"""
# MusicGen Continuation
This a [MusicGen](https://github.com/facebookresearch/audiocraft), a simple and controllable model for music generation
presented at: ["Simple and Controllable Music Generation"](https://huggingface.co./papers/2306.05284)
This Spaces only does melody continuation, you can try other features [here](https://huggingface.co./spaces/facebook/MusicGen)
"""
)
gr.Markdown(
"""
<a href="https://huggingface.co./spaces/radames/MusicGen-Continuation?duplicate=true" style="display: inline-block;margin-top: .5em;margin-right: .25em;" target="_blank">
<img style="margin-bottom: 0em;display: inline;margin-top: -.25em;" src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>
to use it privately
"""
)
with gr.Row():
with gr.Column():
with gr.Row():
text = gr.Text(
label="Describe your music",
lines=2,
interactive=True,
elem_id="text-input",
)
with gr.Column():
radio = gr.Radio(
["file", "mic"],
value="file",
label="Melody Inital Condition File or Mic",
info="Make sure the audio is no longer than total generation duration which is max 30 seconds, you can trim the audio in the next section",
)
melody = gr.Audio(
source="upload",
type="filepath",
label="File",
interactive=True,
elem_id="melody-input",
)
with gr.Row():
submit = gr.Button("Submit")
with gr.Row():
duration = gr.Slider(
minimum=1,
maximum=30,
value=10,
label="Total Generation Duration",
interactive=True,
)
with gr.Accordion(label="Input Melody Trimming (optional)", open=False):
with gr.Row():
continuation_start = gr.Slider(
minimum=0,
maximum=30,
step=0.01,
value=0,
label="melody cut start",
interactive=True,
)
continuation_end = gr.Slider(
minimum=0,
maximum=30,
step=0.01,
value=0,
label="melody cut end",
interactive=True,
)
cut_btn = gr.Button("Cut Melody").style(full_width=False)
with gr.Row():
preview_cut = gr.Audio(
type="numpy",
label="Cut Preview",
)
with gr.Accordion(label="Advanced Settings", open=False):
with gr.Row():
topk = gr.Number(label="Top-k", value=250, interactive=True)
topp = gr.Number(label="Top-p", value=0, interactive=True)
temperature = gr.Number(
label="Temperature", value=1.0, interactive=True
)
cfg_coef = gr.Number(
label="Classifier Free Guidance",
value=3.0,
interactive=True,
)
with gr.Column():
output = gr.Video(label="Generated Music", elem_id="generated-video")
output_melody = gr.Audio(label="Melody ", elem_id="melody-output")
with gr.Row(visible=False) as share_row:
with gr.Group(elem_id="share-btn-container"):
community_icon = gr.HTML(community_icon_html)
loading_icon = gr.HTML(loading_icon_html)
share_button = gr.Button(
"Share to community", elem_id="share-btn"
)
share_button.click(None, [], [], _js=share_js)
melody.change(
check_melody_length,
melody,
[continuation_start, continuation_end],
queue=False,
)
cut_btn.click(
preview_melody_cut,
[melody, continuation_start, continuation_end],
preview_cut,
queue=False,
)
submit.click(
lambda x: gr.update(visible=False),
None,
[share_row],
queue=False,
show_progress=False,
).then(
predict,
inputs=[
text,
melody,
duration,
continuation_start,
continuation_end,
topk,
topp,
temperature,
cfg_coef,
],
outputs=[output, output_melody],
).then(
lambda x: gr.update(visible=True),
None,
[share_row],
queue=False,
show_progress=False,
)
radio.change(toggle, radio, [melody], queue=False, show_progress=False)
examples = gr.Examples(
fn=predict,
examples=[
[
"An 80s driving pop song with heavy drums and synth pads in the background",
"./assets/bach.mp3",
25,
0,
5,
],
[
"A cheerful country song with acoustic guitars",
"./assets/bolero_ravel.mp3",
25,
0,
5,
],
[
"90s rock song with electric guitar and heavy drums",
"./assets/bach.mp3",
25,
0,
5,
],
[
"a light and cheerly EDM track, with syncopated drums, aery pads, and strong emotions",
"./assets/bach.mp3",
25,
0,
5,
],
[
"lofi slow bpm electro chill with organic samples",
"./assets/bolero_ravel.mp3",
25,
0,
5,
],
],
inputs=[text, melody, duration, continuation_start, continuation_end],
outputs=[output],
)
gr.Markdown(
"""
### More details
The model will generate a short music extract based on the description you provided.
You can generate up to 30 seconds of audio.
We present 4 model variations:
1. Melody -- a music generation model capable of generating music condition on text and melody inputs. **Note**, you can also use text only.
2. Small -- a 300M transformer decoder conditioned on text only.
3. Medium -- a 1.5B transformer decoder conditioned on text only.
4. Large -- a 3.3B transformer decoder conditioned on text only (might OOM for the longest sequences.)
When using `melody`, ou can optionaly provide a reference audio from
which a broad melody will be extracted. The model will then try to follow both the description and melody provided.
You can also use your own GPU or a Google Colab by following the instructions on our repo.
See [github.com/facebookresearch/audiocraft](https://github.com/facebookresearch/audiocraft)
for more details.
"""
)
# Show the interface
launch_kwargs = {}
username = kwargs.get("username")
password = kwargs.get("password")
server_port = kwargs.get("server_port", 0)
inbrowser = kwargs.get("inbrowser", False)
share = kwargs.get("share", False)
server_name = kwargs.get("listen")
launch_kwargs["server_name"] = server_name
if username and password:
launch_kwargs["auth"] = (username, password)
if server_port > 0:
launch_kwargs["server_port"] = server_port
if inbrowser:
launch_kwargs["inbrowser"] = inbrowser
if share:
launch_kwargs["share"] = share
interface.queue().launch(**launch_kwargs, max_threads=1)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--listen",
type=str,
default="0.0.0.0",
help="IP to listen on for connections to Gradio",
)
parser.add_argument(
"--username", type=str, default="", help="Username for authentication"
)
parser.add_argument(
"--password", type=str, default="", help="Password for authentication"
)
parser.add_argument(
"--server_port",
type=int,
default=7860,
help="Port to run the server listener on",
)
parser.add_argument("--inbrowser", action="store_true", help="Open in browser")
parser.add_argument("--share", action="store_true", help="Share the gradio UI")
args = parser.parse_args()
ui(
username=args.username,
password=args.password,
inbrowser=args.inbrowser,
server_port=args.server_port,
share=args.share,
listen=args.listen,
)
|