Spaces:
Running
on
Zero
Running
on
Zero
asigalov61
commited on
Commit
•
e2f25e4
1
Parent(s):
d34cdb3
Update app.py
Browse files
app.py
CHANGED
@@ -19,13 +19,22 @@ in_space = os.getenv("SYSTEM") == "spaces"
|
|
19 |
#=================================================================================================
|
20 |
|
21 |
def generate(
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
out = torch.LongTensor([start_tokens])
|
31 |
|
@@ -41,7 +50,7 @@ def generate(
|
|
41 |
try:
|
42 |
|
43 |
x = out[:, -max_seq_len:]
|
44 |
-
|
45 |
torch_in = x.tolist()[0]
|
46 |
|
47 |
logits = torch.FloatTensor(session.run(None, {'input': [torch_in]})[0])[:, -1]
|
@@ -60,16 +69,7 @@ def generate(
|
|
60 |
|
61 |
else:
|
62 |
return out[:, st:]
|
63 |
-
|
64 |
-
#=================================================================================================
|
65 |
-
|
66 |
-
def create_msg(name, data):
|
67 |
-
return {"name": name, "data": data}
|
68 |
|
69 |
-
def GenerateMIDI():
|
70 |
-
|
71 |
-
melody_chords_f = generate([3087, 3073+1, 3075+1], 512)
|
72 |
-
|
73 |
melody_chords_f = melody_chords_f.tolist()[0]
|
74 |
|
75 |
print('=' * 70)
|
@@ -141,7 +141,7 @@ def GenerateMIDI():
|
|
141 |
|
142 |
audio = synthesis(TMIDIX.score2opus(output), 'SGM-v2.01-YamahaGrand-Guit-Bass-v2.7.sf2')
|
143 |
|
144 |
-
yield output, "Allegro-Music-Transformer-Music-Composition.mid", (44100, audio)
|
145 |
|
146 |
#=================================================================================================
|
147 |
|
@@ -151,38 +151,7 @@ def cancel_run(output_midi_seq):
|
|
151 |
with open(f"Allegro-Music-Transformer-Music-Composition.mid", 'wb') as f:
|
152 |
f.write(TMIDIX.score2midi(output_midi_seq))
|
153 |
audio = synthesis(TMIDIX.score2opus(output_midi_seq), 'SGM-v2.01-YamahaGrand-Guit-Bass-v2.7.sf2')
|
154 |
-
return "Allegro-Music-Transformer-Music-Composition.mid", (44100, audio)
|
155 |
-
|
156 |
-
def load_javascript(dir="javascript"):
|
157 |
-
scripts_list = glob.glob(f"app.js")
|
158 |
-
javascript = ""
|
159 |
-
for path in scripts_list:
|
160 |
-
with open(path, "r", encoding="utf8") as jsfile:
|
161 |
-
javascript += f"\n<!-- {path} --><script>{jsfile.read()}</script>"
|
162 |
-
template_response_ori = gr.routes.templates.TemplateResponse
|
163 |
-
|
164 |
-
def template_response(*args, **kwargs):
|
165 |
-
res = template_response_ori(*args, **kwargs)
|
166 |
-
res.body = res.body.replace(
|
167 |
-
b'</head>', f'{javascript}</head>'.encode("utf8"))
|
168 |
-
res.init_headers()
|
169 |
-
return res
|
170 |
-
|
171 |
-
gr.routes.templates.TemplateResponse = template_response
|
172 |
-
|
173 |
-
|
174 |
-
class JSMsgReceiver(gr.HTML):
|
175 |
-
|
176 |
-
def __init__(self, **kwargs):
|
177 |
-
super().__init__(elem_id="msg_receiver", visible=False, **kwargs)
|
178 |
-
|
179 |
-
def postprocess(self, y):
|
180 |
-
if y:
|
181 |
-
y = f"<p>{json.dumps(y)}</p>"
|
182 |
-
return super().postprocess(y)
|
183 |
-
|
184 |
-
def get_block_name(self) -> str:
|
185 |
-
return "html"
|
186 |
|
187 |
#=================================================================================================
|
188 |
|
@@ -197,7 +166,6 @@ if __name__ == "__main__":
|
|
197 |
session = rt.InferenceSession('Allegro_Music_Transformer_Small_Trained_Model_56000_steps_0.9399_loss_0.7374_acc.onnx', providers=['CUDAExecutionProvider'])
|
198 |
print('Done!')
|
199 |
|
200 |
-
#load_javascript()
|
201 |
app = gr.Blocks()
|
202 |
with app:
|
203 |
gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Allegro Music Transformer</h1>")
|
@@ -208,9 +176,7 @@ if __name__ == "__main__":
|
|
208 |
"(https://colab.research.google.com/github/asigalov61/Allegro-Music-Transformer/blob/main/Allegro_Music_Transformer_Composer.ipynb)"
|
209 |
" for faster execution and endless generation"
|
210 |
)
|
211 |
-
|
212 |
-
js_msg = JSMsgReceiver()
|
213 |
-
|
214 |
run_btn = gr.Button("generate", variant="primary")
|
215 |
stop_btn = gr.Button("stop and output")
|
216 |
|
@@ -218,7 +184,7 @@ if __name__ == "__main__":
|
|
218 |
output_midi_visualizer = gr.HTML(elem_id="midi_visualizer_container")
|
219 |
output_audio = gr.Audio(label="output audio", format="mp3", elem_id="midi_audio")
|
220 |
output_midi = gr.File(label="output midi", file_types=[".mid"])
|
221 |
-
run_event = run_btn.click(GenerateMIDI, [], [output_midi_seq, output_midi, output_audio
|
222 |
-
stop_btn.click(cancel_run, output_midi_seq, [output_midi, output_audio
|
223 |
|
224 |
app.queue(2).launch(server_port=opt.port, share=opt.share, inbrowser=True)
|
|
|
19 |
#=================================================================================================
|
20 |
|
21 |
def generate(
|
22 |
+
|
23 |
+
|
24 |
+
#=================================================================================================
|
25 |
+
|
26 |
+
def create_msg(name, data):
|
27 |
+
return {"name": name, "data": data}
|
28 |
+
|
29 |
+
def GenerateMIDI():
|
30 |
+
|
31 |
+
start_tokens = [3087, 3073+1, 3075+1]
|
32 |
+
seq_len = 512
|
33 |
+
max_seq_len = 2048,
|
34 |
+
temperature = 0.9,
|
35 |
+
verbose=False,
|
36 |
+
return_prime=False,
|
37 |
+
progress=gr.Progress()
|
38 |
|
39 |
out = torch.LongTensor([start_tokens])
|
40 |
|
|
|
50 |
try:
|
51 |
|
52 |
x = out[:, -max_seq_len:]
|
53 |
+
|
54 |
torch_in = x.tolist()[0]
|
55 |
|
56 |
logits = torch.FloatTensor(session.run(None, {'input': [torch_in]})[0])[:, -1]
|
|
|
69 |
|
70 |
else:
|
71 |
return out[:, st:]
|
|
|
|
|
|
|
|
|
|
|
72 |
|
|
|
|
|
|
|
|
|
73 |
melody_chords_f = melody_chords_f.tolist()[0]
|
74 |
|
75 |
print('=' * 70)
|
|
|
141 |
|
142 |
audio = synthesis(TMIDIX.score2opus(output), 'SGM-v2.01-YamahaGrand-Guit-Bass-v2.7.sf2')
|
143 |
|
144 |
+
yield output, "Allegro-Music-Transformer-Music-Composition.mid", (44100, audio)
|
145 |
|
146 |
#=================================================================================================
|
147 |
|
|
|
151 |
with open(f"Allegro-Music-Transformer-Music-Composition.mid", 'wb') as f:
|
152 |
f.write(TMIDIX.score2midi(output_midi_seq))
|
153 |
audio = synthesis(TMIDIX.score2opus(output_midi_seq), 'SGM-v2.01-YamahaGrand-Guit-Bass-v2.7.sf2')
|
154 |
+
return "Allegro-Music-Transformer-Music-Composition.mid", (44100, audio)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
|
156 |
#=================================================================================================
|
157 |
|
|
|
166 |
session = rt.InferenceSession('Allegro_Music_Transformer_Small_Trained_Model_56000_steps_0.9399_loss_0.7374_acc.onnx', providers=['CUDAExecutionProvider'])
|
167 |
print('Done!')
|
168 |
|
|
|
169 |
app = gr.Blocks()
|
170 |
with app:
|
171 |
gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Allegro Music Transformer</h1>")
|
|
|
176 |
"(https://colab.research.google.com/github/asigalov61/Allegro-Music-Transformer/blob/main/Allegro_Music_Transformer_Composer.ipynb)"
|
177 |
" for faster execution and endless generation"
|
178 |
)
|
179 |
+
|
|
|
|
|
180 |
run_btn = gr.Button("generate", variant="primary")
|
181 |
stop_btn = gr.Button("stop and output")
|
182 |
|
|
|
184 |
output_midi_visualizer = gr.HTML(elem_id="midi_visualizer_container")
|
185 |
output_audio = gr.Audio(label="output audio", format="mp3", elem_id="midi_audio")
|
186 |
output_midi = gr.File(label="output midi", file_types=[".mid"])
|
187 |
+
run_event = run_btn.click(GenerateMIDI, [], [output_midi_seq, output_midi, output_audio])
|
188 |
+
stop_btn.click(cancel_run, output_midi_seq, [output_midi, output_audio], cancels=run_event, queue=False)
|
189 |
|
190 |
app.queue(2).launch(server_port=opt.port, share=opt.share, inbrowser=True)
|