skytnt commited on
Commit
5d16d44
β€’
1 Parent(s): 8c486cf

add download button

Browse files
Files changed (1) hide show
  1. app.py +28 -3
app.py CHANGED
@@ -145,6 +145,24 @@ def create_to_symbol_fn(hps):
145
  return to_symbol_fn
146
 
147
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
  if __name__ == '__main__':
149
  parser = argparse.ArgumentParser()
150
  parser.add_argument('--device', type=str, default='cpu')
@@ -221,7 +239,10 @@ if __name__ == '__main__':
221
  symbol_list_json = gr.Json(value=symbols, visible=False)
222
  tts_submit = gr.Button("Generate", variant="primary")
223
  tts_output1 = gr.Textbox(label="Output Message")
224
- tts_output2 = gr.Audio(label="Output Audio")
 
 
 
225
  tts_submit.click(tts_fn, [tts_input1, tts_input2, tts_input3, symbol_input],
226
  [tts_output1, tts_output2])
227
  symbol_input.change(to_symbol_fn,
@@ -262,7 +283,9 @@ if __name__ == '__main__':
262
  vc_input3 = gr.Audio(label="Input Audio (30s limitation)")
263
  vc_submit = gr.Button("Convert", variant="primary")
264
  vc_output1 = gr.Textbox(label="Output Message")
265
- vc_output2 = gr.Audio(label="Output Audio")
 
 
266
  vc_submit.click(vc_fn, [vc_input1, vc_input2, vc_input3], [vc_output1, vc_output2])
267
  with gr.TabItem("Soft Voice Conversion"):
268
  with gr.Tabs():
@@ -281,7 +304,9 @@ if __name__ == '__main__':
281
  vc_input3 = gr.Audio(label="Input Audio (30s limitation)", source="upload")
282
  vc_submit = gr.Button("Convert", variant="primary")
283
  vc_output1 = gr.Textbox(label="Output Message")
284
- vc_output2 = gr.Audio(label="Output Audio")
 
 
285
  # clear inputs
286
  source_tabs.set_event_trigger("change", None, [], [vc_input2, vc_input3],
287
  js="()=>[null,null]")
 
145
  return to_symbol_fn
146
 
147
 
148
+ download_audio_js = """
149
+ () =>{{
150
+ let root = document.querySelector("body > gradio-app");
151
+ if (root.shadowRoot != null)
152
+ root = root.shadowRoot;
153
+ let audio = root.querySelector("#{audio_id}").querySelector("audio");
154
+ if (audio == undefined)
155
+ return;
156
+ audio = audio.src.replace("audio/wav", "audio/octet-stream");
157
+ let oA = document.createElement("a");
158
+ oA.download = Math.floor(Math.random()*100000000)+'.wav';
159
+ oA.href = audio;
160
+ document.body.appendChild(oA);
161
+ oA.click();
162
+ oA.remove();
163
+ }}
164
+ """
165
+
166
  if __name__ == '__main__':
167
  parser = argparse.ArgumentParser()
168
  parser.add_argument('--device', type=str, default='cpu')
 
239
  symbol_list_json = gr.Json(value=symbols, visible=False)
240
  tts_submit = gr.Button("Generate", variant="primary")
241
  tts_output1 = gr.Textbox(label="Output Message")
242
+ tts_output2 = gr.Audio(label="Output Audio", elem_id=f"tts-audio{i}")
243
+ download = gr.Button("Download Audio")
244
+ download.click(None, [], [], _js=download_audio_js.format(audio_id=f"tts-audio{i}"))
245
+
246
  tts_submit.click(tts_fn, [tts_input1, tts_input2, tts_input3, symbol_input],
247
  [tts_output1, tts_output2])
248
  symbol_input.change(to_symbol_fn,
 
283
  vc_input3 = gr.Audio(label="Input Audio (30s limitation)")
284
  vc_submit = gr.Button("Convert", variant="primary")
285
  vc_output1 = gr.Textbox(label="Output Message")
286
+ vc_output2 = gr.Audio(label="Output Audio", elem_id=f"vc-audio{i}")
287
+ download = gr.Button("Download Audio")
288
+ download.click(None, [], [], _js=download_audio_js.format(audio_id=f"vc-audio{i}"))
289
  vc_submit.click(vc_fn, [vc_input1, vc_input2, vc_input3], [vc_output1, vc_output2])
290
  with gr.TabItem("Soft Voice Conversion"):
291
  with gr.Tabs():
 
304
  vc_input3 = gr.Audio(label="Input Audio (30s limitation)", source="upload")
305
  vc_submit = gr.Button("Convert", variant="primary")
306
  vc_output1 = gr.Textbox(label="Output Message")
307
+ vc_output2 = gr.Audio(label="Output Audio", elem_id=f"svc-audio{i}")
308
+ download = gr.Button("Download Audio")
309
+ download.click(None, [], [], _js=download_audio_js.format(audio_id=f"svc-audio{i}"))
310
  # clear inputs
311
  source_tabs.set_event_trigger("change", None, [], [vc_input2, vc_input3],
312
  js="()=>[null,null]")