asigalov61 commited on
Commit
b6bb234
1 Parent(s): e59bba8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +222 -0
app.py ADDED
@@ -0,0 +1,222 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import glob
3
+ import os.path
4
+
5
+ import torch
6
+ import torch.nn.functional as F
7
+
8
+ import gradio as gr
9
+
10
+ import onnxruntime as rt
11
+ import tqdm
12
+
13
+ from midi_synthesizer import synthesis
14
+ import TMIDIX
15
+
16
+ import matplotlib.pyplot as plt
17
+
18
+ in_space = os.getenv("SYSTEM") == "spaces"
19
+
20
+ #=================================================================================================
21
+
22
+ @torch.no_grad()
23
+ def GenerateMIDI(idrums, iinstr, progress=gr.Progress()):
24
+
25
+ if idrums:
26
+ drums = 3074
27
+ else:
28
+ drums = 3073
29
+
30
+ instruments_list = ["Piano", "Guitar", "Bass", "Violin", "Cello", "Harp", "Trumpet", "Sax", "Flute", 'Drums', "Choir", "Organ"]
31
+ first_note_instrument_number = instruments_list.index(iinstr)
32
+
33
+ start_tokens = [3087, drums, 3075+first_note_instrument_number]
34
+
35
+ seq_len = 512
36
+ max_seq_len = 2048
37
+ temperature = 0.9
38
+ verbose=False
39
+ return_prime=False
40
+
41
+
42
+ out = torch.FloatTensor([start_tokens])
43
+
44
+ st = len(start_tokens)
45
+
46
+ if verbose:
47
+ print("Generating sequence of max length:", seq_len)
48
+
49
+ progress(0, desc="Starting...")
50
+ step = 0
51
+
52
+ for i in progress.tqdm(range(seq_len)):
53
+
54
+ try:
55
+ x = out[:, -max_seq_len:]
56
+
57
+ torch_in = x.tolist()[0]
58
+
59
+ logits = torch.FloatTensor(session.run(None, {'input': [torch_in]})[0])[:, -1]
60
+
61
+ probs = F.softmax(logits / temperature, dim=-1)
62
+
63
+ sample = torch.multinomial(probs, 1)
64
+
65
+ out = torch.cat((out, sample), dim=-1)
66
+
67
+ if step % 16 == 0:
68
+ print(step, '/', seq_len)
69
+
70
+ step += 1
71
+
72
+ if step >= seq_len:
73
+ break
74
+
75
+ except Exception as e:
76
+ print('Error', e)
77
+ break
78
+
79
+ if return_prime:
80
+ melody_chords_f = out[:, :]
81
+
82
+ else:
83
+ melody_chords_f = out[:, st:]
84
+
85
+ melody_chords_f = melody_chords_f.tolist()[0]
86
+
87
+ print('=' * 70)
88
+ print('Sample INTs', melody_chords_f[:12])
89
+ print('=' * 70)
90
+
91
+ if len(melody_chords_f) != 0:
92
+
93
+ song = melody_chords_f
94
+ song_f = []
95
+ time = 0
96
+ dur = 0
97
+ vel = 0
98
+ pitch = 0
99
+ channel = 0
100
+
101
+ for ss in song:
102
+
103
+ ss1 = int(ss)
104
+
105
+ if ss1 > 0 and ss1 < 256:
106
+
107
+ time += ss1 * 8
108
+
109
+ if ss1 >= 256 and ss1 < 1280:
110
+
111
+ dur = ((ss1-256) // 8) * 32
112
+ vel = (((ss1-256) % 8)+1) * 15
113
+
114
+ if ss1 >= 1280 and ss1 < 2816:
115
+ channel = (ss1-1280) // 128
116
+ pitch = (ss1-1280) % 128
117
+
118
+ song_f.append(['note', int(time), int(dur), int(channel), int(pitch), int(vel) ])
119
+
120
+ output_signature = 'Allegro Music Transformer'
121
+ output_file_name = 'Allegro-Music-Transformer-Music-Composition'
122
+ track_name='Project Los Angeles'
123
+ list_of_MIDI_patches=[0, 24, 32, 40, 42, 46, 56, 71, 73, 0, 53, 19, 0, 0, 0, 0]
124
+ number_of_ticks_per_quarter=500
125
+ text_encoding='ISO-8859-1'
126
+
127
+ output_header = [number_of_ticks_per_quarter,
128
+ [['track_name', 0, bytes(output_signature, text_encoding)]]]
129
+
130
+ patch_list = [['patch_change', 0, 0, list_of_MIDI_patches[0]],
131
+ ['patch_change', 0, 1, list_of_MIDI_patches[1]],
132
+ ['patch_change', 0, 2, list_of_MIDI_patches[2]],
133
+ ['patch_change', 0, 3, list_of_MIDI_patches[3]],
134
+ ['patch_change', 0, 4, list_of_MIDI_patches[4]],
135
+ ['patch_change', 0, 5, list_of_MIDI_patches[5]],
136
+ ['patch_change', 0, 6, list_of_MIDI_patches[6]],
137
+ ['patch_change', 0, 7, list_of_MIDI_patches[7]],
138
+ ['patch_change', 0, 8, list_of_MIDI_patches[8]],
139
+ ['patch_change', 0, 9, list_of_MIDI_patches[9]],
140
+ ['patch_change', 0, 10, list_of_MIDI_patches[10]],
141
+ ['patch_change', 0, 11, list_of_MIDI_patches[11]],
142
+ ['patch_change', 0, 12, list_of_MIDI_patches[12]],
143
+ ['patch_change', 0, 13, list_of_MIDI_patches[13]],
144
+ ['patch_change', 0, 14, list_of_MIDI_patches[14]],
145
+ ['patch_change', 0, 15, list_of_MIDI_patches[15]],
146
+ ['track_name', 0, bytes(track_name, text_encoding)]]
147
+
148
+ output = output_header + [patch_list + song_f]
149
+
150
+ midi_data = TMIDIX.score2midi(output, text_encoding)
151
+
152
+ with open(f"Allegro-Music-Transformer-Music-Composition.mid", 'wb') as f:
153
+ f.write(midi_data)
154
+
155
+ output1 = []
156
+ itrack = 1
157
+
158
+ opus = TMIDIX.score2opus(output)
159
+
160
+ while itrack < len(opus):
161
+ for event in opus[itrack]:
162
+ if (event[0] == 'note_on') or (event[0] == 'note_off'):
163
+ output1.append(event)
164
+ itrack += 1
165
+
166
+ audio = synthesis([500, output1], 'SGM-v2.01-YamahaGrand-Guit-Bass-v2.7.sf2')
167
+
168
+ x = []
169
+ y =[]
170
+ c = []
171
+
172
+ colors = ['red', 'yellow', 'green', 'cyan', 'blue', 'pink', 'orange', 'purple', 'gray', 'white', 'gold', 'silver']
173
+
174
+ for s in song_f:
175
+ x.append(s[1] / 1000)
176
+ y.append(s[4])
177
+ c.append(colors[s[3]])
178
+
179
+ plt.figure(figsize=(14,5))
180
+ ax=plt.axes(title='Allegro Music Transformer')
181
+ ax.set_facecolor('black')
182
+
183
+ plt.scatter(x,y, c=c)
184
+ plt.xlabel("Time")
185
+ plt.ylabel("Pitch")
186
+
187
+ yield [500, output1], plt, "Allegro-Music-Transformer-Music-Composition.mid", (44100, audio)
188
+
189
+ #=================================================================================================
190
+
191
+ if __name__ == "__main__":
192
+
193
+ parser = argparse.ArgumentParser()
194
+ parser.add_argument("--share", action="store_true", default=False, help="share gradio app")
195
+ parser.add_argument("--port", type=int, default=7860, help="gradio server port")
196
+ opt = parser.parse_args()
197
+
198
+ print('Loading model...')
199
+ session = rt.InferenceSession('Allegro_Music_Transformer_Small_Trained_Model_56000_steps_0.9399_loss_0.7374_acc.onnx', providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
200
+ print('Done!')
201
+
202
+ app = gr.Blocks()
203
+ with app:
204
+ gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Allegro Music Transformer</h1>")
205
+ gr.Markdown("![Visitors](https://api.visitorbadge.io/api/visitors?path=asigalov61.Allegro-Music-Transformer&style=flat)\n\n"
206
+ "Full-attention multi-instrumental music transformer featuring asymmetrical encoding with octo-velocity, and chords counters tokens, optimized for speed and performance\n\n"
207
+ "Check out [Allegro Music Transformer](https://github.com/asigalov61/Allegro-Music-Transformer) on GitHub!\n\n"
208
+ "[Open In Colab]"
209
+ "(https://colab.research.google.com/github/asigalov61/Allegro-Music-Transformer/blob/main/Allegro_Music_Transformer_Composer.ipynb)"
210
+ " for faster execution and endless generation"
211
+ )
212
+ input_drums = gr.Checkbox(label="Drums Controls", value = True, info="Drums present or not")
213
+ input_instrument = gr.Radio(["Piano", "Guitar", "Bass", "Violin", "Cello", "Harp", "Trumpet", "Sax", "Flute", "Choir", "Organ"], value="Guitar", label="Lead Instrument Controls", info="Desired lead instrument")
214
+ run_btn = gr.Button("generate", variant="primary")
215
+
216
+ output_midi_seq = gr.Variable()
217
+ output_audio = gr.Audio(label="output audio", format="mp3", elem_id="midi_audio")
218
+ output_plot = gr.Plot(label="output plot")
219
+ output_midi = gr.File(label="output midi", file_types=[".mid"])
220
+ run_event = run_btn.click(GenerateMIDI, [input_drums, input_instrument], [output_midi_seq, output_plot, output_midi, output_audio])
221
+
222
+ app.queue(concurrency_count=1).launch(server_port=opt.port, share=opt.share, inbrowser=True)