-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstt_app.py
More file actions
455 lines (371 loc) · 17.1 KB
/
stt_app.py
File metadata and controls
455 lines (371 loc) · 17.1 KB
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
import sys
import os
import numpy as np
import sounddevice as sd
from PyQt6.QtWidgets import (
QApplication,
QMainWindow,
QPushButton,
QVBoxLayout,
QWidget,
QTextEdit,
QLabel,
QComboBox,
QHBoxLayout,
QSpinBox,
QFileDialog,
QCheckBox,
QDialog,
)
from PyQt6.QtCore import Qt
from datetime import datetime
from db import DatabaseManager
from translation import TranscriptionThread, TranslationManager, AIAssistant
from audiostream import AudioStreamer
from ui import APIConfigDialog, RoleConfigDialog, ROLE_DESCRIPTIONS
CHUNK_DURATION = 3 # seconds
SAMPLE_RATE = 16000
# Define model configurations
WHISPER_MODELS = {
"Tiny (fast, less accurate)": ("tiny", "whisper"),
"Base": ("base", "whisper"),
"Small": ("small", "whisper"),
"Medium": ("medium", "whisper"),
"Large (slow, most accurate)": ("large", "whisper"),
"Faster Tiny": ("tiny", "faster"),
"Faster Base": ("base", "faster"),
"Faster Small": ("small", "faster"),
"Faster Medium": ("medium", "faster"),
"Faster Large": ("large", "faster")
}
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Speech to Text Converter")
self.setMinimumSize(1200, 600) # Increased width for 3 columns
# Initialize managers
self.db_manager = DatabaseManager()
self.translation_manager = TranslationManager()
self.ai_assistant = AIAssistant() # Initialize AI Assistant
# Create central widget and layout
central_widget = QWidget()
self.setCentralWidget(central_widget)
layout = QVBoxLayout(central_widget)
# Create control panel
control_panel = QHBoxLayout()
# Left side controls
left_controls = QHBoxLayout()
# Create language selection
self.language_combo = QComboBox()
self.language_combo.addItems(["English", "Chinese"])
left_controls.addWidget(QLabel("Language:"))
left_controls.addWidget(self.language_combo)
self.language_combo.currentTextChanged.connect(self.language_changed)
# Create model selection
self.model_combo = QComboBox()
self.model_combo.addItems(WHISPER_MODELS.keys())
left_controls.addWidget(QLabel("Model:"))
left_controls.addWidget(self.model_combo)
self.model_combo.currentTextChanged.connect(self.model_changed)
# Right side controls
right_controls = QHBoxLayout()
# Create role selection
self.role_combo = QComboBox()
self.role_combo.addItems([
"General Assistant",
"Technical Interviewer",
"HR Interviewer",
"Meeting Participant",
"Student",
"Teacher",
"Custom..."
])
self.role_combo.currentTextChanged.connect(self.role_changed)
right_controls.addWidget(QLabel("AI Role:"))
right_controls.addWidget(self.role_combo)
# Create translation and AI answer checkboxes
self.translate_check = QCheckBox("Auto Translate")
self.answer_check = QCheckBox("AI Answer Questions")
self.api_config_button = QPushButton("API Settings")
right_controls.addWidget(self.translate_check)
right_controls.addWidget(self.answer_check)
right_controls.addWidget(self.api_config_button)
self.api_config_button.clicked.connect(self.configure_api)
# Create start/stop button
self.toggle_button = QPushButton("Start Streaming")
right_controls.addWidget(self.toggle_button)
# Add controls to main control panel
control_panel.addLayout(left_controls)
control_panel.addStretch()
control_panel.addLayout(right_controls)
# Add control panel to main layout
layout.addLayout(control_panel)
# Add status label
self.status_label = QLabel("Ready")
self.status_label.setStyleSheet("color: gray;")
layout.addWidget(self.status_label)
# Create text output containers
text_container = QHBoxLayout()
# Original text section
original_section = QVBoxLayout()
original_section.addWidget(QLabel("Original Transcription:"))
self.original_output = QTextEdit()
self.original_output.setReadOnly(True)
original_section.addWidget(self.original_output)
text_container.addLayout(original_section)
# Translation section
translation_section = QVBoxLayout()
translation_section.addWidget(QLabel("Translation:"))
self.translation_output = QTextEdit()
self.translation_output.setReadOnly(True)
translation_section.addWidget(self.translation_output)
text_container.addLayout(translation_section)
# AI Answer section
answer_section = QVBoxLayout()
answer_section.addWidget(QLabel("AI Answers:"))
self.answer_output = QTextEdit()
self.answer_output.setReadOnly(True)
answer_section.addWidget(self.answer_output)
text_container.addLayout(answer_section)
# Add text container to main layout
layout.addLayout(text_container)
# Create export panel
export_panel = QHBoxLayout()
self.line_count_spinbox = QSpinBox()
self.line_count_spinbox.setRange(1, 1000)
self.line_count_spinbox.setValue(1000)
self.export_button = QPushButton("Export")
self.load_history_button = QPushButton("Load History")
export_panel.addWidget(QLabel("Number of lines:"))
export_panel.addWidget(self.line_count_spinbox)
export_panel.addWidget(self.export_button)
export_panel.addWidget(self.load_history_button)
export_panel.addStretch()
# Add export panel to main layout
layout.addLayout(export_panel)
# Connect signals
self.toggle_button.clicked.connect(self.toggle_streaming)
self.export_button.clicked.connect(self.export_transcription)
self.load_history_button.clicked.connect(self.load_history)
# Initialize threads
self.audio_streamer = None
self.transcriber = None
self.is_streaming = False
# Store conversation context
self.conversation_context = []
self.context_window_size = 100 # Store last 10 exchanges
# Store current role description
self.current_role = "You are a helpful assistant providing concise answers based on conversation context."
def language_changed(self, new_language):
if self.transcriber:
language_codes = {"English": "en", "Chinese": "zh"}
self.transcriber.set_language(language_codes[new_language])
def model_changed(self, new_model):
if self.transcriber:
model_name, model_type = WHISPER_MODELS[new_model]
self.transcriber.set_model(model_name, model_type)
def toggle_streaming(self):
if not self.is_streaming:
self.start_streaming()
else:
self.stop_streaming()
def start_streaming(self):
language_codes = {"English": "en", "Chinese": "zh"}
current_language = language_codes[self.language_combo.currentText()]
model_name, model_type = WHISPER_MODELS[self.model_combo.currentText()]
if self.translate_check.isChecked() and not self.translation_manager.api_key:
self.status_label.setText("Please configure API settings for translation")
return
self.transcriber = TranscriptionThread(
language=current_language,
model_name=model_name,
model_type=model_type,
db_manager=self.db_manager,
translation_manager=self.translation_manager,
auto_translate=self.translate_check.isChecked()
)
# Connect new signals
self.transcriber.word_ready.connect(self.handle_word)
self.transcriber.sentence_complete.connect(self.handle_sentence_complete)
self.transcriber.model_loaded.connect(self.update_status)
self.audio_streamer = AudioStreamer()
self.audio_streamer.chunk_ready.connect(self.handle_audio_chunk)
self.audio_streamer.start()
self.transcriber.start()
self.is_streaming = True
self.toggle_button.setText("Stop Streaming")
self.model_combo.setEnabled(False)
self.translate_check.setEnabled(False)
self.api_config_button.setEnabled(False)
def stop_streaming(self):
if self.audio_streamer:
self.audio_streamer.stop()
self.audio_streamer.wait()
self.audio_streamer.deleteLater()
self.audio_streamer = None
if self.transcriber:
self.transcriber.stop()
self.transcriber.wait()
self.transcriber.deleteLater()
self.transcriber = None
self.is_streaming = False
self.toggle_button.setText("Start Streaming")
self.model_combo.setEnabled(True)
self.translate_check.setEnabled(True)
self.api_config_button.setEnabled(True)
self.status_label.setText("Ready")
def update_status(self, message):
self.status_label.setText(message)
def handle_audio_chunk(self, audio_chunk):
if self.transcriber:
self.transcriber.audio_queue.put(audio_chunk)
def handle_word(self, word, timestamp, is_translation):
# Get the appropriate text widget
text_widget = self.translation_output if is_translation else self.original_output
# Clean up the word
word = word.strip()
if not word: # Skip empty words
return
# Check if it's a punctuation mark
is_punctuation = word in ['.', '!', '?', '。', '!', '?', ',', ',', '、']
# Get current text and determine if we need a new line
current_text = text_widget.toPlainText()
needs_new_line = current_text == '' or current_text.endswith('\n')
# Start new line with timestamp if needed (but not for lone punctuation)
if needs_new_line and not is_punctuation:
text_widget.insertPlainText(f"[{timestamp}] {word}")
else:
# Add space before word unless it's a punctuation mark
if not is_punctuation:
text_widget.insertPlainText(f" {word}")
# else:
# text_widget.insertPlainText(word)
# Scroll to bottom
text_widget.verticalScrollBar().setValue(
text_widget.verticalScrollBar().maximum()
)
def handle_sentence_complete(self, is_translation):
# Get the appropriate text widget
text_widget = self.translation_output if is_translation else self.original_output
# Get the current line before adding newline
current_text = text_widget.toPlainText()
current_line = current_text.split('\n')[-1] if current_text else ""
# Only add newline if the current line has content
if current_text and not current_text.endswith('\n'):
text_widget.insertPlainText('\n')
# Handle question detection and answering for original text only
if not is_translation:
# Extract the actual text without timestamp
if current_line.startswith('[') and ']' in current_line:
actual_text = current_line[current_line.index(']')+1:].strip()
# Add to conversation context
self.conversation_context.append(actual_text)
if len(self.conversation_context) > self.context_window_size * 2:
self.conversation_context.pop(0)
# Check if it's a question and AI answering is enabled
if self.answer_check.isChecked() and self.ai_assistant.is_question(actual_text):
timestamp = current_line[1:current_line.index(']')]
answer = self.ai_assistant.answer_question(
actual_text,
self.get_context(),
self.current_role,
timestamp,
self.language_combo.currentText()
)
self.answer_output.append(answer)
self.answer_output.verticalScrollBar().setValue(
self.answer_output.verticalScrollBar().maximum()
)
def export_transcription(self):
try:
num_lines = self.line_count_spinbox.value()
lines = self.db_manager.get_last_n_lines(num_lines)
if not lines:
self.status_label.setText("No transcriptions to export")
return
file_name, _ = QFileDialog.getSaveFileName(
self, "Save Transcription",
f"transcription_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt",
"Text Files (*.txt)"
)
if file_name:
with open(file_name, 'w', encoding='utf-8') as f:
for timestamp, text in reversed(lines):
parts = text.split("\n[Translation] ")
original_text = parts[0]
translation_text = parts[1] if len(parts) > 1 else ""
f.write(f"[{timestamp}] Original: {original_text}\n")
if translation_text:
f.write(f"[{timestamp}] Translation: {translation_text}\n")
f.write("\n")
self.status_label.setText(f"Successfully exported to {file_name}")
except Exception as e:
self.status_label.setText(f"Error exporting: {str(e)}")
def load_history(self):
try:
num_lines = self.line_count_spinbox.value()
lines = self.db_manager.get_last_n_lines(num_lines)
if not lines:
self.status_label.setText("No history found in database")
return
# Clear both text outputs
self.original_output.clear()
self.translation_output.clear()
# Add historical entries in chronological order
for timestamp, text in reversed(lines):
parts = text.split("\n[Translation] ")
original_text = parts[0]
translation_text = parts[1] if len(parts) > 1 else ""
self.original_output.append(f"[{timestamp}] {original_text}")
if translation_text:
self.translation_output.append(f"[{timestamp}] {translation_text}")
self.status_label.setText(f"Loaded {len(lines)} historical transcriptions")
except Exception as e:
self.status_label.setText(f"Error loading history: {str(e)}")
def closeEvent(self, event):
if self.is_streaming:
self.stop_streaming()
self.db_manager.close()
event.accept()
def configure_api(self):
dialog = APIConfigDialog(self)
if dialog.exec() == QDialog.DialogCode.Accepted:
base_url = dialog.base_url_input.text() or dialog.base_url_input.placeholderText()
api_key = dialog.api_key_input.text()
model_name = dialog.model_input.text() or dialog.model_input.placeholderText()
if api_key:
try:
# Configure both translation manager and AI assistant
self.translation_manager.configure(base_url, api_key, model_name)
self.ai_assistant.configure(base_url, api_key, model_name)
self.status_label.setText(f"API configured successfully (Model: {model_name})")
except Exception as e:
self.status_label.setText(f"API configuration failed: {str(e)}")
def get_context(self):
# Get the last few exchanges as context
context = []
for text in self.conversation_context[-self.context_window_size:]:
context.append(text)
return "\n".join(context)
def role_changed(self, new_role):
if new_role == "Custom...":
dialog = RoleConfigDialog(self)
if dialog.exec() == QDialog.DialogCode.Accepted:
custom_role = dialog.get_role_description()
if custom_role:
self.current_role = custom_role
else:
self.role_combo.setCurrentText("General Assistant")
else:
self.role_combo.setCurrentText("General Assistant")
else:
self.current_role = ROLE_DESCRIPTIONS.get(
new_role, ROLE_DESCRIPTIONS["General Assistant"]
)
def main():
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())
if __name__ == "__main__":
main()