-
Notifications
You must be signed in to change notification settings - Fork 33
/
t2sUI.py
288 lines (255 loc) · 11.3 KB
/
t2sUI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import (QApplication, QDialog, QMainWindow, QMessageBox)
# from _zaloapi import final_path_mp3
import sys
import requests
import os
import random
import re
import time
import nltk
from urllib.parse import quote
from bs4 import BeautifulSoup
class text2voice:
def get_free_proxies():
url = "https://free-proxy-list.net/"
# request and grab content
soup = BeautifulSoup(requests.get(url).content, 'html.parser')
# to store proxies
proxies = []
for row in soup.find("table", attrs={"class": "table table-striped table-bordered"}).find_all("tr")[1:]:
tds = row.find_all("td")
try:
ip = tds[0].text.strip()
port = tds[1].text.strip()
proxies.append(str(ip) + ":" + str(port))
except IndexError:
continue
return proxies
def zalo_api(payload, voidid, speed):
# get proxies
proxies = []
proxies = text2voice.get_free_proxies()
url = "https://zalo.ai/api/demo/v1/tts/synthesize"
f = open("output.txt", "w")
links = []
source = "https://zalo.ai/"
cookie=''
for p in payload:
session = requests.Session()
proxy = random.choice(proxies)
phttp = "http://" + proxy
text = quote(str(p))
response = session.get(source)
cookie = response.cookies.get_dict()
# text.encode('utf-8') # Totally fine.
payload = "input="+text+"&speaker_id=" + \
voidid+"&speed="+speed+"&dict_id=0"
for k,v in cookie.items():
cookie = k+'='+v
headers = {
"content-type": "application/x-www-form-urlencoded; charset=utf-8",
"origin": "https://zalo.ai",
"referer": "https://zalo.ai/experiments/text-to-audio-converter",
"cookie": cookie,
# "cookie": "zai_did=8k9uAj3FNiTevcSSryzXoYYo64l0pcN8AB0TI38m; _ga=GA1.2.306647033.1634021428; zpsid=eMKnVbo-PZEPI60H5S5lHez9Pn8VmNT7aq8hJ5MvGHg0MKn9KeKvKwbBILT8iZPpqrTVLYQd2NAsQqPYEU4IO_S3LdeO_WeqoJK4PHB3Q3V78sLa; zai_sid=deN0KVCzFc28XCTI-JqcOPIFYmopHH0yXvJi2RHvQ7MkXkaNhavdLTIQ_nR0MrXKiQRc8jyTGqNmplOw_W9R4ixuf2NqFpaHmEx59AqVQqYnvyqf; ozi=2000.SSZzejyD0jydXQcYsa00d3xBfxgO71AMSOUclD5I6v9tXQszqXuPbtN8fRZP7nUICJ8p.1; _zlang=vn; _gid=GA1.2.1396954612.1641556131; __zi=3000.SSZzejyD0jydXQcYsa00d3xBfxgP71AM8Tdbg8yDLybdsUFXomnGoctPgUVF3Ht2QDAewSe47yO.1; _gat_gtag_UA_158812682_2=1"
}
response = requests.request("POST", url, data=payload.encode(
'utf-8'), headers=headers, proxies={'http': phttp})
f.write(response.text+"\n")
out = open('output.txt', 'r').read()
links = re.findall(
r'(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}.m3u8)', out)
f.close()
return links
def split_text(payload):
text = []
long_sentence = []
if len(payload) <= 200:
text.append(payload)
return text
elif len(payload) > 200:
sentences = nltk.sent_tokenize(payload)
sub_para = ''
for sen in sentences:
if sub_para == '':
sub_para = sen
elif sub_para != '':
if len(sub_para)+len(sen) <= 200 and sen != sentences[-1]:
sub_para = sub_para + " " + sen
elif len(sub_para)+len(sen) <= 200 and sen == sentences[-1]:
sub_para = sub_para + " " + sen
long_sentence.append(sub_para)
elif len(sub_para)+len(sen) > 200:
long_sentence.append(sub_para)
sub_para = ''
sub_para = sen
elif sen == sentences[-1]:
long_sentence.append(sub_para)
return long_sentence
def connect_audio(links):
id = 1
path = str(os.getcwd())
full = path + '/tmp_audio/'
command = 'cd '+full+' && rm -rf *'
os.system(command)
f = open('list_name.txt', 'w')
for i in links:
url = i
des_fol = str(os.getcwd())+"/tmp_audio/"
namefile = str(id)+".mp3"
command = 'ffmpeg -i '+url+' -ab 256k ' + des_fol + namefile + ' -y'
id = id + 1
os.system(command)
f.write("file '"+full+namefile+"'\n")
f.close()
print("done")
def get_links():
out = open('output.txt', 'r').read()
links = re.findall(
r'(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}.m3u8)', out)
return links
def mer_audio(id):
path_list = str(os.getcwd()) + "/list_name.txt"
path = str(os.getcwd())+"/final_audio/"
mp3_path = path+id+".mp3"
command = 'ffmpeg -f concat -safe 0 -i ' + \
path_list + ' -c copy '+mp3_path + ' -y'
os.system(command)
mp3_path = mp3_path.replace(os.getcwd(), '.')
return mp3_path
class final_path_mp3():
def get_path_mp3(id, payload, voiceid, speed):
path = str(os.getcwd())+"/tmp_audio"
if os.path.exists(path) == False:
os.system("mkdir tmp_audio")
path = str(os.getcwd()) + "/final_audio"
if os.path.exists(path) == False:
os.system("mkdir final_audio")
data = text2voice.split_text(payload)
text2voice.zalo_api(data, voiceid, speed)
links = text2voice.get_links()
text2voice.connect_audio(links)
path = text2voice.mer_audio(id)
return path
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("T2SBT")
MainWindow.resize(647, 412)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.plainTextEdit = QtWidgets.QPlainTextEdit(self.centralwidget)
self.plainTextEdit.setObjectName("plainTextEdit")
self.gridLayout.addWidget(self.plainTextEdit, 0, 1, 1, 1)
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(18)
self.comboBox.setFont(font)
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.gridLayout.addWidget(self.comboBox, 2, 0, 1, 1)
self.label = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(19)
self.label.setFont(font)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 1, 0, 1, 1)
self.label_2 = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(21)
font.setBold(False)
font.setWeight(50)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.gridLayout.addWidget(self.label_2, 0, 0, 1, 1)
self.pushButton = QtWidgets.QPushButton(
self.centralwidget, clicked=lambda: self.do_it())
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(19)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 2, 1, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 647, 24))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
self.menuBart_Tran = QtWidgets.QMenu(self.menubar)
self.menuBart_Tran.setObjectName("menuBart_Tran")
MainWindow.setMenuBar(self.menubar)
self.actionOpen_2 = QtWidgets.QAction(MainWindow)
self.actionOpen_2.setCheckable(False)
self.actionOpen_2.setChecked(False)
self.actionOpen_2.setObjectName("actionOpen_2")
self.actionQuit = QtWidgets.QAction(MainWindow)
self.actionQuit.setObjectName("actionQuit")
self.menuFile.addAction(self.actionOpen_2)
self.menuFile.addSeparator()
self.menuFile.addAction(self.actionQuit)
self.menubar.addAction(self.menuFile.menuAction())
self.menubar.addAction(self.menuBart_Tran.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
MainWindow.setTabOrder(self.plainTextEdit, self.comboBox)
MainWindow.setTabOrder(self.comboBox, self.pushButton)
def do_it(self):
id = "Thank_to_you_for_using_this_program"
payload = self.plainTextEdit.toPlainText()
choice = self.comboBox.currentText()
if choice == "Nữ - Miền Nam":
voiceid = '1'
filename = "Nữ_Miền_Nam" + \
"_" + str(random.randint(1, 10000))
elif choice == "Nam - Miền Nam":
voiceid = '3'
filename = "Nam_Miền_Nam" + \
"_" + str(random.randint(1, 10000))
elif choice == "Nữ - Miền Bắc":
voiceid = '2'
filename = "Nữ_Miền_Bắc" + \
"_" + str(random.randint(1, 10000))
elif choice == "Nam - Miền Bắc":
voiceid = '4'
filename = "Nam_Miền_Bắc" + \
"_" + str(random.randint(1, 10000))
else:
filename = id
voiceid = '1'
filename = str(filename)
path = final_path_mp3.get_path_mp3(
id=filename, payload=payload, voiceid=voiceid, speed="1.0")
print(path)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate(
"MainWindow", "Text to Speech Bart Tran"))
self.comboBox.setItemText(0, _translate("MainWindow", "Chọn..."))
self.comboBox.setItemText(1, _translate("MainWindow", "Nữ - Miền Nam"))
self.comboBox.setItemText(
2, _translate("MainWindow", "Nam - Miền Nam"))
self.comboBox.setItemText(3, _translate("MainWindow", "Nữ - Miền Bắc"))
self.comboBox.setItemText(
4, _translate("MainWindow", "Nam - Miền Bắc"))
self.label.setText(_translate("MainWindow", "Giọng đọc:"))
self.label_2.setText(_translate("MainWindow", "Văn bản:"))
self.pushButton.setText(_translate("MainWindow", "Tạo giọng nói"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.menuBart_Tran.setTitle(_translate("MainWindow", "Bart Tran"))
self.actionOpen_2.setText(_translate("MainWindow", "Open..."))
self.actionQuit.setText(_translate("MainWindow", "Quit"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())