Spaces:
Running
on
Zero
Running
on
Zero
| #============================================================================================ | |
| # https://huggingface.co/spaces/projectlosangeles/Godzilla-Piano-Chords-Texturing-Transformer | |
| #============================================================================================ | |
| print('=' * 70) | |
| print('Godzilla Piano Chords Texturing Transformer Gradio App') | |
| print('=' * 70) | |
| print('Loading core Godzilla Piano Chords Texturing Transformer modules...') | |
| import os | |
| import copy | |
| import time as reqtime | |
| import datetime | |
| from pytz import timezone | |
| print('=' * 70) | |
| print('Loading main Godzilla Piano Chords Texturing Transformer modules...') | |
| os.environ['USE_FLASH_ATTENTION'] = '1' | |
| import torch | |
| torch.set_float32_matmul_precision('high') | |
| torch.backends.cuda.matmul.allow_tf32 = True # allow tf32 on matmul | |
| torch.backends.cudnn.allow_tf32 = True # allow tf32 on cudnn | |
| torch.backends.cuda.enable_flash_sdp(True) | |
| from huggingface_hub import hf_hub_download | |
| import TMIDIX | |
| from midi_to_colab_audio import midi_to_colab_audio | |
| from x_transformer_2_3_1 import * | |
| import random | |
| import tqdm | |
| print('=' * 70) | |
| print('Loading aux Godzilla Piano Chords Texturing Transformer modules...') | |
| import matplotlib.pyplot as plt | |
| import gradio as gr | |
| import spaces | |
| print('=' * 70) | |
| print('PyTorch version:', torch.__version__) | |
| print('=' * 70) | |
| print('Done!') | |
| print('Enjoy! :)') | |
| print('=' * 70) | |
| #================================================================================== | |
| MODEL_CHECKPOINT = 'Godzilla_Piano_Chords_Texturing_Trained_Model_18001_steps_0.8099_loss_0.7677_acc.pth' | |
| SOUDFONT_PATH = 'SGM-v2.01-YamahaGrand-Guit-Bass-v2.7.sf2' | |
| MAX_MELODY_NOTES = 64 | |
| MAX_GEN_TOKS = 3072 | |
| #================================================================================== | |
| print('=' * 70) | |
| print('Instantiating model...') | |
| device_type = 'cuda' | |
| dtype = 'bfloat16' | |
| ptdtype = {'bfloat16': torch.bfloat16, 'float16': torch.float16}[dtype] | |
| ctx = torch.amp.autocast(device_type=device_type, dtype=ptdtype) | |
| SEQ_LEN = 1536 | |
| PAD_IDX = 708 | |
| model = TransformerWrapper(num_tokens = PAD_IDX+1, | |
| max_seq_len = SEQ_LEN, | |
| attn_layers = Decoder(dim = 2048, | |
| depth = 8, | |
| heads = 32, | |
| rotary_pos_emb = True, | |
| attn_flash = True | |
| ) | |
| ) | |
| model = AutoregressiveWrapper(model, ignore_index=PAD_IDX, pad_value=PAD_IDX) | |
| print('=' * 70) | |
| print('Loading model checkpoint...') | |
| model_checkpoint = hf_hub_download(repo_id='asigalov61/Godzilla-Piano-Transformer', filename=MODEL_CHECKPOINT) | |
| model.load_state_dict(torch.load(model_checkpoint, map_location=device_type, weights_only=True)) | |
| model = torch.compile(model, mode='max-autotune') | |
| model.to(device_type) | |
| model.eval() | |
| print('=' * 70) | |
| print('Done!') | |
| print('=' * 70) | |
| print('Model will use', dtype, 'precision...') | |
| print('=' * 70) | |
| #================================================================================== | |
| def load_midi(input_midi): | |
| raw_score = TMIDIX.midi2single_track_ms_score(input_midi) | |
| escore_notes = TMIDIX.advanced_score_processor(raw_score, return_enhanced_score_notes=True)[0] | |
| sp_escore_notes = TMIDIX.solo_piano_escore_notes(escore_notes) | |
| zscore = TMIDIX.recalculate_score_timings(sp_escore_notes) | |
| escore = TMIDIX.augment_enhanced_score_notes(zscore, timings_divider=32) | |
| escore = TMIDIX.fix_escore_notes_durations(escore) | |
| cscore = TMIDIX.chordify_score([1000, escore]) | |
| score = [] | |
| chords = [] | |
| pc = cscore[0] | |
| for c in cscore: | |
| tones_chord = sorted(set([e[4] % 12 for e in c])) | |
| if tones_chord not in TMIDIX.ALL_CHORDS_SORTED: | |
| tones_chord = TMIDIX.check_and_fix_tones_chord(tones_chord, use_full_chords=False) | |
| chord_tok = TMIDIX.ALL_CHORDS_SORTED.index(tones_chord) | |
| chords.append(chord_tok+384) | |
| score.append(chord_tok+384) | |
| score.append(max(0, min(127, c[0][1]-pc[0][1]))) | |
| for n in c: | |
| score.extend([max(1, min(127, n[2]))+128, max(1, min(127, n[4]))+256]) | |
| pc = c | |
| print('Done!') | |
| print('=' * 70) | |
| print('Score has', len(chords), 'chords') | |
| print('Score hss', len(score), 'tokens') | |
| print('=' * 70) | |
| return score, chords | |
| #================================================================================== | |
| def Generate_Chords_Textures(input_midi, | |
| input_melody, | |
| melody_patch, | |
| use_nth_note, | |
| model_temperature, | |
| model_sampling_top_p | |
| ): | |
| #=============================================================================== | |
| print('=' * 70) | |
| print('Req start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT))) | |
| start_time = reqtime.time() | |
| print('=' * 70) | |
| print('=' * 70) | |
| print('Requested settings:') | |
| print('=' * 70) | |
| fn = os.path.basename(input_midi) | |
| fn1 = fn.split('.')[0] | |
| print('Input MIDI file name:', fn) | |
| print('Source melody patch:', melody_patch) | |
| print('Use nth melody note:', use_nth_note) | |
| print('Model temperature:', model_temperature) | |
| print('Model top p:', model_sampling_top_p) | |
| print('=' * 70) | |
| #================================================================== | |
| print('Loading MIDI...') | |
| score, chords = load_midi(input_midi) | |
| print('Sample score chords', chords[:10]) | |
| print('Sample score tokens', score[:10]) | |
| #================================================================== | |
| print('=' * 70) | |
| print('Generating...') | |
| x = torch.LongTensor([705] + chords[:128] + [706]).cuda() | |
| with ctx: | |
| out = model.generate(x, | |
| 1024, | |
| temperature=model_temperature, | |
| filter_logits_fn=top_p, | |
| filter_kwargs={'thres': model_sampling_top_p}, | |
| return_prime=False, | |
| eos_token=707, | |
| verbose=False) | |
| final_song = out.tolist() | |
| #================================================================== | |
| print('=' * 70) | |
| print('Done!') | |
| print('=' * 70) | |
| #=============================================================================== | |
| print('Rendering results...') | |
| print('=' * 70) | |
| print('Sample INTs', final_song[:15]) | |
| print('=' * 70) | |
| song_f = [] | |
| if len(final_song) != 0: | |
| time = 0 | |
| dur = 1 | |
| vel = 90 | |
| pitch = 60 | |
| channel = 0 | |
| patch = 0 | |
| patches = [0] * 16 | |
| for m in song: | |
| if 0 <= m < 128: | |
| time += m * 32 | |
| elif 128 < m < 256: | |
| dur = (m-128) * 32 | |
| elif 256 < m < 384: | |
| pitch = (m-256) | |
| song_f.append(['note', time, dur, 0, pitch, max(40, pitch), 0]) | |
| fn1 = "Godzilla-Piano-Chords-Texturing-Transformer-Composition" | |
| detailed_stats = TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(song_f, | |
| output_signature = 'Godzilla Piano Chords Texturing Transformer', | |
| output_file_name = fn1, | |
| track_name='Project Los Angeles', | |
| list_of_MIDI_patches=patches_map | |
| ) | |
| new_fn = fn1+'.mid' | |
| audio = midi_to_colab_audio(new_fn, | |
| soundfont_path=SOUDFONT_PATH, | |
| sample_rate=16000, | |
| volume_scale=10, | |
| output_for_gradio=True | |
| ) | |
| print('Done!') | |
| print('=' * 70) | |
| #======================================================== | |
| output_midi = str(new_fn) | |
| output_audio = (16000, audio) | |
| output_plot = TMIDIX.plot_ms_SONG(song_f, plot_title=output_midi, return_plt=True) | |
| print('Output MIDI file name:', output_midi) | |
| print('=' * 70) | |
| #======================================================== | |
| print('-' * 70) | |
| print('Req end time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT))) | |
| print('-' * 70) | |
| print('Req execution time:', (reqtime.time() - start_time), 'sec') | |
| return output_audio, output_plot, output_midi | |
| #================================================================================== | |
| PDT = timezone('US/Pacific') | |
| print('=' * 70) | |
| print('App start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT))) | |
| print('=' * 70) | |
| #================================================================================== | |
| with gr.Blocks() as demo: | |
| #================================================================================== | |
| gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Godzilla Piano Chords Texturing Transformer</h1>") | |
| gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Solo Piano chords Texturing Transformer music transformer</h1>") | |
| gr.HTML(""" | |
| <p> | |
| <a href="https://huggingface.co/spaces/projectlosangeles/Godzilla-Piano-Chords-Texturing-Transformer?duplicate=true"> | |
| <img src="https://huggingface.co/datasets/huggingface/badges/resolve/main/duplicate-this-space-md.svg" alt="Duplicate in Hugging Face"> | |
| </a> | |
| </p> | |
| for faster execution and endless generation! | |
| """) | |
| #================================================================================== | |
| gr.Markdown("## Upload source melody MIDI or enter a search query for a sample melody below") | |
| gr.Markdown("### PLEASE NOTE: The demo is limited and will only texture first 128 chords of the MIDI file") | |
| input_midi = gr.File(label="Input MIDI", | |
| file_types=[".midi", ".mid", ".kar"] | |
| ) | |
| gr.Markdown("## Generation options") | |
| model_temperature = gr.Slider(0.1, 1, value=0.9, step=0.01, label="Model temperature") | |
| model_sampling_top_p = gr.Slider(0.1, 0.99, value=0.96, step=0.01, label="Model sampling top p value") | |
| generate_btn = gr.Button("Generate", variant="primary") | |
| gr.Markdown("## Generation results") | |
| output_title = gr.Textbox(label="MIDI melody title") | |
| output_audio = gr.Audio(label="MIDI audio", format="wav", elem_id="midi_audio") | |
| output_plot = gr.Plot(label="MIDI score plot") | |
| output_midi = gr.File(label="MIDI file", file_types=[".mid"]) | |
| generate_btn.click(Generate_Chords_Textures, | |
| [input_midi, | |
| model_temperature, | |
| model_sampling_top_p | |
| ], | |
| [output_audio, | |
| output_plot, | |
| output_midi | |
| ] | |
| ) | |
| gr.Examples( | |
| [["Sharing The Night Together.kar", 0.9, 0.96] | |
| ], | |
| [input_midi, | |
| model_temperature, | |
| model_sampling_top_p | |
| ], | |
| [output_audio, | |
| output_plot, | |
| output_midi | |
| ], | |
| Generate_Chords_Textures | |
| ) | |
| #================================================================================== | |
| demo.launch() | |
| #================================================================================== |