#================================================================================== # https://huggingface.co/spaces/asigalov61/Delta-Melody-Transformer #================================================================================== print('=' * 70) print('Delta Melody Transformer Gradio App') print('=' * 70) print('Loading core Delta Melody Transformer modules...') import os import time as reqtime import datetime from pytz import timezone print('=' * 70) print('Loading main Delta Melody 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_mem_efficient_sdp(True) torch.backends.cuda.enable_math_sdp(True) torch.backends.cuda.enable_flash_sdp(True) torch.backends.cuda.enable_cudnn_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 Delta Melody 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 = 'Delta_Melody_Transformer_Medium_Trained_Model_4382_steps_0.2846_loss_0.9193_acc.pth' SOUNDFONT = 'SGM-v2.01-YamahaGrand-Guit-Bass-v2.7.sf2' #================================================================================== print('Loading SoundFont...') soundfont = hf_hub_download(repo_id='projectlosangeles/soundfonts4u', repo_type='dataset', filename='SGM-v2.01-YamahaGrand-Guit-Bass-v2.7.sf2' ) print('Done!') print('=' * 70) #================================================================================== print('=' * 70) print('Instantiating Delta Melody Transformer model...') device_type = 'cpu' dtype = 'bfloat16' ptdtype = {'bfloat16': torch.bfloat16, 'float16': torch.float16}[dtype] ctx = torch.amp.autocast(device_type=device_type, dtype=ptdtype) SEQ_LEN = 194 PAD_IDX = 705 model = TransformerWrapper( num_tokens = PAD_IDX+1, max_seq_len = SEQ_LEN, attn_layers = Decoder(dim = 1024, depth = 4, heads = 8, 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/Delta-Melody-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 generate_melody_seq(inp, model_temp=0.9, model_top_k=15): x = torch.LongTensor(inp).to(device_type) with ctx: out = model.generate(x, 194, temperature=model_temp, filter_logits_fn=top_k, filter_kwargs={'k': model_top_k}, return_prime=False, eos_token=704, verbose=True) out = out.tolist() return out #================================================================================== def Generate_Melody(melody_len, melody_start_pitch, melody_min_run_time, melody_patch, model_temperature, model_sampling_top_k ): #=============================================================================== print('=' * 70) print('Req start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT))) start_time = reqtime.time() print('=' * 70) print('Requested settings:') print('=' * 70) print('Melody len:', melody_len) print('Melody start pitch:', melody_start_pitch) print('Melody min run time:', melody_min_run_time) print('Melody patch:', melody_patch) print('Model temperature:', model_temperature) print('Model top k:', model_sampling_top_k) print('=' * 70) #================================================================== print('=' * 70) print('Generating...') #================================================================== inp = [512+melody_len] mel_run_time = -1 tries = 0 while mel_run_time < melody_min_run_time and tries < 50: out = generate_melody_seq(inp, model_temperature, model_sampling_top_k ) mel_run_time = (sum(t for t in out if t < 128) * 32) / 1000 tries += 1 print('Generation completed in', tries, 'tries') print('=' * 70) print('Done!') print('=' * 70) #=============================================================================== print('Rendering results...') print('=' * 70) print('Sample INTs', out[:15]) print('=' * 70) song_f = [] time = 0 dur = 1 vel = 90 pitch = 60 channel = 0 pat = melody_patch patches = [0] * 16 patches[0] = melody_patch for ss in out: if 0 <= ss < 128: time += ss * 32 if 128 < ss < 256: dur = (ss-128) * 32 if 256 < ss < 512: dptc = (ss-256-128) pitch += dptc song_f.append(['note', time, dur, channel, pitch, vel, pat]) fn1 = "Delta-Melody-Transformer-Composition" detailed_stats = TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(song_f, output_signature = 'Delta Melody Transformer', output_file_name = fn1, track_name='Project Los Angeles', list_of_MIDI_patches=patches ) new_fn = fn1+'.mid' audio = midi_to_colab_audio(new_fn, soundfont_path=soundfont, sample_rate=16000, 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('-' * 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("