projectlosangeles commited on
Commit
a609809
·
verified ·
1 Parent(s): 35840fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +213 -186
app.py CHANGED
@@ -71,7 +71,7 @@ dtype = 'bfloat16'
71
  ptdtype = {'bfloat16': torch.bfloat16, 'float16': torch.float16}[dtype]
72
  ctx = torch.amp.autocast(device_type=device_type, dtype=ptdtype)
73
 
74
- SEQ_LEN = 8192
75
  PAD_IDX = 18819
76
 
77
  model = TransformerWrapper(num_tokens = PAD_IDX+1,
@@ -110,56 +110,79 @@ def load_midi(input_midi):
110
 
111
  raw_score = TMIDIX.midi2single_track_ms_score(input_midi)
112
 
113
- escore_notes = TMIDIX.advanced_score_processor(raw_score, return_enhanced_score_notes=True)[0]
114
-
115
- sp_escore_notes = TMIDIX.solo_piano_escore_notes(escore_notes)
116
- zscore = TMIDIX.recalculate_score_timings(sp_escore_notes)
117
-
118
- escore = TMIDIX.augment_enhanced_score_notes(zscore, timings_divider=32)
119
-
120
- escore = TMIDIX.fix_escore_notes_durations(escore)
121
-
122
- cscore = TMIDIX.chordify_score([1000, escore])
123
-
124
- score = []
125
- chords = []
126
-
127
- pc = cscore[0]
128
-
129
- for c in cscore:
130
-
131
- tones_chord = sorted(set([e[4] % 12 for e in c]))
132
-
133
- if tones_chord not in TMIDIX.ALL_CHORDS_SORTED:
134
- tones_chord = TMIDIX.check_and_fix_tones_chord(tones_chord, use_full_chords=False)
135
-
136
- chord_tok = TMIDIX.ALL_CHORDS_SORTED.index(tones_chord)
137
- chords.append(chord_tok+384)
138
-
139
- score.append(chord_tok+384)
140
- score.append(max(0, min(127, c[0][1]-pc[0][1])))
141
-
142
- for n in c:
143
- score.extend([max(1, min(127, n[2]))+128, max(1, min(127, n[4]))+256])
144
-
145
- pc = c
146
-
147
- print('Done!')
148
- print('=' * 70)
149
- print('Score has', len(chords), 'chords')
150
- print('Score hss', len(score), 'tokens')
151
- print('=' * 70)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
 
153
- return score, chords
 
154
 
155
  #==================================================================================
156
 
157
  @spaces.GPU
158
- def Generate_Chords_Textures(input_midi,
159
- prime_with_first_note,
160
- model_temperature,
161
- model_sampling_top_p
162
- ):
163
 
164
  #===============================================================================
165
 
@@ -174,7 +197,6 @@ def Generate_Chords_Textures(input_midi,
174
  fn = os.path.basename(input_midi)
175
  fn1 = fn.split('.')[0]
176
  print('Input MIDI file name:', fn)
177
- print('Prime with first note:', prime_with_first_note)
178
  print('Model temperature:', model_temperature)
179
  print('Model top p:', model_sampling_top_p)
180
 
@@ -182,145 +204,153 @@ def Generate_Chords_Textures(input_midi,
182
 
183
  #==================================================================
184
 
185
- print('Loading MIDI...')
186
-
187
- score, chords = load_midi(input_midi.name)
188
-
189
- print('Sample score chords', chords[:10])
190
- print('Sample score tokens', score[:10])
191
-
192
- #==================================================================
193
-
194
- chords_len = 128
195
-
196
- if len(chords) < 128:
197
- chords_len = len(chords)
198
- chords = chords * ((128 // chords_len)+1)
199
-
200
- chords = chords[:128]
201
-
202
- #==================================================================
203
-
204
- print('=' * 70)
205
- print('Generating...')
206
-
207
- if prime_with_first_note:
208
- seq = [705] + chords[:128] + [706] + score[:4]
209
-
210
- else:
211
- seq = [705] + chords[:128] + [706]
212
-
213
- x = torch.LongTensor(seq).to(device_type)
214
-
215
- with ctx:
216
- out = model.generate(x,
217
- 1024,
218
- temperature=model_temperature,
219
- filter_logits_fn=top_p,
220
- filter_kwargs={'thres': model_sampling_top_p},
221
- return_prime=True,
222
- eos_token=707,
223
- verbose=False)
224
-
225
- if prime_with_first_note:
226
- final_song = out.tolist()[len(seq)-4:]
227
-
228
- else:
229
- final_song = out.tolist()[len(seq):]
230
-
231
- #==================================================================
232
-
233
- ccount = -1
234
-
235
- for i, f in enumerate(final_song):
236
-
237
- if f > 384:
238
- ccount += 1
239
 
240
- if ccount == chords_len:
241
- break
 
 
 
 
 
 
 
 
242
 
243
- final_song = final_song[:i]
244
-
245
- #==================================================================
246
-
247
- print('=' * 70)
248
- print('Done!')
249
- print('=' * 70)
250
-
251
- #===============================================================================
252
-
253
- print('Rendering results...')
254
-
255
- print('=' * 70)
256
- print('Sample INTs', final_song[:15])
257
- print('=' * 70)
258
-
259
- song_f = []
260
-
261
- if len(final_song) != 0:
262
-
263
- time = 0
264
- dur = 1
265
- vel = 90
266
- pitch = 60
267
- channel = 0
268
- patch = 0
269
-
270
- patches = [0] * 16
271
-
272
- for m in final_song:
273
-
274
- if 0 <= m < 128:
275
- time += m * 32
276
-
277
- elif 128 < m < 256:
278
- dur = (m-128) * 32
279
-
280
- elif 256 < m < 384:
281
- pitch = (m-256)
282
-
283
- song_f.append(['note', time, dur, 0, pitch, max(40, pitch), 0])
284
-
285
- fn1 = "Orpheus-Bridge-Music-Transformer-Composition"
286
-
287
- detailed_stats = TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(song_f,
288
- output_signature = 'Orpheus Bridge Music Transformer',
289
- output_file_name = fn1,
290
- track_name='Project Los Angeles',
291
- list_of_MIDI_patches=patches
292
- )
293
-
294
- new_fn = fn1+'.mid'
295
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
296
 
297
- audio = midi_to_colab_audio(new_fn,
298
- soundfont_path=SOUDFONT_PATH,
299
- sample_rate=16000,
300
- volume_scale=10,
301
- output_for_gradio=True
302
- )
303
-
304
- print('Done!')
305
- print('=' * 70)
306
-
307
- #========================================================
308
-
309
- output_midi = str(new_fn)
310
- output_audio = (16000, audio)
311
- output_plot = TMIDIX.plot_ms_SONG(song_f, plot_title=output_midi, return_plt=True)
312
-
313
- print('Output MIDI file name:', output_midi)
314
- print('=' * 70)
315
 
316
- #========================================================
 
 
 
317
 
318
- print('-' * 70)
319
- print('Req end time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
320
- print('-' * 70)
321
- print('Req execution time:', (reqtime.time() - start_time), 'sec')
322
 
323
- return output_audio, output_plot, output_midi
 
324
 
325
  #==================================================================================
326
 
@@ -351,7 +381,7 @@ with gr.Blocks() as demo:
351
  #==================================================================================
352
 
353
  gr.Markdown("## Upload source MIDI or select a sample MIDI on the bottom of the page")
354
- gr.Markdown("### PLEASE NOTE: The demo is limited and will only texture first 128 chords of the MIDI file")
355
 
356
  input_midi = gr.File(label="Input MIDI",
357
  file_types=[".midi", ".mid", ".kar"]
@@ -359,7 +389,6 @@ with gr.Blocks() as demo:
359
 
360
  gr.Markdown("## Generation options")
361
 
362
- prime_with_first_note = gr.Checkbox(value=True, label="Use the first note onset, duration and pitch to prime the model")
363
  model_temperature = gr.Slider(0.1, 1, value=0.9, step=0.01, label="Model temperature")
364
  model_sampling_top_p = gr.Slider(0.1, 0.99, value=0.96, step=0.01, label="Model sampling top p value")
365
 
@@ -372,9 +401,8 @@ with gr.Blocks() as demo:
372
  output_plot = gr.Plot(label="MIDI score plot")
373
  output_midi = gr.File(label="MIDI file", file_types=[".mid"])
374
 
375
- generate_btn.click(Generate_Chords_Textures,
376
  [input_midi,
377
- prime_with_first_note,
378
  model_temperature,
379
  model_sampling_top_p
380
  ],
@@ -385,10 +413,9 @@ with gr.Blocks() as demo:
385
  )
386
 
387
  gr.Examples(
388
- [["Sharing The Night Together.kar", True, 0.9, 0.96]
389
  ],
390
  [input_midi,
391
- prime_with_first_note,
392
  model_temperature,
393
  model_sampling_top_p
394
  ],
@@ -396,7 +423,7 @@ with gr.Blocks() as demo:
396
  output_plot,
397
  output_midi
398
  ],
399
- Generate_Chords_Textures
400
  )
401
 
402
  #==================================================================================
 
71
  ptdtype = {'bfloat16': torch.bfloat16, 'float16': torch.float16}[dtype]
72
  ctx = torch.amp.autocast(device_type=device_type, dtype=ptdtype)
73
 
74
+ SEQ_LEN = 1668
75
  PAD_IDX = 18819
76
 
77
  model = TransformerWrapper(num_tokens = PAD_IDX+1,
 
110
 
111
  raw_score = TMIDIX.midi2single_track_ms_score(input_midi)
112
 
113
+ escore_notes = TMIDIX.advanced_score_processor(raw_score, return_enhanced_score_notes=True, apply_sustain=True)
114
+
115
+ if escore_notes:
116
+
117
+ escore_notes = TMIDIX.augment_enhanced_score_notes(escore_notes[0], sort_drums_last=True)
118
+
119
+ dscore = TMIDIX.delta_score_notes(escore_notes)
120
+
121
+ dcscore = TMIDIX.chordify_score([d[1:] for d in dscore])
122
+
123
+ melody_chords = [18816]
124
+
125
+ #=======================================================
126
+ # MAIN PROCESSING CYCLE
127
+ #=======================================================
128
+
129
+ for i, c in enumerate(dcscore):
130
+
131
+ delta_time = c[0][0]
132
+
133
+ melody_chords.append(delta_time)
134
+
135
+ for e in c:
136
+
137
+ #=======================================================
138
+
139
+ # Durations
140
+ dur = max(1, min(255, e[1]))
141
+
142
+ # Patches
143
+ pat = max(0, min(128, e[5]))
144
+
145
+ # Pitches
146
+ ptc = max(1, min(127, e[3]))
147
+
148
+ # Velocities
149
+ # Calculating octo-velocity
150
+
151
+ vel = max(8, min(127, e[4]))
152
+ velocity = round(vel / 15)-1
153
+
154
+ #=======================================================
155
+ # FINAL NOTE SEQ
156
+ #=======================================================
157
+
158
+ # Writing final note
159
+ pat_ptc = (128 * pat) + ptc
160
+ dur_vel = (8 * dur) + velocity
161
+
162
+ melody_chords.extend([pat_ptc+256, dur_vel+16768]) # 18816
163
+
164
+
165
+ print('Done!')
166
+ print('=' * 70)
167
+ print('Score hss', len(melody_chords), 'tokens')
168
+ print('=' * 70)
169
+
170
+ if len(melody_chords) > SEQ_LEN:
171
+ return melody_chords
172
+
173
+ else:
174
+ return None
175
 
176
+ else:
177
+ return None
178
 
179
  #==================================================================================
180
 
181
  @spaces.GPU
182
+ def Generate_Music_Bridge(input_midi,
183
+ model_temperature,
184
+ model_sampling_top_p
185
+ ):
 
186
 
187
  #===============================================================================
188
 
 
197
  fn = os.path.basename(input_midi)
198
  fn1 = fn.split('.')[0]
199
  print('Input MIDI file name:', fn)
 
200
  print('Model temperature:', model_temperature)
201
  print('Model top p:', model_sampling_top_p)
202
 
 
204
 
205
  #==================================================================
206
 
207
+ if input_midi is not None:
208
+
209
+ print('Loading MIDI...')
210
+
211
+ score = load_midi(input_midi.name)
212
+
213
+ if score is not None:
214
+ print('Sample score tokens', score[:10])
215
+
216
+ #==================================================================
217
+
218
+ chords_len = 128
219
+
220
+ if len(chords) < 128:
221
+ chords_len = len(chords)
222
+ chords = chords * ((128 // chords_len)+1)
223
+
224
+ chords = chords[:128]
225
+
226
+ #==================================================================
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
227
 
228
+ print('=' * 70)
229
+ print('Generating...')
230
+
231
+ if prime_with_first_note:
232
+ seq = [705] + chords[:128] + [706] + score[:4]
233
+
234
+ else:
235
+ seq = [705] + chords[:128] + [706]
236
+
237
+ x = torch.LongTensor(seq).to(device_type)
238
 
239
+ with ctx:
240
+ out = model.generate(x,
241
+ 1024,
242
+ temperature=model_temperature,
243
+ filter_logits_fn=top_p,
244
+ filter_kwargs={'thres': model_sampling_top_p},
245
+ return_prime=True,
246
+ eos_token=707,
247
+ verbose=False)
248
+
249
+ if prime_with_first_note:
250
+ final_song = out.tolist()[len(seq)-4:]
251
+
252
+ else:
253
+ final_song = out.tolist()[len(seq):]
254
+
255
+ #==================================================================
256
+
257
+ ccount = -1
258
+
259
+ for i, f in enumerate(final_song):
260
+
261
+ if f > 384:
262
+ ccount += 1
263
+
264
+ if ccount == chords_len:
265
+ break
266
+
267
+ final_song = final_song[:i]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
268
 
269
+ #==================================================================
270
+
271
+ print('=' * 70)
272
+ print('Done!')
273
+ print('=' * 70)
274
+
275
+ #===============================================================================
276
+
277
+ print('Rendering results...')
278
+
279
+ print('=' * 70)
280
+ print('Sample INTs', final_song[:15])
281
+ print('=' * 70)
282
+
283
+ song_f = []
284
+
285
+ if len(final_song) != 0:
286
+
287
+ time = 0
288
+ dur = 1
289
+ vel = 90
290
+ pitch = 60
291
+ channel = 0
292
+ patch = 0
293
+
294
+ patches = [0] * 16
295
+
296
+ for m in final_song:
297
+
298
+ if 0 <= m < 128:
299
+ time += m * 32
300
+
301
+ elif 128 < m < 256:
302
+ dur = (m-128) * 32
303
+
304
+ elif 256 < m < 384:
305
+ pitch = (m-256)
306
+
307
+ song_f.append(['note', time, dur, 0, pitch, max(40, pitch), 0])
308
+
309
+ fn1 = "Orpheus-Bridge-Music-Transformer-Composition"
310
+
311
+ detailed_stats = TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(song_f,
312
+ output_signature = 'Orpheus Bridge Music Transformer',
313
+ output_file_name = fn1,
314
+ track_name='Project Los Angeles',
315
+ list_of_MIDI_patches=patches
316
+ )
317
+
318
+ new_fn = fn1+'.mid'
319
+
320
+
321
+ audio = midi_to_colab_audio(new_fn,
322
+ soundfont_path=SOUDFONT_PATH,
323
+ sample_rate=16000,
324
+ volume_scale=10,
325
+ output_for_gradio=True
326
+ )
327
+
328
+ print('Done!')
329
+ print('=' * 70)
330
+
331
+ #========================================================
332
+
333
+ output_midi = str(new_fn)
334
+ output_audio = (16000, audio)
335
+ output_plot = TMIDIX.plot_ms_SONG(song_f, plot_title=output_midi, return_plt=True)
336
+
337
+ print('Output MIDI file name:', output_midi)
338
+ print('=' * 70)
339
+
340
+ #========================================================
341
 
342
+ else:
343
+ return None, None, None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
344
 
345
+ print('-' * 70)
346
+ print('Req end time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
347
+ print('-' * 70)
348
+ print('Req execution time:', (reqtime.time() - start_time), 'sec')
349
 
350
+ return output_audio, output_plot, output_midi
 
 
 
351
 
352
+ return
353
+ None, None, None
354
 
355
  #==================================================================================
356
 
 
381
  #==================================================================================
382
 
383
  gr.Markdown("## Upload source MIDI or select a sample MIDI on the bottom of the page")
384
+ gr.Markdown("### PLEASE NOTE: The MIDI file MUST HAVE at least 800 MIDI pitches for the demo to work properly!")
385
 
386
  input_midi = gr.File(label="Input MIDI",
387
  file_types=[".midi", ".mid", ".kar"]
 
389
 
390
  gr.Markdown("## Generation options")
391
 
 
392
  model_temperature = gr.Slider(0.1, 1, value=0.9, step=0.01, label="Model temperature")
393
  model_sampling_top_p = gr.Slider(0.1, 0.99, value=0.96, step=0.01, label="Model sampling top p value")
394
 
 
401
  output_plot = gr.Plot(label="MIDI score plot")
402
  output_midi = gr.File(label="MIDI file", file_types=[".mid"])
403
 
404
+ generate_btn.click(Generate_Music_Bridge,
405
  [input_midi,
 
406
  model_temperature,
407
  model_sampling_top_p
408
  ],
 
413
  )
414
 
415
  gr.Examples(
416
+ [["Sharing The Night Together.kar", 0.9, 0.96]
417
  ],
418
  [input_midi,
 
419
  model_temperature,
420
  model_sampling_top_p
421
  ],
 
423
  output_plot,
424
  output_midi
425
  ],
426
+ Generate_Music_Bridge
427
  )
428
 
429
  #==================================================================================