SayaSS commited on
Commit
2a7c07b
1 Parent(s): 8d72504

Add download button

Browse files
Files changed (1) hide show
  1. app.py +26 -2
app.py CHANGED
@@ -62,6 +62,28 @@ def change_lang(language):
62
  else:
63
  return 0.6, 0.668, 1.1
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  if __name__ == '__main__':
66
  with gr.Blocks() as app:
67
  gr.Markdown(
@@ -75,7 +97,7 @@ if __name__ == '__main__':
75
  with gr.TabItem("vits"):
76
  with gr.Row():
77
  with gr.Column():
78
- input_text = gr.Textbox(label="Text (100 words limitation)", lines=5, value="今天晚上吃啥好呢。")
79
  lang = gr.Dropdown(label="Language", choices=["中文", "日语", "中日混合(中文用[ZH][ZH]包裹起来,日文用[JA][JA]包裹起来)"],
80
  type="index", value="中文")
81
  btn = gr.Button(value="Submit")
@@ -89,9 +111,11 @@ if __name__ == '__main__':
89
  ls = gr.Slider(label="length_scale(控制整体语速)", minimum=0.1, maximum=2.0, step=0.1, value=1.2, interactive=True)
90
  with gr.Column():
91
  o1 = gr.Textbox(label="Output Message")
92
- o2 = gr.Audio(label="Output Audio")
93
  o3 = gr.Textbox(label="Extra Info")
 
94
  btn.click(vits, inputs=[input_text, lang, sid, ns, nsw, ls], outputs=[o1, o2, o3])
 
95
  btn2.click(search_speaker, inputs=[search], outputs=[sid])
96
  lang.change(change_lang, inputs=[lang], outputs=[ns, nsw, ls])
97
  with gr.TabItem("可用人物一览"):
 
62
  else:
63
  return 0.6, 0.668, 1.1
64
 
65
+ download_audio_js = """
66
+ () =>{{
67
+ let root = document.querySelector("body > gradio-app");
68
+ if (root.shadowRoot != null)
69
+ root = root.shadowRoot;
70
+ let audio = root.querySelector("#tts-audio").querySelector("audio");
71
+ let text = root.querySelector("#input-text").querySelector("textarea");
72
+ if (audio == undefined)
73
+ return;
74
+ text = text.value;
75
+ if (text == undefined)
76
+ text = Math.floor(Math.random()*100000000);
77
+ audio = audio.src;
78
+ let oA = document.createElement("a");
79
+ oA.download = text.substr(0, 20)+'.wav';
80
+ oA.href = audio;
81
+ document.body.appendChild(oA);
82
+ oA.click();
83
+ oA.remove();
84
+ }}
85
+ """
86
+
87
  if __name__ == '__main__':
88
  with gr.Blocks() as app:
89
  gr.Markdown(
 
97
  with gr.TabItem("vits"):
98
  with gr.Row():
99
  with gr.Column():
100
+ input_text = gr.Textbox(label="Text (100 words limitation)", lines=5, value="今天晚上吃啥好呢。", elem_id=f"input-text")
101
  lang = gr.Dropdown(label="Language", choices=["中文", "日语", "中日混合(中文用[ZH][ZH]包裹起来,日文用[JA][JA]包裹起来)"],
102
  type="index", value="中文")
103
  btn = gr.Button(value="Submit")
 
111
  ls = gr.Slider(label="length_scale(控制整体语速)", minimum=0.1, maximum=2.0, step=0.1, value=1.2, interactive=True)
112
  with gr.Column():
113
  o1 = gr.Textbox(label="Output Message")
114
+ o2 = gr.Audio(label="Output Audio", elem_id=f"tts-audio")
115
  o3 = gr.Textbox(label="Extra Info")
116
+ download = gr.Button("Download Audio")
117
  btn.click(vits, inputs=[input_text, lang, sid, ns, nsw, ls], outputs=[o1, o2, o3])
118
+ download.click(None, [], [], _js=download_audio_js.format())
119
  btn2.click(search_speaker, inputs=[search], outputs=[sid])
120
  lang.change(change_lang, inputs=[lang], outputs=[ns, nsw, ls])
121
  with gr.TabItem("可用人物一览"):