Update app.py
Browse files
app.py
CHANGED
|
@@ -234,7 +234,7 @@ def first_note_idx(score_tokens, patch):
|
|
| 234 |
|
| 235 |
@spaces.GPU
|
| 236 |
def Inpaint_MIDI_Template(midi_template_idx,
|
| 237 |
-
|
| 238 |
model_temperature,
|
| 239 |
model_sampling_top_p
|
| 240 |
):
|
|
@@ -242,31 +242,35 @@ def Inpaint_MIDI_Template(midi_template_idx,
|
|
| 242 |
#===============================================================================
|
| 243 |
|
| 244 |
def inpaint(melody_chords,
|
| 245 |
-
inpaint_MIDI_patch=0,
|
| 246 |
number_of_prime_tokens=0,
|
| 247 |
number_of_memory_tokens=4096,
|
| 248 |
temperature=1.0,
|
| 249 |
model_sampling_top_p_value=0.96,
|
| 250 |
verbose=False
|
| 251 |
):
|
|
|
|
|
|
|
| 252 |
|
| 253 |
if verbose:
|
| 254 |
print('=' * 70)
|
| 255 |
print('Giant Music Transformer Inpainting Model Generator')
|
| 256 |
print('=' * 70)
|
|
|
|
|
|
|
| 257 |
|
| 258 |
out2 = []
|
| 259 |
|
| 260 |
for m in melody_chords[:number_of_prime_tokens]:
|
| 261 |
out2.append(m)
|
| 262 |
-
|
| 263 |
-
|
| 264 |
|
| 265 |
for i in tqdm.tqdm(range(number_of_prime_tokens, len(melody_chords))):
|
| 266 |
|
| 267 |
cpatch = (melody_chords[i]-2304) // 129
|
| 268 |
|
| 269 |
-
if 2304 <= melody_chords[i] < 18945 and
|
| 270 |
|
| 271 |
inp = torch.LongTensor(out2[-number_of_memory_tokens:]).cuda()
|
| 272 |
|
|
@@ -294,7 +298,8 @@ def Inpaint_MIDI_Template(midi_template_idx,
|
|
| 294 |
|
| 295 |
else:
|
| 296 |
out2.append(melody_chords[i])
|
| 297 |
-
|
|
|
|
| 298 |
|
| 299 |
song_f = []
|
| 300 |
|
|
@@ -344,6 +349,8 @@ def Inpaint_MIDI_Template(midi_template_idx,
|
|
| 344 |
pitch = (ss-2304) % 129
|
| 345 |
|
| 346 |
song_f.append(['note', time, dur, channel, pitch, vel, patch ])
|
|
|
|
|
|
|
| 347 |
|
| 348 |
if verbose:
|
| 349 |
print('Done!')
|
|
@@ -360,7 +367,7 @@ def Inpaint_MIDI_Template(midi_template_idx,
|
|
| 360 |
print('Requested settings:')
|
| 361 |
print('=' * 70)
|
| 362 |
print('MIDI template idx:', midi_template_idx)
|
| 363 |
-
print('
|
| 364 |
print('Model temperature:', model_temperature)
|
| 365 |
print('Model sampling top p:', model_sampling_top_p)
|
| 366 |
print('=' * 70)
|
|
@@ -383,7 +390,7 @@ def Inpaint_MIDI_Template(midi_template_idx,
|
|
| 383 |
mt_idx = midi_templates.index(midi_template)
|
| 384 |
mt_md5 = midi_template[0]
|
| 385 |
|
| 386 |
-
|
| 387 |
|
| 388 |
print('=' * 70)
|
| 389 |
print('Selected MIDI template idx:', mt_idx)
|
|
@@ -392,84 +399,43 @@ def Inpaint_MIDI_Template(midi_template_idx,
|
|
| 392 |
#==================================================================
|
| 393 |
|
| 394 |
print('=' * 70)
|
| 395 |
-
print('
|
| 396 |
|
| 397 |
mel_pat = 40
|
| 398 |
acc_pat = 24
|
| 399 |
bass_pat = 35
|
| 400 |
-
|
| 401 |
-
mel_score = []
|
| 402 |
-
acc_score = []
|
| 403 |
-
bass_score = []
|
| 404 |
-
drums_score = []
|
| 405 |
-
|
| 406 |
-
fnote = False
|
| 407 |
-
|
| 408 |
-
for e in events_matrix:
|
| 409 |
-
|
| 410 |
-
if e[6] == mel_pat:
|
| 411 |
-
mel_score.append(e)
|
| 412 |
-
fnote = True
|
| 413 |
-
|
| 414 |
-
elif e[6] == acc_pat:
|
| 415 |
-
acc_score.append(e)
|
| 416 |
-
|
| 417 |
-
elif e[6] == bass_pat:
|
| 418 |
-
bass_score.append(e)
|
| 419 |
-
|
| 420 |
-
elif e[6] == 128:
|
| 421 |
-
drums_score.append(e)
|
| 422 |
-
|
| 423 |
-
if not fnote:
|
| 424 |
-
mel_score.append(e)
|
| 425 |
-
|
| 426 |
-
print('=' * 70)
|
| 427 |
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
print('Inpainting melody...')
|
| 431 |
|
| 432 |
-
|
| 433 |
-
|
| 434 |
|
| 435 |
-
|
| 436 |
-
|
| 437 |
|
| 438 |
-
|
|
|
|
| 439 |
|
| 440 |
-
if
|
|
|
|
| 441 |
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
mel_drums_acc_tokens = score_to_toks(mel_drums_score+acc_score)
|
| 445 |
-
pidx = first_note_idx(mel_drums_acc_tokens, mel_pat)
|
| 446 |
-
mel_drums_acc_score = inpaint(mel_drums_acc_tokens, acc_pat, number_of_prime_tokens=pidx)
|
| 447 |
-
|
| 448 |
-
mel_drums_acc_bass_tokens = score_to_toks(mel_drums_acc_score+bass_score)
|
| 449 |
-
|
| 450 |
-
print('Done!')
|
| 451 |
-
print('=' * 70)
|
| 452 |
-
|
| 453 |
-
else:
|
| 454 |
-
print('Skipping accompaniment inpainting...')
|
| 455 |
-
mel_drums_acc_bass_tokens = score_to_toks(mel_drums_score+bass_score)
|
| 456 |
-
print('=' * 70)
|
| 457 |
|
| 458 |
#==================================================================
|
| 459 |
|
| 460 |
-
print('Inpainting
|
| 461 |
|
| 462 |
-
|
| 463 |
-
|
| 464 |
|
| 465 |
print('Done!')
|
| 466 |
print('=' * 70)
|
| 467 |
-
|
| 468 |
#==================================================================
|
| 469 |
|
| 470 |
print('Patching final score...')
|
| 471 |
|
| 472 |
-
output_score, patches, overflow_patches = TMIDIX.patch_enhanced_score_notes(
|
| 473 |
|
| 474 |
print('Done!')
|
| 475 |
print('=' * 70)
|
|
@@ -560,7 +526,7 @@ with gr.Blocks() as demo:
|
|
| 560 |
gr.Markdown("## MIDI Templates options")
|
| 561 |
|
| 562 |
midi_template_idx = gr.Slider(-1, 16383, value=-1, label="Desired MIDI template number")
|
| 563 |
-
|
| 564 |
|
| 565 |
gr.Markdown("## Model options")
|
| 566 |
|
|
@@ -577,7 +543,7 @@ with gr.Blocks() as demo:
|
|
| 577 |
|
| 578 |
generate_btn.click(Inpaint_MIDI_Template,
|
| 579 |
[midi_template_idx,
|
| 580 |
-
|
| 581 |
model_temperature,
|
| 582 |
model_sampling_top_p
|
| 583 |
],
|
|
|
|
| 234 |
|
| 235 |
@spaces.GPU
|
| 236 |
def Inpaint_MIDI_Template(midi_template_idx,
|
| 237 |
+
inpainting_mode,
|
| 238 |
model_temperature,
|
| 239 |
model_sampling_top_p
|
| 240 |
):
|
|
|
|
| 242 |
#===============================================================================
|
| 243 |
|
| 244 |
def inpaint(melody_chords,
|
| 245 |
+
inpaint_MIDI_patch=[0],
|
| 246 |
number_of_prime_tokens=0,
|
| 247 |
number_of_memory_tokens=4096,
|
| 248 |
temperature=1.0,
|
| 249 |
model_sampling_top_p_value=0.96,
|
| 250 |
verbose=False
|
| 251 |
):
|
| 252 |
+
|
| 253 |
+
#=====================================================================
|
| 254 |
|
| 255 |
if verbose:
|
| 256 |
print('=' * 70)
|
| 257 |
print('Giant Music Transformer Inpainting Model Generator')
|
| 258 |
print('=' * 70)
|
| 259 |
+
|
| 260 |
+
#=====================================================================
|
| 261 |
|
| 262 |
out2 = []
|
| 263 |
|
| 264 |
for m in melody_chords[:number_of_prime_tokens]:
|
| 265 |
out2.append(m)
|
| 266 |
+
|
| 267 |
+
#=====================================================================
|
| 268 |
|
| 269 |
for i in tqdm.tqdm(range(number_of_prime_tokens, len(melody_chords))):
|
| 270 |
|
| 271 |
cpatch = (melody_chords[i]-2304) // 129
|
| 272 |
|
| 273 |
+
if 2304 <= melody_chords[i] < 18945 and cpatch in inpaint_MIDI_patch:
|
| 274 |
|
| 275 |
inp = torch.LongTensor(out2[-number_of_memory_tokens:]).cuda()
|
| 276 |
|
|
|
|
| 298 |
|
| 299 |
else:
|
| 300 |
out2.append(melody_chords[i])
|
| 301 |
+
|
| 302 |
+
#=====================================================================
|
| 303 |
|
| 304 |
song_f = []
|
| 305 |
|
|
|
|
| 349 |
pitch = (ss-2304) % 129
|
| 350 |
|
| 351 |
song_f.append(['note', time, dur, channel, pitch, vel, patch ])
|
| 352 |
+
|
| 353 |
+
#=====================================================================
|
| 354 |
|
| 355 |
if verbose:
|
| 356 |
print('Done!')
|
|
|
|
| 367 |
print('Requested settings:')
|
| 368 |
print('=' * 70)
|
| 369 |
print('MIDI template idx:', midi_template_idx)
|
| 370 |
+
print('Parts to inpaint:', inpainting_mode)
|
| 371 |
print('Model temperature:', model_temperature)
|
| 372 |
print('Model sampling top p:', model_sampling_top_p)
|
| 373 |
print('=' * 70)
|
|
|
|
| 390 |
mt_idx = midi_templates.index(midi_template)
|
| 391 |
mt_md5 = midi_template[0]
|
| 392 |
|
| 393 |
+
inp_score = toks_to_score(midi_template[1])[:MAX_NOTES_TO_INPAINT]
|
| 394 |
|
| 395 |
print('=' * 70)
|
| 396 |
print('Selected MIDI template idx:', mt_idx)
|
|
|
|
| 399 |
#==================================================================
|
| 400 |
|
| 401 |
print('=' * 70)
|
| 402 |
+
print('Prepping patches...')
|
| 403 |
|
| 404 |
mel_pat = 40
|
| 405 |
acc_pat = 24
|
| 406 |
bass_pat = 35
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 407 |
|
| 408 |
+
ipatches = []
|
|
|
|
|
|
|
| 409 |
|
| 410 |
+
if 'Melody' in inpainting_mode:
|
| 411 |
+
ipatches.append(mel_pat)
|
| 412 |
|
| 413 |
+
if 'Accompaniment' in inpainting_mode:
|
| 414 |
+
ipatches.append(acc_pat)
|
| 415 |
|
| 416 |
+
if 'Base' in inpainting_mode:
|
| 417 |
+
ipatches.append(bass_pat)
|
| 418 |
|
| 419 |
+
if not inpainting_mode:
|
| 420 |
+
ipatches.append(mel_pat)
|
| 421 |
|
| 422 |
+
print('=' * 70)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 423 |
|
| 424 |
#==================================================================
|
| 425 |
|
| 426 |
+
print('Inpainting...')
|
| 427 |
|
| 428 |
+
inp_score_tokens = score_to_toks(inp_score)
|
| 429 |
+
out_score = inpaint(inp_score_tokens, ipatches, 300)
|
| 430 |
|
| 431 |
print('Done!')
|
| 432 |
print('=' * 70)
|
| 433 |
+
|
| 434 |
#==================================================================
|
| 435 |
|
| 436 |
print('Patching final score...')
|
| 437 |
|
| 438 |
+
output_score, patches, overflow_patches = TMIDIX.patch_enhanced_score_notes(out_score)
|
| 439 |
|
| 440 |
print('Done!')
|
| 441 |
print('=' * 70)
|
|
|
|
| 526 |
gr.Markdown("## MIDI Templates options")
|
| 527 |
|
| 528 |
midi_template_idx = gr.Slider(-1, 16383, value=-1, label="Desired MIDI template number")
|
| 529 |
+
inpainting_mode = gr.CheckboxGroup(['Melody', 'Accompaniment', 'Base'], value=['Melody'], label="Which MIDI template parts to inpaint")
|
| 530 |
|
| 531 |
gr.Markdown("## Model options")
|
| 532 |
|
|
|
|
| 543 |
|
| 544 |
generate_btn.click(Inpaint_MIDI_Template,
|
| 545 |
[midi_template_idx,
|
| 546 |
+
inpainting_mode,
|
| 547 |
model_temperature,
|
| 548 |
model_sampling_top_p
|
| 549 |
],
|