forked from benmaster82/writher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
680 lines (567 loc) Β· 21.9 KB
/
Copy pathmain.py
File metadata and controls
680 lines (567 loc) Β· 21.9 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
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
import ctypes
import locale
import queue
import re
import threading
import time
import tkinter as tk
# Fix DPI awareness before any window is created.
# CustomTkinter changes the DPI mode which shifts widget positioning.
try:
ctypes.windll.shcore.SetProcessDpiAwareness(1) # PROCESS_SYSTEM_DPI_AWARE
except Exception:
try:
ctypes.windll.user32.SetProcessDPIAware()
except Exception:
pass
# Use the OS locale for date/time formatting (%x, %X).
try:
locale.setlocale(locale.LC_TIME, "")
except locale.Error:
pass
_STOP = object() # sentinel to shut down pipeline workers
from logger import log
from recorder import Recorder
from transcriber import Transcriber
from injector import inject
from hotkey import HotkeyListener
from tray_icon import TrayIcon
from widget import RecordingWidget
import assistant
import config
import database as db
import locales
import notifier
from notifier import ReminderScheduler
from notes_window import NotesWindow
from settings_window import SettingsWindow
from replacements import apply_replacements
_pipeline_queue = queue.Queue()
_assistant_queue = queue.Queue()
recorder = Recorder()
transcriber = None
tray = None
widget = None
root = None
notes_win = None
settings_win = None
scheduler = None
hotkey_listener = None
_rec_start = 0.0
_MIN_DURATION = 0.5
_DELETE_CONFIRM_SECONDS = 15.0
_DELETE_CONFIRM_TOKEN = re.compile(r"^__confirm_delete__:(note|appointment|reminder):(\d+)$")
_pending_delete = None
# Toggle-mode timeout timers
_dict_timeout_timer = None
_assist_timeout_timer = None
# ββ Load persisted settings into config at startup ββββββββββββββββββββββββ
def _load_settings():
"""Read settings from DB and apply them to config module."""
from hotkey_util import str_to_key
hold = db.get_setting("hold_to_record", "")
if hold != "":
config.HOLD_TO_RECORD = hold == "1"
keep_clip = db.get_setting("keep_transcript_in_clipboard", "")
if keep_clip != "":
config.KEEP_TRANSCRIPT_IN_CLIPBOARD = keep_clip == "1"
max_sec = db.get_setting("max_record_seconds", "")
if max_sec != "":
try:
config.MAX_RECORD_SECONDS = int(max_sec)
except ValueError:
pass
mic = db.get_setting("mic_device_name", "")
if mic != "":
config.MIC_DEVICE_NAME = mic if mic != "none" else None
ollama_model = db.get_setting("ollama_model", "")
if ollama_model:
config.OLLAMA_MODEL = ollama_model
ollama_url = db.get_setting("ollama_url", "")
if ollama_url:
config.OLLAMA_URL = ollama_url
whisper = db.get_setting("whisper_model", "")
if whisper:
config.MODEL_SIZE = whisper
lang = db.get_setting("language", "")
if lang:
config.LANGUAGE = lang
wlang = db.get_setting("whisper_language", "")
if wlang != "":
config.WHISPER_LANGUAGE = None if wlang == "auto" else wlang
# Hotkeys
hk_dict = db.get_setting("hotkey_dictation", "")
if hk_dict:
parsed = str_to_key(hk_dict)
if parsed is not None:
config.HOTKEY = parsed
hk_assist = db.get_setting("hotkey_assistant", "")
if hk_assist:
parsed = str_to_key(hk_assist)
if parsed is not None:
config.ASSISTANT_HOTKEY = parsed
# ββ Toggle-mode timeout helpers βββββββββββββββββββββββββββββββββββββββββββ
def _start_timeout(mode: str):
"""Start a safety timer that auto-stops recording in toggle mode."""
global _dict_timeout_timer, _assist_timeout_timer
if config.HOLD_TO_RECORD:
return
seconds = getattr(config, "MAX_RECORD_SECONDS", 120)
if seconds <= 0:
return
if mode == "dictation":
_dict_timeout_timer = threading.Timer(seconds, _timeout_dictation)
_dict_timeout_timer.daemon = True
_dict_timeout_timer.start()
elif mode == "assistant":
_assist_timeout_timer = threading.Timer(seconds, _timeout_assistant)
_assist_timeout_timer.daemon = True
_assist_timeout_timer.start()
def _cancel_timeout(mode: str):
global _dict_timeout_timer, _assist_timeout_timer
if mode == "dictation" and _dict_timeout_timer is not None:
_dict_timeout_timer.cancel()
_dict_timeout_timer = None
elif mode == "assistant" and _assist_timeout_timer is not None:
_assist_timeout_timer.cancel()
_assist_timeout_timer = None
def _timeout_dictation():
log.warning("Toggle-mode dictation timeout reached.")
if hotkey_listener:
hotkey_listener.force_stop_dictation()
def _timeout_assistant():
log.warning("Toggle-mode assistant timeout reached.")
if hotkey_listener:
hotkey_listener.force_stop_assistant()
# ββ Dictation callbacks (AltGr) ββββββββββββββββββββββββββββββββββββββββββ
def _on_hotkey_press():
global _rec_start
_rec_start = time.monotonic()
recorder.start()
if tray:
tray.set_recording(True)
if widget:
widget.show_recording()
_start_timeout("dictation")
log.info("Recording started (dictation).")
def _on_hotkey_release():
_cancel_timeout("dictation")
audio = recorder.stop()
duration = time.monotonic() - _rec_start
if tray:
tray.set_recording(False)
log.info("Recording stopped (%.2fs).", duration)
if audio is not None and len(audio) > 0 and duration >= _MIN_DURATION:
if widget:
widget.show_processing()
_pipeline_queue.put(audio)
else:
if widget:
widget.hide()
if duration < _MIN_DURATION:
log.info("Too short (%.2fs), skipping.", duration)
else:
log.info("Empty audio, skipping.")
# ββ Assistant callbacks (Ctrl+R) ββββββββββββββββββββββββββββββββββββββββββ
def _on_assist_press():
global _rec_start
_rec_start = time.monotonic()
recorder.start()
if tray:
tray.set_recording(True)
if widget:
widget.show_assistant()
widget.set_expression("listening")
_start_timeout("assistant")
log.info("Recording started (assistant).")
def _on_assist_release():
_cancel_timeout("assistant")
audio = recorder.stop()
duration = time.monotonic() - _rec_start
if tray:
tray.set_recording(False)
log.info("Assistant recording stopped (%.2fs).", duration)
if audio is not None and len(audio) > 0 and duration >= _MIN_DURATION:
if widget:
widget.show_processing()
widget.set_expression("thinking")
_assistant_queue.put(audio)
else:
if widget:
widget.hide()
# ββ Pipeline workers ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _dictation_worker():
"""Transcribe audio and paste the result into the active application."""
while True:
item = _pipeline_queue.get()
if item is _STOP:
break
try:
log.info("Transcribing (dictation)...")
text = transcriber.transcribe(item)
if text:
log.debug("Raw: %r", text)
text = apply_replacements(text)
log.info("Transcribed: %r", text)
inject(text)
else:
log.info("No speech detected.")
except Exception as exc:
log.error("Dictation pipeline error: %s", exc)
finally:
if widget:
widget.hide()
def _parse_delete_token(result: str):
m = _DELETE_CONFIRM_TOKEN.match(result or "")
if not m:
return None
return m.group(1), int(m.group(2))
def _run_on_ui_thread(func):
"""Schedule a Tk/CustomTkinter operation on the Tk main thread."""
if root:
root.after(0, func)
def _set_pending_delete(kind: str, item_id: int):
global _pending_delete
_pending_delete = {
"kind": kind,
"id": item_id,
"expires_at": time.monotonic() + _DELETE_CONFIRM_SECONDS,
}
def _open_dialog():
try:
if not notes_win:
return
if not (notes_win._win and notes_win._win.winfo_exists()):
notes_win.show("notes")
notes_win.show_voice_delete_confirmation(kind, item_id)
log.info("Pending delete dialog opened: %s #%d", kind, item_id)
except Exception as exc:
log.error("Could not open voice delete dialog: %s", exc)
_run_on_ui_thread(_open_dialog)
log.info(
"Pending delete set: %s #%d (expires in %.1fs)",
kind,
item_id,
_DELETE_CONFIRM_SECONDS,
)
def _clear_pending_delete():
global _pending_delete
_pending_delete = None
def _close_voice_delete_dialog():
"""Close/hide the voice delete dialog on the Tk main thread."""
if not root:
return
def _close():
try:
if notes_win and notes_win._voice_delete_dialog:
dialog = notes_win._voice_delete_dialog
notes_win._voice_delete_dialog = None
notes_win._safe_destroy_dialog(dialog)
except Exception as exc:
log.error("Could not close voice delete dialog: %s", exc)
root.after(0, _close)
def _matches_locale_choice(text: str, key: str) -> bool:
t = " ".join((text or "").strip().lower().split())
if not t:
return False
choices = [
" ".join(choice.strip().lower().split())
for choice in locales.get_choices(key)
if choice.strip()
]
if not choices:
return False
alternatives = "|".join(re.escape(choice) for choice in choices)
pattern = rf"(?<!\w)(?:{alternatives})(?!\w)"
return bool(re.search(pattern, t))
def _is_affirmative(text: str) -> bool:
return _matches_locale_choice(text, "delete_confirmations")
def _is_negative(text: str) -> bool:
return _matches_locale_choice(text, "delete_rejections")
def _refresh_notes_window_if_open():
if notes_win and notes_win._win and notes_win._win.winfo_exists():
root.after(0, notes_win._refresh)
def _delete_by_pending(kind: str, item_id: int) -> str:
if kind == "note":
title = locales.get("default_note_title")
for note in db.get_all_notes():
if note["id"] == item_id:
title = note["title"] or title
break
else:
return locales.get("delete_item_missing", item=locales.get("delete_item_note"))
db.delete_note(item_id)
_refresh_notes_window_if_open()
return locales.get("note_deleted", title=title, nid=item_id)
if kind == "appointment":
title = None
for appointment in db.get_appointments():
if appointment["id"] == item_id:
title = appointment["title"]
break
if title is None:
return locales.get("delete_item_missing", item=locales.get("delete_item_appointment"))
db.delete_appointment(item_id)
_refresh_notes_window_if_open()
return locales.get("appointment_deleted", title=title, aid=item_id)
if kind == "reminder":
message = None
for reminder in db.get_all_reminders(include_notified=True):
if reminder["id"] == item_id:
message = reminder["message"]
break
if message is None:
return locales.get("delete_item_missing", item=locales.get("delete_item_reminder"))
db.delete_reminder(item_id)
_refresh_notes_window_if_open()
return locales.get("reminder_deleted", message=message, rid=item_id)
return locales.get("error", detail=f"Unsupported delete kind: {kind}")
def _handle_pending_delete_confirmation(text: str):
if _pending_delete is None:
return None
now = time.monotonic()
if now > _pending_delete["expires_at"]:
log.info("Pending delete expired.")
_close_voice_delete_dialog()
_clear_pending_delete()
return "__delete_confirm_timeout__"
if _is_affirmative(text):
kind = _pending_delete["kind"]
item_id = _pending_delete["id"]
_close_voice_delete_dialog()
_clear_pending_delete()
return _delete_by_pending(kind, item_id)
if _is_negative(text):
_close_voice_delete_dialog()
_clear_pending_delete()
return "__delete_cancelled__"
remaining = int(max(1, _pending_delete["expires_at"] - now))
return f"__delete_confirm_repeat__:{remaining}"
def _assistant_worker():
"""Transcribe audio, send to Ollama, and execute the returned action."""
while True:
item = _assistant_queue.get()
if item is _STOP:
break
try:
log.info("Transcribing (assistant)...")
text = transcriber.transcribe(item)
if not text:
log.info("No speech detected.")
if widget:
widget.hide()
continue
log.info("Assistant heard: %r", text)
result = _handle_pending_delete_confirmation(text)
if result is None:
if not assistant.ping_ollama():
log.warning("Ollama unreachable at %s β aborting assistant call.",
config.OLLAMA_URL)
notifier.notify(
locales.get("ollama_unreachable_title"),
locales.get("ollama_unreachable_body"),
)
if widget:
widget.set_expression("error")
widget.show_message(
locales.get("ollama_unreachable_body"), 3000)
continue
result = assistant.process(text)
log.info("Assistant result: %s", result)
token = _parse_delete_token(result)
if token:
kind, item_id = token
_set_pending_delete(kind, item_id)
if widget:
widget.set_expression("listening")
widget.show_message(
locales.get(
"delete_confirm_prompt",
item=locales.get(f"delete_item_{kind}"),
seconds=int(_DELETE_CONFIRM_SECONDS),
),
2200,
)
continue
# Handle special show commands
if result == "__delete_confirm_timeout__":
if widget:
widget.set_expression("sad")
widget.show_message(locales.get("delete_confirm_timeout"), 2200)
elif result == "__delete_cancelled__":
if widget:
widget.set_expression("sad")
widget.show_message(locales.get("delete_cancelled"), 2200)
elif result.startswith("__delete_confirm_repeat__:"):
remaining = int(result.rsplit(":", 1)[1])
if widget:
widget.set_expression("listening")
widget.show_message(locales.get("delete_confirm_repeat", seconds=remaining), 2200)
elif result == "__show_notes__":
if notes_win:
root.after(0, lambda: notes_win.show("notes"))
if widget:
widget.set_expression("happy")
widget.show_message(locales.get("show_notes"), 2000)
elif result == "__show_appointments__":
if notes_win:
root.after(0, lambda: notes_win.show("appointments"))
if widget:
widget.set_expression("happy")
widget.show_message(locales.get("show_appointments"), 2000)
elif result == "__show_reminders__":
if notes_win:
root.after(0, lambda: notes_win.show("reminders"))
if widget:
widget.set_expression("happy")
widget.show_message(locales.get("show_reminders"), 2000)
elif result == locales.get("not_understood") or result.startswith(locales.get("error", detail="")):
if widget:
widget.set_expression("sad")
widget.show_message("β", 2000)
else:
if widget:
widget.set_expression("happy")
widget.show_message("β", 2000)
except Exception as exc:
log.error("Assistant pipeline error: %s", exc)
if widget:
widget.set_expression("error")
widget.show_message(locales.get("assistant_error"), 2000)
# ββ Quit & Main βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _show_notes():
"""Open notes window from tray menu."""
if notes_win:
root.after(0, lambda: notes_win.show("notes"))
def _show_settings():
"""Open settings window from tray menu."""
if settings_win:
root.after(0, lambda: settings_win.show())
def _quit():
log.info("Quitting...")
_cancel_timeout("dictation")
_cancel_timeout("assistant")
_pipeline_queue.put(_STOP)
_assistant_queue.put(_STOP)
if scheduler:
scheduler.stop()
if hotkey_listener:
try:
hotkey_listener.stop()
except Exception:
pass
if tray:
try:
tray.stop()
except Exception:
pass
try:
recorder.stop()
except Exception:
pass
# Hide child windows immediately, then destroy root after event queue drains
if root:
try:
if notes_win and notes_win._win and notes_win._win.winfo_exists():
notes_win._win.withdraw()
if settings_win and settings_win._win and settings_win._win.winfo_exists():
settings_win._win.withdraw()
except Exception:
pass
try:
root.after(50, _destroy_root)
except Exception:
pass
log.info("Shutdown complete.")
def _destroy_root():
"""Destroy root after pending Tk events have been processed."""
try:
root.destroy()
except Exception:
pass
def _acquire_instance_lock():
"""Return a Win32 mutex handle if this is the first instance, else exit."""
import ctypes
mutex = ctypes.windll.kernel32.CreateMutexW(None, True, "Local\\WritHerSingleInstance")
if ctypes.windll.kernel32.GetLastError() == 183: # ERROR_ALREADY_EXISTS
log.error("Another instance of WritHer is already running. Exiting.")
try:
notifier.notify(
locales.get("already_running_title"),
locales.get("already_running_body"),
)
except Exception:
pass
raise SystemExit(1)
return mutex # keep reference so it isn't garbage-collected
def _finish_startup():
"""Load the Whisper model and start the pipelines.
Runs on a background thread so the Tk mainloop can paint the widget
while the model loads β on first launch the download takes minutes,
and without visible feedback the app looks dead.
"""
global transcriber, scheduler, hotkey_listener
from hotkey_util import key_display_name
model_flag = f"model_ready_{config.MODEL_SIZE}"
first_model_load = db.get_setting(model_flag, "") != "1"
widget.show_status(locales.get(
"model_downloading" if first_model_load else "model_loading"))
try:
transcriber = Transcriber()
except Exception as exc:
log.error("Whisper model load failed: %s", exc)
widget.show_status(locales.get("model_error"), expression="error")
notifier.notify(
locales.get("model_error_title"),
locales.get("model_error_body"),
)
return # tray stays alive so the user can read the toast and quit
db.save_setting(model_flag, "1")
scheduler = ReminderScheduler()
scheduler.start()
t1 = threading.Thread(target=_dictation_worker, daemon=True)
t1.start()
t2 = threading.Thread(target=_assistant_worker, daemon=True)
t2.start()
hotkey_listener = HotkeyListener(
on_press_cb=_on_hotkey_press,
on_release_cb=_on_hotkey_release,
on_assist_press_cb=_on_assist_press,
on_assist_release_cb=_on_assist_release,
)
hotkey_listener.start()
dict_key = key_display_name(config.HOTKEY)
widget.set_expression("happy")
widget.show_message(locales.get("startup_ready", hotkey=dict_key), 3500)
if db.get_setting("welcome_shown", "") != "1":
notifier.notify(
locales.get("welcome_title"),
locales.get(
"welcome_body",
dict_key=dict_key,
assist_key=key_display_name(config.ASSISTANT_HOTKEY),
),
)
db.save_setting("welcome_shown", "1")
log.info("Ready. %s=dictate, %s=assistant.",
dict_key, key_display_name(config.ASSISTANT_HOTKEY))
def main():
global tray, widget, root, notes_win, settings_win
_mutex = _acquire_instance_lock()
db.init()
_load_settings()
root = tk.Tk()
root.withdraw()
widget = RecordingWidget(root)
notes_win = NotesWindow(root)
settings_win = SettingsWindow(root)
recorder.on_level = lambda rms: widget.update_level(min(1.0, rms * 8))
recorder.on_mic_error = lambda msg: widget.show_message(msg, 4000)
tray = TrayIcon(on_quit=_quit, on_show_notes=_show_notes,
on_show_settings=_show_settings)
tray.start()
tray.set_tooltip(locales.get("tray_idle"))
threading.Thread(target=_finish_startup, daemon=True).start()
root.mainloop()
if __name__ == "__main__":
main()