-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
333 lines (263 loc) · 11.7 KB
/
main.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import os
from collections import Counter
from PyQt5.QtCore import QThread, pyqtSignal, QSettings, QCoreApplication, Qt
from PyQt5.QtGui import QFont, QIcon
from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication, QVBoxLayout, QLineEdit, QTextBrowser, QWidget, \
QMessageBox, QGroupBox, QHBoxLayout, QRadioButton, QFrame, QLabel, QMenu, QAction, QSystemTrayIcon
from apiWidget import ApiWidget
from findPathWidget import FindPathWidget
from loadingLbl import LoadingLabel
from notifier import NotifierWidget
from script import install_audio, GPTTranscribeWrapper, remove_trim, convert_to_srt
QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
QCoreApplication.setAttribute(Qt.AA_UseHighDpiPixmaps) # HighDPI support
QApplication.setFont(QFont('Arial', 12))
class Thread1(QThread):
audioReadyFinished = pyqtSignal(str)
def __init__(self, url):
super(Thread1, self).__init__()
self.__url = url
def run(self):
try:
downloaded_file = install_audio(self.__url)
# If you want to trim the video from specific time to specific time, you can use the following line
# But we don't need it currently
dst_filename = remove_trim(downloaded_file)
self.audioReadyFinished.emit(dst_filename)
except Exception as e:
raise Exception(e)
class Thread2(QThread):
afterGenerated = pyqtSignal(list)
def __init__(self, wrapper, dst_filename):
super(Thread2, self).__init__()
self.__wrapper = wrapper
self.__dst_filename = dst_filename
def run(self):
try:
result_obj_lst, result_audio_file_paths = self.__wrapper.transcribe_audio(self.__dst_filename, response_format='verbose_json' ,timestamp_granularities=['segment'])
self.afterGenerated.emit(result_obj_lst)
except Exception as e:
raise Exception(e)
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.__initVal()
self.__initUi()
def __initVal(self):
self.__settings_ini = QSettings(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'settings.ini'), QSettings.Format.IniFormat)
if not self.__settings_ini.contains('API_KEY'):
self.__settings_ini.setValue('API_KEY', '')
self.__api_key = self.__settings_ini.value('API_KEY', type=str)
self.__wrapper = GPTTranscribeWrapper(self.__api_key)
self.__is_local = True
self.__used_language_list = []
self.__duration = 0
self.__is_stopped = False
def __initUi(self):
self.setWindowTitle('PyQt app example of transcribing Youtube video with Whisper')
# Top
# API input
self.__apiWidget = ApiWidget(self.__api_key, wrapper=self.__wrapper, settings=self.__settings_ini)
self.__apiWidget.apiKeyAccepted.connect(self.__api_key_accepted)
self.__fromYoutubeWidget = QLineEdit()
self.__fromYoutubeWidget.setPlaceholderText('Write Youtube video address...')
self.__fromYoutubeWidget.textChanged.connect(self.__setAiEnabled)
self.__fromLocalWidget = FindPathWidget()
self.__fromLocalWidget.setExtOfFiles('Audio Files (*.mp3);; Video Files (*.mp4)')
self.__fromLocalWidget.getLineEdit().setPlaceholderText('Select a file...')
self.__fromLocalWidget.added.connect(self.__setAiEnabled)
fromLocalRadioBtn = QRadioButton('From local')
fromYoutubeRadioBtn = QRadioButton('From Youtube')
fromLocalRadioBtn.toggled.connect(self.__toggleFromWhereRadioWidgets)
fromYoutubeRadioBtn.toggled.connect(self.__toggleFromWhereRadioWidgets)
fromLocalRadioBtn.toggle()
lay = QHBoxLayout()
lay.addWidget(fromLocalRadioBtn)
lay.addWidget(fromYoutubeRadioBtn)
self.__fromWhereGrpBox = QGroupBox()
self.__fromWhereGrpBox.setTitle('From where?')
self.__fromWhereGrpBox.setLayout(lay)
sep = QFrame()
sep.setFrameShape(QFrame.VLine)
sep.setFrameShadow(QFrame.Sunken)
lay = QHBoxLayout()
lay.addWidget(self.__fromLocalWidget)
lay.addWidget(sep)
lay.addWidget(self.__fromYoutubeWidget)
getFromWidget = QWidget()
getFromWidget.setLayout(lay)
self.__loadingLbl = LoadingLabel()
self.__btn = QPushButton('Transcribe the Video')
self.__btn.clicked.connect(self.__run)
self.__btn.setEnabled(False)
self.__browser = QTextBrowser()
self.__browser.setPlaceholderText('Result would be shown here...')
resultGrpBox = QGroupBox()
resultGrpBox.setTitle('Result')
self.__transcriptionLanguageLbl = QLabel('Transcription language: ')
self.__transcriptionDurationLbl = QLabel('Transcription duration: ')
self.__stopBtn = QPushButton('Stop')
self.__stopBtn.clicked.connect(self.__stop)
self.__stopBtn.setVisible(False)
lay = QVBoxLayout()
lay.addWidget(self.__transcriptionLanguageLbl)
lay.addWidget(self.__transcriptionDurationLbl)
descriptionWidget = QWidget()
descriptionWidget.setLayout(lay)
self.__convertToSrtBtn = QPushButton('Convert to SRT')
self.__convertToSrtBtn.clicked.connect(self.__convertToSrt)
self.__convertToSrtBtn.setEnabled(False)
lay = QVBoxLayout()
lay.addWidget(descriptionWidget)
lay.addWidget(self.__browser)
resultGrpBox.setLayout(lay)
lay = QVBoxLayout()
lay.addWidget(self.__apiWidget)
lay.addWidget(self.__fromWhereGrpBox)
lay.addWidget(getFromWidget)
lay.addWidget(self.__btn)
lay.addWidget(self.__loadingLbl)
lay.addWidget(resultGrpBox)
lay.addWidget(self.__stopBtn)
lay.addWidget(self.__convertToSrtBtn)
mainWidget = QWidget()
mainWidget.setLayout(lay)
self.setCentralWidget(mainWidget)
self.__setAiEnabled(self.__wrapper.is_available())
self.__setTrayMenu()
QApplication.setQuitOnLastWindowClosed(False)
def __setTrayMenu(self):
# background app
menu = QMenu()
action = QAction("Quit", self)
action.setIcon(QIcon('ico/close.svg'))
action.triggered.connect(app.quit)
menu.addAction(action)
tray_icon = QSystemTrayIcon(app)
tray_icon.setIcon(QIcon('logo.png'))
tray_icon.activated.connect(self.__activated)
tray_icon.setContextMenu(menu)
tray_icon.show()
def __activated(self, reason):
if reason == QSystemTrayIcon.DoubleClick:
self.show()
def __api_key_accepted(self, api_key, f):
# Enable AI related features if API key is valid
self.__setAiEnabled(f)
def get_current_url(self):
if self.__is_local:
url = self.__fromLocalWidget.getFileName()
else:
url = self.__fromYoutubeWidget.text()
url = url.strip()
return url
def __setAiEnabled(self, f):
if isinstance(f, str):
f = f.strip() != ''
text = self.get_current_url() != ''
self.__btn.setEnabled(f and text)
def __toggleWidgets(self, f):
self.__btn.setEnabled(f)
self.__fromLocalWidget.setEnabled(f)
self.__fromYoutubeWidget.setEnabled(f)
self.__stopBtn.setVisible(not f)
self.__convertToSrtBtn.setEnabled(f)
def __toggleFromWhereRadioWidgets(self):
self.__is_local = self.sender().text() == 'From local'
self.__fromLocalWidget.setEnabled(self.__is_local)
self.__fromYoutubeWidget.setEnabled(not self.__is_local)
def __run(self):
try:
url = self.get_current_url()
if self.__is_local:
self.__audioReadyFinished(url)
self.__runSecondThread()
else:
self.__t = Thread1(url)
self.__t.started.connect(self.__started)
self.__t.audioReadyFinished.connect(self.__audioReadyFinished)
self.__t.finished.connect(self.__runSecondThread)
self.__t.start()
except Exception as e:
QMessageBox.critical(self, 'Error', str(e))
def __started(self):
self.__loadingLbl.start()
self.__browser.clear()
self.__transcriptionLanguageLbl.setText('Transcription language: ')
self.__transcriptionDurationLbl.setText('Transcription duration: ')
self.__toggleWidgets(False)
def __audioReadyFinished(self, dst_filename):
self.__dst_filename = dst_filename
def __runSecondThread(self):
if self.__is_stopped:
self.__is_stopped = False
else:
self.__t = Thread2(self.__wrapper, self.__dst_filename)
self.__t.started.connect(self.__started)
self.__t.afterGenerated.connect(self.__afterGenerated)
self.__t.finished.connect(self.__finished)
self.__t.start()
def __afterGenerated(self, result_obj_lst):
self.__loadingLbl.stop()
self.__btn.setEnabled(True)
for result_obj in result_obj_lst:
self.__used_language_list.append(result_obj['language'])
self.__duration += result_obj['duration']
segments = result_obj['segments']
for segment in segments:
start = segment['start']
end = segment['end']
text = segment['text']
self.__browser.append(f"[{start} --> {end}] {text}")
def __finished(self):
if self.__is_stopped:
self.__is_stopped = False
else:
mostCommonUsedLanguage = Counter(self.__used_language_list).most_common(1)[0][0]
self.__transcriptionLanguageLbl.setText(f'Transcription language (Most commonly used): {mostCommonUsedLanguage}')
self.__transcriptionDurationLbl.setText(f'Transcription duration: {str(round(self.__duration, 2))} seconds')
self.__toggleWidgets(True)
if not self.isVisible():
self.__notifierWidget = NotifierWidget(informative_text='Transcription Complete 💻', detailed_text='Click this!')
self.__notifierWidget.show()
self.__notifierWidget.doubleClicked.connect(self.show)
def __stop(self):
self.__is_stopped = True
self.__t.terminate()
self.__loadingLbl.stop()
self.__toggleWidgets(True)
def __beforeClose(self):
message = 'Would you like to exit the application? If you won\'t, it will be running in the background.'
closeMessageBox = QMessageBox(self)
closeMessageBox.setWindowTitle('Exit')
closeMessageBox.setText(message)
closeMessageBox.setStandardButtons(QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No | QMessageBox.StandardButton.Cancel)
reply = closeMessageBox.exec()
# Cancel
if reply == QMessageBox.StandardButton.Cancel:
return True
else:
# Yes
if reply == QMessageBox.StandardButton.Yes:
app = QApplication.instance()
app.quit()
# No
elif reply == QMessageBox.StandardButton.No:
self.close()
def closeEvent(self, e):
f = self.__beforeClose()
if f:
e.ignore()
else:
return super().closeEvent(e)
def __convertToSrt(self):
original_filename = self.__fromLocalWidget.getLineEdit().text()
content = self.__browser.toPlainText().split('\n')
convert_to_srt(original_filename, content)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
QApplication.setWindowIcon(QIcon('logo.png'))
w = MainWindow()
w.show()
sys.exit(app.exec())