Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -198,185 +198,141 @@ def save_midi(tokens, batch_number=None, model_selector=''):
|
|
| 198 |
#==================================================================================
|
| 199 |
|
| 200 |
@spaces.GPU
|
| 201 |
-
def
|
| 202 |
num_gen_tokens,
|
| 203 |
model_temperature
|
| 204 |
):
|
| 205 |
|
| 206 |
-
|
| 207 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
-
|
| 210 |
-
|
| 211 |
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
print('Generating...')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
|
| 220 |
-
|
|
|
|
|
|
|
| 221 |
|
| 222 |
-
|
| 223 |
|
| 224 |
-
|
| 225 |
-
out = model.generate(inp,
|
| 226 |
-
num_gen_tokens,
|
| 227 |
-
#filter_logits_fn=top_p,
|
| 228 |
-
#filter_kwargs={'thres': model_sampling_top_p},
|
| 229 |
-
temperature=model_temperature,
|
| 230 |
-
return_prime=False,
|
| 231 |
-
verbose=False)
|
| 232 |
|
| 233 |
-
output = out.tolist()
|
| 234 |
-
|
| 235 |
-
print('Done!')
|
| 236 |
print('=' * 70)
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
#==================================================================================
|
| 241 |
|
| 242 |
-
|
| 243 |
-
num_prime_tokens,
|
| 244 |
-
num_gen_tokens,
|
| 245 |
-
num_mem_tokens,
|
| 246 |
-
model_temperature,
|
| 247 |
-
# model_sampling_top_p,
|
| 248 |
-
final_composition,
|
| 249 |
-
generated_batches,
|
| 250 |
-
block_lines,
|
| 251 |
-
model_state
|
| 252 |
-
):
|
| 253 |
-
|
| 254 |
-
generated_batches = []
|
| 255 |
-
|
| 256 |
-
if not final_composition and input_midi is not None:
|
| 257 |
-
final_composition = load_midi(input_midi, model_selector=model_state[2])[:num_prime_tokens]
|
| 258 |
-
midi_score = save_midi(final_composition, model_selector=model_state[2])
|
| 259 |
-
block_lines.append(midi_score[-1][1] / 1000)
|
| 260 |
-
|
| 261 |
-
batched_gen_tokens = generate_music(final_composition,
|
| 262 |
-
num_gen_tokens,
|
| 263 |
-
num_mem_tokens,
|
| 264 |
-
NUM_OUT_BATCHES,
|
| 265 |
-
model_temperature,
|
| 266 |
-
# model_sampling_top_p,
|
| 267 |
-
model_state
|
| 268 |
-
)
|
| 269 |
|
| 270 |
-
|
| 271 |
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
|
|
|
|
|
|
|
|
|
| 275 |
|
| 276 |
-
|
| 277 |
-
tokens_preview = final_composition[-PREVIEW_LENGTH:]
|
| 278 |
|
| 279 |
-
|
| 280 |
-
midi_score = save_midi(tokens_preview + tokens, i, model_selector=model_state[2])
|
| 281 |
-
|
| 282 |
-
# MIDI plot
|
| 283 |
-
|
| 284 |
-
if len(final_composition) > PREVIEW_LENGTH:
|
| 285 |
-
midi_plot = TMIDIX.plot_ms_SONG(midi_score,
|
| 286 |
-
plot_title='Batch # ' + str(i),
|
| 287 |
-
preview_length_in_notes=int(PREVIEW_LENGTH / 3),
|
| 288 |
-
return_plt=True
|
| 289 |
-
)
|
| 290 |
-
|
| 291 |
-
else:
|
| 292 |
-
midi_plot = TMIDIX.plot_ms_SONG(midi_score,
|
| 293 |
-
plot_title='Batch # ' + str(i),
|
| 294 |
-
return_plt=True
|
| 295 |
-
)
|
| 296 |
-
|
| 297 |
-
# File name
|
| 298 |
-
fname = 'Guided-Accompaniment-Transformer-Music-Composition_'+str(i)
|
| 299 |
|
| 300 |
-
|
| 301 |
-
midi_audio = midi_to_colab_audio(fname + '.mid',
|
| 302 |
-
soundfont_path=SOUDFONT_PATH,
|
| 303 |
-
sample_rate=16000,
|
| 304 |
-
output_for_gradio=True
|
| 305 |
-
)
|
| 306 |
-
|
| 307 |
-
outputs.append([(16000, midi_audio), midi_plot, tokens])
|
| 308 |
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
):
|
| 325 |
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 329 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 330 |
print('=' * 70)
|
| 331 |
-
if input_midi is not None:
|
| 332 |
-
fn = os.path.basename(input_midi.name)
|
| 333 |
-
fn1 = fn.split('.')[0]
|
| 334 |
-
print('Input file name:', fn)
|
| 335 |
|
| 336 |
-
|
| 337 |
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
else:
|
| 343 |
-
if model_selector != model_state[2]:
|
| 344 |
-
print('=' * 70)
|
| 345 |
-
print('Switching model...')
|
| 346 |
-
model_state = load_model(model_selector)
|
| 347 |
-
model_state.append(model_selector)
|
| 348 |
-
print('=' * 70)
|
| 349 |
-
|
| 350 |
-
print('Num prime tokens:', num_prime_tokens)
|
| 351 |
-
print('Num gen tokens:', num_gen_tokens)
|
| 352 |
-
print('Num mem tokens:', num_mem_tokens)
|
| 353 |
-
|
| 354 |
-
print('Model temp:', model_temperature)
|
| 355 |
-
# print('Model top_p:', model_sampling_top_p)
|
| 356 |
-
print('=' * 70)
|
| 357 |
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
final_composition,
|
| 365 |
-
generated_batches,
|
| 366 |
-
block_lines,
|
| 367 |
-
model_state
|
| 368 |
-
)
|
| 369 |
|
| 370 |
-
generated_batches = [sublist[-1] for sublist in result[0]]
|
| 371 |
|
| 372 |
-
|
|
|
|
|
|
|
| 373 |
print('Req end time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
|
| 374 |
-
print('
|
| 375 |
print('Req execution time:', (reqtime.time() - start_time), 'sec')
|
| 376 |
-
print('*' * 70)
|
| 377 |
-
|
| 378 |
-
return tuple([result[1], generated_batches, result[3]] + [item for sublist in result[0] for item in sublist[:-1]] + [model_state])
|
| 379 |
|
|
|
|
|
|
|
| 380 |
#==================================================================================
|
| 381 |
|
| 382 |
PDT = timezone('US/Pacific')
|
|
@@ -391,10 +347,6 @@ with gr.Blocks() as demo:
|
|
| 391 |
|
| 392 |
#==================================================================================
|
| 393 |
|
| 394 |
-
demo.load(reset_demo)
|
| 395 |
-
|
| 396 |
-
#==================================================================================
|
| 397 |
-
|
| 398 |
gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Guided Accompaniment Transformer</h1>")
|
| 399 |
gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Guided melody accompaniment generation with transformers</h1>")
|
| 400 |
gr.HTML("""
|
|
@@ -435,7 +387,7 @@ with gr.Blocks() as demo:
|
|
| 435 |
|
| 436 |
outputs.extend([model_state])
|
| 437 |
|
| 438 |
-
generate_btn.click(
|
| 439 |
[input_midi,
|
| 440 |
num_gen_tokens,
|
| 441 |
model_temperature
|
|
@@ -446,11 +398,27 @@ with gr.Blocks() as demo:
|
|
| 446 |
output_midi,
|
| 447 |
]
|
| 448 |
)
|
| 449 |
-
|
| 450 |
-
#==================================================================================
|
| 451 |
-
|
| 452 |
-
demo.unload(reset_demo)
|
| 453 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 454 |
#==================================================================================
|
| 455 |
|
| 456 |
demo.launch()
|
|
|
|
| 198 |
#==================================================================================
|
| 199 |
|
| 200 |
@spaces.GPU
|
| 201 |
+
def Generate_Accompaniment(input_midi,
|
| 202 |
num_gen_tokens,
|
| 203 |
model_temperature
|
| 204 |
):
|
| 205 |
|
| 206 |
+
#===============================================================================
|
| 207 |
+
|
| 208 |
+
print('=' * 70)
|
| 209 |
+
print('Req start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
|
| 210 |
+
start_time = reqtime.time()
|
| 211 |
+
print('=' * 70)
|
| 212 |
|
| 213 |
+
fn = os.path.basename(input_midi)
|
| 214 |
+
fn1 = fn.split('.')[0]
|
| 215 |
|
| 216 |
+
print('=' * 70)
|
| 217 |
+
print('Requested settings:')
|
| 218 |
+
print('=' * 70)
|
| 219 |
+
print('Input MIDI file name:', fn)
|
| 220 |
+
print('Input MIDI type:', input_midi_type)
|
| 221 |
+
print('Conversion type:', input_conv_type)
|
| 222 |
+
print('Number of prime notes:', input_number_prime_notes)
|
| 223 |
+
print('Number of notes to convert:', input_number_conv_notes)
|
| 224 |
+
print('Model durations sampling top value:', input_model_dur_top_k)
|
| 225 |
+
print('Model durations temperature:', input_model_dur_temperature)
|
| 226 |
+
print('Model velocities temperature:', input_model_vel_temperature)
|
| 227 |
+
|
| 228 |
+
print('=' * 70)
|
| 229 |
|
| 230 |
+
#==================================================================
|
| 231 |
+
|
| 232 |
+
src_melody_chords_f = load_midi(input_midi.name)
|
| 233 |
+
|
| 234 |
+
#==================================================================
|
| 235 |
+
|
| 236 |
+
print('Sample output events', src_melody_chords_f[0][1][:3])
|
| 237 |
+
print('=' * 70)
|
| 238 |
print('Generating...')
|
| 239 |
+
|
| 240 |
+
model.to(DEVICE)
|
| 241 |
+
model.eval()
|
| 242 |
+
|
| 243 |
+
#==================================================================
|
| 244 |
|
| 245 |
+
print('=' * 70)
|
| 246 |
+
print('Done!')
|
| 247 |
+
print('=' * 70)
|
| 248 |
|
| 249 |
+
#===============================================================================
|
| 250 |
|
| 251 |
+
print('Rendering results...')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
|
|
|
|
|
|
|
|
|
|
| 253 |
print('=' * 70)
|
| 254 |
+
print('Sample INTs', final_song[:15])
|
| 255 |
+
print('=' * 70)
|
|
|
|
|
|
|
| 256 |
|
| 257 |
+
song_f = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
|
| 259 |
+
if len(final_song) != 0:
|
| 260 |
|
| 261 |
+
time = 0
|
| 262 |
+
dur = 0
|
| 263 |
+
vel = 90
|
| 264 |
+
pitch = 60
|
| 265 |
+
channel = 0
|
| 266 |
+
patch = 0
|
| 267 |
|
| 268 |
+
patches = [0] * 16
|
|
|
|
| 269 |
|
| 270 |
+
for ss in final_song:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
|
| 272 |
+
if 0 <= ss < 256:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
|
| 274 |
+
time += ss * 16
|
| 275 |
+
|
| 276 |
+
if 256 <= ss < 384:
|
| 277 |
+
|
| 278 |
+
pitch = ss-256
|
| 279 |
+
|
| 280 |
+
if 384 <= ss < 640:
|
| 281 |
+
|
| 282 |
+
dur = (ss-384) * 16
|
| 283 |
+
|
| 284 |
+
if 640 <= ss < 768:
|
| 285 |
+
|
| 286 |
+
vel = (ss-640)
|
| 287 |
+
|
| 288 |
+
song_f.append(['note', time, dur, channel, pitch, vel, patch])
|
|
|
|
| 289 |
|
| 290 |
+
fn1 = "Score-2-Performance-Transformer-Composition"
|
| 291 |
+
|
| 292 |
+
detailed_stats = TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(song_f,
|
| 293 |
+
output_signature = 'Score 2 Performance Transformer',
|
| 294 |
+
output_file_name = fn1,
|
| 295 |
+
track_name='Project Los Angeles',
|
| 296 |
+
list_of_MIDI_patches=patches
|
| 297 |
+
)
|
| 298 |
|
| 299 |
+
new_fn = fn1+'.mid'
|
| 300 |
+
|
| 301 |
+
|
| 302 |
+
audio = midi_to_colab_audio(new_fn,
|
| 303 |
+
soundfont_path=soundfont,
|
| 304 |
+
sample_rate=16000,
|
| 305 |
+
volume_scale=10,
|
| 306 |
+
output_for_gradio=True
|
| 307 |
+
)
|
| 308 |
+
|
| 309 |
+
print('Done!')
|
| 310 |
print('=' * 70)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
|
| 312 |
+
#========================================================
|
| 313 |
|
| 314 |
+
output_midi_title = str(fn1)
|
| 315 |
+
output_midi_summary = str(song_f[:3])
|
| 316 |
+
output_midi = str(new_fn)
|
| 317 |
+
output_audio = (16000, audio)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 318 |
|
| 319 |
+
output_plot = TMIDIX.plot_ms_SONG(song_f, plot_title=output_midi, return_plt=True)
|
| 320 |
+
|
| 321 |
+
print('Output MIDI file name:', output_midi)
|
| 322 |
+
print('Output MIDI title:', output_midi_title)
|
| 323 |
+
print('Output MIDI summary:', output_midi_summary)
|
| 324 |
+
print('=' * 70)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
|
|
|
|
| 326 |
|
| 327 |
+
#========================================================
|
| 328 |
+
|
| 329 |
+
print('-' * 70)
|
| 330 |
print('Req end time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
|
| 331 |
+
print('-' * 70)
|
| 332 |
print('Req execution time:', (reqtime.time() - start_time), 'sec')
|
|
|
|
|
|
|
|
|
|
| 333 |
|
| 334 |
+
return output_midi, output_audio, output_plot
|
| 335 |
+
|
| 336 |
#==================================================================================
|
| 337 |
|
| 338 |
PDT = timezone('US/Pacific')
|
|
|
|
| 347 |
|
| 348 |
#==================================================================================
|
| 349 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 350 |
gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Guided Accompaniment Transformer</h1>")
|
| 351 |
gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Guided melody accompaniment generation with transformers</h1>")
|
| 352 |
gr.HTML("""
|
|
|
|
| 387 |
|
| 388 |
outputs.extend([model_state])
|
| 389 |
|
| 390 |
+
generate_btn.click(Generate_Accompaniment,
|
| 391 |
[input_midi,
|
| 392 |
num_gen_tokens,
|
| 393 |
model_temperature
|
|
|
|
| 398 |
output_midi,
|
| 399 |
]
|
| 400 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 401 |
|
| 402 |
+
'''gr.Examples(
|
| 403 |
+
[["asap_midi_score_21.mid", "Score", "Durations and Velocities", 8, 600, 1, 1.1, 1.5],
|
| 404 |
+
["asap_midi_score_45.mid", "Score", "Durations and Velocities", 8, 600, 1, 1.1, 1.5],
|
| 405 |
+
["asap_midi_score_69.mid", "Score", "Durations and Velocities", 8, 600, 1, 1.1, 1.5],
|
| 406 |
+
["asap_midi_score_118.mid", "Score", "Durations and Velocities", 8, 600, 1, 1.1, 1.5],
|
| 407 |
+
["asap_midi_score_167.mid", "Score", "Durations and Velocities", 8, 600, 1, 1.1, 1.5],
|
| 408 |
+
],
|
| 409 |
+
[input_midi,
|
| 410 |
+
input_midi_type,
|
| 411 |
+
input_conv_type,
|
| 412 |
+
input_number_prime_notes,
|
| 413 |
+
input_number_conv_notes,
|
| 414 |
+
input_model_dur_top_k,
|
| 415 |
+
input_model_dur_temperature,
|
| 416 |
+
input_model_vel_temperature
|
| 417 |
+
],
|
| 418 |
+
[output_midi_title, output_midi_summary, output_midi, output_audio, output_plot],
|
| 419 |
+
Convert_Score_to_Performance
|
| 420 |
+
)'''
|
| 421 |
+
|
| 422 |
#==================================================================================
|
| 423 |
|
| 424 |
demo.launch()
|