asigalov61 commited on
Commit
98cb603
·
verified ·
1 Parent(s): b378f5e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +125 -157
app.py CHANGED
@@ -198,185 +198,141 @@ def save_midi(tokens, batch_number=None, model_selector=''):
198
  #==================================================================================
199
 
200
  @spaces.GPU
201
- def generate_accompaniment(input_midi,
202
  num_gen_tokens,
203
  model_temperature
204
  ):
205
 
206
- if not prime:
207
- inputs = [0]
 
 
 
 
208
 
209
- else:
210
- inputs = prime[-num_mem_tokens:]
211
 
212
- model = model_state[0]
213
- ctx = model_state[1]
214
-
215
- model.cuda()
216
- model.eval()
 
 
 
 
 
 
 
 
217
 
 
 
 
 
 
 
 
 
218
  print('Generating...')
 
 
 
 
 
219
 
220
- inp = [inputs] * num_gen_batches
 
 
221
 
222
- inp = torch.LongTensor(inp).cuda()
223
 
224
- with ctx:
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
- return output
239
-
240
- #==================================================================================
241
 
242
- def generate_callback(input_midi,
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
- outputs = []
271
 
272
- for i in range(len(batched_gen_tokens)):
273
-
274
- tokens = batched_gen_tokens[i]
 
 
 
275
 
276
- # Preview
277
- tokens_preview = final_composition[-PREVIEW_LENGTH:]
278
 
279
- # Save MIDI to a temporary file
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
- # Save audio to a temporary file
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
- return outputs, final_composition, generated_batches, block_lines
310
-
311
- #==================================================================================
312
-
313
- def generate_callback_wrapper(input_midi,
314
- num_prime_tokens,
315
- num_gen_tokens,
316
- num_mem_tokens,
317
- model_temperature,
318
- # model_sampling_top_p,
319
- final_composition,
320
- generated_batches,
321
- block_lines,
322
- model_selector,
323
- model_state
324
- ):
325
 
326
- print('=' * 70)
327
- print('Req start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
328
- start_time = reqtime.time()
 
 
 
 
 
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
- print('Selected model type:', model_selector)
337
 
338
- if not model_state:
339
- model_state = load_model(model_selector)
340
- model_state.append(model_selector)
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
- result = generate_callback(input_midi,
359
- num_prime_tokens,
360
- num_gen_tokens,
361
- num_mem_tokens,
362
- model_temperature,
363
- # model_sampling_top_p,
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
- print('=' * 70)
 
 
373
  print('Req end time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
374
- print('=' * 70)
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(generate_accompaniment,
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()