Update app.py
Browse files
app.py
CHANGED
|
@@ -293,74 +293,6 @@ def generate_music_and_state(input_midi, num_prime_tokens, num_gen_tokens, num_m
|
|
| 293 |
outputs_flat.extend([batch[0], batch[1]])
|
| 294 |
return [final_composition, generated_batches, block_lines] + outputs_flat
|
| 295 |
|
| 296 |
-
# -----------------------------
|
| 297 |
-
# BATCH HANDLING FUNCTIONS
|
| 298 |
-
# -----------------------------
|
| 299 |
-
def add_batch(batch_number, final_composition, generated_batches, block_lines):
|
| 300 |
-
"""Add tokens from the specified batch to the final composition and update outputs."""
|
| 301 |
-
if generated_batches:
|
| 302 |
-
final_composition.extend(generated_batches[batch_number])
|
| 303 |
-
midi_fname, midi_score = save_midi(final_composition)
|
| 304 |
-
block_lines.append(midi_score[-1][1] / 1000 if final_composition else 0)
|
| 305 |
-
TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(
|
| 306 |
-
midi_score,
|
| 307 |
-
output_signature='Godzilla Piano Transformer',
|
| 308 |
-
output_file_name=midi_fname,
|
| 309 |
-
track_name='Project Los Angeles',
|
| 310 |
-
list_of_MIDI_patches=[0]*16,
|
| 311 |
-
verbose=False
|
| 312 |
-
)
|
| 313 |
-
midi_plot = TMIDIX.plot_ms_SONG(
|
| 314 |
-
midi_score,
|
| 315 |
-
plot_title='Godzilla Piano Transformer Composition',
|
| 316 |
-
block_lines_times_list=block_lines[:-1],
|
| 317 |
-
return_plt=True
|
| 318 |
-
)
|
| 319 |
-
midi_audio = midi_to_colab_audio(midi_fname + '.mid',
|
| 320 |
-
soundfont_path=SOUDFONT_PATH,
|
| 321 |
-
sample_rate=16000,
|
| 322 |
-
output_for_gradio=True)
|
| 323 |
-
print("Added batch #", batch_number)
|
| 324 |
-
print_sep()
|
| 325 |
-
return (16000, midi_audio), midi_plot, midi_fname + '.mid', final_composition, generated_batches, block_lines
|
| 326 |
-
else:
|
| 327 |
-
return None, None, None, [], [], []
|
| 328 |
-
|
| 329 |
-
def remove_batch(batch_number, num_tokens, final_composition, generated_batches, block_lines):
|
| 330 |
-
"""Remove tokens from the final composition and update outputs."""
|
| 331 |
-
if final_composition and len(final_composition) > num_tokens:
|
| 332 |
-
final_composition = final_composition[:-num_tokens]
|
| 333 |
-
if block_lines:
|
| 334 |
-
block_lines.pop()
|
| 335 |
-
midi_fname, midi_score = save_midi(final_composition)
|
| 336 |
-
TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(
|
| 337 |
-
midi_score,
|
| 338 |
-
output_signature='Godzilla Piano Transformer',
|
| 339 |
-
output_file_name=midi_fname,
|
| 340 |
-
track_name='Project Los Angeles',
|
| 341 |
-
list_of_MIDI_patches=[0]*16,
|
| 342 |
-
verbose=False
|
| 343 |
-
)
|
| 344 |
-
midi_plot = TMIDIX.plot_ms_SONG(
|
| 345 |
-
midi_score,
|
| 346 |
-
plot_title='Godzilla Piano Transformer Composition',
|
| 347 |
-
block_lines_times_list=block_lines[:-1],
|
| 348 |
-
return_plt=True
|
| 349 |
-
)
|
| 350 |
-
midi_audio = midi_to_colab_audio(midi_fname + '.mid',
|
| 351 |
-
soundfont_path=SOUDFONT_PATH,
|
| 352 |
-
sample_rate=16000,
|
| 353 |
-
output_for_gradio=True)
|
| 354 |
-
print("Removed batch #", batch_number)
|
| 355 |
-
print_sep()
|
| 356 |
-
return (16000, midi_audio), midi_plot, midi_fname + '.mid', final_composition, generated_batches, block_lines
|
| 357 |
-
else:
|
| 358 |
-
return None, None, None, [], [], []
|
| 359 |
-
|
| 360 |
-
def clear():
|
| 361 |
-
"""Clear outputs and reset state."""
|
| 362 |
-
return None, None, None, [], []
|
| 363 |
-
|
| 364 |
def reset(final_composition=[], generated_batches=[], block_lines=[]):
|
| 365 |
"""Reset composition state."""
|
| 366 |
return [], [], []
|
|
@@ -370,12 +302,12 @@ def reset(final_composition=[], generated_batches=[], block_lines=[]):
|
|
| 370 |
# -----------------------------
|
| 371 |
with gr.Blocks() as demo:
|
| 372 |
|
| 373 |
-
gr.Markdown("<h1 style='text-align: left; margin-bottom: 1rem'>
|
| 374 |
-
gr.Markdown("<h1 style='text-align: left; margin-bottom: 1rem'>
|
| 375 |
gr.HTML("""
|
| 376 |
-
Check out <a href="https://
|
| 377 |
<p>
|
| 378 |
-
<a href="https://huggingface.co/spaces/
|
| 379 |
<img src="https://huggingface.co/datasets/huggingface/badges/resolve/main/duplicate-this-space-md.svg" alt="Duplicate in Hugging Face">
|
| 380 |
</a>
|
| 381 |
</p>
|
|
@@ -384,8 +316,6 @@ with gr.Blocks() as demo:
|
|
| 384 |
|
| 385 |
# Global state variables for composition
|
| 386 |
final_composition = gr.State([])
|
| 387 |
-
generated_batches = gr.State([])
|
| 388 |
-
block_lines = gr.State([])
|
| 389 |
|
| 390 |
gr.Markdown("## Upload seed MIDI or click 'Generate' for a random output")
|
| 391 |
input_midi = gr.File(label="Input MIDI", file_types=[".midi", ".mid", ".kar"])
|
|
@@ -399,42 +329,14 @@ with gr.Blocks() as demo:
|
|
| 399 |
model_temperature = gr.Slider(0.1, 1, value=0.9, step=0.01, label="Model temperature")
|
| 400 |
generate_btn = gr.Button("Generate", variant="primary")
|
| 401 |
|
| 402 |
-
gr.Markdown("##
|
| 403 |
-
|
| 404 |
-
# Two outputs (audio and plot) for each batch
|
| 405 |
-
for i in range(NUM_OUT_BATCHES):
|
| 406 |
-
with gr.Tab(f"Batch # {i}"):
|
| 407 |
-
audio_output = gr.Audio(label=f"Batch # {i} MIDI Audio", format="mp3")
|
| 408 |
-
plot_output = gr.Plot(label=f"Batch # {i} MIDI Plot")
|
| 409 |
-
outputs.extend([audio_output, plot_output])
|
| 410 |
generate_btn.click(
|
| 411 |
generate_music_and_state,
|
| 412 |
-
[input_midi, num_prime_tokens, num_gen_tokens, num_mem_tokens, model_temperature,
|
| 413 |
-
|
| 414 |
-
outputs
|
| 415 |
)
|
| 416 |
|
| 417 |
-
gr.
|
| 418 |
-
batch_number = gr.Slider(0, NUM_OUT_BATCHES - 1, value=0, step=1, label="Batch number to add/remove")
|
| 419 |
-
add_btn = gr.Button("Add batch", variant="primary")
|
| 420 |
-
remove_btn = gr.Button("Remove batch", variant="stop")
|
| 421 |
-
clear_btn = gr.ClearButton()
|
| 422 |
-
|
| 423 |
-
final_audio_output = gr.Audio(label="Final MIDI audio", format="mp3")
|
| 424 |
-
final_plot_output = gr.Plot(label="Final MIDI plot")
|
| 425 |
-
final_file_output = gr.File(label="Final MIDI file")
|
| 426 |
-
|
| 427 |
-
add_btn.click(
|
| 428 |
-
add_batch,
|
| 429 |
-
[batch_number, final_composition, generated_batches, block_lines],
|
| 430 |
-
[final_audio_output, final_plot_output, final_file_output, final_composition, generated_batches, block_lines]
|
| 431 |
-
)
|
| 432 |
-
remove_btn.click(
|
| 433 |
-
remove_batch,
|
| 434 |
-
[batch_number, num_gen_tokens, final_composition, generated_batches, block_lines],
|
| 435 |
-
[final_audio_output, final_plot_output, final_file_output, final_composition, generated_batches, block_lines]
|
| 436 |
-
)
|
| 437 |
-
clear_btn.click(clear, inputs=None,
|
| 438 |
-
outputs=[final_audio_output, final_plot_output, final_file_output, final_composition, block_lines])
|
| 439 |
|
| 440 |
demo.launch()
|
|
|
|
| 293 |
outputs_flat.extend([batch[0], batch[1]])
|
| 294 |
return [final_composition, generated_batches, block_lines] + outputs_flat
|
| 295 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
def reset(final_composition=[], generated_batches=[], block_lines=[]):
|
| 297 |
"""Reset composition state."""
|
| 298 |
return [], [], []
|
|
|
|
| 302 |
# -----------------------------
|
| 303 |
with gr.Blocks() as demo:
|
| 304 |
|
| 305 |
+
gr.Markdown("<h1 style='text-align: left; margin-bottom: 1rem'>MuseCraft Piano Transformer</h1>")
|
| 306 |
+
gr.Markdown("<h1 style='text-align: left; margin-bottom: 1rem'>Solo Piano music transformer for MuseCraft project</h1>")
|
| 307 |
gr.HTML("""
|
| 308 |
+
Check out <a href="https://github.com/MIDIAI/MuseCraft">MuseCraft project</a> on GitHub
|
| 309 |
<p>
|
| 310 |
+
<a href="https://huggingface.co/spaces/projectlosangeles/MuseCraft-Piano-Transformer?duplicate=true">
|
| 311 |
<img src="https://huggingface.co/datasets/huggingface/badges/resolve/main/duplicate-this-space-md.svg" alt="Duplicate in Hugging Face">
|
| 312 |
</a>
|
| 313 |
</p>
|
|
|
|
| 316 |
|
| 317 |
# Global state variables for composition
|
| 318 |
final_composition = gr.State([])
|
|
|
|
|
|
|
| 319 |
|
| 320 |
gr.Markdown("## Upload seed MIDI or click 'Generate' for a random output")
|
| 321 |
input_midi = gr.File(label="Input MIDI", file_types=[".midi", ".mid", ".kar"])
|
|
|
|
| 329 |
model_temperature = gr.Slider(0.1, 1, value=0.9, step=0.01, label="Model temperature")
|
| 330 |
generate_btn = gr.Button("Generate", variant="primary")
|
| 331 |
|
| 332 |
+
gr.Markdown("## MIDI Output")
|
| 333 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 334 |
generate_btn.click(
|
| 335 |
generate_music_and_state,
|
| 336 |
+
[input_midi, num_prime_tokens, num_gen_tokens, num_mem_tokens, model_temperature],
|
| 337 |
+
generated_MIDI_file
|
|
|
|
| 338 |
)
|
| 339 |
|
| 340 |
+
generated_MIDI_file = gr.File(label="Generated MIDI file")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
|
| 342 |
demo.launch()
|