admin commited on
Commit
4a548a6
·
1 Parent(s): 3c5b8a2
Files changed (1) hide show
  1. app.py +28 -28
app.py CHANGED
@@ -54,7 +54,7 @@ def get_text(text, language_str, hps):
54
  return bert, phone, tone, language
55
 
56
 
57
- def infer(text, sdp_ratio, noise_scale, noise_scale_w, length_scale, sid):
58
  global net_g
59
  bert, phones, tones, lang_ids = get_text(text, "ZH", hps)
60
  with torch.no_grad():
@@ -88,7 +88,7 @@ def infer(text, sdp_ratio, noise_scale, noise_scale_w, length_scale, sid):
88
 
89
  def tts_fn(text, speaker, sdp_ratio, noise_scale, noise_scale_w, length_scale):
90
  with torch.no_grad():
91
- audio = infer(
92
  text,
93
  sdp_ratio=sdp_ratio,
94
  noise_scale=noise_scale,
@@ -121,23 +121,14 @@ def concatenate_audios(audio_samples, sample_rate=44100):
121
 
122
 
123
  def read_text(file_path: str):
124
- try:
125
- # 打开文件并读取内容
126
- with open(file_path, "r", encoding="utf-8") as file:
127
- content = file.read()
128
- return content
129
-
130
- except FileNotFoundError:
131
- print(f"文件未找到: {file_path}")
132
-
133
- except IOError:
134
- print(f"读取文件时发生错误: {file_path}")
135
-
136
- except Exception as e:
137
- print(f"发生未知错误: {e}")
138
 
139
 
140
- def infer_tab1(text, speaker, sdp_ratio, noise_scale, noise_scale_w, length_scale):
 
 
141
  try:
142
  content = read_text(text)
143
  sentences = text_splitter(content)
@@ -145,7 +136,7 @@ def infer_tab1(text, speaker, sdp_ratio, noise_scale, noise_scale_w, length_scal
145
  for sentence in tqdm(sentences, desc="TTS 推理中..."):
146
  with torch.no_grad():
147
  audios.append(
148
- infer(
149
  sentence,
150
  sdp_ratio=sdp_ratio,
151
  noise_scale=noise_scale,
@@ -155,20 +146,24 @@ def infer_tab1(text, speaker, sdp_ratio, noise_scale, noise_scale_w, length_scal
155
  )
156
  )
157
 
158
- return concatenate_audios(audios, hps.data.sampling_rate), content
159
 
160
  except Exception as e:
161
- return None, f"{e}"
162
 
 
163
 
164
- def infer_tab2(content, speaker, sdp_ratio, noise_scale, noise_scale_w, length_scale):
 
 
 
165
  try:
166
  sentences = text_splitter(content)
167
  audios = []
168
  for sentence in tqdm(sentences, desc="TTS 推理中..."):
169
  with torch.no_grad():
170
  audios.append(
171
- infer(
172
  sentence,
173
  sdp_ratio=sdp_ratio,
174
  noise_scale=noise_scale,
@@ -178,11 +173,12 @@ def infer_tab2(content, speaker, sdp_ratio, noise_scale, noise_scale_w, length_s
178
  )
179
  )
180
 
181
- return concatenate_audios(audios, hps.data.sampling_rate)
182
 
183
  except Exception as e:
184
- print(f"{e}")
185
- return None
 
186
 
187
 
188
  if __name__ == "__main__":
@@ -222,7 +218,7 @@ if __name__ == "__main__":
222
 
223
  with gr.Tab(_L("输入模式")):
224
  gr.Interface(
225
- fn=infer_tab2, # 使用 text_to_speech 函数
226
  inputs=[
227
  gr.TextArea(
228
  label=_L("请输入简体中文文案"),
@@ -251,14 +247,17 @@ if __name__ == "__main__":
251
  minimum=0.1, maximum=2, value=1, step=0.1, label=_L("生成时长")
252
  ),
253
  ],
254
- outputs=gr.Audio(label=_L("输出音频")),
 
 
 
255
  flagging_mode="never",
256
  concurrency_limit=4,
257
  )
258
 
259
  with gr.Tab(_L("上传模式")):
260
  gr.Interface(
261
- fn=infer_tab1, # 使用 text_to_speech 函数
262
  inputs=[
263
  gr.components.File(
264
  label=_L("请上传简体中文 TXT 文案"),
@@ -288,6 +287,7 @@ if __name__ == "__main__":
288
  ),
289
  ],
290
  outputs=[
 
291
  gr.Audio(label=_L("输出音频")),
292
  gr.TextArea(label=_L("文案提取结果"), show_copy_button=True),
293
  ],
 
54
  return bert, phone, tone, language
55
 
56
 
57
+ def TTS_infer(text, sdp_ratio, noise_scale, noise_scale_w, length_scale, sid):
58
  global net_g
59
  bert, phones, tones, lang_ids = get_text(text, "ZH", hps)
60
  with torch.no_grad():
 
88
 
89
  def tts_fn(text, speaker, sdp_ratio, noise_scale, noise_scale_w, length_scale):
90
  with torch.no_grad():
91
+ audio = TTS_infer(
92
  text,
93
  sdp_ratio=sdp_ratio,
94
  noise_scale=noise_scale,
 
121
 
122
 
123
  def read_text(file_path: str):
124
+ with open(file_path, "r", encoding="utf-8") as file:
125
+ content = file.read()
126
+ return content
 
 
 
 
 
 
 
 
 
 
 
127
 
128
 
129
+ def infer_upl(text, speaker, sdp_ratio, noise_scale, noise_scale_w, length_scale):
130
+ status = "Success"
131
+ audio = content = None
132
  try:
133
  content = read_text(text)
134
  sentences = text_splitter(content)
 
136
  for sentence in tqdm(sentences, desc="TTS 推理中..."):
137
  with torch.no_grad():
138
  audios.append(
139
+ TTS_infer(
140
  sentence,
141
  sdp_ratio=sdp_ratio,
142
  noise_scale=noise_scale,
 
146
  )
147
  )
148
 
149
+ audio = concatenate_audios(audios, hps.data.sampling_rate)
150
 
151
  except Exception as e:
152
+ status = f"{e}"
153
 
154
+ return status, audio, content
155
 
156
+
157
+ def infer_txt(content, speaker, sdp_ratio, noise_scale, noise_scale_w, length_scale):
158
+ status = "Success"
159
+ audio = None
160
  try:
161
  sentences = text_splitter(content)
162
  audios = []
163
  for sentence in tqdm(sentences, desc="TTS 推理中..."):
164
  with torch.no_grad():
165
  audios.append(
166
+ TTS_infer(
167
  sentence,
168
  sdp_ratio=sdp_ratio,
169
  noise_scale=noise_scale,
 
173
  )
174
  )
175
 
176
+ audio = concatenate_audios(audios, hps.data.sampling_rate)
177
 
178
  except Exception as e:
179
+ status = f"{e}"
180
+
181
+ return status, audio
182
 
183
 
184
  if __name__ == "__main__":
 
218
 
219
  with gr.Tab(_L("输入模式")):
220
  gr.Interface(
221
+ fn=infer_txt, # 使用 text_to_speech 函数
222
  inputs=[
223
  gr.TextArea(
224
  label=_L("请输入简体中文文案"),
 
247
  minimum=0.1, maximum=2, value=1, step=0.1, label=_L("生成时长")
248
  ),
249
  ],
250
+ outputs=[
251
+ gr.Textbox(label=_L("状态栏"), show_copy_button=True),
252
+ gr.Audio(label=_L("输出音频")),
253
+ ],
254
  flagging_mode="never",
255
  concurrency_limit=4,
256
  )
257
 
258
  with gr.Tab(_L("上传模式")):
259
  gr.Interface(
260
+ fn=infer_upl, # 使用 text_to_speech 函数
261
  inputs=[
262
  gr.components.File(
263
  label=_L("请上传简体中文 TXT 文案"),
 
287
  ),
288
  ],
289
  outputs=[
290
+ gr.Textbox(label=_L("状态栏"), show_copy_button=True),
291
  gr.Audio(label=_L("输出音频")),
292
  gr.TextArea(label=_L("文案提取结果"), show_copy_button=True),
293
  ],