Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import time
|
3 |
+
import gradio as gr
|
4 |
+
import wave
|
5 |
+
import numpy as np
|
6 |
+
from io import BytesIO
|
7 |
+
from huggingface_hub import login, hf_hub_download
|
8 |
+
from piper import PiperVoice
|
9 |
+
from vinorm import TTSnorm
|
10 |
+
login(os.environ["hf_token"])
|
11 |
+
|
12 |
+
def normalize_vietnamese_text(text):
|
13 |
+
text = (
|
14 |
+
TTSnorm(text, unknown=False, lower=False, rule=True)
|
15 |
+
.replace("..", ".")
|
16 |
+
.replace("!.", "!")
|
17 |
+
.replace("?.", "?")
|
18 |
+
.replace(" .", ".")
|
19 |
+
.replace(" ,", ",")
|
20 |
+
.replace('"', "")
|
21 |
+
.replace("'", "")
|
22 |
+
.replace("AI", "Ây Ai")
|
23 |
+
.replace("A.I", "Ây Ai")
|
24 |
+
)
|
25 |
+
return text
|
26 |
+
|
27 |
+
def synthesize_speech(text, sentence_silence, length_scale, normalize_text=True):
|
28 |
+
model_path = hf_hub_download(
|
29 |
+
repo_id="nam194/piper-tts-w5n",
|
30 |
+
filename="tts_model.onnx"
|
31 |
+
)
|
32 |
+
config_path = hf_hub_download(
|
33 |
+
repo_id="nam194/piper-tts-w5n",
|
34 |
+
filename="tts_model.onnx.json"
|
35 |
+
)
|
36 |
+
if normalize_text:
|
37 |
+
text = normalize_vietnamese_text(text)
|
38 |
+
|
39 |
+
voice = PiperVoice.load(model_path, config_path)
|
40 |
+
buffer = BytesIO()
|
41 |
+
start = time.time()
|
42 |
+
with wave.open(buffer, "wb") as wav_file:
|
43 |
+
wav_file.setframerate(voice.config.sample_rate)
|
44 |
+
wav_file.setsampwidth(2)
|
45 |
+
wav_file.setnchannels(1)
|
46 |
+
voice.synthesize(
|
47 |
+
text, wav_file, sentence_silence=sentence_silence, length_scale=length_scale
|
48 |
+
)
|
49 |
+
|
50 |
+
buffer.seek(0)
|
51 |
+
audio_data = np.frombuffer(buffer.read(), dtype=np.int16)
|
52 |
+
inference_time = time.time() - start
|
53 |
+
return (voice.config.sample_rate, audio_data), "{}s".format(inference_time)
|
54 |
+
|
55 |
+
|
56 |
+
with gr.Blocks(analytics_enabled=False) as demo:
|
57 |
+
with gr.Row():
|
58 |
+
with gr.Column():
|
59 |
+
gr.Markdown(
|
60 |
+
"""
|
61 |
+
# Vietnamese Text-to-speech Demo ✨
|
62 |
+
"""
|
63 |
+
)
|
64 |
+
with gr.Column():
|
65 |
+
pass
|
66 |
+
|
67 |
+
with gr.Row():
|
68 |
+
with gr.Column():
|
69 |
+
input_text_gr = gr.Textbox(
|
70 |
+
label="Text Prompt (Văn bản cần đọc)",
|
71 |
+
info="Mỗi câu nên từ 10 từ trở lên.",
|
72 |
+
value="Xin chào, tôi là một mô hình chuyển đổi văn bản thành giọng nói tiếng Việt.",
|
73 |
+
)
|
74 |
+
sentence_silence = gr.Slider(
|
75 |
+
label="Khoảng lặng giữa câu (giây)",
|
76 |
+
minimum=0.0,
|
77 |
+
maximum=2.0,
|
78 |
+
step=0.05,
|
79 |
+
value=0.1,
|
80 |
+
info="Điều chỉnh độ dài khoảng lặng giữa các câu."
|
81 |
+
)
|
82 |
+
length_scale = gr.Slider(
|
83 |
+
label="Tốc độ đọc",
|
84 |
+
minimum=0.5,
|
85 |
+
maximum=2.0,
|
86 |
+
step=0.05,
|
87 |
+
value=1.0,
|
88 |
+
info="Điều chỉnh tốc độ đọc (1.0 là tốc độ bình thường)."
|
89 |
+
)
|
90 |
+
normalize_text = gr.Checkbox(
|
91 |
+
label="Chuẩn hóa văn bản tiếng Việt",
|
92 |
+
info="Normalize Vietnamese text",
|
93 |
+
value=True,
|
94 |
+
)
|
95 |
+
submit_button = gr.Button(
|
96 |
+
"Đọc 🗣️🔥",
|
97 |
+
elem_id="send-btn",
|
98 |
+
visible=True,
|
99 |
+
variant="primary",
|
100 |
+
)
|
101 |
+
with gr.Column():
|
102 |
+
output_audio = gr.Audio(
|
103 |
+
label="Synthesised Audio",
|
104 |
+
autoplay=True,
|
105 |
+
)
|
106 |
+
out_text_gr = gr.Text(label="Metrics")
|
107 |
+
|
108 |
+
submit_button.click(
|
109 |
+
synthesize_speech,
|
110 |
+
inputs=[input_text,
|
111 |
+
sentence_silence,
|
112 |
+
length_scale,
|
113 |
+
normalize_text],
|
114 |
+
outputs=[output_audio, out_text_gr],
|
115 |
+
)
|
116 |
+
|
117 |
+
demo.launch()
|