Update app.py
Browse files
app.py
CHANGED
|
@@ -167,11 +167,8 @@ def load_midi(input_midi):
|
|
| 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 |
-
|
| 174 |
-
return None
|
| 175 |
|
| 176 |
else:
|
| 177 |
return None
|
|
@@ -215,35 +212,71 @@ def Generate_Music_Bridge(input_midi,
|
|
| 215 |
print('Sample score tokens', score[:10])
|
| 216 |
|
| 217 |
#==================================================================
|
|
|
|
|
|
|
|
|
|
| 218 |
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
|
|
|
|
|
|
| 222 |
|
| 223 |
-
|
|
|
|
| 224 |
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
#==================================================================
|
| 228 |
|
| 229 |
-
|
| 230 |
-
print('Generating...')
|
| 231 |
-
|
| 232 |
-
x = torch.LongTensor(seq).to(device_type)
|
| 233 |
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
|
| 248 |
#==================================================================
|
| 249 |
|
|
@@ -372,12 +405,16 @@ print('=' * 70)
|
|
| 372 |
|
| 373 |
#==================================================================================
|
| 374 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 375 |
with gr.Blocks() as demo:
|
| 376 |
|
| 377 |
#==================================================================================
|
| 378 |
|
| 379 |
gr.Markdown("<h1 style='text-align: left; margin-bottom: 1rem'>Orpheus Pitches Inpainter</h1>")
|
| 380 |
-
gr.Markdown("<h1 style='text-align: left; margin-bottom: 1rem'>
|
| 381 |
gr.HTML("""
|
| 382 |
<p>
|
| 383 |
<a href="https://huggingface.co/spaces/projectlosangeles/Orpheus-Pitches-Inpainter?duplicate=true">
|
|
@@ -391,7 +428,6 @@ with gr.Blocks() as demo:
|
|
| 391 |
#==================================================================================
|
| 392 |
|
| 393 |
gr.Markdown("## Upload source MIDI or select a sample MIDI on the bottom of the page")
|
| 394 |
-
gr.Markdown("### PLEASE NOTE: The MIDI file MUST HAVE at least 800 MIDI pitches for the demo to work properly!")
|
| 395 |
|
| 396 |
input_midi = gr.File(label="Input MIDI",
|
| 397 |
file_types=[".midi", ".mid", ".kar"]
|
|
@@ -399,8 +435,24 @@ with gr.Blocks() as demo:
|
|
| 399 |
|
| 400 |
gr.Markdown("## Generation options")
|
| 401 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 402 |
model_temperature = gr.Slider(0.1, 1, value=0.9, step=0.01, label="Model temperature")
|
| 403 |
-
|
| 404 |
|
| 405 |
generate_btn = gr.Button("Generate", variant="primary")
|
| 406 |
|
|
|
|
| 167 |
print('Score hss', len(melody_chords), 'tokens')
|
| 168 |
print('=' * 70)
|
| 169 |
|
|
|
|
|
|
|
| 170 |
|
| 171 |
+
return melody_chords
|
|
|
|
| 172 |
|
| 173 |
else:
|
| 174 |
return None
|
|
|
|
| 212 |
print('Sample score tokens', score[:10])
|
| 213 |
|
| 214 |
#==================================================================
|
| 215 |
+
|
| 216 |
+
print('=' * 70)
|
| 217 |
+
print('Inpainting...')
|
| 218 |
|
| 219 |
+
patches_to_inpaint = [0]
|
| 220 |
+
inpaint_every_nth_note = 1
|
| 221 |
+
forbid_original_pitches = True
|
| 222 |
+
max_inpainted_pitch_dev = 12
|
| 223 |
+
max_inpaint_tries_per_note = 20
|
| 224 |
|
| 225 |
+
num_prime_tokens = 128
|
| 226 |
+
num_mem_tokens = 4096
|
| 227 |
|
| 228 |
+
notes_counter = 0
|
|
|
|
|
|
|
| 229 |
|
| 230 |
+
inpainted_song = melody_chords[:num_prime_tokens]
|
|
|
|
|
|
|
|
|
|
| 231 |
|
| 232 |
+
for i, t in enumerate(melody_chords[num_prime_tokens:]):
|
| 233 |
+
|
| 234 |
+
if 256 <= t < 16768:
|
| 235 |
+
|
| 236 |
+
old_patch = (t-256) // 128
|
| 237 |
+
old_pitch = (t-256) % 128
|
| 238 |
+
|
| 239 |
+
if old_patch in patches_to_inpaint and notes_counter % inpaint_every_nth_note == 0:
|
| 240 |
+
|
| 241 |
+
x = torch.LongTensor(inpainted_song[-num_mem_tokens:]).cuda()
|
| 242 |
+
|
| 243 |
+
new_pitch = old_pitch
|
| 244 |
+
tries = 0
|
| 245 |
+
|
| 246 |
+
while (old_pitch == new_pitch or new_pitch > old_pitch + max_inpainted_pitch_dev or new_pitch < old_pitch - max_inpainted_pitch_dev) and tries < max_inpaint_tries_per_note:
|
| 247 |
+
|
| 248 |
+
with ctx:
|
| 249 |
+
out = model.generate(x,
|
| 250 |
+
1,
|
| 251 |
+
temperature=0.9,
|
| 252 |
+
filter_logits_fn=top_k,
|
| 253 |
+
filter_kwargs={'k': 2},
|
| 254 |
+
return_prime=False,
|
| 255 |
+
verbose=False)
|
| 256 |
+
|
| 257 |
+
y = out.tolist()[0]
|
| 258 |
+
|
| 259 |
+
new_pitch = (y-256) % 128
|
| 260 |
+
|
| 261 |
+
tries += 1
|
| 262 |
+
|
| 263 |
+
if not forbid_original_pitches:
|
| 264 |
+
break
|
| 265 |
+
|
| 266 |
+
if tries == max_inpaint_tries_per_note:
|
| 267 |
+
new_pitch = old_pitch
|
| 268 |
+
|
| 269 |
+
new_patch_pitch_tok = (128 * old_patch) + new_pitch + 256
|
| 270 |
+
|
| 271 |
+
inpainted_song.append(new_patch_pitch_tok)
|
| 272 |
+
|
| 273 |
+
else:
|
| 274 |
+
inpainted_song.append(t)
|
| 275 |
+
|
| 276 |
+
else:
|
| 277 |
+
inpainted_song.append(t)
|
| 278 |
+
|
| 279 |
+
notes_counter += 1
|
| 280 |
|
| 281 |
#==================================================================
|
| 282 |
|
|
|
|
| 405 |
|
| 406 |
#==================================================================================
|
| 407 |
|
| 408 |
+
patch2number = {v: k for k, v in TMIDIX.Number2patch.items()}
|
| 409 |
+
|
| 410 |
+
#==================================================================================
|
| 411 |
+
|
| 412 |
with gr.Blocks() as demo:
|
| 413 |
|
| 414 |
#==================================================================================
|
| 415 |
|
| 416 |
gr.Markdown("<h1 style='text-align: left; margin-bottom: 1rem'>Orpheus Pitches Inpainter</h1>")
|
| 417 |
+
gr.Markdown("<h1 style='text-align: left; margin-bottom: 1rem'>Inpaint pitches in any MIDI composition</h1>")
|
| 418 |
gr.HTML("""
|
| 419 |
<p>
|
| 420 |
<a href="https://huggingface.co/spaces/projectlosangeles/Orpheus-Pitches-Inpainter?duplicate=true">
|
|
|
|
| 428 |
#==================================================================================
|
| 429 |
|
| 430 |
gr.Markdown("## Upload source MIDI or select a sample MIDI on the bottom of the page")
|
|
|
|
| 431 |
|
| 432 |
input_midi = gr.File(label="Input MIDI",
|
| 433 |
file_types=[".midi", ".mid", ".kar"]
|
|
|
|
| 435 |
|
| 436 |
gr.Markdown("## Generation options")
|
| 437 |
|
| 438 |
+
|
| 439 |
+
patches_to_inpaint = [0]
|
| 440 |
+
inpaint_every_nth_note = 1
|
| 441 |
+
forbid_original_pitches = True
|
| 442 |
+
max_inpainted_pitch_dev = 12
|
| 443 |
+
max_inpaint_tries_per_note = 20
|
| 444 |
+
|
| 445 |
+
num_prime_tokens = 128
|
| 446 |
+
num_mem_tokens = 4096
|
| 447 |
+
|
| 448 |
+
|
| 449 |
+
inpaint_instruments = gr.Dropdown(label="Select instruments to inpaint", choices=list(patch2number.keys()),
|
| 450 |
+
multiselect=True, type="value",
|
| 451 |
+
info="Instruments MUST be present in the composition. For best results select a single instrument."
|
| 452 |
+
)
|
| 453 |
+
|
| 454 |
model_temperature = gr.Slider(0.1, 1, value=0.9, step=0.01, label="Model temperature")
|
| 455 |
+
model_sampling_top_k = gr.Slider(1, 100, value=15, step=1, label="Model sampling top k value")
|
| 456 |
|
| 457 |
generate_btn = gr.Button("Generate", variant="primary")
|
| 458 |
|