asigalov61 commited on
Commit
bc18781
1 Parent(s): 18b652e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -65
app.py CHANGED
@@ -139,60 +139,8 @@ def GenerateMIDI(num_tok, idrums, iinstr):
139
  yield output, "Allegro-Music-Transformer-Music-Composition.mid", (44100, audio), [
140
  create_msg("visualizer_end", None)]
141
 
142
-
143
- def cancel_run(mid_seq):
144
- if mid_seq is None:
145
- return None, None, None
146
- text_encoding = 'ISO-8859-1'
147
- midi_data = TMIDIX.score2midi(mid_seq, text_encoding)
148
-
149
- with open(f"Allegro-Music-Transformer-Music-Composition.mid", 'wb') as f:
150
- f.write(midi_data)
151
-
152
- audio = synthesis(TMIDIX.score2opus(mid_seq), 'SGM-v2.01-YamahaGrand-Guit-Bass-v2.7.sf2')
153
-
154
- yield "Allegro-Music-Transformer-Music-Composition.mid", (44100, audio), [
155
- create_msg("visualizer_end", None)]
156
-
157
-
158
  # =================================================================================================
159
 
160
- def load_javascript(dir="javascript"):
161
- scripts_list = glob.glob(f"{dir}/*.js")
162
- javascript = ""
163
- for path in scripts_list:
164
- with open(path, "r", encoding="utf8") as jsfile:
165
- javascript += f"\n<!-- {path} --><script>{jsfile.read()}</script>"
166
- template_response_ori = gr.routes.templates.TemplateResponse
167
-
168
- def template_response(*args, **kwargs):
169
- res = template_response_ori(*args, **kwargs)
170
- res.body = res.body.replace(
171
- b'</head>', f'{javascript}</head>'.encode("utf8"))
172
- res.init_headers()
173
- return res
174
-
175
- gr.routes.templates.TemplateResponse = template_response
176
-
177
-
178
- class JSMsgReceiver(gr.HTML):
179
-
180
- def __init__(self, **kwargs):
181
- super().__init__(elem_id="msg_receiver", visible=False, **kwargs)
182
-
183
- def postprocess(self, y):
184
- if y:
185
- y = f"<p>{json.dumps(y)}</p>"
186
- return super().postprocess(y)
187
-
188
- def get_block_name(self) -> str:
189
- return "html"
190
-
191
-
192
- def create_msg(name, data):
193
- return {"name": name, "data": data}
194
-
195
-
196
  if __name__ == "__main__":
197
 
198
  PDT = timezone('US/Pacific')
@@ -205,6 +153,8 @@ if __name__ == "__main__":
205
  parser.add_argument("--share", action="store_true", default=False, help="share gradio app")
206
  parser.add_argument("--port", type=int, default=7860, help="gradio server port")
207
  opt = parser.parse_args()
 
 
208
 
209
  print('Loading model...')
210
 
@@ -250,21 +200,22 @@ if __name__ == "__main__":
250
  "(https://colab.research.google.com/github/asigalov61/Allegro-Music-Transformer/blob/main/Allegro_Music_Transformer_Composer.ipynb)"
251
  " for faster execution and endless generation"
252
  )
253
- js_msg = JSMsgReceiver()
254
- input_drums = gr.Checkbox(label="Add Drums", value=False, info="Add drums to the composition")
255
- input_instrument = gr.Radio(
256
- ["Piano", "Guitar", "Bass", "Violin", "Cello", "Harp", "Trumpet", "Sax", "Flute", "Choir", "Organ"],
257
- value="Piano", label="Lead Instrument Controls", info="Desired lead instrument")
258
  input_num_tokens = gr.Slider(16, 512, value=256, label="Number of Tokens", info="Number of tokens to generate")
 
259
  run_btn = gr.Button("generate", variant="primary")
260
  interrupt_btn = gr.Button("interrupt")
261
 
262
- output_midi_seq = gr.Variable()
263
- output_midi_visualizer = gr.HTML(elem_id="midi_visualizer_container")
264
- output_audio = gr.Audio(label="output audio", format="mp3", elem_id="midi_audio")
265
- output_midi = gr.File(label="output midi", file_types=[".mid"])
266
- run_event = run_btn.click(GenerateMIDI, [input_num_tokens, input_drums, input_instrument],
267
- [output_midi_seq, output_midi, output_audio, js_msg])
268
- interrupt_btn.click(cancel_run, output_midi_seq, [output_midi, output_audio, js_msg],
269
- cancels=run_event, queue=False)
 
 
270
  app.queue(concurrency_count=1).launch(server_port=opt.port, share=opt.share, inbrowser=True)
 
139
  yield output, "Allegro-Music-Transformer-Music-Composition.mid", (44100, audio), [
140
  create_msg("visualizer_end", None)]
141
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
  # =================================================================================================
143
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
  if __name__ == "__main__":
145
 
146
  PDT = timezone('US/Pacific')
 
153
  parser.add_argument("--share", action="store_true", default=False, help="share gradio app")
154
  parser.add_argument("--port", type=int, default=7860, help="gradio server port")
155
  opt = parser.parse_args()
156
+
157
+ soundfonts = ["SGM-v2.01-YamahaGrand-Guit-Bass-v2.7.sf2"]
158
 
159
  print('Loading model...')
160
 
 
200
  "(https://colab.research.google.com/github/asigalov61/Allegro-Music-Transformer/blob/main/Allegro_Music_Transformer_Composer.ipynb)"
201
  " for faster execution and endless generation"
202
  )
203
+ gr.Markdown("## Upload your MIDI")
204
+
205
+ input_midi = gr.File(label="Input MIDI", file_types=[".midi", ".mid", ".kar"], type="filepath")
 
 
206
  input_num_tokens = gr.Slider(16, 512, value=256, label="Number of Tokens", info="Number of tokens to generate")
207
+
208
  run_btn = gr.Button("generate", variant="primary")
209
  interrupt_btn = gr.Button("interrupt")
210
 
211
+ output_midi_title = gr.Textbox(label="Output MIDI title")
212
+ output_midi_summary = gr.Textbox(label="Output MIDI summary")
213
+ output_audio = gr.Audio(label="Output MIDI audio", format="wav", elem_id="midi_audio")
214
+ output_plot = gr.Plot(label="Output MIDI score plot")
215
+ output_midi = gr.File(label="Output MIDI file", file_types=[".mid"])
216
+
217
+
218
+ run_event = run_btn.click(GenerateMIDI, [input_midi, input_num_tokens],
219
+ [output_midi_title, output_midi_summary, output_audio, output_plot, output_midi])
220
+
221
  app.queue(concurrency_count=1).launch(server_port=opt.port, share=opt.share, inbrowser=True)