Plachta commited on
Commit
19a8f04
1 Parent(s): 8177515

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -6
app.py CHANGED
@@ -1,19 +1,45 @@
1
  import argparse
2
- import gradio as gr
 
 
 
 
 
 
3
  import torch
 
4
  import commons
5
  import utils
6
- import re
 
 
7
  from models import SynthesizerTrn
 
8
  from text.symbols import symbols
9
- from text import text_to_sequence
10
- import numpy as np
11
- import os
12
  import translators.server as tss
13
  import psutil
14
  from datetime import datetime
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
 
 
 
 
17
 
18
  limitation = os.getenv("SYSTEM") == "spaces" # limit text and audio length in huggingface spaces
19
  max_len = 150
@@ -105,6 +131,24 @@ def infer(text_raw, character, language, duration, noise_scale, noise_scale_w):
105
  show_memory_info(str(currentDateAndTime) + " infer调用后")
106
  return (text, (22050, audio))
107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  if __name__ == "__main__":
109
  parser = argparse.ArgumentParser()
110
  parser.add_argument("--share", action="store_true", default=False, help="share gradio app")
@@ -135,7 +179,9 @@ if __name__ == "__main__":
135
  noise_scale_w_slider = gr.Slider(minimum=0.1, maximum=5, value=0.8, step=0.1, label='噪声偏差 noise_scale_w')
136
  with gr.Column():
137
  text_output = gr.Textbox(label="Output Text")
138
- audio_output = gr.Audio(label="Output Voice")
 
 
139
  btn = gr.Button("Generate!")
140
  btn.click(infer, inputs=[textbox, char_dropdown, language_dropdown,
141
  duration_slider, noise_scale_slider, noise_scale_w_slider],
 
1
  import argparse
2
+ import json
3
+ import os
4
+ import re
5
+ import tempfile
6
+
7
+ import librosa
8
+ import numpy as np
9
  import torch
10
+ from torch import no_grad, LongTensor
11
  import commons
12
  import utils
13
+ import gradio as gr
14
+ import gradio.utils as gr_utils
15
+ import gradio.processing_utils as gr_processing_utils
16
  from models import SynthesizerTrn
17
+ from text import text_to_sequence, _clean_text
18
  from text.symbols import symbols
19
+ from mel_processing import spectrogram_torch
 
 
20
  import translators.server as tss
21
  import psutil
22
  from datetime import datetime
23
 
24
+ def audio_postprocess(self, y):
25
+ if y is None:
26
+ return None
27
+
28
+ if gr_utils.validate_url(y):
29
+ file = gr_processing_utils.download_to_file(y, dir=self.temp_dir)
30
+ elif isinstance(y, tuple):
31
+ sample_rate, data = y
32
+ file = tempfile.NamedTemporaryFile(
33
+ suffix=".wav", dir=self.temp_dir, delete=False
34
+ )
35
+ gr_processing_utils.audio_to_file(sample_rate, data, file.name)
36
+ else:
37
+ file = gr_processing_utils.create_tmp_copy_of_file(y, dir=self.temp_dir)
38
 
39
+ return gr_processing_utils.encode_url_or_file_to_base64(file.name)
40
+
41
+
42
+ gr.Audio.postprocess = audio_postprocess
43
 
44
  limitation = os.getenv("SYSTEM") == "spaces" # limit text and audio length in huggingface spaces
45
  max_len = 150
 
131
  show_memory_info(str(currentDateAndTime) + " infer调用后")
132
  return (text, (22050, audio))
133
 
134
+ download_audio_js = """
135
+ () =>{{
136
+ let root = document.querySelector("body > gradio-app");
137
+ if (root.shadowRoot != null)
138
+ root = root.shadowRoot;
139
+ let audio = root.querySelector("#{audio_id}").querySelector("audio");
140
+ if (audio == undefined)
141
+ return;
142
+ audio = audio.src;
143
+ let oA = document.createElement("a");
144
+ oA.download = Math.floor(Math.random()*100000000)+'.wav';
145
+ oA.href = audio;
146
+ document.body.appendChild(oA);
147
+ oA.click();
148
+ oA.remove();
149
+ }}
150
+ """
151
+
152
  if __name__ == "__main__":
153
  parser = argparse.ArgumentParser()
154
  parser.add_argument("--share", action="store_true", default=False, help="share gradio app")
 
179
  noise_scale_w_slider = gr.Slider(minimum=0.1, maximum=5, value=0.8, step=0.1, label='噪声偏差 noise_scale_w')
180
  with gr.Column():
181
  text_output = gr.Textbox(label="Output Text")
182
+ audio_output = gr.Audio(label="Output Audio", elem_id="tts-audio")
183
+ download = gr.Button("Download Audio")
184
+ download.click(None, [], [], _js=download_audio_js.format(audio_id="tts-audio"))
185
  btn = gr.Button("Generate!")
186
  btn.click(infer, inputs=[textbox, char_dropdown, language_dropdown,
187
  duration_slider, noise_scale_slider, noise_scale_w_slider],