Update app.py
Browse files
app.py
CHANGED
|
@@ -33,9 +33,7 @@ SEP = '=' * 70
|
|
| 33 |
PDT = timezone('US/Pacific')
|
| 34 |
|
| 35 |
MODEL_CHECKPOINT = 'Godzilla_Piano_Transformer_No_Velocity_Trained_Model_21113_steps_0.3454_loss_0.895_acc.pth'
|
| 36 |
-
|
| 37 |
-
NUM_OUT_BATCHES = 12
|
| 38 |
-
PREVIEW_LENGTH = 120 # in tokens
|
| 39 |
|
| 40 |
# -----------------------------
|
| 41 |
# PRINT START-UP INFO
|
|
@@ -109,27 +107,6 @@ print_sep()
|
|
| 109 |
model.cuda()
|
| 110 |
model.eval()
|
| 111 |
|
| 112 |
-
# -----------------------------
|
| 113 |
-
# HELPER FUNCTIONS
|
| 114 |
-
# -----------------------------
|
| 115 |
-
def render_midi_output(final_composition):
|
| 116 |
-
"""Generate MIDI score, plot, and audio from final composition."""
|
| 117 |
-
fname, midi_score = save_midi(final_composition)
|
| 118 |
-
time_val = midi_score[-1][1] / 1000 # seconds marker from last note
|
| 119 |
-
midi_plot = TMIDIX.plot_ms_SONG(
|
| 120 |
-
midi_score,
|
| 121 |
-
plot_title='Godzilla Piano Transformer Composition',
|
| 122 |
-
block_lines_times_list=[],
|
| 123 |
-
return_plt=True
|
| 124 |
-
)
|
| 125 |
-
midi_audio = midi_to_colab_audio(
|
| 126 |
-
fname + '.mid',
|
| 127 |
-
soundfont_path=SOUDFONT_PATH,
|
| 128 |
-
sample_rate=16000,
|
| 129 |
-
output_for_gradio=True
|
| 130 |
-
)
|
| 131 |
-
return (16000, midi_audio), midi_plot, fname + '.mid', time_val
|
| 132 |
-
|
| 133 |
# -----------------------------
|
| 134 |
# MIDI PROCESSING FUNCTIONS
|
| 135 |
# -----------------------------
|
|
@@ -212,7 +189,7 @@ def generate_music(prime, num_gen_tokens, num_mem_tokens, num_gen_batches, model
|
|
| 212 |
inp,
|
| 213 |
num_gen_tokens,
|
| 214 |
temperature=model_temperature,
|
| 215 |
-
return_prime=
|
| 216 |
verbose=False
|
| 217 |
)
|
| 218 |
print("Done!")
|
|
@@ -220,7 +197,7 @@ def generate_music(prime, num_gen_tokens, num_mem_tokens, num_gen_batches, model
|
|
| 220 |
return out.tolist()
|
| 221 |
|
| 222 |
def generate_music_and_state(input_midi, num_prime_tokens, num_gen_tokens, num_mem_tokens,
|
| 223 |
-
model_temperature
|
| 224 |
"""
|
| 225 |
Generate tokens using the model, update the composition state, and prepare outputs.
|
| 226 |
This function combines seed loading, token generation, and UI output packaging.
|
|
@@ -244,54 +221,26 @@ def generate_music_and_state(input_midi, num_prime_tokens, num_gen_tokens, num_m
|
|
| 244 |
# Load seed from MIDI if there is no existing composition.
|
| 245 |
if not final_composition and input_midi is not None:
|
| 246 |
final_composition = load_midi(input_midi)[:num_prime_tokens]
|
| 247 |
-
midi_fname, midi_score = save_midi(final_composition)
|
| 248 |
-
# Use the last note's time as a marker.
|
| 249 |
-
TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(
|
| 250 |
-
midi_score,
|
| 251 |
-
output_signature='Godzilla Piano Transformer',
|
| 252 |
-
output_file_name=midi_fname,
|
| 253 |
-
track_name='Project Los Angeles',
|
| 254 |
-
list_of_MIDI_patches=[0]*16,
|
| 255 |
-
verbose=False
|
| 256 |
-
)
|
| 257 |
-
block_lines.append(midi_score[-1][1] / 1000 if final_composition else 0)
|
| 258 |
|
| 259 |
-
|
| 260 |
NUM_OUT_BATCHES, model_temperature)
|
| 261 |
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
preview_tokens = final_composition[-PREVIEW_LENGTH:]
|
| 265 |
-
midi_fname, midi_score = save_midi(preview_tokens + tokens, batch_number=i)
|
| 266 |
-
plot_kwargs = {'plot_title': f'Batch # {i}', 'return_plt': True}
|
| 267 |
-
if len(final_composition) > PREVIEW_LENGTH:
|
| 268 |
-
plot_kwargs['preview_length_in_notes'] = len([t for t in preview_tokens if t > 256])
|
| 269 |
-
TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(
|
| 270 |
-
midi_score,
|
| 271 |
-
output_signature='Godzilla Piano Transformer',
|
| 272 |
-
output_file_name=midi_fname,
|
| 273 |
-
track_name='Project Los Angeles',
|
| 274 |
-
list_of_MIDI_patches=[0]*16,
|
| 275 |
-
verbose=False
|
| 276 |
-
)
|
| 277 |
-
midi_plot = TMIDIX.plot_ms_SONG(midi_score, **plot_kwargs)
|
| 278 |
-
midi_audio = midi_to_colab_audio(midi_fname + '.mid',
|
| 279 |
-
soundfont_path=SOUDFONT_PATH,
|
| 280 |
-
sample_rate=16000,
|
| 281 |
-
output_for_gradio=True)
|
| 282 |
-
output_batches.append([(16000, midi_audio), midi_plot, tokens])
|
| 283 |
-
|
| 284 |
-
# Update generated_batches (for use by add/remove functions)
|
| 285 |
-
generated_batches = batched_gen_tokens
|
| 286 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
print("Request end time:", datetime.datetime.now(PDT).strftime("%Y-%m-%d %H:%M:%S"))
|
| 288 |
print_sep()
|
| 289 |
|
| 290 |
-
|
| 291 |
-
outputs_flat = []
|
| 292 |
-
for batch in output_batches:
|
| 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."""
|
|
|
|
| 33 |
PDT = timezone('US/Pacific')
|
| 34 |
|
| 35 |
MODEL_CHECKPOINT = 'Godzilla_Piano_Transformer_No_Velocity_Trained_Model_21113_steps_0.3454_loss_0.895_acc.pth'
|
| 36 |
+
NUM_OUT_BATCHES = 1
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# -----------------------------
|
| 39 |
# PRINT START-UP INFO
|
|
|
|
| 107 |
model.cuda()
|
| 108 |
model.eval()
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
# -----------------------------
|
| 111 |
# MIDI PROCESSING FUNCTIONS
|
| 112 |
# -----------------------------
|
|
|
|
| 189 |
inp,
|
| 190 |
num_gen_tokens,
|
| 191 |
temperature=model_temperature,
|
| 192 |
+
return_prime=True,
|
| 193 |
verbose=False
|
| 194 |
)
|
| 195 |
print("Done!")
|
|
|
|
| 197 |
return out.tolist()
|
| 198 |
|
| 199 |
def generate_music_and_state(input_midi, num_prime_tokens, num_gen_tokens, num_mem_tokens,
|
| 200 |
+
model_temperature):
|
| 201 |
"""
|
| 202 |
Generate tokens using the model, update the composition state, and prepare outputs.
|
| 203 |
This function combines seed loading, token generation, and UI output packaging.
|
|
|
|
| 221 |
# Load seed from MIDI if there is no existing composition.
|
| 222 |
if not final_composition and input_midi is not None:
|
| 223 |
final_composition = load_midi(input_midi)[:num_prime_tokens]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
+
generated_batches = generate_music(final_composition, num_gen_tokens, num_mem_tokens,
|
| 226 |
NUM_OUT_BATCHES, model_temperature)
|
| 227 |
|
| 228 |
+
|
| 229 |
+
midi_fname, midi_score = save_midi(generated_batches[0])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
|
| 231 |
+
TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(
|
| 232 |
+
midi_score,
|
| 233 |
+
output_signature='Godzilla Piano Transformer',
|
| 234 |
+
output_file_name=midi_fname,
|
| 235 |
+
track_name='Project Los Angeles',
|
| 236 |
+
list_of_MIDI_patches=[0]*16,
|
| 237 |
+
verbose=False
|
| 238 |
+
)
|
| 239 |
+
|
| 240 |
print("Request end time:", datetime.datetime.now(PDT).strftime("%Y-%m-%d %H:%M:%S"))
|
| 241 |
print_sep()
|
| 242 |
|
| 243 |
+
return midi_fname
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
|
| 245 |
def reset(final_composition=[], generated_batches=[], block_lines=[]):
|
| 246 |
"""Reset composition state."""
|