asigalov61 commited on
Commit
30ed745
·
verified ·
1 Parent(s): 07c6c95

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -91
app.py CHANGED
@@ -48,7 +48,7 @@ def mode_dur(seq):
48
  def mode_pitch(seq):
49
  return statistics.mode([t % 128 for t in seq if 256 < t < 512])
50
 
51
- parts_dict = sorted(set([str_strip(s[2]).rstrip('-') for s in melody_chords_f]))
52
 
53
  train_data = []
54
 
@@ -58,12 +58,12 @@ for m in tqdm.tqdm(melody_chords_f):
58
 
59
  for tv in range(-3, 3):
60
 
61
- part = str_strip(m[2])
62
- part_tok = parts_dict.index(part)
63
 
64
  score = [t+tv if 256 < t < 512 else t for t in m[5]]
65
 
66
- seq = [916] + [part_tok+512, mode_time(score)+532, mode_dur(score)+660, mode_pitch(score)+tv+788]
67
 
68
  seq += score
69
 
@@ -144,61 +144,6 @@ def Generate_POP_Section(input_parsons_code,
144
 
145
  #===============================================================================
146
 
147
- print('Instantiating Parsons Code Melody Transformer model...')
148
-
149
- SEQ_LEN = 322
150
- PAD_IDX = 392
151
-
152
- model = TransformerWrapper(
153
- num_tokens = PAD_IDX+1,
154
- max_seq_len = SEQ_LEN,
155
- attn_layers = Decoder(dim = 1024,
156
- depth = 4,
157
- heads = 8,
158
- rotary_pos_emb = True,
159
- attn_flash = True
160
- )
161
- )
162
-
163
- model = AutoregressiveWrapper(model, ignore_index = PAD_IDX, pad_value=PAD_IDX)
164
-
165
- print('=' * 70)
166
- print('Loading model checkpoint...')
167
-
168
- model_path = 'Parsons_Code_Melody_Transformer_Trained_Model_13786_steps_0.3058_loss_0.8819_acc.pth'
169
-
170
- model.load_state_dict(torch.load(model_path, map_location='cpu'))
171
-
172
- model.cpu()
173
- model.eval()
174
-
175
- dtype = torch.bfloat16
176
-
177
- ctx = torch.amp.autocast(device_type='cpu', dtype=dtype)
178
-
179
- print('Done!')
180
- print('=' * 70)
181
-
182
- #===============================================================================
183
-
184
- print('Prepping Parsons code string...')
185
-
186
- td_str = re.sub('[^*DRU]', '', input_parsons_code)
187
-
188
- print(len(td_str))
189
- print('=' * 70)
190
-
191
- if '*' in td_str and len(td_str) > 1:
192
- code_mult = (64 // len(td_str[1:]))+1
193
- mult_code = ('*' + (td_str[1:] * code_mult))[:64]
194
-
195
- else:
196
- mult_code = '*UUUUUUUDDDDDDDUUUUUUUDDDDDDDUUUUUUUDDDDDDDUUUUUUUDDDDDDDUUUUUUU'
197
-
198
- pcode = parsons_code_to_tokens(mult_code)
199
-
200
- print('Done!')
201
- print('=' * 70)
202
 
203
  #===============================================================================
204
 
@@ -244,31 +189,42 @@ def Generate_POP_Section(input_parsons_code,
244
  song_f = []
245
 
246
  time = 0
247
- dur = 4
248
  vel = 90
249
- pitch = 60
250
  channel = 0
251
-
252
  for ss in song:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
253
 
254
- if 0 <= ss < 128:
255
-
256
- time += ss * 32
257
-
258
- if 128 <= ss < 256:
259
-
260
- dur = (ss-128) * 32
261
-
262
- if 256 <= ss < 384:
263
-
264
- pitch = ss-256
265
-
266
- song_f.append(['note', time, dur, channel, pitch, vel, 0])
267
-
268
- fn1 = 'Parsons-Code-Melody-Transformer-Composition'
269
 
270
  detailed_stats = TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(song_f,
271
- output_signature = 'Parsons Code Melody Transformer',
272
  output_file_name = fn1,
273
  track_name='Project Los Angeles'
274
  )
@@ -324,15 +280,14 @@ if __name__ == "__main__":
324
 
325
  with app:
326
 
327
- gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Parsons Code Melody Transformer</h1>")
328
- gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Generate unique melodies from Parsons codes</h1>")
329
  gr.Markdown(
330
- "![Visitors](https://api.visitorbadge.io/api/visitors?path=asigalov61.Parsons-Code-Melody-Transformer&style=flat)\n\n"
331
- "This is a demo for Clean Melodies subset of Tegridy MIDI Dataset\n\n"
332
- "Check out [Tegridy MIDI Dataset](https://github.com/asigalov61/Tegridy-MIDI-Dataset) on GitHub!\n\n"
333
  )
334
 
335
- gr.Markdown("## Enter Parsons code:")
336
 
337
  input_parsons_code = gr.Textbox(label="Parsons code",
338
  info="Make sure your Parsons code starts with *",
@@ -340,13 +295,6 @@ if __name__ == "__main__":
340
  value="*"
341
  )
342
 
343
- clr_btn = gr.ClearButton(components=input_parsons_code)
344
-
345
- def reset_pcode():
346
- return '*'
347
-
348
- clr_btn.click(reset_pcode, outputs=input_parsons_code)
349
-
350
  gr.Markdown("## Select generation options:")
351
 
352
  input_first_note_duration = gr.Slider(1, 127, value=15, step=1, label="First note duration value")
 
48
  def mode_pitch(seq):
49
  return statistics.mode([t % 128 for t in seq if 256 < t < 512])
50
 
51
+ sections_dict = sorted(set([str_strip(s[2]).rstrip('-') for s in melody_chords_f]))
52
 
53
  train_data = []
54
 
 
58
 
59
  for tv in range(-3, 3):
60
 
61
+ section = str_strip(m[2])
62
+ section_tok = sections_dict.index(part)
63
 
64
  score = [t+tv if 256 < t < 512 else t for t in m[5]]
65
 
66
+ seq = [916] + [section_tok+512, mode_time(score)+532, mode_dur(score)+660, mode_pitch(score)+tv+788]
67
 
68
  seq += score
69
 
 
144
 
145
  #===============================================================================
146
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
 
148
  #===============================================================================
149
 
 
189
  song_f = []
190
 
191
  time = 0
192
+ dur = 0
193
  vel = 90
194
+ pitch = 0
195
  channel = 0
196
+
197
  for ss in song:
198
+
199
+ if 0 <= ss < 128:
200
+
201
+ time += ss * 32
202
+
203
+ if 128 <= ss < 256:
204
+
205
+ dur = (ss-128)* 32
206
+
207
+ if 256 <= ss < 512:
208
+
209
+ pitch = (ss-256) % 128
210
+ cha = (ss-256) // 128
211
+
212
+ if cha == 0:
213
+ channel = 3
214
+ vel = 110
215
+ patch = 40
216
+
217
+ else:
218
+ channel = 0
219
+ vel = 80
220
+ patch = 0
221
+
222
+ song_f.append(['note', time, dur, channel, pitch, vel, patch ])
223
 
224
+ fn1 = 'Popular-Hook-Transformer-Composition'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
225
 
226
  detailed_stats = TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(song_f,
227
+ output_signature = 'Popular Hook Transformer',
228
  output_file_name = fn1,
229
  track_name='Project Los Angeles'
230
  )
 
280
 
281
  with app:
282
 
283
+ gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Popular Hook Transformer</h1>")
284
+ gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Generate unique POP music sections</h1>")
285
  gr.Markdown(
286
+ "This is a demo for popular-hook MIDI Dataset\n\n"
287
+ "Check out [popular-hook](https://huggingface.co/datasets/NEXTLab-ZJU/popular-hook) on Hugging Face!\n\n"
 
288
  )
289
 
290
+ gr.Markdown("## Select generation options:")
291
 
292
  input_parsons_code = gr.Textbox(label="Parsons code",
293
  info="Make sure your Parsons code starts with *",
 
295
  value="*"
296
  )
297
 
 
 
 
 
 
 
 
298
  gr.Markdown("## Select generation options:")
299
 
300
  input_first_note_duration = gr.Slider(1, 127, value=15, step=1, label="First note duration value")