add flask
Browse files
server.py
ADDED
@@ -0,0 +1,187 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask
|
2 |
+
from flask import Flask, request, jsonify
|
3 |
+
import yaml
|
4 |
+
from vietTTS.hifigan.mel2wave import mel2wave
|
5 |
+
from vietTTS.nat.text2mel import text2mel
|
6 |
+
from vietTTS.synthesizer import nat_normalize_text
|
7 |
+
import numpy as np
|
8 |
+
import gradio as gr
|
9 |
+
import re
|
10 |
+
from vietnam_number import n2w
|
11 |
+
from vietnam_number import n2w_single
|
12 |
+
from synthesize import synthesizer
|
13 |
+
import noisereduce as nr
|
14 |
+
import os
|
15 |
+
import scipy.io.wavfile as wavf
|
16 |
+
from scipy.io import wavfile
|
17 |
+
|
18 |
+
app = Flask(__name__)
|
19 |
+
|
20 |
+
@app.route('/hello/', methods=['GET', 'POST'])
|
21 |
+
def welcome():
|
22 |
+
return "Hello World!"
|
23 |
+
|
24 |
+
|
25 |
+
@app.route('/tts',methods = ['POST'])
|
26 |
+
def tts():
|
27 |
+
text = request.json['data'][0]
|
28 |
+
model = request.json['data'][1]
|
29 |
+
duration = request.json['data'][2]
|
30 |
+
if(model == "Tacotron2"):
|
31 |
+
return using_tacotron(text)
|
32 |
+
else:
|
33 |
+
return '1'
|
34 |
+
|
35 |
+
|
36 |
+
|
37 |
+
def text_to_speech(text,stop_duration):
|
38 |
+
print("starting")
|
39 |
+
# prevent too long text
|
40 |
+
if len(text) > 500:
|
41 |
+
text = text[:500]
|
42 |
+
# text_to_speech_tacotron(text)
|
43 |
+
# stop_duration_float = float(stop_duration_text)
|
44 |
+
text = clean_text(text)
|
45 |
+
text = nat_normalize_text(text)
|
46 |
+
mel = text2mel(
|
47 |
+
text,
|
48 |
+
"lexicon.txt",
|
49 |
+
stop_duration,
|
50 |
+
"acoustic_latest_ckpt.pickle",
|
51 |
+
"duration_latest_ckpt.pickle",
|
52 |
+
)
|
53 |
+
wave = mel2wave(mel, "config.json", "hk_hifi.pickle")
|
54 |
+
return (wave * (2**15)).astype(np.int16)
|
55 |
+
|
56 |
+
def text_to_speech_tacotron(text):
|
57 |
+
print("starting")
|
58 |
+
# prevent too long text
|
59 |
+
if len(text) > 500:
|
60 |
+
text = text[:500]
|
61 |
+
|
62 |
+
wav = synthesizer.tts(text)
|
63 |
+
output = './out.wav'
|
64 |
+
|
65 |
+
synthesizer.save_wav(wav,output)
|
66 |
+
return output
|
67 |
+
|
68 |
+
|
69 |
+
|
70 |
+
def using_viettts(text,stop_duration):
|
71 |
+
y = text_to_speech(text,stop_duration)
|
72 |
+
fs = 16000
|
73 |
+
output = './out.wav'
|
74 |
+
output_denoise = './output_denoise.wav'
|
75 |
+
|
76 |
+
wavf.write(output, fs, y)
|
77 |
+
rate, data = wavfile.read(output)
|
78 |
+
# perform noise reduction
|
79 |
+
reduced_noise = nr.reduce_noise(y=data, sr=rate)
|
80 |
+
wavfile.write(output_denoise, rate, reduced_noise)
|
81 |
+
return (output,output_denoise)
|
82 |
+
|
83 |
+
def using_tacotron(text):
|
84 |
+
y = text_to_speech_tacotron(text)
|
85 |
+
output_denoise = "./output_denoise.wav"
|
86 |
+
rate, data = wavfile.read(y)
|
87 |
+
# perform noise reduction
|
88 |
+
reduced_noise = nr.reduce_noise(y=data, sr=rate)
|
89 |
+
wavfile.write(output_denoise, rate, reduced_noise)
|
90 |
+
return (y,output_denoise)
|
91 |
+
|
92 |
+
|
93 |
+
def clean_text(test_string):
|
94 |
+
list_word = test_string.split()
|
95 |
+
# print(list_word)
|
96 |
+
|
97 |
+
regex = r"\d{2}(?P<sep>[-/])\d{1,2}(?P=sep)\d{4}"
|
98 |
+
|
99 |
+
for word in list_word :
|
100 |
+
try:
|
101 |
+
# print(word)
|
102 |
+
searchbox_result = re.match(regex, word)
|
103 |
+
day = searchbox_result.group(0)
|
104 |
+
day2 = day
|
105 |
+
day2 = day2.replace('/',' ').replace('-',' ')
|
106 |
+
list_date = day2.split(' ')
|
107 |
+
date_result = 'Ngày ' + n2w(list_date[0]) + ' tháng ' + n2w(list_date[1].replace('0','') if list_date[1].startswith('0') else list_date[1]) + ' năm ' + n2w(list_date[2])
|
108 |
+
# print(date_result)
|
109 |
+
test_string = test_string.replace(word, date_result)
|
110 |
+
|
111 |
+
except AttributeError:
|
112 |
+
# print(word)
|
113 |
+
# print("can't make a group")
|
114 |
+
continue
|
115 |
+
|
116 |
+
|
117 |
+
# print(test_string)
|
118 |
+
|
119 |
+
regex2 = r"\d{2}(?P<sep>[-/])\d{1,2}"
|
120 |
+
|
121 |
+
for word in list_word :
|
122 |
+
try:
|
123 |
+
# print(word)
|
124 |
+
searchbox_result = re.match(regex2, word)
|
125 |
+
day = searchbox_result.group(0)
|
126 |
+
day2 = day
|
127 |
+
day2 = day2.replace('/',' ').replace('-',' ')
|
128 |
+
list_date = day2.split(' ')
|
129 |
+
date_result = 'Ngày ' + n2w(list_date[0]) + ' tháng ' + n2w(list_date[1].replace('0','') if list_date[1].startswith('0') else list_date[1])
|
130 |
+
# print(date_result)
|
131 |
+
test_string = test_string.replace(word, date_result)
|
132 |
+
|
133 |
+
except AttributeError:
|
134 |
+
# print(word)
|
135 |
+
# print("can't make a group")
|
136 |
+
continue
|
137 |
+
|
138 |
+
|
139 |
+
# print(test_string)
|
140 |
+
|
141 |
+
regex3 = r"\d{1,2}(?P<sep>[h:])\d{1,2}"
|
142 |
+
|
143 |
+
for word in list_word :
|
144 |
+
try:
|
145 |
+
# print(word)
|
146 |
+
searchbox_result = re.match(regex3, word)
|
147 |
+
day = searchbox_result.group(0)
|
148 |
+
day2 = day
|
149 |
+
day2 = day2.replace('h',' ').replace(':',' ')
|
150 |
+
list_date = day2.split(' ')
|
151 |
+
date_result = n2w(list_date[0]) + ' giờ ' + n2w(list_date[1].replace('0','') if list_date[1].startswith('0') else list_date[1]) + ' phút '
|
152 |
+
# print(date_result)
|
153 |
+
test_string = test_string.replace(word, date_result)
|
154 |
+
|
155 |
+
except AttributeError:
|
156 |
+
# print(word)
|
157 |
+
# print("can't make a group")
|
158 |
+
continue
|
159 |
+
|
160 |
+
|
161 |
+
print(test_string)
|
162 |
+
|
163 |
+
for word in list_word :
|
164 |
+
try:
|
165 |
+
if word.isdigit() :
|
166 |
+
# print(word)
|
167 |
+
text_result = n2w_single(word)
|
168 |
+
# print(text_result)
|
169 |
+
test_string = test_string.replace(word, text_result, 1)
|
170 |
+
|
171 |
+
except AttributeError:
|
172 |
+
# print(word)
|
173 |
+
print("can't make a group")
|
174 |
+
continue
|
175 |
+
|
176 |
+
|
177 |
+
return test_string
|
178 |
+
|
179 |
+
|
180 |
+
|
181 |
+
|
182 |
+
|
183 |
+
|
184 |
+
|
185 |
+
|
186 |
+
if __name__ == '__main__':
|
187 |
+
app.run(host='0.0.0.0', port=105)
|