Spaces:
Runtime error
Runtime error
TranGiaBao
commited on
Commit
·
41bb41d
1
Parent(s):
77852b1
Update app.py
Browse files
app.py
CHANGED
@@ -6,7 +6,10 @@ from transformers import SpeechT5HifiGan
|
|
6 |
from datasets import load_dataset
|
7 |
from IPython.display import Audio
|
8 |
import numpy as np
|
9 |
-
|
|
|
|
|
|
|
10 |
|
11 |
#processor
|
12 |
processor = SpeechT5Processor.from_pretrained(model_name)
|
@@ -14,16 +17,14 @@ tokenizer = processor.tokenizer
|
|
14 |
|
15 |
#model
|
16 |
model = SpeechT5ForTextToSpeech.from_pretrained(model_name)
|
17 |
-
model.resize_token_embeddings(len(tokenizer))
|
18 |
-
model.eval()
|
19 |
|
20 |
#vocoder
|
21 |
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
|
22 |
-
vocoder.eval()
|
23 |
|
24 |
#speaker embedding
|
25 |
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
26 |
-
|
|
|
27 |
|
28 |
#cleaner text
|
29 |
def convert_string_to_numbers(input_str):
|
@@ -82,16 +83,18 @@ def number_to_vietnamese_words(number):
|
|
82 |
result_decimal += ' ' + ones[int(digit)]
|
83 |
|
84 |
return result_integer + result_decimal
|
85 |
-
|
86 |
def is_num(string):
|
87 |
try:
|
88 |
-
float(string)
|
|
|
|
|
89 |
except ValueError:
|
90 |
return False
|
91 |
return True
|
92 |
-
|
93 |
def normalize(input):
|
94 |
-
input = input.lower()
|
95 |
newstr = map(lambda x: number_to_vietnamese_words(convert_string_to_numbers(x)) if is_num(x) else x, input.split(" "))
|
96 |
return ' '.join(newstr)
|
97 |
|
@@ -111,38 +114,54 @@ def split_paragraph_into_sentences(paragraph, max_chars = 300):
|
|
111 |
sentences.append(current_sentence)
|
112 |
|
113 |
return sentences
|
|
|
|
|
|
|
|
|
114 |
|
115 |
# generator speech
|
116 |
def text_to_speech(paragraph):
|
|
|
|
|
|
|
117 |
try:
|
118 |
-
|
119 |
except:
|
120 |
-
|
|
|
121 |
list_sentence = split_paragraph_into_sentences(paragraph)
|
|
|
122 |
final_speech = np.array([])
|
123 |
-
|
124 |
for sentence in list_sentence:
|
125 |
|
126 |
inputs = processor(text=sentence, return_tensors="pt")
|
127 |
-
|
|
|
|
|
|
|
128 |
final_speech = np.concatenate((final_speech, speech.numpy()))
|
129 |
-
|
130 |
-
|
131 |
-
return
|
|
|
|
|
132 |
|
133 |
tts_examples = [
|
134 |
"xin chào mọi người, đây là sản phẩm thử nghiệm cho tiếng việt.",
|
135 |
"Mình sẽ tổ chức sinh nhật vào thứ 6 ngày 7 tháng này",
|
|
|
136 |
]
|
137 |
|
|
|
|
|
|
|
138 |
#gradio interface
|
139 |
iface = gr.Interface(
|
140 |
fn=text_to_speech,
|
141 |
-
inputs=gr.Textbox(),
|
142 |
-
outputs=gr.Audio(),
|
143 |
-
title=
|
144 |
-
|
145 |
-
|
146 |
)
|
147 |
-
|
148 |
iface.launch()
|
|
|
6 |
from datasets import load_dataset
|
7 |
from IPython.display import Audio
|
8 |
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(model_name)
|
|
|
17 |
|
18 |
#model
|
19 |
model = SpeechT5ForTextToSpeech.from_pretrained(model_name)
|
|
|
|
|
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[7200]["xvector"]).unsqueeze(0)
|
28 |
|
29 |
#cleaner text
|
30 |
def convert_string_to_numbers(input_str):
|
|
|
83 |
result_decimal += ' ' + ones[int(digit)]
|
84 |
|
85 |
return result_integer + result_decimal
|
86 |
+
|
87 |
def is_num(string):
|
88 |
try:
|
89 |
+
temp = float(string)
|
90 |
+
if math.isnan(temp):
|
91 |
+
return False
|
92 |
except ValueError:
|
93 |
return False
|
94 |
return True
|
95 |
+
|
96 |
def normalize(input):
|
97 |
+
input = input.strip().lower()
|
98 |
newstr = map(lambda x: number_to_vietnamese_words(convert_string_to_numbers(x)) if is_num(x) else x, input.split(" "))
|
99 |
return ' '.join(newstr)
|
100 |
|
|
|
114 |
sentences.append(current_sentence)
|
115 |
|
116 |
return sentences
|
117 |
+
|
118 |
+
|
119 |
+
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:
|
130 |
+
paragraph = cleanup_text(paragraph)
|
131 |
+
|
132 |
list_sentence = split_paragraph_into_sentences(paragraph)
|
133 |
+
|
134 |
final_speech = np.array([])
|
|
|
135 |
for sentence in list_sentence:
|
136 |
|
137 |
inputs = processor(text=sentence, return_tensors="pt")
|
138 |
+
spectrogram = model.generate_speech(inputs["input_ids"], speaker_embeddings)
|
139 |
+
with torch.no_grad():
|
140 |
+
speech = vocoder(spectrogram)
|
141 |
+
|
142 |
final_speech = np.concatenate((final_speech, speech.numpy()))
|
143 |
+
|
144 |
+
final_speech = (final_speech * 32767).astype(np.int16)
|
145 |
+
return (16000, final_speech)
|
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 |
"Mình sẽ tổ chức sinh nhật vào thứ 6 ngày 7 tháng này",
|
152 |
+
"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."
|
153 |
]
|
154 |
|
155 |
+
title = "SpeechT5:Text-To-Speech"
|
156 |
+
|
157 |
+
description ="Nhập bất kỳ văn bản nào và mô hình sẽ chuyển nó thành giọng nói."
|
158 |
#gradio interface
|
159 |
iface = gr.Interface(
|
160 |
fn=text_to_speech,
|
161 |
+
inputs=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"),
|
162 |
+
outputs=gr.Audio(label="Audio kết quả"),
|
163 |
+
title=title,
|
164 |
+
description=description,
|
165 |
+
examples=tts_examples
|
166 |
)
|
|
|
167 |
iface.launch()
|