asigalov61 commited on
Commit
186f625
1 Parent(s): 0b44319

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -3
app.py CHANGED
@@ -108,9 +108,17 @@ def GenerateMIDI(params):
108
 
109
  #=================================================================================================
110
 
 
 
 
 
 
 
 
 
111
 
112
  def load_javascript(dir="javascript"):
113
- scripts_list = glob.glob(f"{dir}/*.js")
114
  javascript = ""
115
  for path in scripts_list:
116
  with open(path, "r", encoding="utf8") as jsfile:
@@ -151,6 +159,7 @@ if __name__ == "__main__":
151
 
152
  session = rt.InferenceSession('Allegro_Music_Transformer_Small_Trained_Model_56000_steps_0.9399_loss_0.7374_acc.onnx', providers=['CUDAExecutionProvider'])
153
 
 
154
  app = gr.Blocks()
155
  with app:
156
  gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Allegro Music Transformer</h1>")
@@ -163,6 +172,13 @@ if __name__ == "__main__":
163
  )
164
 
165
  js_msg = JSMsgReceiver()
166
- tab_select = gr.Variable(value=0)
167
-
 
 
 
 
 
 
 
168
  app.queue(2).launch(server_port=opt.port, share=opt.share, inbrowser=True)
 
108
 
109
  #=================================================================================================
110
 
111
+ def cancel_run(mid_seq):
112
+ if mid_seq is None:
113
+ return None, None
114
+ mid = tokenizer.detokenize(mid_seq)
115
+ with open(f"output.mid", 'wb') as f:
116
+ f.write(MIDI.score2midi(mid))
117
+ audio = synthesis(MIDI.score2opus(mid), soundfont_path)
118
+ return "output.mid", (44100, audio), [create_msg("visualizer_end", None)]
119
 
120
  def load_javascript(dir="javascript"):
121
+ scripts_list = glob.glob(f"*.js")
122
  javascript = ""
123
  for path in scripts_list:
124
  with open(path, "r", encoding="utf8") as jsfile:
 
159
 
160
  session = rt.InferenceSession('Allegro_Music_Transformer_Small_Trained_Model_56000_steps_0.9399_loss_0.7374_acc.onnx', providers=['CUDAExecutionProvider'])
161
 
162
+ # load_javascript()
163
  app = gr.Blocks()
164
  with app:
165
  gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Allegro Music Transformer</h1>")
 
172
  )
173
 
174
  js_msg = JSMsgReceiver()
175
+
176
+ run_btn = gr.Button("generate", variant="primary")
177
+ stop_btn = gr.Button("stop and output")
178
+
179
+ output_audio = gr.Audio(label="output audio", format="mp3", elem_id="midi_audio")
180
+ output_midi = gr.File(label="output midi", file_types=[".mid"])
181
+ run_event = run_btn.click(run, [output_midi_seq, output_midi, output_audio, js_msg])
182
+ stop_btn.click(cancel_run, output_midi_seq, [output_midi, output_audio, js_msg], cancels=run_event, queue=False)
183
+
184
  app.queue(2).launch(server_port=opt.port, share=opt.share, inbrowser=True)