File size: 8,846 Bytes
dcfdcda 2c36e57 dcfdcda d61a6b2 dcfdcda 0763854 dcfdcda 0763854 dcfdcda d61a6b2 dcfdcda d61a6b2 dcfdcda 2c36e57 dcfdcda 319efe0 dcfdcda 4a22cf3 dcfdcda 9b4ebda 4a22cf3 dcfdcda d61a6b2 dcfdcda 2c36e57 dcfdcda 9c384f7 dcfdcda cbbea10 dcfdcda cbbea10 dcfdcda cbbea10 dcfdcda 9536462 dcfdcda cbbea10 a22f22b cbbea10 dcfdcda 319efe0 cbbea10 dcfdcda | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | #====================================================================
# https://huggingface.co/spaces/asigalov61/Godzilla-Piano-Transformer
#====================================================================
"""
Godzilla Piano Transformer Gradio App - Single Model, Simplified Version
Fast 807M 4k solo Piano music transformer trained on 1.14M+ MIDIs (2.7M+ samples)
Using only one model: "without velocity - 3 epochs"
"""
import os
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
import time as reqtime
import datetime
from pytz import timezone
import torch
import matplotlib.pyplot as plt
import gradio as gr
import spaces
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 TransformerWrapper, AutoregressiveWrapper, Decoder
# -----------------------------
# CONFIGURATION & GLOBALS
# -----------------------------
SEP = '=' * 70
PDT = timezone('US/Pacific')
MODEL_CHECKPOINT = 'Godzilla_Piano_Transformer_No_Velocity_Trained_Model_21113_steps_0.3454_loss_0.895_acc.pth'
NUM_OUT_BATCHES = 1
# -----------------------------
# PRINT START-UP INFO
# -----------------------------
def print_sep():
print(SEP)
print_sep()
print("Godzilla Piano Transformer Gradio App")
print_sep()
print("Loading modules...")
# -----------------------------
# ENVIRONMENT & PyTorch Settings
# -----------------------------
os.environ['USE_FLASH_ATTENTION'] = '1'
torch.set_float32_matmul_precision('high')
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True
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)
print_sep()
print("PyTorch version:", torch.__version__)
print("Done loading modules!")
print_sep()
# -----------------------------
# MODEL INITIALIZATION
# -----------------------------
print_sep()
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 = 4096
PAD_IDX = 384
model = TransformerWrapper(
num_tokens=PAD_IDX + 1,
max_seq_len=SEQ_LEN,
attn_layers=Decoder(
dim=2048,
depth=16,
heads=32,
rotary_pos_emb=True,
attn_flash=True
)
)
model = AutoregressiveWrapper(model, ignore_index=PAD_IDX, pad_value=PAD_IDX)
print_sep()
print("Loading model checkpoint...")
checkpoint = hf_hub_download(
repo_id='asigalov61/Godzilla-Piano-Transformer',
filename=MODEL_CHECKPOINT
)
model.load_state_dict(torch.load(checkpoint, map_location='cuda', weights_only=True))
model = torch.compile(model, mode='max-autotune')
print_sep()
print("Done!")
print("Model will use", dtype, "precision...")
print_sep()
model.cuda()
model.eval()
# -----------------------------
# MIDI PROCESSING FUNCTIONS
# -----------------------------
def load_midi(input_midi):
"""Process the input MIDI file and create a token sequence using without velocity logic."""
raw_score = TMIDIX.midi2single_track_ms_score(input_midi.name)
escore_notes = TMIDIX.advanced_score_processor(
raw_score, return_enhanced_score_notes=True, apply_sustain=True
)[0]
sp_escore_notes = TMIDIX.solo_piano_escore_notes(escore_notes)
zscore = TMIDIX.recalculate_score_timings(sp_escore_notes)
zscore = TMIDIX.augment_enhanced_score_notes(zscore, timings_divider=32)
fscore = TMIDIX.fix_escore_notes_durations(zscore)
cscore = TMIDIX.chordify_score([1000, fscore])
score = []
prev_chord = cscore[0]
for chord in cscore:
# Time difference token.
score.append(max(0, min(127, chord[0][1] - prev_chord[0][1])))
for note in chord:
score.extend([
max(1, min(127, note[2])) + 128,
max(1, min(127, note[4])) + 256
])
prev_chord = chord
return score
def save_midi(tokens):
"""Convert token sequence back to a MIDI score and write it using TMIDIX (without velocity).
The output MIDI file name incorporates a date-time stamp.
"""
song_events = []
time_marker = 0
duration = 0
pitch = 0
patches = [0] * 16
for token in tokens:
if 0 <= token < 128:
time_marker += token * 32
elif 128 <= token < 256:
duration = (token - 128) * 32
elif 256 <= token < 384:
pitch = token - 256
song_events.append(['note', time_marker, duration, 0, pitch, max(40, pitch), 0])
# No velocity tokens are used.
# Generate a time stamp using the PDT timezone.
timestamp = datetime.datetime.now(PDT).strftime("%Y%m%d_%H%M%S")
fname = f"MuseCraft-Piano-Composition"
TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(
song_events,
output_signature='MuseCraft Piano',
output_file_name=fname,
track_name='Project Los Angeles',
list_of_MIDI_patches=patches,
verbose=False
)
return fname, song_events
# -----------------------------
# MUSIC GENERATION FUNCTION (Combined)
# -----------------------------
@spaces.GPU
def generate(prime, num_gen_tokens, num_mem_tokens, model_temperature):
"""Generate music tokens given prime tokens and parameters."""
inputs = prime[-num_mem_tokens:] if prime else [0]
print("Generating...")
inp = torch.LongTensor([inputs]).cuda()
with ctx:
out = model.generate(
inp,
num_gen_tokens,
temperature=model_temperature,
return_prime=True,
verbose=False
)
print("Done!")
print_sep()
return out.tolist()
def generate_music(input_midi, num_prime_tokens, num_gen_tokens, num_mem_tokens, model_temperature):
"""
Generate tokens using the model, update the composition state, and prepare outputs.
This function combines seed loading, token generation, and UI output packaging.
"""
print_sep()
print("Request start time:", datetime.datetime.now(PDT).strftime("%Y-%m-%d %H:%M:%S"))
print('=' * 70)
if input_midi is not None:
fn = os.path.basename(input_midi.name)
fn1 = fn.split('.')[0]
print('Input file name:', fn)
print('Num prime tokens:', num_prime_tokens)
print('Num gen tokens:', num_gen_tokens)
print('Num mem tokens:', num_mem_tokens)
print('Model temp:', model_temperature)
print('=' * 70)
input_composition = [0]
# Load seed from MIDI if there is no existing composition.
if input_midi is not None:
input_composition = load_midi(input_midi)[:num_prime_tokens]
generated_batches = generate(input_composition, num_gen_tokens, num_mem_tokens, model_temperature)
midi_fname, midi_score = save_midi(generated_batches[0])
print("Request end time:", datetime.datetime.now(PDT).strftime("%Y-%m-%d %H:%M:%S"))
print_sep()
return midi_fname+'.mid'
# -----------------------------
# GRADIO INTERFACE SETUP
# -----------------------------
with gr.Blocks() as demo:
gr.Markdown("<h1 style='text-align: left; margin-bottom: 1rem'>MuseCraft Piano Transformer</h1>")
gr.Markdown("<h1 style='text-align: left; margin-bottom: 1rem'>Solo Piano music transformer for MuseCraft project</h1>")
gr.HTML("""
Check out <a href="https://github.com/MIDIAI/MuseCraft">MuseCraft project</a> on GitHub
<p>
<a href="https://huggingface.co/spaces/projectlosangeles/MuseCraft-Piano-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 seed MIDI or click 'Generate' for a random output")
input_midi = gr.File(label="Input MIDI", file_types=[".midi", ".mid", ".kar"])
input_midi.upload()
gr.Markdown("## Generate")
num_prime_tokens = gr.Slider(15, 3072, value=3072, step=1, label="Number of prime tokens")
num_gen_tokens = gr.Slider(15, 1024, value=512, step=1, label="Number of tokens to generate")
num_mem_tokens = gr.Slider(15, 4096, value=4096, step=1, label="Number of memory tokens")
model_temperature = gr.Slider(0.1, 1, value=0.9, step=0.01, label="Model temperature")
generate_btn = gr.Button("Generate", variant="primary")
gr.Markdown("## MIDI Output")
generated_MIDI_file = gr.File(label="Generated MIDI file")
generate_btn.click(
generate_music,
[input_midi, num_prime_tokens, num_gen_tokens, num_mem_tokens, model_temperature],
generated_MIDI_file
)
demo.launch() |