Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
| 1 |
-
import glob
|
| 2 |
-
import json
|
| 3 |
import os.path
|
| 4 |
|
| 5 |
import time
|
|
@@ -15,7 +13,6 @@ import spaces
|
|
| 15 |
from x_transformer import *
|
| 16 |
import tqdm
|
| 17 |
|
| 18 |
-
from midi_synthesizer import synthesis
|
| 19 |
import TMIDIX
|
| 20 |
|
| 21 |
import matplotlib.pyplot as plt
|
|
@@ -149,17 +146,22 @@ def GenerateMIDI(num_tok, idrums, iinstr):
|
|
| 149 |
if 1280 <= ss1 < 2816:
|
| 150 |
channel = (ss1 - 1280) // 128
|
| 151 |
pitch = (ss1 - 1280) % 128
|
| 152 |
-
event = ['note', ctime, dur, channel, pitch, vel]
|
| 153 |
output[-1].append(event)
|
| 154 |
|
| 155 |
-
yield output, None, None, [create_msg("visualizer_append", event), create_msg("progress", [i + 1, num_tok])]
|
| 156 |
-
|
| 157 |
midi_data = TMIDIX.score2midi(output, text_encoding)
|
| 158 |
|
| 159 |
with open(f"Allegro-Music-Transformer-Music-Composition.mid", 'wb') as f:
|
| 160 |
f.write(midi_data)
|
| 161 |
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
print('Sample INTs', outy[:16])
|
| 165 |
print('-' * 70)
|
|
@@ -169,61 +171,10 @@ def GenerateMIDI(num_tok, idrums, iinstr):
|
|
| 169 |
print('-' * 70)
|
| 170 |
print('Req execution time:', (time.time() - start_time), 'sec')
|
| 171 |
|
| 172 |
-
|
| 173 |
-
create_msg("visualizer_end", None)]
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
def cancel_run(mid_seq):
|
| 177 |
-
if mid_seq is None:
|
| 178 |
-
return None, None, None
|
| 179 |
-
text_encoding = 'ISO-8859-1'
|
| 180 |
-
midi_data = TMIDIX.score2midi(mid_seq, text_encoding)
|
| 181 |
-
|
| 182 |
-
with open(f"Allegro-Music-Transformer-Music-Composition.mid", 'wb') as f:
|
| 183 |
-
f.write(midi_data)
|
| 184 |
-
|
| 185 |
-
audio = synthesis(TMIDIX.score2opus(mid_seq), 'SGM-v2.01-YamahaGrand-Guit-Bass-v2.7.sf2')
|
| 186 |
-
|
| 187 |
-
yield "Allegro-Music-Transformer-Music-Composition.mid", (44100, audio), [
|
| 188 |
-
create_msg("visualizer_end", None)]
|
| 189 |
-
|
| 190 |
|
| 191 |
# =================================================================================================
|
| 192 |
|
| 193 |
-
def load_javascript(dir="javascript"):
|
| 194 |
-
scripts_list = glob.glob(f"{dir}/*.js")
|
| 195 |
-
javascript = ""
|
| 196 |
-
for path in scripts_list:
|
| 197 |
-
with open(path, "r", encoding="utf8") as jsfile:
|
| 198 |
-
javascript += f"\n<!-- {path} --><script>{jsfile.read()}</script>"
|
| 199 |
-
template_response_ori = gr.routes.templates.TemplateResponse
|
| 200 |
-
|
| 201 |
-
def template_response(*args, **kwargs):
|
| 202 |
-
res = template_response_ori(*args, **kwargs)
|
| 203 |
-
res.body = res.body.replace(
|
| 204 |
-
b'</head>', f'{javascript}</head>'.encode("utf8"))
|
| 205 |
-
res.init_headers()
|
| 206 |
-
return res
|
| 207 |
-
|
| 208 |
-
gr.routes.templates.TemplateResponse = template_response
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
class JSMsgReceiver(gr.HTML):
|
| 212 |
-
def __init__(self, **kwargs):
|
| 213 |
-
super().__init__(elem_id="msg_receiver", visible=False, **kwargs)
|
| 214 |
-
|
| 215 |
-
def postprocess(self, y):
|
| 216 |
-
if y:
|
| 217 |
-
y = f"<p>{json.dumps(y)}</p>"
|
| 218 |
-
return super().postprocess(y)
|
| 219 |
-
|
| 220 |
-
def get_block_name(self) -> str:
|
| 221 |
-
return "html"
|
| 222 |
-
|
| 223 |
-
def create_msg(name, data):
|
| 224 |
-
return {"name": name, "data": data}
|
| 225 |
-
|
| 226 |
-
|
| 227 |
if __name__ == "__main__":
|
| 228 |
|
| 229 |
PDT = timezone('US/Pacific')
|
|
@@ -232,7 +183,6 @@ if __name__ == "__main__":
|
|
| 232 |
print('App start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
|
| 233 |
print('=' * 70)
|
| 234 |
|
| 235 |
-
load_javascript()
|
| 236 |
app = gr.Blocks()
|
| 237 |
with app:
|
| 238 |
gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Allegro Music Transformer</h1>")
|
|
@@ -245,7 +195,6 @@ if __name__ == "__main__":
|
|
| 245 |
"(https://colab.research.google.com/github/asigalov61/Allegro-Music-Transformer/blob/main/Allegro_Music_Transformer_Composer.ipynb)"
|
| 246 |
" for faster execution and endless generation"
|
| 247 |
)
|
| 248 |
-
js_msg = JSMsgReceiver()
|
| 249 |
input_drums = gr.Checkbox(label="Add Drums", value=False, info="Add drums to the composition")
|
| 250 |
input_instrument = gr.Radio(
|
| 251 |
["Piano", "Guitar", "Bass", "Violin", "Cello", "Harp", "Trumpet", "Sax", "Flute", "Choir", "Organ"],
|
|
@@ -254,12 +203,9 @@ if __name__ == "__main__":
|
|
| 254 |
run_btn = gr.Button("generate", variant="primary")
|
| 255 |
interrupt_btn = gr.Button("interrupt")
|
| 256 |
|
| 257 |
-
|
| 258 |
-
output_midi_visualizer = gr.HTML(elem_id="midi_visualizer_container")
|
| 259 |
output_audio = gr.Audio(label="output audio", format="mp3", elem_id="midi_audio")
|
| 260 |
output_midi = gr.File(label="output midi", file_types=[".mid"])
|
| 261 |
run_event = run_btn.click(GenerateMIDI, [input_num_tokens, input_drums, input_instrument],
|
| 262 |
-
[
|
| 263 |
-
interrupt_btn.click(cancel_run, output_midi_seq, [output_midi, output_audio, js_msg],
|
| 264 |
-
cancels=run_event, queue=False)
|
| 265 |
app.queue().launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import os.path
|
| 2 |
|
| 3 |
import time
|
|
|
|
| 13 |
from x_transformer import *
|
| 14 |
import tqdm
|
| 15 |
|
|
|
|
| 16 |
import TMIDIX
|
| 17 |
|
| 18 |
import matplotlib.pyplot as plt
|
|
|
|
| 146 |
if 1280 <= ss1 < 2816:
|
| 147 |
channel = (ss1 - 1280) // 128
|
| 148 |
pitch = (ss1 - 1280) % 128
|
| 149 |
+
event = ['note', ctime, dur, channel, pitch, vel, list_of_MIDI_patches[chanel]]
|
| 150 |
output[-1].append(event)
|
| 151 |
|
|
|
|
|
|
|
| 152 |
midi_data = TMIDIX.score2midi(output, text_encoding)
|
| 153 |
|
| 154 |
with open(f"Allegro-Music-Transformer-Music-Composition.mid", 'wb') as f:
|
| 155 |
f.write(midi_data)
|
| 156 |
|
| 157 |
+
output_plot = TMIDIX.plot_ms_SONG(output, plot_title='Allegro-Music-Transformer-Music-Composition', return_plt=True)
|
| 158 |
+
|
| 159 |
+
audio = midi_to_colab_audio(new_fn,
|
| 160 |
+
soundfont_path="SGM-v2.01-YamahaGrand-Guit-Bass-v2.7.sf2",
|
| 161 |
+
sample_rate=16000,
|
| 162 |
+
volume_scale=10,
|
| 163 |
+
output_for_gradio=True
|
| 164 |
+
)
|
| 165 |
|
| 166 |
print('Sample INTs', outy[:16])
|
| 167 |
print('-' * 70)
|
|
|
|
| 171 |
print('-' * 70)
|
| 172 |
print('Req execution time:', (time.time() - start_time), 'sec')
|
| 173 |
|
| 174 |
+
return output_plot, "Allegro-Music-Transformer-Music-Composition.mid", (16000, audio)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
# =================================================================================================
|
| 177 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
if __name__ == "__main__":
|
| 179 |
|
| 180 |
PDT = timezone('US/Pacific')
|
|
|
|
| 183 |
print('App start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
|
| 184 |
print('=' * 70)
|
| 185 |
|
|
|
|
| 186 |
app = gr.Blocks()
|
| 187 |
with app:
|
| 188 |
gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Allegro Music Transformer</h1>")
|
|
|
|
| 195 |
"(https://colab.research.google.com/github/asigalov61/Allegro-Music-Transformer/blob/main/Allegro_Music_Transformer_Composer.ipynb)"
|
| 196 |
" for faster execution and endless generation"
|
| 197 |
)
|
|
|
|
| 198 |
input_drums = gr.Checkbox(label="Add Drums", value=False, info="Add drums to the composition")
|
| 199 |
input_instrument = gr.Radio(
|
| 200 |
["Piano", "Guitar", "Bass", "Violin", "Cello", "Harp", "Trumpet", "Sax", "Flute", "Choir", "Organ"],
|
|
|
|
| 203 |
run_btn = gr.Button("generate", variant="primary")
|
| 204 |
interrupt_btn = gr.Button("interrupt")
|
| 205 |
|
| 206 |
+
output_plot = gr.Plot(label='output plot')
|
|
|
|
| 207 |
output_audio = gr.Audio(label="output audio", format="mp3", elem_id="midi_audio")
|
| 208 |
output_midi = gr.File(label="output midi", file_types=[".mid"])
|
| 209 |
run_event = run_btn.click(GenerateMIDI, [input_num_tokens, input_drums, input_instrument],
|
| 210 |
+
[output_plot, output_midi, output_audio])
|
|
|
|
|
|
|
| 211 |
app.queue().launch()
|