-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
1202 lines (995 loc) · 48.1 KB
/
app.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
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys
import os
import sounddevice as sd
import soundfile as sf
import numpy as np
import torch
import subprocess
from datetime import datetime
from PyQt6.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout,
QPushButton, QLabel, QComboBox, QSystemTrayIcon,
QMenu, QDialog, QTabWidget, QGridLayout, QCheckBox,
QLineEdit, QTextEdit, QHBoxLayout, QDialogButtonBox)
from PyQt6.QtCore import Qt, QThread, pyqtSignal, QSettings, QEvent, QTimer
from PyQt6.QtGui import QIcon, QAction
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
from pynput import keyboard as kb
import threading
import time
import csv
import requests
WHISPER_MODELS = {
"Tiny": "openai/whisper-tiny",
"Base": "openai/whisper-base",
"Small": "openai/whisper-small",
"Medium": "openai/whisper-medium",
"Large-v3": "openai/whisper-large-v3",
"Large-v3 Turbo": "openai/whisper-large-v3-turbo"
}
LANGUAGES = {
"Auto-detect": "auto",
"English": "en",
"Danish": "da",
"German": "de",
"Spanish": "es",
"French": "fr",
"Italian": "it",
"Japanese": "ja",
"Korean": "ko",
"Dutch": "nl",
"Polish": "pl",
"Portuguese": "pt",
"Russian": "ru",
"Turkish": "tr",
"Chinese": "zh"
}
def format_hotkey_for_pynput(hotkey_str):
"""Convert a hotkey string like 'ctrl+shift+r' to '<ctrl>+<shift>+r'"""
parts = hotkey_str.split('+')
formatted_parts = []
for part in parts:
if part.lower() in ['ctrl', 'alt', 'shift']:
formatted_parts.append(f'<{part.lower()}>')
else:
formatted_parts.append(part.lower())
return '+'.join(formatted_parts)
def copy_to_clipboard(text):
"""Use xsel to copy text to clipboard without requiring root"""
try:
process = subprocess.Popen(['xsel', '-bi'], stdin=subprocess.PIPE)
process.communicate(input=text.encode())
return True
except Exception as e:
print(f"Error copying to clipboard: {e}")
return False
def paste_text(text):
"""Use xdotool to paste text without requiring root"""
try:
# Use xdotool type directly with the text
# The --delay option adds a small delay between keystrokes for reliability
subprocess.run(["xdotool", "type", "--delay", "12", text], check=True)
return True
except Exception as e:
print(f"Error pasting text: {e}")
return False
class AudioRecorder(QThread):
finished = pyqtSignal(str)
error = pyqtSignal(str)
def __init__(self, sample_rate=44100):
super().__init__()
self.sample_rate = sample_rate
self.recording = False
self.audio_data = []
self.device = None
self.audio_file = None
self._stream = None
self.max_recording_size = 1024 * 1024 * 100 # 100MB limit
self._lock = threading.Lock()
self.channels = 1 # Default to mono recording
def set_device(self, device):
try:
# Get fresh device info
sd._terminate()
sd._initialize()
device_info = sd.query_devices(device)
# Adjust sample rate if needed
if self.sample_rate > device_info['default_samplerate']:
print(f"Adjusting sample rate from {self.sample_rate} to {device_info['default_samplerate']}")
self.sample_rate = int(device_info['default_samplerate'])
# Ensure we have at least 1 input channel
if device_info['max_input_channels'] < 1:
raise ValueError("Device has no input channels")
# Always use 1 channel (mono) for recording
self.channels = 1
# Test if the configuration is valid
sd.check_input_settings(
device=device,
samplerate=self.sample_rate,
channels=self.channels
)
self.device = device
print(f"Audio device configured: {device_info['name']}, "
f"Sample rate: {self.sample_rate}, Channels: {self.channels}")
except Exception as e:
print(f"Error setting audio device: {e}")
# Fall back to default device
self.device = None
raise
def run(self):
try:
if self.device is None:
raise ValueError("No valid audio device configured")
# Create stream with validated parameters
self._stream = sd.InputStream(
samplerate=self.sample_rate,
channels=self.channels,
callback=self._audio_callback,
device=self.device,
blocksize=1024,
dtype=np.float32
)
print(f"Starting recording with: Device={self.device}, "
f"Rate={self.sample_rate}, Channels={self.channels}")
with self._stream:
while self.recording:
sd.sleep(100)
if len(self.audio_data) > 0:
with self._lock:
audio_array = np.concatenate(self.audio_data)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
self.audio_file = f"recordings/{timestamp}.wav"
os.makedirs("recordings", exist_ok=True)
sf.write(self.audio_file, audio_array, self.sample_rate)
self.finished.emit(self.audio_file)
except Exception as e:
print(f"Audio recording error: {e}")
self.error.emit(str(e))
finally:
if self._stream:
try:
self._stream.close()
except Exception as e:
print(f"Error closing stream: {e}")
with self._lock:
self.audio_data = []
def _audio_callback(self, indata, frames, time, status):
if status:
print(f"Status: {status}")
with self._lock:
if not self.recording:
return
# Check size before appending
current_size = sum(data.nbytes for data in self.audio_data)
if current_size + indata.nbytes > self.max_recording_size:
self.recording = False
# Schedule error emission to main thread to avoid Qt warnings
QApplication.instance().postEvent(
self,
QEvent(QEvent.Type.User)
)
return
self.audio_data.append(indata.copy())
def event(self, event):
# Handle custom events for error emission
if event.type() == QEvent.Type.User:
self.error.emit("Recording size limit exceeded")
return True
return super().event(event)
def start_recording(self):
with self._lock:
self.recording = True
self.audio_data = []
self.start()
def stop_recording(self):
with self._lock:
self.recording = False
class SettingsDialog(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("Settings")
self.setMinimumWidth(400)
layout = QVBoxLayout()
tabs = QTabWidget()
# Audio Settings
audio_widget = QWidget()
audio_layout = QGridLayout()
# Create all UI elements first
self.device_combo = QComboBox()
self.refresh_button = QPushButton("Refresh")
self.sample_rate_combo = QComboBox() # Create this before populate_devices
self.model_combo = QComboBox()
self.language_combo = QComboBox()
# Now populate devices and connect signals
self.refresh_button.clicked.connect(self.refresh_devices)
self.populate_devices()
audio_layout.addWidget(QLabel("Input Device:"), 0, 0)
audio_layout.addWidget(self.device_combo, 0, 1)
audio_layout.addWidget(self.refresh_button, 0, 2)
# Add sample rate selection
audio_layout.addWidget(QLabel("Sample Rate (Hz):"), 1, 0)
audio_layout.addWidget(self.sample_rate_combo, 1, 1)
# Model Selection
for model_name in WHISPER_MODELS:
self.model_combo.addItem(model_name)
audio_layout.addWidget(QLabel("Whisper Model:"), 2, 0)
audio_layout.addWidget(self.model_combo, 2, 1)
# Language Selection
for lang_name in LANGUAGES:
self.language_combo.addItem(lang_name)
audio_layout.addWidget(QLabel("Language:"), 3, 0)
audio_layout.addWidget(self.language_combo, 3, 1)
audio_widget.setLayout(audio_layout)
tabs.addTab(audio_widget, "Audio")
# Connect device combo signal after creation
self.device_combo.currentIndexChanged.connect(self.on_device_changed)
# Hotkey Settings
hotkey_widget = QWidget()
hotkey_layout = QGridLayout()
self.record_hotkey = QLineEdit()
self.record_hotkey.setPlaceholderText("Press keys to set hotkey")
self.record_hotkey.setReadOnly(True)
# Add a clear button for hotkey
self.clear_hotkey = QPushButton("Clear")
self.clear_hotkey.clicked.connect(lambda: self.record_hotkey.setText(""))
hotkey_layout.addWidget(QLabel("Record Hotkey:"), 0, 0)
hotkey_layout.addWidget(self.record_hotkey, 0, 1)
hotkey_layout.addWidget(self.clear_hotkey, 0, 2)
# Add hotkey recording functionality
self.current_keys = set()
self.keyboard_listener = None
self.record_hotkey.focusInEvent = self.start_hotkey_recording
self.record_hotkey.focusOutEvent = self.stop_hotkey_recording
hotkey_widget.setLayout(hotkey_layout)
tabs.addTab(hotkey_widget, "Hotkeys")
layout.addWidget(tabs)
# Add Save and Cancel buttons using QDialogButtonBox
self.buttonBox = QDialogButtonBox(QDialogButtonBox.StandardButton.Save | QDialogButtonBox.StandardButton.Cancel)
self.buttonBox.accepted.connect(self.save_and_accept)
self.buttonBox.rejected.connect(self.reject)
layout.addWidget(self.buttonBox)
self.setLayout(layout)
self.load_settings()
def on_device_changed(self, index):
"""Handle device selection changes"""
device_id = self.device_combo.currentData()
if device_id is not None and device_id >= 0:
try:
device = sd.query_devices()[device_id]
self.update_sample_rates(device)
except Exception as e:
print(f"Error updating device settings: {e}")
# Reset to first available device if current is invalid
if self.device_combo.count() > 0:
self.device_combo.setCurrentIndex(0)
def populate_devices(self):
"""Populate the device combo box with available input devices"""
self.device_combo.clear()
try:
# Reset sounddevice to detect new devices
sd._terminate()
sd._initialize()
devices = sd.query_devices()
valid_devices = False
settings = QSettings('Voice2Input', 'Voice2Input')
current_device_id = settings.value('audio/device', 0, type=int)
for i, dev in enumerate(devices):
if dev['max_input_channels'] >= 1:
name = dev['name']
self.device_combo.addItem(f"{name} (Channels: {dev['max_input_channels']})", i)
valid_devices = True
# Select this device if it matches the saved device ID
if i == current_device_id:
self.device_combo.setCurrentIndex(self.device_combo.count() - 1)
if not valid_devices:
self.device_combo.addItem("No input devices found - plug in mic and click refresh", -1)
self.device_combo.setEnabled(False)
self.sample_rate_combo.setEnabled(False)
else:
self.device_combo.setEnabled(True)
self.sample_rate_combo.setEnabled(True)
# Update sample rates for the selected device
if self.device_combo.currentData() is not None:
current_device = devices[self.device_combo.currentData()]
self.update_sample_rates(current_device)
except Exception as e:
print(f"Error populating devices: {e}")
self.device_combo.addItem("Error detecting devices - click refresh", -1)
self.device_combo.setEnabled(False)
self.sample_rate_combo.setEnabled(False)
def update_sample_rates(self, device):
if not hasattr(self, 'sample_rate_combo'):
return
self.sample_rate_combo.clear()
default_rate = int(device['default_samplerate'])
# Common sample rates to try
sample_rates = [16000, 22050, 44100, 48000]
# Add only supported rates
supported_rates = []
device_id = self.device_combo.currentData()
if device_id is not None and device_id >= 0:
for rate in sample_rates:
try:
sd.check_input_settings(
device=device_id,
samplerate=rate,
channels=1
)
supported_rates.append(rate)
except:
continue
# If no common rates work, at least add the default
if not supported_rates:
supported_rates = [default_rate]
for rate in supported_rates:
self.sample_rate_combo.addItem(str(rate))
# Select default rate if available, otherwise first supported rate
index = self.sample_rate_combo.findText(str(default_rate))
if index >= 0:
self.sample_rate_combo.setCurrentIndex(index)
else:
self.sample_rate_combo.setCurrentIndex(0)
def refresh_devices(self):
self.populate_devices()
settings = QSettings('Voice2Input', 'Voice2Input')
device_index = settings.value('audio/device', 0, type=int)
# Find the combo box index that corresponds to the device ID
for i in range(self.device_combo.count()):
if self.device_combo.itemData(i) == device_index:
self.device_combo.setCurrentIndex(i)
break
# Update sample rates when device changes
current_device_id = self.device_combo.currentData()
if current_device_id is not None and current_device_id >= 0:
current_device = sd.query_devices()[current_device_id]
self.update_sample_rates(current_device)
def start_hotkey_recording(self, event):
self.current_keys.clear()
self.record_hotkey.setText("")
self.keyboard_listener = kb.Listener(
on_press=self.on_hotkey_press,
on_release=self.on_hotkey_release
)
self.keyboard_listener.start()
super().focusInEvent(event)
def stop_hotkey_recording(self, event):
if self.keyboard_listener:
self.keyboard_listener.stop()
self.keyboard_listener = None
super().focusOutEvent(event)
def on_hotkey_press(self, key):
try:
if hasattr(key, 'char'):
key_name = key.char.lower()
else:
key_name = key.name.lower()
self.current_keys.add(key_name)
self.update_hotkey_text()
except AttributeError:
pass
def on_hotkey_release(self, key):
try:
if hasattr(key, 'char'):
key_name = key.char.lower()
else:
key_name = key.name.lower()
self.current_keys.discard(key_name)
except AttributeError:
pass
def update_hotkey_text(self):
hotkey = '+'.join(sorted(self.current_keys))
self.record_hotkey.setText(hotkey)
def load_settings(self):
"""Load all settings from QSettings"""
settings = QSettings('Voice2Input', 'Voice2Input')
# Load audio settings
device_index = settings.value('audio/device', 0, type=int)
sample_rate = settings.value('audio/sample_rate', '44100')
self.device_combo.setCurrentIndex(device_index)
index = self.sample_rate_combo.findText(str(sample_rate))
if index >= 0:
self.sample_rate_combo.setCurrentIndex(index)
# Load hotkey settings
self.record_hotkey.setText(settings.value('hotkeys/record', 'ctrl+shift+r'))
# Load model settings
model_name = settings.value('model/name', 'Large-v3 Turbo')
language = settings.value('model/language', 'Auto-detect')
model_index = self.model_combo.findText(model_name)
if model_index >= 0:
self.model_combo.setCurrentIndex(model_index)
lang_index = self.language_combo.findText(language)
if lang_index >= 0:
self.language_combo.setCurrentIndex(lang_index)
def save_settings(self):
"""Save all settings to QSettings"""
settings = QSettings('Voice2Input', 'Voice2Input')
# Save audio settings with both ID and name
device_id = self.device_combo.currentData()
if device_id is not None and device_id >= 0:
settings.setValue('audio/device', device_id)
device_name = sd.query_devices()[device_id]['name']
settings.setValue('audio/device_name', device_name)
settings.setValue('audio/sample_rate', self.sample_rate_combo.currentText())
settings.setValue('hotkeys/record', self.record_hotkey.text())
settings.setValue('model/name', self.model_combo.currentText())
settings.setValue('model/language', self.language_combo.currentText())
def save_and_accept(self):
self.save_settings()
self.accept()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Voice2Input")
self.setMinimumSize(400, 500)
# Initialize settings first
self.settings = QSettings('Voice2Input', 'Voice2Input')
# Create necessary directories
os.makedirs("recordings", exist_ok=True)
# Initialize keyboard state before anything else
self.pressed_keys = set()
self.hotkey_lock = threading.Lock()
self.keyboard_listener = None
self.is_recording = False
self.hotkey_active = False
self.hotkey_timer = None
self.hotkey = self.settings.value('hotkeys/record', 'ctrl+shift+r')
# Initialize audio recorder before anything else
self.setup_audio_recorder()
# Initialize transcription mode before model setup
self.setup_transcription_mode()
# Only initialize the model if using local mode
if not self.use_remote:
self.setup_transcription_model()
# Create central widget and layout
central_widget = QWidget()
layout = QVBoxLayout()
# Status label
self.status_label = QLabel("Ready")
self.status_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
layout.addWidget(self.status_label)
# Transcription text box
self.transcription_text = QTextEdit()
self.transcription_text.setReadOnly(True)
self.transcription_text.setMinimumHeight(200)
layout.addWidget(self.transcription_text)
# Checkbox options
checkbox_layout = QGridLayout()
self.auto_copy = QCheckBox("Auto-copy to clipboard")
self.auto_paste = QCheckBox("Auto-paste to active window")
# Load checkbox states from settings
self.auto_copy.setChecked(self.settings.value('options/auto_copy', True, type=bool))
self.auto_paste.setChecked(self.settings.value('options/auto_paste', True, type=bool))
# Connect the auto_copy state change to update auto-paste behavior immediately
self.auto_copy.stateChanged.connect(self.update_auto_paste_state)
checkbox_layout.addWidget(self.auto_copy, 0, 0)
checkbox_layout.addWidget(self.auto_paste, 0, 1)
layout.addLayout(checkbox_layout)
# Record button
self.record_button = QPushButton("Start Recording")
self.record_button.clicked.connect(self.toggle_recording)
layout.addWidget(self.record_button)
# Settings button
settings_button = QPushButton("Settings")
settings_button.clicked.connect(self.show_settings)
layout.addWidget(settings_button)
# Add a horizontal layout at the bottom of the main window for local model controls
bottom_layout = QHBoxLayout()
self.use_local_checkbox = QCheckBox("Use Local Model")
# Default state: unchecked (meaning remote mode)
self.use_local_checkbox.setChecked(False)
self.use_local_checkbox.stateChanged.connect(self.on_local_model_toggle)
bottom_layout.addWidget(self.use_local_checkbox)
self.load_model_button = QPushButton("Load Local Model")
self.load_model_button.clicked.connect(self.toggle_local_model)
# Disabled by default because remote mode is active
self.load_model_button.setEnabled(False)
bottom_layout.addWidget(self.load_model_button)
# Add the bottom_layout to the main vertical layout
layout.addLayout(bottom_layout)
# Update the status label according to current mode
if self.use_local_checkbox.isChecked():
self.status_label.setText("Local Model Mode selected. Click 'Load Local Model' to load.")
else:
self.status_label.setText("Remote Mode (HuggingFace) active.")
# Initialize mode flag
self.use_remote = not self.use_local_checkbox.isChecked()
central_widget.setLayout(layout)
self.setCentralWidget(central_widget)
# System tray
self.tray_icon = QSystemTrayIcon(self)
icon = QIcon.fromTheme("audio-input-microphone", QIcon("icons/microphone.png"))
self.tray_icon.setIcon(icon)
self.tray_icon.setToolTip("Voice2Input")
self.tray_icon.setVisible(True)
# Create tray menu
tray_menu = QMenu()
show_action = QAction("Show", self)
show_action.triggered.connect(self.show)
restart_hotkeys_action = QAction("Restart Hotkeys", self)
restart_hotkeys_action.triggered.connect(self.setup_hotkeys)
quit_action = QAction("Quit", self)
quit_action.triggered.connect(self.close)
tray_menu.addAction(show_action)
tray_menu.addAction(restart_hotkeys_action)
tray_menu.addAction(quit_action)
self.tray_icon.setContextMenu(tray_menu)
# Setup hotkeys with retry
if self.setup_hotkeys(): # Only proceed if hotkey setup succeeds
# Initialize timer for hotkey checks
self.hotkey_timer = QTimer(self)
self.hotkey_timer.timeout.connect(self.check_hotkey_listener)
self.hotkey_timer.start(20000) # Check every 20 seconds
# Update record button text with current hotkey
self.update_record_button_text()
self.device_check_timer = QTimer(self)
self.device_check_timer.timeout.connect(self.check_audio_devices)
self.device_check_timer.start(5000) # Check every 5 seconds
def update_record_button_text(self):
hotkey = self.settings.value('hotkeys/record', 'ctrl+shift+r')
self.record_button.setText(f"Start Recording ({hotkey})")
def setup_hotkeys(self):
"""Setup keyboard listener with error handling and retries"""
try:
with self.hotkey_lock:
# Clean up existing listener if any
if self.keyboard_listener:
try:
self.keyboard_listener.stop()
self.keyboard_listener = None
except Exception as e:
print(f"Error stopping existing listener: {e}")
# Reset state
self.pressed_keys = set()
self.hotkey_active = False # Ensure inactive during setup
# Create and start new listener
self.keyboard_listener = kb.Listener(
on_press=self.on_key_press,
on_release=self.on_key_release,
suppress=False
)
self.keyboard_listener.daemon = True
self.keyboard_listener.start()
# Wait for listener to start and verify it's functioning
start_time = time.time()
max_wait = 2.0 # Maximum wait time in seconds
while time.time() - start_time < max_wait:
if self.keyboard_listener.is_alive():
# Additional verification - try to join briefly
self.keyboard_listener.join(timeout=0.1)
if self.keyboard_listener.is_alive():
self.hotkey_active = True # Only set active after verification
print("Keyboard listener verified and active")
return True
time.sleep(0.1)
raise RuntimeError("Failed to verify keyboard listener")
except Exception as e:
print(f"Error setting up hotkeys: {e}")
self.status_label.setText("Error: Failed to setup hotkeys")
self.hotkey_active = False
return False
def on_key_press(self, key):
"""Handle key press events with proper error checking"""
if not hasattr(self, 'pressed_keys'):
print("Warning: pressed_keys not initialized")
self.pressed_keys = set() # Auto-initialize if missing
if not hasattr(self, 'audio_recorder') or not self.hotkey_active:
return
try:
# Convert key to string representation
key_str = None
if hasattr(key, 'char') and key.char is not None:
key_str = key.char.lower()
elif hasattr(key, 'name') and key.name is not None:
key_str = key.name.lower()
if key_str is None:
return # Skip invalid keys
with self.hotkey_lock:
self.pressed_keys.add(key_str)
current_combo = '+'.join(sorted(self.pressed_keys))
if current_combo == self.hotkey and not self.is_recording:
print("Hotkey pressed - starting recording")
self.is_recording = True
self.start_recording()
except Exception as e:
print(f"Error in key press handler: {e}")
self.hotkey_active = False # Mark for restart
if hasattr(self, 'status_label'):
self.status_label.setText("Error: Keyboard handler failed")
def on_key_release(self, key):
"""Handle key release events with proper error checking"""
if not hasattr(self, 'pressed_keys'):
print("Warning: pressed_keys not initialized")
self.pressed_keys = set() # Auto-initialize if missing
if not hasattr(self, 'audio_recorder') or not self.hotkey_active:
return
try:
# Convert key to string representation
key_str = None
if hasattr(key, 'char') and key.char is not None:
key_str = key.char.lower()
elif hasattr(key, 'name') and key.name is not None:
key_str = key.name.lower()
if key_str is None:
return # Skip invalid keys
with self.hotkey_lock:
self.pressed_keys.discard(key_str)
current_combo = '+'.join(sorted(self.pressed_keys))
if self.is_recording and current_combo != self.hotkey:
print("Hotkey released - stopping recording")
self.is_recording = False
self.stop_recording()
except Exception as e:
print(f"Error in key release handler: {e}")
self.hotkey_active = False # Mark for restart
if hasattr(self, 'status_label'):
self.status_label.setText("Error: Keyboard handler failed")
def show_settings(self):
dialog = SettingsDialog(self)
if dialog.exec():
dialog.save_settings()
self.load_settings()
# Reconfigure audio recorder with new settings
try:
sample_rate = int(self.settings.value('audio/sample_rate', '44100'))
device_id = self.settings.value('audio/device', 0, type=int)
print(f"Updating audio configuration - Device: {device_id}, Sample Rate: {sample_rate}")
# Create new recorder with updated settings
self.audio_recorder = AudioRecorder(sample_rate=sample_rate)
self.audio_recorder.finished.connect(self.handle_recording_finished)
self.audio_recorder.error.connect(self.handle_recording_error)
self.audio_recorder.set_device(device_id)
except Exception as e:
print(f"Error updating audio configuration: {e}")
self.status_label.setText("Error: Failed to update audio device")
def setup_transcription_model(self):
try:
# Only skip loading if the model is already loaded (i.e., pipe is not None)
if hasattr(self, 'pipe') and self.pipe is not None:
return # Already loaded
self.device = "cuda:0" if torch.cuda.is_available() else "cpu"
self.torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
model_name = self.settings.value('model/name', 'Large-v3 Turbo')
model_id = WHISPER_MODELS[model_name]
print(f"Loading model {model_id} on {self.device} with {self.torch_dtype}")
model = AutoModelForSpeechSeq2Seq.from_pretrained(
model_id,
torch_dtype=self.torch_dtype,
low_cpu_mem_usage=True,
use_safetensors=True
)
model.to(self.device)
processor = AutoProcessor.from_pretrained(model_id)
self.pipe = pipeline(
"automatic-speech-recognition",
model=model,
tokenizer=processor.tokenizer,
feature_extractor=processor.feature_extractor,
torch_dtype=self.torch_dtype,
device=self.device,
)
print("Model loaded successfully")
except Exception as e:
print(f"Error setting up transcription model: {e}")
self.status_label.setText("Error: Failed to load transcription model")
def load_settings(self):
"""Load all application settings"""
try:
# Load audio settings
self.device_id = self.settings.value('audio/device', 0, type=int)
# Load checkbox states with proper type conversion
self.auto_copy.setChecked(self.settings.value('options/auto_copy', False, type=bool))
self.auto_paste.setChecked(self.settings.value('options/auto_paste', False, type=bool))
# Load local mode setting and update the tickbox
self.use_local_checkbox.setChecked(self.settings.value('api/local_mode', False, type=bool))
# Reload audio recorder if sample rate changed
current_sample_rate = self.settings.value('audio/sample_rate', '44100')
if hasattr(self, 'audio_recorder') and self.audio_recorder.sample_rate != int(current_sample_rate):
self.setup_audio_recorder()
# Setup transcription mode
self.setup_transcription_mode()
except Exception as e:
print(f"Error loading settings: {e}")
def save_settings(self):
"""Save all application settings including UI state"""
try:
# Save checkbox states
self.settings.setValue('options/auto_copy', self.auto_copy.isChecked())
self.settings.setValue('options/auto_paste', self.auto_paste.isChecked())
# Save the local mode selection
self.settings.setValue('api/local_mode', self.use_local_checkbox.isChecked())
self.settings.sync() # Force settings to be written to disk
except Exception as e:
print(f"Error saving settings: {e}")
def setup_audio_recorder(self):
try:
# Reset sounddevice to detect changes
sd._terminate()
sd._initialize()
# Query all devices and find valid input devices
devices = sd.query_devices()
valid_input_devices = [
(i, dev) for i, dev in enumerate(devices)
if dev['max_input_channels'] > 0
]
if not valid_input_devices:
raise ValueError("No input devices found")
# Get saved device name and ID
default_device = valid_input_devices[0][0] # First valid device as default
saved_device_id = self.settings.value('audio/device', default_device, type=int)
saved_device_name = self.settings.value('audio/device_name', '')
# Initialize device variables
device_id = default_device # Always start with a valid default
device_name = devices[default_device]['name'] # Get default device name
# First try to find device by name (helps with USB devices)
if saved_device_name:
for device_index, device_dict in valid_input_devices:
if device_dict['name'] == saved_device_name:
device_id = device_index
device_name = device_dict['name']
break
# If name lookup failed, try the saved ID
if device_id == default_device and saved_device_id != default_device:
valid_ids = [i for i, _ in valid_input_devices]
if saved_device_id in valid_ids:
device_id = saved_device_id
device_name = devices[device_id]['name']
# Validate the selected device
try:
sd.check_input_settings(
device=device_id,
channels=1,
dtype=np.float32
)
except Exception as e:
print(f"Selected device {device_id} is invalid: {e}")
# Fall back to first valid device
device_id = default_device
device_name = devices[default_device]['name']
print(f"Selected audio device: {device_name} (ID: {device_id})")
# Save current device info
self.settings.setValue('audio/device', device_id)
self.settings.setValue('audio/device_name', device_name)
# Initialize audio recorder
sample_rate = int(self.settings.value('audio/sample_rate', '44100'))
self.audio_recorder = AudioRecorder(sample_rate=sample_rate)
self.audio_recorder.set_device(device_id)
self.audio_recorder.finished.connect(self.handle_recording_finished)
self.audio_recorder.error.connect(self.handle_recording_error)
except Exception as e:
print(f"Error setting up audio recorder: {e}")
self.status_label.setText("Error: Could not initialize audio device")
# Create basic recorder without device for error handling
self.audio_recorder = AudioRecorder()
self.audio_recorder.finished.connect(self.handle_recording_finished)
self.audio_recorder.error.connect(self.handle_recording_error)
def start_recording(self):
self.status_label.setText('Recording...')
self.record_button.setText('Stop Recording')
self.audio_recorder.start_recording()
def stop_recording(self):
self.audio_recorder.stop_recording()
self.status_label.setText('Processing...')
self.record_button.setText('Start Recording')
def handle_recording_finished(self, filename):
try:
self.status_label.setText('Transcribing...')
self.transcribe_audio(filename)
except Exception as e:
self.handle_recording_error(f"Error processing recording: {str(e)}")
def handle_recording_error(self, error_message):
self.status_label.setText(f"Error: {error_message}")
self.record_button.setText('Start Recording')
def toggle_recording(self):
if not hasattr(self.audio_recorder, 'device') or self.audio_recorder.device is None:
self.status_label.setText("Error: No valid audio device")
return
if not self.is_recording:
self.is_recording = True
self.start_recording()
else:
self.is_recording = False
self.stop_recording()
def transcribe_audio(self, filename):
try:
self.status_label.setText('Transcribing...')
# Check the local mode tickbox to decide which transcription to use
if self.use_local_checkbox.isChecked():
if not hasattr(self, 'pipe'):
raise Exception("Local model is not loaded. Please click the 'Load Local Model' button.")
result = self.local_transcribe(filename)
else:
result = self.remote_transcribe(filename)
text = result["text"].strip()
# Update text box
self.transcription_text.setText(text)
# Save to CSV in the recordings directory with proper escaping
csv_file = os.path.join("recordings", "transcriptions.csv")
if not os.path.exists(csv_file):
with open(csv_file, "w", encoding='utf-8') as f:
f.write("id,text\n")
with open(csv_file, "a", encoding='utf-8') as f:
# Use csv module for proper escaping
writer = csv.writer(f, quoting=csv.QUOTE_MINIMAL)
writer.writerow([os.path.basename(filename), text])
# Handle clipboard and pasting based on checkbox settings
if self.auto_copy.isChecked():
success = copy_to_clipboard(text)
if not success:
self.status_label.setText("Warning: Could not copy to clipboard")