Spaces:
Runtime error
Runtime error
TranGiaBao
commited on
Commit
·
d08ac9f
1
Parent(s):
41bb41d
Update app.py
Browse files
app.py
CHANGED
@@ -9,22 +9,25 @@ import numpy as np
|
|
9 |
import math
|
10 |
import re
|
11 |
|
12 |
-
model_name = "trangiabao17032000/model_tts_mix"
|
13 |
-
|
14 |
#processor
|
15 |
-
processor = SpeechT5Processor.from_pretrained(
|
16 |
tokenizer = processor.tokenizer
|
17 |
|
18 |
#model
|
19 |
-
|
|
|
|
|
|
|
|
|
20 |
|
21 |
#vocoder
|
22 |
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
|
|
|
23 |
|
24 |
#speaker embedding
|
25 |
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
26 |
|
27 |
-
speaker_embeddings = torch.tensor(embeddings_dataset[
|
28 |
|
29 |
#cleaner text
|
30 |
def convert_string_to_numbers(input_str):
|
@@ -120,10 +123,15 @@ def cleanup_text(inputs):
|
|
120 |
return re.sub('[0-9]', '', inputs.strip().lower())
|
121 |
|
122 |
# generator speech
|
123 |
-
def text_to_speech(paragraph):
|
124 |
if len(paragraph.strip()) == 0:
|
125 |
return (16000, np.zeros(0).astype(np.int16))
|
126 |
|
|
|
|
|
|
|
|
|
|
|
127 |
try:
|
128 |
paragraph = normalize(str(paragraph))
|
129 |
except:
|
@@ -135,7 +143,7 @@ def text_to_speech(paragraph):
|
|
135 |
for sentence in list_sentence:
|
136 |
|
137 |
inputs = processor(text=sentence, return_tensors="pt")
|
138 |
-
spectrogram =
|
139 |
with torch.no_grad():
|
140 |
speech = vocoder(spectrogram)
|
141 |
|
@@ -146,10 +154,14 @@ def text_to_speech(paragraph):
|
|
146 |
# sf.write("tts_example.wav", final_speech, samplerate=16000)
|
147 |
# return "tts_example.wav"
|
148 |
|
|
|
149 |
tts_examples = [
|
150 |
-
"xin chào mọi người, đây là sản phẩm thử nghiệm cho tiếng việt.",
|
151 |
-
"
|
152 |
-
"
|
|
|
|
|
|
|
153 |
]
|
154 |
|
155 |
title = "SpeechT5:Text-To-Speech"
|
@@ -158,8 +170,15 @@ description ="Nhập bất kỳ văn bản nào và mô hình sẽ chuyển nó
|
|
158 |
#gradio interface
|
159 |
iface = gr.Interface(
|
160 |
fn=text_to_speech,
|
161 |
-
inputs=
|
162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
title=title,
|
164 |
description=description,
|
165 |
examples=tts_examples
|
|
|
9 |
import math
|
10 |
import re
|
11 |
|
|
|
|
|
12 |
#processor
|
13 |
+
processor = SpeechT5Processor.from_pretrained("trangiabao17032000/final_tts")
|
14 |
tokenizer = processor.tokenizer
|
15 |
|
16 |
#model
|
17 |
+
model_model_tts_mix = SpeechT5ForTextToSpeech.from_pretrained("trangiabao17032000/model_tts_mix")
|
18 |
+
model_model_tts_mix.eval()
|
19 |
+
|
20 |
+
model_final_tts = SpeechT5ForTextToSpeech.from_pretrained("trangiabao17032000/final_tts")
|
21 |
+
model_final_tts.eval()
|
22 |
|
23 |
#vocoder
|
24 |
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
|
25 |
+
vocoder.eval()
|
26 |
|
27 |
#speaker embedding
|
28 |
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
29 |
|
30 |
+
speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
|
31 |
|
32 |
#cleaner text
|
33 |
def convert_string_to_numbers(input_str):
|
|
|
123 |
return re.sub('[0-9]', '', inputs.strip().lower())
|
124 |
|
125 |
# generator speech
|
126 |
+
def text_to_speech(paragraph, model_selected):
|
127 |
if len(paragraph.strip()) == 0:
|
128 |
return (16000, np.zeros(0).astype(np.int16))
|
129 |
|
130 |
+
if model_selected == "final_tts":
|
131 |
+
model_generate = model_final_tts
|
132 |
+
else:
|
133 |
+
model_generate = model_model_tts_mix
|
134 |
+
|
135 |
try:
|
136 |
paragraph = normalize(str(paragraph))
|
137 |
except:
|
|
|
143 |
for sentence in list_sentence:
|
144 |
|
145 |
inputs = processor(text=sentence, return_tensors="pt")
|
146 |
+
spectrogram = model_generate.generate_speech(inputs["input_ids"], speaker_embeddings)
|
147 |
with torch.no_grad():
|
148 |
speech = vocoder(spectrogram)
|
149 |
|
|
|
154 |
# sf.write("tts_example.wav", final_speech, samplerate=16000)
|
155 |
# return "tts_example.wav"
|
156 |
|
157 |
+
|
158 |
tts_examples = [
|
159 |
+
["xin chào mọi người, đây là sản phẩm thử nghiệm cho tiếng việt.", "final_tts"],
|
160 |
+
["xin chào mọi người, đây là sản phẩm thử nghiệm cho tiếng việt.", "model_tts_mix"],
|
161 |
+
["Mình sẽ tổ chức sinh nhật vào thứ 6 ngày 7 tháng này", "final_tts"],
|
162 |
+
["Mình sẽ tổ chức sinh nhật vào thứ 6 ngày 7 tháng này", "model_tts_mix"],
|
163 |
+
["Mùa thu đã đến với sự thanh bình và mát mẻ. Trời cao trải dài một tấm bầu trời xanh thăm thẳm, và những tia nắng ấm áp từ mặt trời chiếu sáng tỏa rạng. Nhiệt độ trong khoảng từ 20 đến 25 độ Celsius khiến cho không khí trở nên dễ chịu, đủ để ta cảm nhận sự se lạnh của mùa thu đang về. Các cây cỏ bắt đầu thay đổi màu sắc, chuyển từ màu xanh tươi sang những gam màu ấm áp và rực rỡ. Mọi người bắt đầu khoác lên mình những chiếc áo len mỏng và khăn quàng cổ để giữ ấm. Mùa thu thật sự là thời điểm tuyệt vời để thưởng thức cái se lạnh dịu dàng và cảm nhận sự thay đổi của thiên nhiên.", "final_tts"],
|
164 |
+
["Mùa thu đã đến với sự thanh bình và mát mẻ. Trời cao trải dài một tấm bầu trời xanh thăm thẳm, và những tia nắng ấm áp từ mặt trời chiếu sáng tỏa rạng. Nhiệt độ trong khoảng từ 20 đến 25 độ Celsius khiến cho không khí trở nên dễ chịu, đủ để ta cảm nhận sự se lạnh của mùa thu đang về. Các cây cỏ bắt đầu thay đổi màu sắc, chuyển từ màu xanh tươi sang những gam màu ấm áp và rực rỡ. Mọi người bắt đầu khoác lên mình những chiếc áo len mỏng và khăn quàng cổ để giữ ấm. Mùa thu thật sự là thời điểm tuyệt vời để thưởng thức cái se lạnh dịu dàng và cảm nhận sự thay đổi của thiên nhiên.", "model_tts_mix"]
|
165 |
]
|
166 |
|
167 |
title = "SpeechT5:Text-To-Speech"
|
|
|
170 |
#gradio interface
|
171 |
iface = gr.Interface(
|
172 |
fn=text_to_speech,
|
173 |
+
inputs=[
|
174 |
+
gr.Textbox("xin chào mọi người, đây là sản phẩm thử nghiệm cho tiếng việt.", label="Văn bản đầu vào"),
|
175 |
+
gr.Radio(label="Model", choices=[
|
176 |
+
"final_tts",
|
177 |
+
"model_tts_mix"
|
178 |
+
],
|
179 |
+
value="final_tts"),
|
180 |
+
],
|
181 |
+
outputs=gr.Audio(label="Audio kết quả", streaming= True),
|
182 |
title=title,
|
183 |
description=description,
|
184 |
examples=tts_examples
|