Spaces:
Runtime error
Runtime error
Commit ·
2f90563
1
Parent(s): d6607f9
trimmed down
Browse files
app.py
CHANGED
|
@@ -26,9 +26,6 @@ from audiocraft.models import MusicGen
|
|
| 26 |
|
| 27 |
|
| 28 |
MODEL = None # Last used model
|
| 29 |
-
IS_BATCHED = "facebook/MusicGen" in os.environ.get('SPACE_ID', '')
|
| 30 |
-
MAX_BATCH_SIZE = 6
|
| 31 |
-
BATCHED_DURATION = 15
|
| 32 |
INTERRUPTING = False
|
| 33 |
# We have to wrap subprocess call to clean a bit the log when using gr.make_waveform
|
| 34 |
_old_call = sp.call
|
|
@@ -137,14 +134,6 @@ def _do_predictions(texts, melodies, duration, progress=False, **gen_kwargs):
|
|
| 137 |
return res
|
| 138 |
|
| 139 |
|
| 140 |
-
def predict_batched(texts, melodies):
|
| 141 |
-
max_text_length = 512
|
| 142 |
-
texts = [text[:max_text_length] for text in texts]
|
| 143 |
-
load_model('melody')
|
| 144 |
-
res = _do_predictions(texts, melodies, BATCHED_DURATION)
|
| 145 |
-
return [res]
|
| 146 |
-
|
| 147 |
-
|
| 148 |
def predict_full(model, text, melody, duration, topk, topp, temperature, cfg_coef, progress=gr.Progress()):
|
| 149 |
global INTERRUPTING
|
| 150 |
INTERRUPTING = False
|
|
@@ -200,165 +189,26 @@ def ui_full(launch_kwargs):
|
|
| 200 |
submit = gr.Button("Submit")
|
| 201 |
# Adapted from https://github.com/rkfg/audiocraft/blob/long/app.py, MIT license.
|
| 202 |
_ = gr.Button("Interrupt").click(fn=interrupt, queue=False)
|
| 203 |
-
with gr.Row():
|
| 204 |
-
model = gr.Radio(["melody", "medium", "small", "large"],
|
| 205 |
-
label="Model", value="melody", interactive=True)
|
| 206 |
-
with gr.Row():
|
| 207 |
-
duration = gr.Slider(minimum=1, maximum=120, value=10, label="Duration", interactive=True)
|
| 208 |
-
with gr.Row():
|
| 209 |
-
topk = gr.Number(label="Top-k", value=250, interactive=True)
|
| 210 |
-
topp = gr.Number(label="Top-p", value=0, interactive=True)
|
| 211 |
-
temperature = gr.Number(label="Temperature", value=1.0, interactive=True)
|
| 212 |
-
cfg_coef = gr.Number(label="Classifier Free Guidance", value=3.0, interactive=True)
|
| 213 |
with gr.Column():
|
| 214 |
output = gr.Video(label="Generated Music")
|
| 215 |
submit.click(predict_full,
|
| 216 |
-
inputs=[
|
| 217 |
outputs=[output])
|
| 218 |
radio.change(toggle_audio_src, radio, [melody], queue=False, show_progress=False)
|
| 219 |
gr.Examples(
|
| 220 |
fn=predict_full,
|
| 221 |
examples=[
|
| 222 |
[
|
| 223 |
-
"
|
| 224 |
-
"./assets/
|
| 225 |
-
"melody"
|
| 226 |
-
],
|
| 227 |
-
[
|
| 228 |
-
"A cheerful country song with acoustic guitars",
|
| 229 |
-
"./assets/bolero_ravel.mp3",
|
| 230 |
"melody"
|
| 231 |
-
]
|
| 232 |
-
[
|
| 233 |
-
"90s rock song with electric guitar and heavy drums",
|
| 234 |
-
None,
|
| 235 |
-
"medium"
|
| 236 |
-
],
|
| 237 |
-
[
|
| 238 |
-
"a light and cheerly EDM track, with syncopated drums, aery pads, and strong emotions",
|
| 239 |
-
"./assets/bach.mp3",
|
| 240 |
-
"melody"
|
| 241 |
-
],
|
| 242 |
-
[
|
| 243 |
-
"lofi slow bpm electro chill with organic samples",
|
| 244 |
-
None,
|
| 245 |
-
"medium",
|
| 246 |
-
],
|
| 247 |
],
|
| 248 |
inputs=[text, melody, model],
|
| 249 |
outputs=[output]
|
| 250 |
)
|
| 251 |
-
gr.Markdown(
|
| 252 |
-
"""
|
| 253 |
-
### More details
|
| 254 |
-
|
| 255 |
-
The model will generate a short music extract based on the description you provided.
|
| 256 |
-
The model can generate up to 30 seconds of audio in one pass. It is now possible
|
| 257 |
-
to extend the generation by feeding back the end of the previous chunk of audio.
|
| 258 |
-
This can take a long time, and the model might lose consistency. The model might also
|
| 259 |
-
decide at arbitrary positions that the song ends.
|
| 260 |
-
|
| 261 |
-
**WARNING:** Choosing long durations will take a long time to generate (2min might take ~10min).
|
| 262 |
-
An overlap of 12 seconds is kept with the previously generated chunk, and 18 "new" seconds
|
| 263 |
-
are generated each time.
|
| 264 |
-
|
| 265 |
-
We present 4 model variations:
|
| 266 |
-
1. Melody -- a music generation model capable of generating music condition
|
| 267 |
-
on text and melody inputs. **Note**, you can also use text only.
|
| 268 |
-
2. Small -- a 300M transformer decoder conditioned on text only.
|
| 269 |
-
3. Medium -- a 1.5B transformer decoder conditioned on text only.
|
| 270 |
-
4. Large -- a 3.3B transformer decoder conditioned on text only (might OOM for the longest sequences.)
|
| 271 |
-
|
| 272 |
-
When using `melody`, ou can optionaly provide a reference audio from
|
| 273 |
-
which a broad melody will be extracted. The model will then try to follow both
|
| 274 |
-
the description and melody provided.
|
| 275 |
-
|
| 276 |
-
You can also use your own GPU or a Google Colab by following the instructions on our repo.
|
| 277 |
-
See [github.com/facebookresearch/audiocraft](https://github.com/facebookresearch/audiocraft)
|
| 278 |
-
for more details.
|
| 279 |
-
"""
|
| 280 |
-
)
|
| 281 |
-
|
| 282 |
interface.queue().launch(**launch_kwargs)
|
| 283 |
|
| 284 |
-
|
| 285 |
-
def ui_batched(launch_kwargs):
|
| 286 |
-
with gr.Blocks() as demo:
|
| 287 |
-
gr.Markdown(
|
| 288 |
-
"""
|
| 289 |
-
# MusicGen
|
| 290 |
-
|
| 291 |
-
This is the demo for [MusicGen](https://github.com/facebookresearch/audiocraft),
|
| 292 |
-
a simple and controllable model for music generation
|
| 293 |
-
presented at: ["Simple and Controllable Music Generation"](https://huggingface.co/papers/2306.05284).
|
| 294 |
-
<br/>
|
| 295 |
-
<a href="https://huggingface.co/spaces/facebook/MusicGen?duplicate=true"
|
| 296 |
-
style="display: inline-block;margin-top: .5em;margin-right: .25em;" target="_blank">
|
| 297 |
-
<img style="margin-bottom: 0em;display: inline;margin-top: -.25em;"
|
| 298 |
-
src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>
|
| 299 |
-
for longer sequences, more control and no queue.</p>
|
| 300 |
-
"""
|
| 301 |
-
)
|
| 302 |
-
with gr.Row():
|
| 303 |
-
with gr.Column():
|
| 304 |
-
with gr.Row():
|
| 305 |
-
text = gr.Text(label="Describe your music", lines=2, interactive=True)
|
| 306 |
-
with gr.Column():
|
| 307 |
-
radio = gr.Radio(["file", "mic"], value="file",
|
| 308 |
-
label="Condition on a melody (optional) File or Mic")
|
| 309 |
-
melody = gr.Audio(source="upload", type="numpy", label="File",
|
| 310 |
-
interactive=True, elem_id="melody-input")
|
| 311 |
-
with gr.Row():
|
| 312 |
-
submit = gr.Button("Generate")
|
| 313 |
-
with gr.Column():
|
| 314 |
-
output = gr.Video(label="Generated Music")
|
| 315 |
-
submit.click(predict_batched, inputs=[text, melody],
|
| 316 |
-
outputs=[output], batch=True, max_batch_size=MAX_BATCH_SIZE)
|
| 317 |
-
radio.change(toggle_audio_src, radio, [melody], queue=False, show_progress=False)
|
| 318 |
-
gr.Examples(
|
| 319 |
-
fn=predict_batched,
|
| 320 |
-
examples=[
|
| 321 |
-
[
|
| 322 |
-
"An 80s driving pop song with heavy drums and synth pads in the background",
|
| 323 |
-
"./assets/bach.mp3",
|
| 324 |
-
],
|
| 325 |
-
[
|
| 326 |
-
"A cheerful country song with acoustic guitars",
|
| 327 |
-
"./assets/bolero_ravel.mp3",
|
| 328 |
-
],
|
| 329 |
-
[
|
| 330 |
-
"90s rock song with electric guitar and heavy drums",
|
| 331 |
-
None,
|
| 332 |
-
],
|
| 333 |
-
[
|
| 334 |
-
"a light and cheerly EDM track, with syncopated drums, aery pads, and strong emotions bpm: 130",
|
| 335 |
-
"./assets/bach.mp3",
|
| 336 |
-
],
|
| 337 |
-
[
|
| 338 |
-
"lofi slow bpm electro chill with organic samples",
|
| 339 |
-
None,
|
| 340 |
-
],
|
| 341 |
-
],
|
| 342 |
-
inputs=[text, melody],
|
| 343 |
-
outputs=[output]
|
| 344 |
-
)
|
| 345 |
-
gr.Markdown("""
|
| 346 |
-
### More details
|
| 347 |
-
|
| 348 |
-
The model will generate 12 seconds of audio based on the description you provided.
|
| 349 |
-
You can optionaly provide a reference audio from which a broad melody will be extracted.
|
| 350 |
-
The model will then try to follow both the description and melody provided.
|
| 351 |
-
All samples are generated with the `melody` model.
|
| 352 |
-
|
| 353 |
-
You can also use your own GPU or a Google Colab by following the instructions on our repo.
|
| 354 |
-
|
| 355 |
-
See [github.com/facebookresearch/audiocraft](https://github.com/facebookresearch/audiocraft)
|
| 356 |
-
for more details.
|
| 357 |
-
""")
|
| 358 |
-
|
| 359 |
-
demo.queue(max_size=8 * 4).launch(**launch_kwargs)
|
| 360 |
-
|
| 361 |
-
|
| 362 |
if __name__ == "__main__":
|
| 363 |
parser = argparse.ArgumentParser()
|
| 364 |
parser.add_argument(
|
|
@@ -401,7 +251,4 @@ if __name__ == "__main__":
|
|
| 401 |
launch_kwargs['share'] = args.share
|
| 402 |
|
| 403 |
# Show the interface
|
| 404 |
-
|
| 405 |
-
ui_batched(launch_kwargs)
|
| 406 |
-
else:
|
| 407 |
-
ui_full(launch_kwargs)
|
|
|
|
| 26 |
|
| 27 |
|
| 28 |
MODEL = None # Last used model
|
|
|
|
|
|
|
|
|
|
| 29 |
INTERRUPTING = False
|
| 30 |
# We have to wrap subprocess call to clean a bit the log when using gr.make_waveform
|
| 31 |
_old_call = sp.call
|
|
|
|
| 134 |
return res
|
| 135 |
|
| 136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
def predict_full(model, text, melody, duration, topk, topp, temperature, cfg_coef, progress=gr.Progress()):
|
| 138 |
global INTERRUPTING
|
| 139 |
INTERRUPTING = False
|
|
|
|
| 189 |
submit = gr.Button("Submit")
|
| 190 |
# Adapted from https://github.com/rkfg/audiocraft/blob/long/app.py, MIT license.
|
| 191 |
_ = gr.Button("Interrupt").click(fn=interrupt, queue=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
with gr.Column():
|
| 193 |
output = gr.Video(label="Generated Music")
|
| 194 |
submit.click(predict_full,
|
| 195 |
+
inputs=["melody", text, melody, 5, 250, 0, 1, 3.0],
|
| 196 |
outputs=[output])
|
| 197 |
radio.change(toggle_audio_src, radio, [melody], queue=False, show_progress=False)
|
| 198 |
gr.Examples(
|
| 199 |
fn=predict_full,
|
| 200 |
examples=[
|
| 201 |
[
|
| 202 |
+
"A Bach inspired orchestral suite.",
|
| 203 |
+
"./assets/twinkle.mp3",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
"melody"
|
| 205 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
],
|
| 207 |
inputs=[text, melody, model],
|
| 208 |
outputs=[output]
|
| 209 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
interface.queue().launch(**launch_kwargs)
|
| 211 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
if __name__ == "__main__":
|
| 213 |
parser = argparse.ArgumentParser()
|
| 214 |
parser.add_argument(
|
|
|
|
| 251 |
launch_kwargs['share'] = args.share
|
| 252 |
|
| 253 |
# Show the interface
|
| 254 |
+
ui_full(launch_kwargs)
|
|
|
|
|
|
|
|
|