Spaces:
Runtime error
Runtime error
liuzhao1225
commited on
Commit
•
d384ee9
1
Parent(s):
fc5adea
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from TTS.api import TTS
|
3 |
+
import torch
|
4 |
+
import os
|
5 |
+
import random
|
6 |
+
import string
|
7 |
+
from cn_tx import TextNorm
|
8 |
+
normalizer = TextNorm()
|
9 |
+
|
10 |
+
def preprocess(text):
|
11 |
+
# text = text.replace("掌柜", "涨柜")
|
12 |
+
text = normalizer(text)
|
13 |
+
return text
|
14 |
+
|
15 |
+
def tts_function(voice_file, text):
|
16 |
+
speaker_wav = voice_file
|
17 |
+
text = preprocess(text)
|
18 |
+
# Generate a random filename with 16 characters
|
19 |
+
random_filename = ''.join(random.choices(
|
20 |
+
string.ascii_letters + string.digits, k=16))
|
21 |
+
file_path = f"output/{random_filename}.wav"
|
22 |
+
if not os.path.exists("output"):
|
23 |
+
os.makedirs("output")
|
24 |
+
|
25 |
+
# Run TTS
|
26 |
+
tts.tts_to_file(text=text, speaker_wav=speaker_wav,
|
27 |
+
language="zh-cn", file_path=file_path)
|
28 |
+
|
29 |
+
return file_path
|
30 |
+
|
31 |
+
|
32 |
+
# Create the Gradio interface with Chinese labels and placeholders
|
33 |
+
iface = gr.Interface(
|
34 |
+
fn=tts_function,
|
35 |
+
inputs=[
|
36 |
+
gr.Audio(sources=["upload", "microphone"],
|
37 |
+
type="filepath", label="上传或录制声音"),
|
38 |
+
gr.Textbox(lines=2, placeholder="请输入要转换的文字...", label="文本输入"),
|
39 |
+
],
|
40 |
+
outputs=gr.Audio(type="filepath", label="生成的语音"),
|
41 |
+
title="数字栩生声音克隆",
|
42 |
+
allow_flagging='never'
|
43 |
+
)
|
44 |
+
|
45 |
+
if __name__ == '__main__':
|
46 |
+
# Initialize TTS
|
47 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
48 |
+
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(device)
|
49 |
+
# tts = None
|
50 |
+
iface.launch(inbrowser=True)
|