-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgentle.py
168 lines (124 loc) · 3.99 KB
/
gentle.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
#!/usr/bin/env python
import multiprocessing
multiprocessing.freeze_support()
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtCore import pyqtSignal
import logging
import os
import tarfile
import tempfile
import threading
from twisted.internet import reactor
import urllib
import webbrowser
import sys
from gentle.__version__ import __version__
from gentle.util.paths import get_resource
import serve
def get_open_port(desired=0):
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind(("",desired))
except socket.error:
return get_open_port(0)
s.listen(1)
port = s.getsockname()[1]
s.close()
return port
PORT = get_open_port(8765)
# Start a thread for the web server.
webthread = threading.Thread(target=serve.serve, args=(PORT,))
webthread.start()
def open_browser():
webbrowser.open("http://localhost:%d/" % (PORT))
def open_about():
webbrowser.open("https://lowerquality.com/gentle")
dl = None
progress = None
fstpath = get_resource('exp/tdnn_7b_chain_online/graph_pp/HCLG.fst')
def download_full_language_model():
global progress, dl
fstdir = os.path.dirname(fstpath)
try:
os.makedirs(fstdir)
except OSError:
pass
progress = QtWidgets.QProgressBar()
progress.setRange(0, 100)
trans.hide()
layout.removeWidget(trans)
dltxt = QtWidgets.QLabel('Downloading language model...')
layout.addWidget(dltxt)
layout.addWidget(progress)
dl = DLThread()
dl.start()
dl.percent.connect(progress.setValue)
dl.finished.connect(prompt_to_relaunch)
def prompt_to_relaunch():
msg = QtWidgets.QMessageBox()
msg.setText("Success! Relaunch Gentle to finish enabling transcription.")
msg.exec_()
quit_server()
class DLThread(QtCore.QThread):
percent = pyqtSignal(int)
def run(self):
url = "https://lowerquality.com/gentle/aspire-hclg.tar.gz"
CHUNK = 128 * 1024
with tempfile.NamedTemporaryFile(suffix=".tar.gz") as fp:
dl = urllib.request.urlopen(url)
size = int(dl.headers['content-length'])
cur_size = 0
while True:
buf = dl.read(CHUNK)
if not buf:
break
fp.write(buf)
cur_size += len(buf)
self.percent.emit(int(100 * (cur_size / float(size))))
fp.seek(0)
# done! uncompress to final location
with open(fstpath, 'wb') as fstout:
tar = tarfile.open(fp.name, 'r:gz')
hclg_info = tar.getmember("exp/tdnn_7b_chain_online/graph_pp/HCLG.fst")
hclg = tar.extractfile(hclg_info)
cur_size = 0
while True:
buf = hclg.read(CHUNK)
if not buf:
break
fstout.write(buf)
cur_size += len(buf)
self.percent.emit(int(100 * (cur_size / float(hclg_info.size)))) # !!!
app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QWidget()
w.resize(250, 150)
w.setWindowTitle('Gentle')
def quit_server():
app.exit()
layout = QtWidgets.QVBoxLayout()
w.setLayout(layout)
txt = QtWidgets.QLabel('''Gentle v%s
A robust yet lenient forced-aligner built on Kaldi.''' % (__version__))
layout.addWidget(txt)
btn = QtWidgets.QPushButton('Open in browser')
btn.setStyleSheet("font-weight: bold;")
layout.addWidget(btn)
btn.clicked.connect(open_browser)
abt = QtWidgets.QPushButton('About Gentle')
layout.addWidget(abt)
abt.clicked.connect(open_about)
if not os.path.exists(fstpath):
trans = QtWidgets.QPushButton('Enable full transcription')
layout.addWidget(trans)
trans.clicked.connect(download_full_language_model)
quitb = QtWidgets.QPushButton('Quit')
layout.addWidget(quitb)
quitb.clicked.connect(quit_server)
w.show()
w.raise_()
w.activateWindow()
app.exec_()
logging.info("Waiting for server to quit.")
reactor.callFromThread(reactor.stop)
webthread.join()