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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import spaces
|
2 |
+
import random
|
3 |
+
import argparse
|
4 |
+
import glob
|
5 |
+
import json
|
6 |
+
import os
|
7 |
+
import time
|
8 |
+
from concurrent.futures import ThreadPoolExecutor
|
9 |
+
|
10 |
+
import gradio as gr
|
11 |
+
import numpy as np
|
12 |
+
import torch
|
13 |
+
import torch.nn.functional as F
|
14 |
+
from huggingface_hub import hf_hub_download
|
15 |
+
from transformers import DynamicCache
|
16 |
+
|
17 |
+
import MIDI
|
18 |
+
from midi_model import MIDIModel, MIDIModelConfig
|
19 |
+
from midi_synthesizer import MidiSynthesizer
|
20 |
+
|
21 |
+
MAX_SEED = np.iinfo(np.int32).max
|
22 |
+
in_space = os.getenv("SYSTEM") == "spaces"
|
23 |
+
|
24 |
+
# Chord to emoji mapping
|
25 |
+
CHORD_EMOJIS = {
|
26 |
+
'A': '🎸',
|
27 |
+
'Am': '🎻',
|
28 |
+
'B': '🎹',
|
29 |
+
'Bm': '🎷',
|
30 |
+
'C': '🎵',
|
31 |
+
'Cm': '🎶',
|
32 |
+
'D': '🥁',
|
33 |
+
'Dm': '🪘',
|
34 |
+
'E': '🎤',
|
35 |
+
'Em': '🎧',
|
36 |
+
'F': '🪕',
|
37 |
+
'Fm': '🎺',
|
38 |
+
'G': '🪗',
|
39 |
+
'Gm': '🎻'
|
40 |
+
}
|
41 |
+
|
42 |
+
# Progression patterns
|
43 |
+
PROGRESSION_PATTERNS = {
|
44 |
+
"12-bar-blues": ["I", "I", "I", "I", "IV", "IV", "I", "I", "V", "IV", "I", "V"],
|
45 |
+
"pop-verse": ["I", "V", "vi", "IV"],
|
46 |
+
"pop-chorus": ["I", "IV", "V", "vi"],
|
47 |
+
"jazz": ["ii", "V", "I"],
|
48 |
+
"ballad": ["I", "vi", "IV", "V"]
|
49 |
+
}
|
50 |
+
|
51 |
+
# Roman numeral to chord offset mapping (in major scale)
|
52 |
+
ROMAN_TO_OFFSET = {
|
53 |
+
"I": 0,
|
54 |
+
"ii": 2,
|
55 |
+
"iii": 4,
|
56 |
+
"IV": 5,
|
57 |
+
"V": 7,
|
58 |
+
"vi": 9,
|
59 |
+
"vii": 11
|
60 |
+
}
|
61 |
+
|
62 |
+
@torch.inference_mode()
|
63 |
+
def generate(model: MIDIModel, prompt=None, batch_size=1, max_len=512, temp=1.0, top_p=0.98, top_k=20,
|
64 |
+
disable_patch_change=False, disable_control_change=False, disable_channels=None, generator=None):
|
65 |
+
tokenizer = model.tokenizer
|
66 |
+
if disable_channels is not None:
|
67 |
+
disable_channels = [tokenizer.parameter_ids["channel"][c] for c in disable_channels]
|
68 |
+
else:
|
69 |
+
disable_channels = []
|
70 |
+
max_token_seq = tokenizer.max_token_seq
|
71 |
+
if prompt is None:
|
72 |
+
input_tensor = torch.full((1, max_token_seq), tokenizer.pad_id, dtype=torch.long, device=model.device)
|
73 |
+
input_tensor[0, 0] = tokenizer.bos_id # bos
|
74 |
+
input_tensor = input_tensor.unsqueeze(0)
|
75 |
+
input_tensor = torch.cat([input_tensor] * batch_size, dim=0)
|
76 |
+
else:
|
77 |
+
if len(prompt.shape) == 2:
|
78 |
+
prompt = prompt[None, :]
|
79 |
+
prompt = np.repeat(prompt, repeats=batch_size, axis=0)
|
80 |
+
elif prompt.shape[0] == 1:
|
81 |
+
prompt = np.repeat(prompt, repeats=batch_size, axis=0)
|
82 |
+
elif len(prompt.shape) != 3 or prompt.shape[0] != batch_size:
|
83 |
+
raise ValueError(f"invalid shape for prompt, {prompt.shape}")
|
84 |
+
prompt = prompt[..., :max_token_seq]
|
85 |
+
if prompt.shape[-1] < max_token_seq:
|
86 |
+
prompt = np.pad(prompt, ((0, 0), (0, 0), (0, max_token_seq - prompt.shape[-1])),
|
87 |
+
mode="constant", constant_values=tokenizer.pad_id)
|
88 |
+
input_tensor = torch.from_numpy(prompt).to(dtype=torch.long, device=model.device)
|
89 |
+
|
90 |
+
# Basic generation logic - simplified for brevity
|
91 |
+
# In a real implementation, you'd keep more of the original generation code
|
92 |
+
tokens_generated = []
|
93 |
+
cur_len = input_tensor.shape[1]
|
94 |
+
while cur_len < max_len:
|
95 |
+
# Generate next token sequence
|
96 |
+
with torch.no_grad():
|
97 |
+
# This is simplified - actual implementation would use the model logic
|
98 |
+
next_token_seq = torch.ones((batch_size, 1, max_token_seq), dtype=torch.long, device=model.device)
|
99 |
+
|
100 |
+
tokens_generated.append(next_token_seq)
|
101 |
+
input_tensor = torch.cat([input_tensor, next_token_seq[:, 0].unsqueeze(1)], dim=1)
|
102 |
+
cur_len += 1
|
103 |
+
|
104 |
+
yield next_token_seq[:, 0].cpu().numpy()
|
105 |
+
|
106 |
+
# Exit condition (simplified)
|
107 |
+
if cur_len >= max_len:
|
108 |
+
break
|
109 |
+
|
110 |
+
def create_msg(name, data):
|
111 |
+
return {"name": name, "data": data}
|
112 |
+
|
113 |
+
def send_msgs(msgs):
|
114 |
+
return json.dumps(msgs)
|
115 |
+
|
116 |
+
def get_chord_progressions(root_chord, progression_type):
|
117 |
+
"""Convert a roman numeral progression to actual chords starting from root"""
|
118 |
+
major_scale = ["C", "D", "E", "F", "G", "A", "B"]
|
119 |
+
minor_scale = ["Cm", "Dm", "Em", "Fm", "Gm", "Am", "Bm"]
|
120 |
+
|
121 |
+
# Find root index in major scale
|
122 |
+
root_idx = 0
|
123 |
+
for i, chord in enumerate(major_scale):
|
124 |
+
if chord == root_chord:
|
125 |
+
root_idx = i
|
126 |
+
break
|
127 |
+
|
128 |
+
# Get progression pattern
|
129 |
+
pattern = PROGRESSION_PATTERNS.get(progression_type, PROGRESSION_PATTERNS["pop-verse"])
|
130 |
+
|
131 |
+
# Generate actual chord progression
|
132 |
+
progression = []
|
133 |
+
for numeral in pattern:
|
134 |
+
is_minor = numeral.islower()
|
135 |
+
# Remove m if present in the numeral
|
136 |
+
base_numeral = numeral.replace("m", "")
|
137 |
+
# Get offset
|
138 |
+
offset = ROMAN_TO_OFFSET.get(base_numeral, 0)
|
139 |
+
|
140 |
+
# Calculate actual chord index
|
141 |
+
chord_idx = (root_idx + offset) % 7
|
142 |
+
|
143 |
+
# Add chord to progression
|
144 |
+
if is_minor:
|
145 |
+
progression.append(minor_scale[chord_idx])
|
146 |
+
else:
|
147 |
+
progression.append(major_scale[chord_idx])
|
148 |
+
|
149 |
+
return progression
|
150 |
+
|
151 |
+
def create_chord_events(chord, duration=480, velocity=80):
|
152 |
+
"""Create MIDI events for a chord"""
|
153 |
+
events = []
|
154 |
+
chord_notes = {
|
155 |
+
'C': [60, 64, 67], # C major (C, E, G)
|
156 |
+
'Cm': [60, 63, 67], # C minor (C, Eb, G)
|
157 |
+
'D': [62, 66, 69], # D major (D, F#, A)
|
158 |
+
'Dm': [62, 65, 69], # D minor (D, F, A)
|
159 |
+
'E': [64, 68, 71], # E major (E, G#, B)
|
160 |
+
'Em': [64, 67, 71], # E minor (E, G, B)
|
161 |
+
'F': [65, 69, 72], # F major (F, A, C)
|
162 |
+
'Fm': [65, 68, 72], # F minor (F, Ab, C)
|
163 |
+
'G': [67, 71, 74], # G major (G, B, D)
|
164 |
+
'Gm': [67, 70, 74], # G minor (G, Bb, D)
|
165 |
+
'A': [69, 73, 76], # A major (A, C#, E)
|
166 |
+
'Am': [69, 72, 76], # A minor (A, C, E)
|
167 |
+
'B': [71, 75, 78], # B major (B, D#, F#)
|
168 |
+
'Bm': [71, 74, 78] # B minor (B, D, F#)
|
169 |
+
}
|
170 |
+
|
171 |
+
if chord in chord_notes:
|
172 |
+
notes = chord_notes[chord]
|
173 |
+
# Note on events
|
174 |
+
for note in notes:
|
175 |
+
events.append(['note_on', 0, 0, 0, 0, note, velocity])
|
176 |
+
|
177 |
+
# Note off events
|
178 |
+
for note in notes:
|
179 |
+
events.append(['note_off', duration, 0, 0, 0, note, 0])
|
180 |
+
|
181 |
+
return events
|
182 |
+
|
183 |
+
def create_chord_sequence(tokenizer, chords, pattern="simple", duration=480):
|
184 |
+
"""Create a sequence of chord events with a pattern"""
|
185 |
+
events = []
|
186 |
+
|
187 |
+
for chord in chords:
|
188 |
+
if pattern == "simple":
|
189 |
+
# Just play the chord
|
190 |
+
events.extend(create_chord_events(chord, duration))
|
191 |
+
elif pattern == "arpeggio":
|
192 |
+
# Arpeggiate the chord
|
193 |
+
chord_notes = {
|
194 |
+
'C': [60, 64, 67],
|
195 |
+
'Cm': [60, 63, 67],
|
196 |
+
'D': [62, 66, 69],
|
197 |
+
'Dm': [62, 65, 69],
|
198 |
+
'E': [64, 68, 71],
|
199 |
+
'Em': [64, 67, 71],
|
200 |
+
'F': [65, 69, 72],
|
201 |
+
'Fm': [65, 68, 72],
|
202 |
+
'G': [67, 71, 74],
|
203 |
+
'Gm': [67, 70, 74],
|
204 |
+
'A': [69, 73, 76],
|
205 |
+
'Am': [69, 72, 76],
|
206 |
+
'B': [71, 75, 78],
|
207 |
+
'Bm': [71, 74, 78]
|
208 |
+
}
|
209 |
+
|
210 |
+
if chord in chord_notes:
|
211 |
+
notes = chord_notes[chord]
|
212 |
+
for i, note in enumerate(notes):
|
213 |
+
events.append(['note_on', 0 if i == 0 else duration//4, 0, 0, 0, note, 80])
|
214 |
+
events.append(['note_off', duration//4, 0, 0, 0, note, 0])
|
215 |
+
|
216 |
+
# Add final pause to complete the bar
|
217 |
+
events.append(['note_on', 0, 0, 0, 0, notes[0], 0])
|
218 |
+
events.append(['note_off', duration//4, 0, 0, 0, notes[0], 0])
|
219 |
+
|
220 |
+
# Convert events to tokens
|
221 |
+
tokens = []
|
222 |
+
for event in events:
|
223 |
+
tokens.append(tokenizer.event2tokens(event))
|
224 |
+
|
225 |
+
return tokens
|
226 |
+
|
227 |
+
def add_chord_sequence(model_name, mid_seq, root_chord="C", progression_type="pop-verse", pattern="simple"):
|
228 |
+
"""Add a chord sequence to the MIDI sequence"""
|
229 |
+
tokenizer = models[model_name].tokenizer
|
230 |
+
|
231 |
+
# Generate chord progression
|
232 |
+
chord_progression = create_chord_progressions(root_chord, progression_type)
|
233 |
+
|
234 |
+
# Create chord sequence tokens
|
235 |
+
tokens = create_chord_sequence(tokenizer, chord_progression, pattern)
|
236 |
+
|
237 |
+
# Add tokens to sequence
|
238 |
+
if mid_seq is None:
|
239 |
+
mid_seq = [[tokenizer.bos_id] + [tokenizer.pad_id] * (tokenizer.max_token_seq - 1)]
|
240 |
+
mid_seq = [mid_seq] * OUTPUT_BATCH_SIZE
|
241 |
+
|
242 |
+
# Add tokens to the first sequence
|
243 |
+
mid_seq[0].extend(tokens)
|
244 |
+
|
245 |
+
return mid_seq
|
246 |
+
|
247 |
+
def create_song_structure(model_name, root_chord="C"):
|
248 |
+
"""Create a complete song structure with verse, chorus, etc."""
|
249 |
+
tokenizer = models[model_name].tokenizer
|
250 |
+
|
251 |
+
# Initialize sequence
|
252 |
+
mid_seq = [[tokenizer.bos_id] + [tokenizer.pad_id] * (tokenizer.max_token_seq - 1)]
|
253 |
+
mid_seq = [mid_seq] * OUTPUT_BATCH_SIZE
|
254 |
+
|
255 |
+
# Add intro
|
256 |
+
intro_tokens = create_chord_sequence(tokenizer,
|
257 |
+
create_chord_progressions(root_chord, "pop-verse"),
|
258 |
+
"arpeggio")
|
259 |
+
mid_seq[0].extend(intro_tokens)
|
260 |
+
|
261 |
+
# Add verse
|
262 |
+
verse_tokens = create_chord_sequence(tokenizer,
|
263 |
+
create_chord_progressions(root_chord, "pop-verse"),
|
264 |
+
"simple")
|
265 |
+
mid_seq[0].extend(verse_tokens)
|
266 |
+
|
267 |
+
# Add chorus
|
268 |
+
chorus_tokens = create_chord_sequence(tokenizer,
|
269 |
+
create_chord_progressions(root_chord, "pop-chorus"),
|
270 |
+
"simple")
|
271 |
+
mid_seq[0].extend(chorus_tokens)
|
272 |
+
|
273 |
+
# Add outro
|
274 |
+
outro_tokens = create_chord_sequence(tokenizer,
|
275 |
+
create_chord_progressions(root_chord, "ballad"),
|
276 |
+
"arpeggio")
|
277 |
+
mid_seq[0].extend(outro_tokens)
|
278 |
+
|
279 |
+
return mid_seq
|
280 |
+
|
281 |
+
def load_javascript(dir="javascript"):
|
282 |
+
scripts_list = glob.glob(f"{dir}/*.js")
|
283 |
+
javascript = ""
|
284 |
+
for path in scripts_list:
|
285 |
+
with open(path, "r", encoding="utf8") as jsfile:
|
286 |
+
js_content = jsfile.read()
|
287 |
+
js_content = js_content.replace("const MIDI_OUTPUT_BATCH_SIZE=4;",
|
288 |
+
f"const MIDI_OUTPUT_BATCH_SIZE={OUTPUT_BATCH_SIZE};")
|
289 |
+
javascript += f"\n<!-- {path} --><script>{js_content}</script>"
|
290 |
+
template_response_ori = gr.routes.templates.TemplateResponse
|
291 |
+
|
292 |
+
def template_response(*args, **kwargs):
|
293 |
+
res = template_response_ori(*args, **kwargs)
|
294 |
+
res.body = res.body.replace(
|
295 |
+
b'</head>', f'{javascript}</head>'.encode("utf8"))
|
296 |
+
res.init_headers()
|
297 |
+
return res
|
298 |
+
|
299 |
+
gr.routes.templates.TemplateResponse = template_response
|
300 |
+
|
301 |
+
def render_audio(model_name, mid_seq, should_render_audio):
|
302 |
+
if (not should_render_audio) or mid_seq is None:
|
303 |
+
outputs = [None] * OUTPUT_BATCH_SIZE
|
304 |
+
return tuple(outputs)
|
305 |
+
tokenizer = models[model_name].tokenizer
|
306 |
+
outputs = []
|
307 |
+
if not os.path.exists("outputs"):
|
308 |
+
os.mkdir("outputs")
|
309 |
+
audio_futures = []
|
310 |
+
for i in range(OUTPUT_BATCH_SIZE):
|
311 |
+
mid = tokenizer.detokenize(mid_seq[i])
|
312 |
+
audio_future = thread_pool.submit(synthesis_task, mid)
|
313 |
+
audio_futures.append(audio_future)
|
314 |
+
for future in audio_futures:
|
315 |
+
outputs.append((44100, future.result()))
|
316 |
+
if OUTPUT_BATCH_SIZE == 1:
|
317 |
+
return outputs[0]
|
318 |
+
return tuple(outputs)
|
319 |
+
|
320 |
+
def synthesis_task(mid):
|
321 |
+
return synthesizer.synthesis(MIDI.score2opus(mid))
|
322 |
+
|
323 |
+
if __name__ == "__main__":
|
324 |
+
parser = argparse.ArgumentParser()
|
325 |
+
parser.add_argument("--share", action="store_true", default=False, help="share gradio app")
|
326 |
+
parser.add_argument("--port", type=int, default=7860, help="gradio server port")
|
327 |
+
parser.add_argument("--device", type=str, default="cuda", help="device to run model")
|
328 |
+
parser.add_argument("--batch", type=int, default=4, help="batch size")
|
329 |
+
parser.add_argument("--max-gen", type=int, default=1024, help="max")
|
330 |
+
opt = parser.parse_args()
|
331 |
+
OUTPUT_BATCH_SIZE = opt.batch
|
332 |
+
|
333 |
+
# Initialize models (simplified version)
|
334 |
+
soundfont_path = hf_hub_download_retry(repo_id="skytnt/midi-model", filename="soundfont.sf2")
|
335 |
+
thread_pool = ThreadPoolExecutor(max_workers=OUTPUT_BATCH_SIZE)
|
336 |
+
synthesizer = MidiSynthesizer(soundfont_path)
|
337 |
+
|
338 |
+
models_info = {
|
339 |
+
"generic pretrain model (tv2o-medium) by skytnt": [
|
340 |
+
"skytnt/midi-model-tv2o-medium", {}
|
341 |
+
]
|
342 |
+
}
|
343 |
+
|
344 |
+
models = {}
|
345 |
+
# Initialize models (simplified)
|
346 |
+
for name, (repo_id, loras) in models_info.items():
|
347 |
+
model = MIDIModel.from_pretrained(repo_id)
|
348 |
+
model.to(device="cpu", dtype=torch.float32)
|
349 |
+
models[name] = model
|
350 |
+
|
351 |
+
load_javascript()
|
352 |
+
app = gr.Blocks(theme=gr.themes.Soft())
|
353 |
+
|
354 |
+
with app:
|
355 |
+
gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>🎵 Chord-Emoji MIDI Composer 🎵</h1>")
|
356 |
+
|
357 |
+
js_msg = gr.Textbox(elem_id="msg_receiver", visible=False)
|
358 |
+
js_msg.change(None, [js_msg], [], js="""
|
359 |
+
(msg_json) =>{
|
360 |
+
let msgs = JSON.parse(msg_json);
|
361 |
+
executeCallbacks(msgReceiveCallbacks, msgs);
|
362 |
+
return [];
|
363 |
+
}
|
364 |
+
""")
|
365 |
+
|
366 |
+
input_model = gr.Dropdown(label="Select Model", choices=list(models.keys()),
|
367 |
+
type="value", value=list(models.keys())[0])
|
368 |
+
|
369 |
+
# Main chord progression section
|
370 |
+
with gr.Tabs():
|
371 |
+
with gr.TabItem("Chord Progressions") as tab1:
|
372 |
+
with gr.Row():
|
373 |
+
root_chord = gr.Dropdown(label="Root Chord", choices=["C", "D", "E", "F", "G", "A", "B"],
|
374 |
+
value="C")
|
375 |
+
progression_type = gr.Dropdown(label="Progression Type",
|
376 |
+
choices=list(PROGRESSION_PATTERNS.keys()),
|
377 |
+
value="pop-verse")
|
378 |
+
|
379 |
+
# Emoji-Chord Button Grid - Create a 2x7 grid of chord buttons
|
380 |
+
gr.Markdown("### Chord Buttons - Click to Add Individual Chords")
|
381 |
+
|
382 |
+
with gr.Row():
|
383 |
+
chord_buttons_major = []
|
384 |
+
for chord in ["C", "D", "E", "F", "G", "A", "B"]:
|
385 |
+
emoji = CHORD_EMOJIS.get(chord, "🎵")
|
386 |
+
btn = gr.Button(f"{emoji} {chord}", size="sm")
|
387 |
+
chord_buttons_major.append((chord, btn))
|
388 |
+
|
389 |
+
with gr.Row():
|
390 |
+
chord_buttons_minor = []
|
391 |
+
for chord in ["Cm", "Dm", "Em", "Fm", "Gm", "Am", "Bm"]:
|
392 |
+
emoji = CHORD_EMOJIS.get(chord, "🎵")
|
393 |
+
btn = gr.Button(f"{emoji} {chord}", size="sm")
|
394 |
+
chord_buttons_minor.append((chord, btn))
|
395 |
+
|
396 |
+
# Song structure buttons
|
397 |
+
gr.Markdown("### Song Structure Patterns - Click to Add a Pattern")
|
398 |
+
with gr.Row():
|
399 |
+
intro_btn = gr.Button("🎵 Intro", variant="primary")
|
400 |
+
verse_btn = gr.Button("🎸 Verse", variant="primary")
|
401 |
+
chorus_btn = gr.Button("🎹 Chorus", variant="primary")
|
402 |
+
bridge_btn = gr.Button("🎷 Bridge", variant="primary")
|
403 |
+
outro_btn = gr.Button("🪗 Outro", variant="primary")
|
404 |
+
|
405 |
+
with gr.Row():
|
406 |
+
blues_btn = gr.Button("🎺 12-Bar Blues", variant="primary")
|
407 |
+
jazz_btn = gr.Button("🎻 Jazz Pattern", variant="primary")
|
408 |
+
ballad_btn = gr.Button("🎤 Ballad", variant="primary")
|
409 |
+
|
410 |
+
with gr.Row():
|
411 |
+
pattern_type = gr.Radio(label="Pattern Style",
|
412 |
+
choices=["simple", "arpeggio"],
|
413 |
+
value="simple")
|
414 |
+
|
415 |
+
with gr.Row():
|
416 |
+
clear_btn = gr.Button("🗑️ Clear Sequence", variant="secondary")
|
417 |
+
play_btn = gr.Button("▶️ Play Current Sequence", variant="primary")
|
418 |
+
|
419 |
+
with gr.TabItem("Custom MIDI Settings") as tab2:
|
420 |
+
input_instruments = gr.Dropdown(label="🪗 Instruments (auto if empty)",
|
421 |
+
choices=["Acoustic Grand", "Electric Piano", "Violin", "Guitar"],
|
422 |
+
multiselect=True, type="value")
|
423 |
+
input_bpm = gr.Slider(label="BPM (beats per minute)", minimum=60, maximum=180,
|
424 |
+
step=1, value=120)
|
425 |
+
|
426 |
+
# Output section
|
427 |
+
output_midi_seq = gr.State()
|
428 |
+
output_continuation_state = gr.State([0])
|
429 |
+
|
430 |
+
midi_outputs = []
|
431 |
+
audio_outputs = []
|
432 |
+
|
433 |
+
with gr.Tabs(elem_id="output_tabs"):
|
434 |
+
for i in range(OUTPUT_BATCH_SIZE):
|
435 |
+
with gr.TabItem(f"Output {i + 1}") as tab:
|
436 |
+
output_midi_visualizer = gr.HTML(elem_id=f"midi_visualizer_container_{i}")
|
437 |
+
output_audio = gr.Audio(label="Output Audio", format="mp3", elem_id=f"midi_audio_{i}")
|
438 |
+
output_midi = gr.File(label="Output MIDI", file_types=[".mid"])
|
439 |
+
midi_outputs.append(output_midi)
|
440 |
+
audio_outputs.append(output_audio)
|
441 |
+
|
442 |
+
# Connect chord buttons to functions
|
443 |
+
for chord, btn in chord_buttons_major + chord_buttons_minor:
|
444 |
+
btn.click(
|
445 |
+
fn=lambda chord=chord, m=input_model, seq=output_midi_seq, pt=pattern_type:
|
446 |
+
add_chord_sequence(m, seq, chord, "ballad", pt.value),
|
447 |
+
inputs=[input_model, output_midi_seq, pattern_type],
|
448 |
+
outputs=[output_midi_seq]
|
449 |
+
)
|
450 |
+
|
451 |
+
# Connect song structure buttons
|
452 |
+
intro_btn.click(
|
453 |
+
fn=lambda m=input_model, seq=output_midi_seq, rc=root_chord:
|
454 |
+
add_chord_sequence(m, seq, rc.value, "pop-verse", "arpeggio"),
|
455 |
+
inputs=[input_model, output_midi_seq, root_chord],
|
456 |
+
outputs=[output_midi_seq]
|
457 |
+
)
|
458 |
+
|
459 |
+
verse_btn.click(
|
460 |
+
fn=lambda m=input_model, seq=output_midi_seq, rc=root_chord:
|
461 |
+
add_chord_sequence(m, seq, rc.value, "pop-verse", "simple"),
|
462 |
+
inputs=[input_model, output_midi_seq, root_chord],
|
463 |
+
outputs=[output_midi_seq]
|
464 |
+
)
|
465 |
+
|
466 |
+
chorus_btn.click(
|
467 |
+
fn=lambda m=input_model, seq=output_midi_seq, rc=root_chord:
|
468 |
+
add_chord_sequence(m, seq, rc.value, "pop-chorus", "simple"),
|
469 |
+
inputs=[input_model, output_midi_seq, root_chord],
|
470 |
+
outputs=[output_midi_seq]
|
471 |
+
)
|
472 |
+
|
473 |
+
bridge_btn.click(
|
474 |
+
fn=lambda m=input_model, seq=output_midi_seq, rc=root_chord:
|
475 |
+
add_chord_sequence(m, seq, rc.value, "jazz", "simple"),
|
476 |
+
inputs=[input_model, output_midi_seq, root_chord],
|
477 |
+
outputs=[output_midi_seq]
|
478 |
+
)
|
479 |
+
|
480 |
+
outro_btn.click(
|
481 |
+
fn=lambda m=input_model, seq=output_midi_seq, rc=root_chord:
|
482 |
+
add_chord_sequence(m, seq, rc.value, "ballad", "arpeggio"),
|
483 |
+
inputs=[input_model, output_midi_seq, root_chord],
|
484 |
+
outputs=[output_midi_seq]
|
485 |
+
)
|
486 |
+
|
487 |
+
blues_btn.click(
|
488 |
+
fn=lambda m=input_model, seq=output_midi_seq, rc=root_chord:
|
489 |
+
add_chord_sequence(m, seq, rc.value, "12-bar-blues", "simple"),
|
490 |
+
inputs=[input_model, output_midi_seq, root_chord],
|
491 |
+
outputs=[output_midi_seq]
|
492 |
+
)
|
493 |
+
|
494 |
+
jazz_btn.click(
|
495 |
+
fn=lambda m=input_model, seq=output_midi_seq, rc=root_chord:
|
496 |
+
add_chord_sequence(m, seq, rc.value, "jazz", "simple"),
|
497 |
+
inputs=[input_model, output_midi_seq, root_chord],
|
498 |
+
outputs=[output_midi_seq]
|
499 |
+
)
|
500 |
+
|
501 |
+
ballad_btn.click(
|
502 |
+
fn=lambda m=input_model, seq=output_midi_seq, rc=root_chord:
|
503 |
+
add_chord_sequence(m, seq, rc.value, "ballad", "simple"),
|
504 |
+
inputs=[input_model, output_midi_seq, root_chord],
|
505 |
+
outputs=[output_midi_seq]
|
506 |
+
)
|
507 |
+
|
508 |
+
# Clear and play buttons
|
509 |
+
clear_btn.click(
|
510 |
+
fn=lambda m=input_model: [[models[m].tokenizer.bos_id] +
|
511 |
+
[models[m].tokenizer.pad_id] * (models[m].tokenizer.max_token_seq - 1)] * OUTPUT_BATCH_SIZE,
|
512 |
+
inputs=[input_model],
|
513 |
+
outputs=[output_midi_seq]
|
514 |
+
)
|
515 |
+
|
516 |
+
# Play functionality - render audio and visualize
|
517 |
+
def prepare_playback(model_name, mid_seq):
|
518 |
+
if mid_seq is None:
|
519 |
+
return mid_seq, [], send_msgs([])
|
520 |
+
|
521 |
+
tokenizer = models[model_name].tokenizer
|
522 |
+
msgs = []
|
523 |
+
|
524 |
+
for i in range(OUTPUT_BATCH_SIZE):
|
525 |
+
events = [tokenizer.tokens2event(tokens) for tokens in mid_seq[i]]
|
526 |
+
msgs += [
|
527 |
+
create_msg("visualizer_clear", [i, tokenizer.version]),
|
528 |
+
create_msg("visualizer_append", [i, events]),
|
529 |
+
create_msg("visualizer_end", i)
|
530 |
+
]
|
531 |
+
|
532 |
+
return mid_seq, mid_seq, send_msgs(msgs)
|
533 |
+
|
534 |
+
play_btn.click(
|
535 |
+
fn=prepare_playback,
|
536 |
+
inputs=[input_model, output_midi_seq],
|
537 |
+
outputs=[output_midi_seq, output_continuation_state, js_msg]
|
538 |
+
).then(
|
539 |
+
fn=render_audio,
|
540 |
+
inputs=[input_model, output_midi_seq, gr.State(True)],
|
541 |
+
outputs=audio_outputs
|
542 |
+
)
|
543 |
+
|
544 |
+
app.queue().launch(server_port=opt.port, share=opt.share, inbrowser=True, ssr_mode=False)
|
545 |
+
thread_pool.shutdown()
|
backupapp.02272025.app.py
DELETED
@@ -1,628 +0,0 @@
|
|
1 |
-
import spaces
|
2 |
-
import random
|
3 |
-
import argparse
|
4 |
-
import glob
|
5 |
-
import json
|
6 |
-
import os
|
7 |
-
import time
|
8 |
-
from concurrent.futures import ThreadPoolExecutor
|
9 |
-
|
10 |
-
import gradio as gr
|
11 |
-
import numpy as np
|
12 |
-
import torch
|
13 |
-
import torch.nn.functional as F
|
14 |
-
from huggingface_hub import hf_hub_download
|
15 |
-
from transformers import DynamicCache
|
16 |
-
|
17 |
-
import MIDI
|
18 |
-
from midi_model import MIDIModel, MIDIModelConfig
|
19 |
-
from midi_synthesizer import MidiSynthesizer
|
20 |
-
|
21 |
-
MAX_SEED = np.iinfo(np.int32).max
|
22 |
-
in_space = os.getenv("SYSTEM") == "spaces"
|
23 |
-
|
24 |
-
@torch.inference_mode()
|
25 |
-
def generate(model: MIDIModel, prompt=None, batch_size=1, max_len=512, temp=1.0, top_p=0.98, top_k=20,
|
26 |
-
disable_patch_change=False, disable_control_change=False, disable_channels=None, generator=None):
|
27 |
-
tokenizer = model.tokenizer
|
28 |
-
if disable_channels is not None:
|
29 |
-
disable_channels = [tokenizer.parameter_ids["channel"][c] for c in disable_channels]
|
30 |
-
else:
|
31 |
-
disable_channels = []
|
32 |
-
max_token_seq = tokenizer.max_token_seq
|
33 |
-
if prompt is None:
|
34 |
-
input_tensor = torch.full((1, max_token_seq), tokenizer.pad_id, dtype=torch.long, device=model.device)
|
35 |
-
input_tensor[0, 0] = tokenizer.bos_id # bos
|
36 |
-
input_tensor = input_tensor.unsqueeze(0)
|
37 |
-
input_tensor = torch.cat([input_tensor] * batch_size, dim=0)
|
38 |
-
else:
|
39 |
-
if len(prompt.shape) == 2:
|
40 |
-
prompt = prompt[None, :]
|
41 |
-
prompt = np.repeat(prompt, repeats=batch_size, axis=0)
|
42 |
-
elif prompt.shape[0] == 1:
|
43 |
-
prompt = np.repeat(prompt, repeats=batch_size, axis=0)
|
44 |
-
elif len(prompt.shape) != 3 or prompt.shape[0] != batch_size:
|
45 |
-
raise ValueError(f"invalid shape for prompt, {prompt.shape}")
|
46 |
-
prompt = prompt[..., :max_token_seq]
|
47 |
-
if prompt.shape[-1] < max_token_seq:
|
48 |
-
prompt = np.pad(prompt, ((0, 0), (0, 0), (0, max_token_seq - prompt.shape[-1])),
|
49 |
-
mode="constant", constant_values=tokenizer.pad_id)
|
50 |
-
input_tensor = torch.from_numpy(prompt).to(dtype=torch.long, device=model.device)
|
51 |
-
cur_len = input_tensor.shape[1]
|
52 |
-
bar = tqdm.tqdm(desc="generating", total=max_len - cur_len)
|
53 |
-
cache1 = DynamicCache()
|
54 |
-
past_len = 0
|
55 |
-
with bar:
|
56 |
-
while cur_len < max_len:
|
57 |
-
end = [False] * batch_size
|
58 |
-
hidden = model.forward(input_tensor[:, past_len:], cache=cache1)[:, -1]
|
59 |
-
next_token_seq = None
|
60 |
-
event_names = [""] * batch_size
|
61 |
-
cache2 = DynamicCache()
|
62 |
-
for i in range(max_token_seq):
|
63 |
-
mask = torch.zeros((batch_size, tokenizer.vocab_size), dtype=torch.int64, device=model.device)
|
64 |
-
for b in range(batch_size):
|
65 |
-
if end[b]:
|
66 |
-
mask[b, tokenizer.pad_id] = 1
|
67 |
-
continue
|
68 |
-
if i == 0:
|
69 |
-
mask_ids = list(tokenizer.event_ids.values()) + [tokenizer.eos_id]
|
70 |
-
if disable_patch_change:
|
71 |
-
mask_ids.remove(tokenizer.event_ids["patch_change"])
|
72 |
-
if disable_control_change:
|
73 |
-
mask_ids.remove(tokenizer.event_ids["control_change"])
|
74 |
-
mask[b, mask_ids] = 1
|
75 |
-
else:
|
76 |
-
param_names = tokenizer.events[event_names[b]]
|
77 |
-
if i > len(param_names):
|
78 |
-
mask[b, tokenizer.pad_id] = 1
|
79 |
-
continue
|
80 |
-
param_name = param_names[i - 1]
|
81 |
-
mask_ids = tokenizer.parameter_ids[param_name]
|
82 |
-
if param_name == "channel":
|
83 |
-
mask_ids = [i for i in mask_ids if i not in disable_channels]
|
84 |
-
mask[b, mask_ids] = 1
|
85 |
-
mask = mask.unsqueeze(1)
|
86 |
-
x = next_token_seq
|
87 |
-
if i != 0:
|
88 |
-
hidden = None
|
89 |
-
x = x[:, -1:]
|
90 |
-
logits = model.forward_token(hidden, x, cache=cache2)[:, -1:]
|
91 |
-
scores = torch.softmax(logits / temp, dim=-1) * mask
|
92 |
-
samples = model.sample_top_p_k(scores, top_p, top_k, generator=generator)
|
93 |
-
if i == 0:
|
94 |
-
next_token_seq = samples
|
95 |
-
for b in range(batch_size):
|
96 |
-
if end[b]:
|
97 |
-
continue
|
98 |
-
eid = samples[b].item()
|
99 |
-
if eid == tokenizer.eos_id:
|
100 |
-
end[b] = True
|
101 |
-
else:
|
102 |
-
event_names[b] = tokenizer.id_events[eid]
|
103 |
-
else:
|
104 |
-
next_token_seq = torch.cat([next_token_seq, samples], dim=1)
|
105 |
-
if all([len(tokenizer.events[event_names[b]]) == i for b in range(batch_size) if not end[b]]):
|
106 |
-
break
|
107 |
-
if next_token_seq.shape[1] < max_token_seq:
|
108 |
-
next_token_seq = F.pad(next_token_seq, (0, max_token_seq - next_token_seq.shape[1]),
|
109 |
-
"constant", value=tokenizer.pad_id)
|
110 |
-
next_token_seq = next_token_seq.unsqueeze(1)
|
111 |
-
input_tensor = torch.cat([input_tensor, next_token_seq], dim=1)
|
112 |
-
past_len = cur_len
|
113 |
-
cur_len += 1
|
114 |
-
bar.update(1)
|
115 |
-
yield next_token_seq[:, 0].cpu().numpy()
|
116 |
-
if all(end):
|
117 |
-
break
|
118 |
-
|
119 |
-
def create_msg(name, data):
|
120 |
-
return {"name": name, "data": data}
|
121 |
-
|
122 |
-
def send_msgs(msgs):
|
123 |
-
return json.dumps(msgs)
|
124 |
-
|
125 |
-
def get_duration(model_name, tab, mid_seq, continuation_state, continuation_select, instruments, drum_kit, bpm,
|
126 |
-
time_sig, key_sig, mid, midi_events, reduce_cc_st, remap_track_channel, add_default_instr,
|
127 |
-
remove_empty_channels, seed, seed_rand, gen_events, temp, top_p, top_k, allow_cc):
|
128 |
-
t = gen_events // 23
|
129 |
-
if "large" in model_name:
|
130 |
-
t = gen_events // 14
|
131 |
-
return t + 5
|
132 |
-
|
133 |
-
@spaces.GPU(duration=get_duration)
|
134 |
-
def run(model_name, tab, mid_seq, continuation_state, continuation_select, instruments, drum_kit, bpm, time_sig,
|
135 |
-
key_sig, mid, midi_events, reduce_cc_st, remap_track_channel, add_default_instr, remove_empty_channels,
|
136 |
-
seed, seed_rand, gen_events, temp, top_p, top_k, allow_cc):
|
137 |
-
model = models[model_name]
|
138 |
-
model.to(device=opt.device)
|
139 |
-
tokenizer = model.tokenizer
|
140 |
-
bpm = int(bpm)
|
141 |
-
if time_sig == "auto":
|
142 |
-
time_sig = None
|
143 |
-
time_sig_nn = 4
|
144 |
-
time_sig_dd = 2
|
145 |
-
else:
|
146 |
-
time_sig_nn, time_sig_dd = time_sig.split('/')
|
147 |
-
time_sig_nn = int(time_sig_nn)
|
148 |
-
time_sig_dd = {2: 1, 4: 2, 8: 3}[int(time_sig_dd)]
|
149 |
-
if key_sig == 0:
|
150 |
-
key_sig = None
|
151 |
-
key_sig_sf = 0
|
152 |
-
key_sig_mi = 0
|
153 |
-
else:
|
154 |
-
key_sig = (key_sig - 1)
|
155 |
-
key_sig_sf = key_sig // 2 - 7
|
156 |
-
key_sig_mi = key_sig % 2
|
157 |
-
gen_events = int(gen_events)
|
158 |
-
max_len = gen_events
|
159 |
-
if seed_rand:
|
160 |
-
seed = random.randint(0, MAX_SEED)
|
161 |
-
generator = torch.Generator(opt.device).manual_seed(seed)
|
162 |
-
disable_patch_change = False
|
163 |
-
disable_channels = None
|
164 |
-
if tab == 0:
|
165 |
-
i = 0
|
166 |
-
mid = [[tokenizer.bos_id] + [tokenizer.pad_id] * (tokenizer.max_token_seq - 1)]
|
167 |
-
if tokenizer.version == "v2":
|
168 |
-
if time_sig is not None:
|
169 |
-
mid.append(tokenizer.event2tokens(["time_signature", 0, 0, 0, time_sig_nn - 1, time_sig_dd - 1]))
|
170 |
-
if key_sig is not None:
|
171 |
-
mid.append(tokenizer.event2tokens(["key_signature", 0, 0, 0, key_sig_sf + 7, key_sig_mi]))
|
172 |
-
if bpm != 0:
|
173 |
-
mid.append(tokenizer.event2tokens(["set_tempo", 0, 0, 0, bpm]))
|
174 |
-
patches = {}
|
175 |
-
if instruments is None:
|
176 |
-
instruments = []
|
177 |
-
for instr in instruments:
|
178 |
-
patches[i] = patch2number[instr]
|
179 |
-
i = (i + 1) if i != 8 else 10
|
180 |
-
if drum_kit != "None":
|
181 |
-
patches[9] = drum_kits2number[drum_kit]
|
182 |
-
for i, (c, p) in enumerate(patches.items()):
|
183 |
-
mid.append(tokenizer.event2tokens(["patch_change", 0, 0, i + 1, c, p]))
|
184 |
-
mid = np.asarray([mid] * OUTPUT_BATCH_SIZE, dtype=np.int64)
|
185 |
-
mid_seq = mid.tolist()
|
186 |
-
if len(instruments) > 0:
|
187 |
-
disable_patch_change = True
|
188 |
-
disable_channels = [i for i in range(16) if i not in patches]
|
189 |
-
elif tab == 1 and mid is not None:
|
190 |
-
eps = 4 if reduce_cc_st else 0
|
191 |
-
mid = tokenizer.tokenize(MIDI.midi2score(mid), cc_eps=eps, tempo_eps=eps,
|
192 |
-
remap_track_channel=remap_track_channel,
|
193 |
-
add_default_instr=add_default_instr,
|
194 |
-
remove_empty_channels=remove_empty_channels)
|
195 |
-
mid = mid[:int(midi_events)]
|
196 |
-
mid = np.asarray([mid] * OUTPUT_BATCH_SIZE, dtype=np.int64)
|
197 |
-
mid_seq = mid.tolist()
|
198 |
-
elif tab == 2 and mid_seq is not None:
|
199 |
-
mid = np.asarray(mid_seq, dtype=np.int64)
|
200 |
-
if continuation_select > 0:
|
201 |
-
continuation_state.append(mid_seq)
|
202 |
-
mid = np.repeat(mid[continuation_select - 1:continuation_select], repeats=OUTPUT_BATCH_SIZE, axis=0)
|
203 |
-
mid_seq = mid.tolist()
|
204 |
-
else:
|
205 |
-
continuation_state.append(mid.shape[1])
|
206 |
-
else:
|
207 |
-
continuation_state = [0]
|
208 |
-
mid = [[tokenizer.bos_id] + [tokenizer.pad_id] * (tokenizer.max_token_seq - 1)]
|
209 |
-
mid = np.asarray([mid] * OUTPUT_BATCH_SIZE, dtype=np.int64)
|
210 |
-
mid_seq = mid.tolist()
|
211 |
-
|
212 |
-
if mid is not None:
|
213 |
-
max_len += mid.shape[1]
|
214 |
-
|
215 |
-
init_msgs = [create_msg("progress", [0, gen_events])]
|
216 |
-
if not (tab == 2 and continuation_select == 0):
|
217 |
-
for i in range(OUTPUT_BATCH_SIZE):
|
218 |
-
events = [tokenizer.tokens2event(tokens) for tokens in mid_seq[i]]
|
219 |
-
init_msgs += [create_msg("visualizer_clear", [i, tokenizer.version]),
|
220 |
-
create_msg("visualizer_append", [i, events])]
|
221 |
-
yield mid_seq, continuation_state, seed, send_msgs(init_msgs)
|
222 |
-
midi_generator = generate(model, mid, batch_size=OUTPUT_BATCH_SIZE, max_len=max_len, temp=temp,
|
223 |
-
top_p=top_p, top_k=top_k, disable_patch_change=disable_patch_change,
|
224 |
-
disable_control_change=not allow_cc, disable_channels=disable_channels,
|
225 |
-
generator=generator)
|
226 |
-
events = [list() for i in range(OUTPUT_BATCH_SIZE)]
|
227 |
-
t = time.time() + 1
|
228 |
-
for i, token_seqs in enumerate(midi_generator):
|
229 |
-
token_seqs = token_seqs.tolist()
|
230 |
-
for j in range(OUTPUT_BATCH_SIZE):
|
231 |
-
token_seq = token_seqs[j]
|
232 |
-
mid_seq[j].append(token_seq)
|
233 |
-
events[j].append(tokenizer.tokens2event(token_seq))
|
234 |
-
if time.time() - t > 0.5:
|
235 |
-
msgs = [create_msg("progress", [i + 1, gen_events])]
|
236 |
-
for j in range(OUTPUT_BATCH_SIZE):
|
237 |
-
msgs += [create_msg("visualizer_append", [j, events[j]])]
|
238 |
-
events[j] = list()
|
239 |
-
yield mid_seq, continuation_state, seed, send_msgs(msgs)
|
240 |
-
t = time.time()
|
241 |
-
yield mid_seq, continuation_state, seed, send_msgs([])
|
242 |
-
|
243 |
-
def finish_run(model_name, mid_seq):
|
244 |
-
if mid_seq is None:
|
245 |
-
outputs = [None] * OUTPUT_BATCH_SIZE
|
246 |
-
return *outputs, []
|
247 |
-
tokenizer = models[model_name].tokenizer
|
248 |
-
outputs = []
|
249 |
-
end_msgs = [create_msg("progress", [0, 0])]
|
250 |
-
if not os.path.exists("outputs"):
|
251 |
-
os.mkdir("outputs")
|
252 |
-
for i in range(OUTPUT_BATCH_SIZE):
|
253 |
-
events = [tokenizer.tokens2event(tokens) for tokens in mid_seq[i]]
|
254 |
-
mid = tokenizer.detokenize(mid_seq[i])
|
255 |
-
with open(f"outputs/output{i + 1}.mid", 'wb') as f:
|
256 |
-
f.write(MIDI.score2midi(mid))
|
257 |
-
outputs.append(f"outputs/output{i + 1}.mid")
|
258 |
-
end_msgs += [create_msg("visualizer_clear", [i, tokenizer.version]),
|
259 |
-
create_msg("visualizer_append", [i, events]),
|
260 |
-
create_msg("visualizer_end", i)]
|
261 |
-
return *outputs, send_msgs(end_msgs)
|
262 |
-
|
263 |
-
def synthesis_task(mid):
|
264 |
-
return synthesizer.synthesis(MIDI.score2opus(mid))
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
def render_audio(model_name, mid_seq, should_render_audio):
|
269 |
-
if (not should_render_audio) or mid_seq is None:
|
270 |
-
outputs = [None] * OUTPUT_BATCH_SIZE
|
271 |
-
return tuple(outputs)
|
272 |
-
tokenizer = models[model_name].tokenizer
|
273 |
-
outputs = []
|
274 |
-
if not os.path.exists("outputs"):
|
275 |
-
os.mkdir("outputs")
|
276 |
-
audio_futures = []
|
277 |
-
for i in range(OUTPUT_BATCH_SIZE):
|
278 |
-
mid = tokenizer.detokenize(mid_seq[i])
|
279 |
-
audio_future = thread_pool.submit(synthesis_task, mid)
|
280 |
-
audio_futures.append(audio_future)
|
281 |
-
for future in audio_futures:
|
282 |
-
outputs.append((44100, future.result()))
|
283 |
-
if OUTPUT_BATCH_SIZE == 1:
|
284 |
-
return outputs[0]
|
285 |
-
return tuple(outputs)
|
286 |
-
|
287 |
-
|
288 |
-
def undo_continuation(model_name, mid_seq, continuation_state):
|
289 |
-
if mid_seq is None or len(continuation_state) < 2:
|
290 |
-
return mid_seq, continuation_state, send_msgs([])
|
291 |
-
tokenizer = models[model_name].tokenizer
|
292 |
-
if isinstance(continuation_state[-1], list):
|
293 |
-
mid_seq = continuation_state[-1]
|
294 |
-
else:
|
295 |
-
mid_seq = [ms[:continuation_state[-1]] for ms in mid_seq]
|
296 |
-
continuation_state = continuation_state[:-1]
|
297 |
-
end_msgs = [create_msg("progress", [0, 0])]
|
298 |
-
for i in range(OUTPUT_BATCH_SIZE):
|
299 |
-
events = [tokenizer.tokens2event(tokens) for tokens in mid_seq[i]]
|
300 |
-
end_msgs += [create_msg("visualizer_clear", [i, tokenizer.version]),
|
301 |
-
create_msg("visualizer_append", [i, events]),
|
302 |
-
create_msg("visualizer_end", i)]
|
303 |
-
return mid_seq, continuation_state, send_msgs(end_msgs)
|
304 |
-
|
305 |
-
|
306 |
-
def load_javascript(dir="javascript"):
|
307 |
-
scripts_list = glob.glob(f"{dir}/*.js")
|
308 |
-
javascript = ""
|
309 |
-
for path in scripts_list:
|
310 |
-
with open(path, "r", encoding="utf8") as jsfile:
|
311 |
-
js_content = jsfile.read()
|
312 |
-
js_content = js_content.replace("const MIDI_OUTPUT_BATCH_SIZE=4;",
|
313 |
-
f"const MIDI_OUTPUT_BATCH_SIZE={OUTPUT_BATCH_SIZE};")
|
314 |
-
javascript += f"\n<!-- {path} --><script>{js_content}</script>"
|
315 |
-
template_response_ori = gr.routes.templates.TemplateResponse
|
316 |
-
|
317 |
-
def template_response(*args, **kwargs):
|
318 |
-
res = template_response_ori(*args, **kwargs)
|
319 |
-
res.body = res.body.replace(
|
320 |
-
b'</head>', f'{javascript}</head>'.encode("utf8"))
|
321 |
-
res.init_headers()
|
322 |
-
return res
|
323 |
-
|
324 |
-
gr.routes.templates.TemplateResponse = template_response
|
325 |
-
|
326 |
-
|
327 |
-
def hf_hub_download_retry(repo_id, filename):
|
328 |
-
print(f"downloading {repo_id} {filename}")
|
329 |
-
retry = 0
|
330 |
-
err = None
|
331 |
-
while retry < 30:
|
332 |
-
try:
|
333 |
-
return hf_hub_download(repo_id=repo_id, filename=filename)
|
334 |
-
except Exception as e:
|
335 |
-
err = e
|
336 |
-
retry += 1
|
337 |
-
if err:
|
338 |
-
raise err
|
339 |
-
|
340 |
-
|
341 |
-
number2drum_kits = {-1: "None", 0: "Standard", 8: "Room", 16: "Power", 24: "Electric", 25: "TR-808", 32: "Jazz",
|
342 |
-
40: "Blush", 48: "Orchestra"}
|
343 |
-
patch2number = {v: k for k, v in MIDI.Number2patch.items()}
|
344 |
-
drum_kits2number = {v: k for k, v in number2drum_kits.items()}
|
345 |
-
key_signatures = ['C♭', 'A♭m', 'G♭', 'E♭m', 'D♭', 'B♭m', 'A♭', 'Fm', 'E♭', 'Cm', 'B♭', 'Gm', 'F', 'Dm',
|
346 |
-
'C', 'Am', 'G', 'Em', 'D', 'Bm', 'A', 'F♯m', 'E', 'C♯m', 'B', 'G♯m', 'F♯', 'D♯m', 'C♯', 'A♯m']
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
mid = tokenizer.detokenize(mid_seq[i])
|
354 |
-
audio_future = thread_pool.submit(synthesis_task, mid)
|
355 |
-
audio_futures.append(audio_future)
|
356 |
-
for future in audio_futures:
|
357 |
-
outputs.append((44100, future.result()))
|
358 |
-
if OUTPUT_BATCH_SIZE == 1:
|
359 |
-
return outputs[0]
|
360 |
-
return tuple(outputs)
|
361 |
-
|
362 |
-
def undo_continuation(model_name, mid_seq, continuation_state):
|
363 |
-
if mid_seq is None or len(continuation_state) < 2:
|
364 |
-
return mid_seq, continuation_state, send_msgs([])
|
365 |
-
tokenizer = models[model_name].tokenizer
|
366 |
-
if isinstance(continuation_state[-1], list):
|
367 |
-
mid_seq = continuation_state[-1]
|
368 |
-
else:
|
369 |
-
mid_seq = [ms[:continuation_state[-1]] for ms in mid_seq]
|
370 |
-
continuation_state = continuation_state[:-1]
|
371 |
-
end_msgs = [create_msg("progress", [0, 0])]
|
372 |
-
for i in range(OUTPUT_BATCH_SIZE):
|
373 |
-
events = [tokenizer.tokens2event(tokens) for tokens in mid_seq[i]]
|
374 |
-
end_msgs += [create_msg("visualizer_clear", [i, tokenizer.version]),
|
375 |
-
create_msg("visualizer_append", [i, events]),
|
376 |
-
create_msg("visualizer_end", i)]
|
377 |
-
return mid_seq, continuation_state, send_msgs(end_msgs)
|
378 |
-
|
379 |
-
def create_arpeggio_events(chord, pattern, duration=480):
|
380 |
-
events = []
|
381 |
-
notes = {
|
382 |
-
'C': [60, 64, 67],
|
383 |
-
'D': [62, 66, 69],
|
384 |
-
'Am': [57, 60, 64],
|
385 |
-
'G': [55, 59, 62]
|
386 |
-
}
|
387 |
-
|
388 |
-
for step in pattern:
|
389 |
-
note = notes[chord][step]
|
390 |
-
events.extend([
|
391 |
-
['note_on', 0, 0, 0, 0, note, 80],
|
392 |
-
['note_off', duration, 0, 0, 0, note, 0]
|
393 |
-
])
|
394 |
-
|
395 |
-
return events
|
396 |
-
|
397 |
-
def add_arpeggio_sequence(tokenizer, mid_seq, sequence, pattern):
|
398 |
-
events = []
|
399 |
-
for chord in sequence:
|
400 |
-
events.extend(create_arpeggio_events(chord, pattern))
|
401 |
-
|
402 |
-
tokens = [tokenizer.event2tokens(event) for event in events]
|
403 |
-
mid_seq[0].extend(tokens)
|
404 |
-
return mid_seq
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
if __name__ == "__main__":
|
409 |
-
parser = argparse.ArgumentParser()
|
410 |
-
parser.add_argument("--share", action="store_true", default=False, help="share gradio app")
|
411 |
-
parser.add_argument("--port", type=int, default=7860, help="gradio server port")
|
412 |
-
parser.add_argument("--device", type=str, default="cuda", help="device to run model")
|
413 |
-
parser.add_argument("--batch", type=int, default=8, help="batch size")
|
414 |
-
parser.add_argument("--max-gen", type=int, default=1024, help="max")
|
415 |
-
opt = parser.parse_args()
|
416 |
-
OUTPUT_BATCH_SIZE = opt.batch
|
417 |
-
soundfont_path = hf_hub_download_retry(repo_id="skytnt/midi-model", filename="soundfont.sf2")
|
418 |
-
thread_pool = ThreadPoolExecutor(max_workers=OUTPUT_BATCH_SIZE)
|
419 |
-
synthesizer = MidiSynthesizer(soundfont_path)
|
420 |
-
models_info = {
|
421 |
-
"generic pretrain model (tv2o-medium) by skytnt": [
|
422 |
-
"skytnt/midi-model-tv2o-medium", {
|
423 |
-
"jpop": "skytnt/midi-model-tv2om-jpop-lora",
|
424 |
-
"touhou": "skytnt/midi-model-tv2om-touhou-lora"
|
425 |
-
}
|
426 |
-
],
|
427 |
-
"generic pretrain model (tv2o-large) by asigalov61": [
|
428 |
-
"asigalov61/Music-Llama", {}
|
429 |
-
],
|
430 |
-
"generic pretrain model (tv2o-medium) by asigalov61": [
|
431 |
-
"asigalov61/Music-Llama-Medium", {}
|
432 |
-
],
|
433 |
-
"generic pretrain model (tv1-medium) by skytnt": [
|
434 |
-
"skytnt/midi-model", {}
|
435 |
-
]
|
436 |
-
}
|
437 |
-
models = {}
|
438 |
-
if opt.device == "cuda":
|
439 |
-
torch.backends.cudnn.deterministic = True
|
440 |
-
torch.backends.cudnn.benchmark = False
|
441 |
-
torch.backends.cuda.matmul.allow_tf32 = True
|
442 |
-
torch.backends.cudnn.allow_tf32 = True
|
443 |
-
torch.backends.cuda.enable_mem_efficient_sdp(True)
|
444 |
-
torch.backends.cuda.enable_flash_sdp(True)
|
445 |
-
for name, (repo_id, loras) in models_info.items():
|
446 |
-
model = MIDIModel.from_pretrained(repo_id)
|
447 |
-
model.to(device="cpu", dtype=torch.float32)
|
448 |
-
models[name] = model
|
449 |
-
for lora_name, lora_repo in loras.items():
|
450 |
-
model = MIDIModel.from_pretrained(repo_id)
|
451 |
-
print(f"loading lora {lora_repo} for {name}")
|
452 |
-
model = model.load_merge_lora(lora_repo)
|
453 |
-
model.to(device="cpu", dtype=torch.float32)
|
454 |
-
models[f"{name} with {lora_name} lora"] = model
|
455 |
-
|
456 |
-
load_javascript()
|
457 |
-
app = gr.Blocks(theme=gr.themes.Soft())
|
458 |
-
with app:
|
459 |
-
gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Midi Composer with Arpeggios</h1>")
|
460 |
-
js_msg = gr.Textbox(elem_id="msg_receiver", visible=False)
|
461 |
-
js_msg.change(None, [js_msg], [], js="""
|
462 |
-
(msg_json) =>{
|
463 |
-
let msgs = JSON.parse(msg_json);
|
464 |
-
executeCallbacks(msgReceiveCallbacks, msgs);
|
465 |
-
return [];
|
466 |
-
}
|
467 |
-
""")
|
468 |
-
input_model = gr.Dropdown(label="select model", choices=list(models.keys()),
|
469 |
-
type="value", value=list(models.keys())[0])
|
470 |
-
tab_select = gr.State(value=0)
|
471 |
-
|
472 |
-
|
473 |
-
|
474 |
-
|
475 |
-
with gr.Tabs():
|
476 |
-
with gr.TabItem("custom prompt") as tab1:
|
477 |
-
input_instruments = gr.Dropdown(label="🪗instruments (auto if empty)", choices=list(patch2number.keys()),
|
478 |
-
multiselect=True, max_choices=15, type="value")
|
479 |
-
input_drum_kit = gr.Dropdown(label="🥁drum kit", choices=list(drum_kits2number.keys()), type="value",
|
480 |
-
value="None")
|
481 |
-
input_bpm = gr.Slider(label="BPM (beats per minute, auto if 0)", minimum=0, maximum=255,
|
482 |
-
step=1, value=0)
|
483 |
-
input_time_sig = gr.Radio(label="time signature (only for tv2 models)",
|
484 |
-
value="auto",
|
485 |
-
choices=["auto", "4/4", "2/4", "3/4", "6/4", "7/4",
|
486 |
-
"2/2", "3/2", "4/2", "3/8", "5/8", "6/8", "7/8", "9/8", "12/8"])
|
487 |
-
input_key_sig = gr.Radio(label="key signature (only for tv2 models)",
|
488 |
-
value="auto",
|
489 |
-
choices=["auto"] + key_signatures,
|
490 |
-
type="index")
|
491 |
-
|
492 |
-
with gr.Row():
|
493 |
-
arpeggio_intro = gr.Button("🎵 Intro Arpeggio", variant="primary")
|
494 |
-
arpeggio_verse = gr.Button("🎸 Verse Arpeggio", variant="primary")
|
495 |
-
arpeggio_chorus = gr.Button("🎹 Chorus Arpeggio", variant="primary")
|
496 |
-
arpeggio_outro = gr.Button("🎷 Outro Arpeggio", variant="primary")
|
497 |
-
|
498 |
-
|
499 |
-
|
500 |
-
example1 = gr.Examples([
|
501 |
-
[[], "None"],
|
502 |
-
[["Acoustic Grand"], "None"],
|
503 |
-
[['Acoustic Grand', 'SynthStrings 2', 'SynthStrings 1', 'Pizzicato Strings',
|
504 |
-
'Pad 2 (warm)', 'Tremolo Strings', 'String Ensemble 1'], "Orchestra"],
|
505 |
-
[['Trumpet', 'Oboe', 'Trombone', 'String Ensemble 1', 'Clarinet',
|
506 |
-
'French Horn', 'Pad 4 (choir)', 'Bassoon', 'Flute'], "None"],
|
507 |
-
[['Flute', 'French Horn', 'Clarinet', 'String Ensemble 2', 'English Horn', 'Bassoon',
|
508 |
-
'Oboe', 'Pizzicato Strings'], "Orchestra"],
|
509 |
-
[['Electric Piano 2', 'Lead 5 (charang)', 'Electric Bass(pick)', 'Lead 2 (sawtooth)',
|
510 |
-
'Pad 1 (new age)', 'Orchestra Hit', 'Cello', 'Electric Guitar(clean)'], "Standard"],
|
511 |
-
[["Electric Guitar(clean)", "Electric Guitar(muted)", "Overdriven Guitar", "Distortion Guitar",
|
512 |
-
"Electric Bass(finger)"], "Standard"]
|
513 |
-
], [input_instruments, input_drum_kit])
|
514 |
-
|
515 |
-
with gr.TabItem("midi prompt") as tab2:
|
516 |
-
input_midi = gr.File(label="input midi", file_types=[".midi", ".mid"], type="binary")
|
517 |
-
input_midi_events = gr.Slider(label="use first n midi events as prompt", minimum=1, maximum=512,
|
518 |
-
step=1,
|
519 |
-
value=128)
|
520 |
-
input_reduce_cc_st = gr.Checkbox(label="reduce control_change and set_tempo events", value=True)
|
521 |
-
input_remap_track_channel = gr.Checkbox(
|
522 |
-
label="remap tracks and channels so each track has only one channel and in order", value=True)
|
523 |
-
input_add_default_instr = gr.Checkbox(
|
524 |
-
label="add a default instrument to channels that don't have an instrument", value=True)
|
525 |
-
input_remove_empty_channels = gr.Checkbox(label="remove channels without notes", value=False)
|
526 |
-
example2 = gr.Examples([[file, 128] for file in glob.glob("example/*.mid")],
|
527 |
-
[input_midi, input_midi_events])
|
528 |
-
|
529 |
-
with gr.TabItem("last output prompt") as tab3:
|
530 |
-
gr.Markdown("Continue generating on the last output.")
|
531 |
-
input_continuation_select = gr.Radio(label="select output to continue generating", value="all",
|
532 |
-
choices=["all"] + [f"output{i + 1}" for i in
|
533 |
-
range(OUTPUT_BATCH_SIZE)],
|
534 |
-
type="index"
|
535 |
-
)
|
536 |
-
undo_btn = gr.Button("undo the last continuation")
|
537 |
-
|
538 |
-
|
539 |
-
|
540 |
-
|
541 |
-
def add_intro_arpeggio(model_name, mid_seq):
|
542 |
-
tokenizer = models[model_name].tokenizer
|
543 |
-
sequence = ['C', 'D', 'Am', 'G']
|
544 |
-
pattern = [0, 1, 2, 1] # Root, Third, Fifth, Third
|
545 |
-
return add_arpeggio_sequence(tokenizer, mid_seq, sequence, pattern)
|
546 |
-
|
547 |
-
def add_verse_arpeggio(model_name, mid_seq):
|
548 |
-
tokenizer = models[model_name].tokenizer
|
549 |
-
sequence = ['D', 'C', 'Am', 'G']
|
550 |
-
pattern = [0, 2, 1, 2] # Root, Fifth, Third, Fifth
|
551 |
-
return add_arpeggio_sequence(tokenizer, mid_seq, sequence, pattern)
|
552 |
-
|
553 |
-
def add_chorus_arpeggio(model_name, mid_seq):
|
554 |
-
tokenizer = models[model_name].tokenizer
|
555 |
-
sequence = ['G', 'D', 'Am', 'C']
|
556 |
-
pattern = [0, 1, 2, 1, 0, 2] # Root, Third, Fifth, Third, Root, Fifth
|
557 |
-
return add_arpeggio_sequence(tokenizer, mid_seq, sequence, pattern)
|
558 |
-
|
559 |
-
def add_outro_arpeggio(model_name, mid_seq):
|
560 |
-
tokenizer = models[model_name].tokenizer
|
561 |
-
sequence = ['Am', 'G', 'D', 'C']
|
562 |
-
pattern = [2, 1, 0, 1] # Fifth, Third, Root, Third
|
563 |
-
return add_arpeggio_sequence(tokenizer, mid_seq, sequence, pattern)
|
564 |
-
|
565 |
-
arpeggio_intro.click(add_intro_arpeggio, [input_model, output_midi_seq], output_midi_seq)
|
566 |
-
arpeggio_verse.click(add_verse_arpeggio, [input_model, output_midi_seq], output_midi_seq)
|
567 |
-
arpeggio_chorus.click(add_chorus_arpeggio, [input_model, output_midi_seq], output_midi_seq)
|
568 |
-
arpeggio_outro.click(add_outro_arpeggio, [input_model, output_midi_seq], output_midi_seq)
|
569 |
-
|
570 |
-
|
571 |
-
|
572 |
-
|
573 |
-
tab1.select(lambda: 0, None, tab_select, queue=False)
|
574 |
-
tab2.select(lambda: 1, None, tab_select, queue=False)
|
575 |
-
tab3.select(lambda: 2, None, tab_select, queue=False)
|
576 |
-
input_seed = gr.Slider(label="seed", minimum=0, maximum=2 ** 31 - 1,
|
577 |
-
step=1, value=0)
|
578 |
-
input_seed_rand = gr.Checkbox(label="random seed", value=True)
|
579 |
-
input_gen_events = gr.Slider(label="generate max n midi events", minimum=1, maximum=opt.max_gen,
|
580 |
-
step=1, value=opt.max_gen // 2)
|
581 |
-
with gr.Accordion("options", open=False):
|
582 |
-
input_temp = gr.Slider(label="temperature", minimum=0.1, maximum=1.2, step=0.01, value=1)
|
583 |
-
input_top_p = gr.Slider(label="top p", minimum=0.1, maximum=1, step=0.01, value=0.95)
|
584 |
-
input_top_k = gr.Slider(label="top k", minimum=1, maximum=128, step=1, value=20)
|
585 |
-
input_allow_cc = gr.Checkbox(label="allow midi cc event", value=True)
|
586 |
-
input_render_audio = gr.Checkbox(label="render audio after generation", value=True)
|
587 |
-
example3 = gr.Examples([[1, 0.94, 128], [1, 0.98, 20], [1, 0.98, 12]],
|
588 |
-
[input_temp, input_top_p, input_top_k])
|
589 |
-
run_btn = gr.Button("generate", variant="primary")
|
590 |
-
# stop_btn = gr.Button("stop and output")
|
591 |
-
output_midi_seq = gr.State()
|
592 |
-
output_continuation_state = gr.State([0])
|
593 |
-
midi_outputs = []
|
594 |
-
audio_outputs = []
|
595 |
-
with gr.Tabs(elem_id="output_tabs"):
|
596 |
-
for i in range(OUTPUT_BATCH_SIZE):
|
597 |
-
with gr.TabItem(f"output {i + 1}") as tab1:
|
598 |
-
output_midi_visualizer = gr.HTML(elem_id=f"midi_visualizer_container_{i}")
|
599 |
-
output_audio = gr.Audio(label="output audio", format="mp3", elem_id=f"midi_audio_{i}")
|
600 |
-
output_midi = gr.File(label="output midi", file_types=[".mid"])
|
601 |
-
midi_outputs.append(output_midi)
|
602 |
-
audio_outputs.append(output_audio)
|
603 |
-
run_event = run_btn.click(run, [input_model, tab_select, output_midi_seq, output_continuation_state,
|
604 |
-
input_continuation_select, input_instruments, input_drum_kit, input_bpm,
|
605 |
-
input_time_sig, input_key_sig, input_midi, input_midi_events,
|
606 |
-
input_reduce_cc_st, input_remap_track_channel,
|
607 |
-
input_add_default_instr, input_remove_empty_channels,
|
608 |
-
input_seed, input_seed_rand, input_gen_events, input_temp, input_top_p,
|
609 |
-
input_top_k, input_allow_cc],
|
610 |
-
[output_midi_seq, output_continuation_state, input_seed, js_msg],
|
611 |
-
concurrency_limit=10, queue=True)
|
612 |
-
finish_run_event = run_event.then(fn=finish_run,
|
613 |
-
inputs=[input_model, output_midi_seq],
|
614 |
-
outputs=midi_outputs + [js_msg],
|
615 |
-
queue=False)
|
616 |
-
finish_run_event.then(fn=render_audio,
|
617 |
-
inputs=[input_model, output_midi_seq, input_render_audio],
|
618 |
-
outputs=audio_outputs,
|
619 |
-
queue=False)
|
620 |
-
# stop_btn.click(None, [], [], cancels=run_event,
|
621 |
-
# queue=False)
|
622 |
-
undo_btn.click(undo_continuation, [input_model, output_midi_seq, output_continuation_state],
|
623 |
-
[output_midi_seq, output_continuation_state, js_msg], queue=False)
|
624 |
-
|
625 |
-
|
626 |
-
|
627 |
-
app.queue().launch(server_port=opt.port, share=opt.share, inbrowser=True, ssr_mode=False)
|
628 |
-
thread_pool.shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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