voizce 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
voizce/__init__.py ADDED
@@ -0,0 +1 @@
1
+ """Voizce: a private, real-time prompter for video calls."""
voizce/__main__.py ADDED
@@ -0,0 +1,37 @@
1
+ """`voizce` opens the window, `voizce --demo` too but visible to screen recordings,
2
+ `voizce digest` writes the repository summary from the terminal."""
3
+ import sys
4
+
5
+
6
+ def _hide_child_consoles():
7
+ # With no console of its own, Windows opens a terminal window for claude.exe when you press
8
+ # Listen. The SDK doesn't let us pass creationflags, so add them to everything it launches.
9
+ import functools
10
+ import subprocess
11
+ import anyio
12
+
13
+ open_process = anyio.open_process
14
+
15
+ @functools.wraps(open_process)
16
+ async def open_process_hidden(*args, **kwargs):
17
+ kwargs.setdefault("creationflags", subprocess.CREATE_NO_WINDOW)
18
+ return await open_process(*args, **kwargs)
19
+
20
+ anyio.open_process = open_process_hidden
21
+
22
+
23
+ def main():
24
+ if sys.stderr is None: # pythonw (shortcut): no console, errors go to voizce.log
25
+ from .app import APP_DIR
26
+ APP_DIR.mkdir(parents=True, exist_ok=True)
27
+ sys.stdout = sys.stderr = open(APP_DIR / "voizce.log", "a", encoding="utf-8")
28
+ _hide_child_consoles()
29
+ if sys.argv[1:2] == ["digest"]:
30
+ from .digest import main as run
31
+ else:
32
+ from .app import main as run
33
+ run()
34
+
35
+
36
+ if __name__ == "__main__":
37
+ main()
voizce/answer.py ADDED
@@ -0,0 +1,257 @@
1
+ """Question filter, prompt, SKIP handling and the two engines (subscription / API)."""
2
+ import asyncio
3
+ import re
4
+
5
+ from .i18n import tr
6
+
7
+ # --- quick filter (local and permissive: Claude discards the rest) --------------------------
8
+
9
+ # Parakeet often drops «¿» and accents («que es lo primero…», «Como sabemos…»), so at the start
10
+ # of a sentence or clause the unaccented forms and the usual yes/no openers count too.
11
+ _CLAUSE_START = r"(?:^|[.,;:!?¡¿]\s*)(?:y |pero |entonces |vale,? |oye,? )?(?:tú )?"
12
+ _CANDIDATE = re.compile(
13
+ r"[?¿]"
14
+ r"|\b(qué|cómo|dónde|adónde|cuándo|cuál|cuáles|quién|quiénes|cuánto|cuánta|cuántos|cuántas|por qué|para qué)\b"
15
+ r"|" + _CLAUSE_START +
16
+ r"(que|como|donde|cuando|cual|cuales|quien|quienes|cuanto|cuanta|cuantos|cuantas|porque|por que|para que"
17
+ r"|se puede|puedes?|podemos|podéis|hay|es posible|tienes?|tenemos|tenéis|has|habéis|existe|está|estás"
18
+ r"|funciona|usáis|usas|soporta|no crees|crees|piensas|consideras|te parece"
19
+ r"|\w+(?:aste|iste))\b" # 2nd person past tense: «usaste…», «elegiste…», «hiciste…»
20
+ r"|\b(me|nos) (explicas|explicarías|cuentas|contarías|dices|dirías|enseñas)\b"
21
+ r"|\b(explícame|explícanos|cuéntame|cuéntanos|dime|dinos|podrías|podríais|sabrías|sab(?:e|es|éis|eis|en) si"
22
+ r"|puedes (contar|explicar|decir)|tienes idea|alguna idea|qu[eé] tal si)\b"
23
+ r"|\b(what|how|where|when|which|who|why|could you|can you|do you know|tell me)\b",
24
+ re.IGNORECASE,
25
+ )
26
+
27
+
28
+ def is_candidate(text: str) -> bool:
29
+ return bool(_CANDIDATE.search(text))
30
+
31
+
32
+ # --- SKIP ------------------------------------------------------------------------------------
33
+
34
+ class SkipGate:
35
+ """Holds back the start of the stream until it's clear whether the answer is SKIP.
36
+
37
+ feed() returns the text that can already be shown; finish() whatever is left at the end.
38
+ """
39
+
40
+ def __init__(self):
41
+ self.buf, self.open, self.skipped = "", False, False
42
+
43
+ def feed(self, chunk: str) -> str:
44
+ if self.open:
45
+ return chunk
46
+ if self.skipped:
47
+ return ""
48
+ self.buf += chunk
49
+ s = self.buf.lstrip()
50
+ if "SKIP".startswith(s): # empty or a prefix of SKIP: can't tell yet
51
+ return ""
52
+ if s.startswith("SKIP"):
53
+ self.skipped = True
54
+ return ""
55
+ self.open = True
56
+ return s
57
+
58
+ def finish(self) -> str:
59
+ if self.open or self.skipped:
60
+ return ""
61
+ s = self.buf.strip()
62
+ if s == "SKIP":
63
+ self.skipped = True
64
+ return ""
65
+ self.open = True
66
+ return s
67
+
68
+
69
+ # --- prompt ----------------------------------------------------------------------------------
70
+
71
+ _INSTRUCTIONS = """\
72
+ You are the user's private prompter during a video call. Other people ask them questions about \
73
+ their work and you give them, in a window only they can see, an answer they can read out loud.
74
+
75
+ You receive the recent conversation (only what the others say) and a PHRASE. Both come from \
76
+ speech recognition: question marks and accents are often missing, and in Spanish «por qué» \
77
+ often comes out as «porque». Judge whether it is a question by its meaning, not its punctuation.
78
+
79
+ 1. Decide whether the PHRASE is a question or request aimed at the user about the project, the \
80
+ code or their work. If it isn't (greetings, comments, rhetorical questions, small talk, questions \
81
+ for someone else), reply exactly SKIP and nothing else. Whether the documents cover the answer \
82
+ plays no part in this decision: a question for the user is never SKIP.
83
+ 2. If it is, answer it:
84
+ - In the language of the PHRASE.
85
+ - At most 2 sentences and 50 words in total.
86
+ - The first sentence is what the user will say out loud, in the first person, as if they were \
87
+ speaking: no "you could say", no quotes. The second, only if it helps, a concrete detail (file, \
88
+ function, figure).
89
+ - Use only the DOCUMENTS below and the conversation. If they don't cover the question, start \
90
+ with "I'm not sure" (in the language of the PHRASE) and say only what they do state. Never make \
91
+ up names, files, figures or behaviour.
92
+ - Don't offer to do anything else. Plain text: no markdown, no lists, no preamble.
93
+ """
94
+
95
+
96
+ def build_system(digest: str, extra: str, personal: str) -> str:
97
+ parts = [_INSTRUCTIONS]
98
+ if personal.strip():
99
+ parts.append("## About the user\n\n" + personal.strip())
100
+ parts.append("## DOCUMENT 1: repository summary\n\n" + (digest.strip() or "(not available)"))
101
+ parts.append("## DOCUMENT 2: additional documentation\n\n" + (extra.strip() or "(not available)"))
102
+ return "\n\n".join(parts) + "\n"
103
+
104
+
105
+ def build_user(recent: list[str], phrase: str, manual: bool = False) -> str:
106
+ convo = "\n".join(f"- {s}" for s in recent) or "(nothing)"
107
+ msg = f"Recent conversation:\n{convo}\n\nPHRASE: «{phrase}»\n\n"
108
+ if manual:
109
+ msg += "The user asked for this phrase to be answered: don't reply SKIP. "
110
+ else:
111
+ msg += ("SKIP only if it isn't a question for the user. Without punctuation it can still be "
112
+ "one: «se puede usar X», «sabe si hay Y», «porque pasa Z», «any plans to open "
113
+ "source it» are questions. ")
114
+ # Repeated at the end: with tens of thousands of document tokens in front, Haiku forgets the
115
+ # limits and copies the documents' tone (in testing, that of a university thesis).
116
+ return msg + ("At most 2 sentences and 50 words, in the first person; if the documents don't "
117
+ "cover it, start with \"I'm not sure\". Spoken tone, like in a conversation: "
118
+ "short sentences and plain words, don't copy the style of the documents.")
119
+
120
+
121
+ # --- engines ---------------------------------------------------------------------------------
122
+ # Common interface: await start(); async for chunk in stream(user_text); await close().
123
+ # stream() can be cancelled (task.cancel()); the engine is ready for the next one afterwards.
124
+
125
+ class ApiEngine:
126
+ def __init__(self, api_key: str, model: str, system: str):
127
+ import anthropic
128
+ self.client = anthropic.AsyncAnthropic(api_key=api_key, max_retries=0)
129
+ self.model = model
130
+ # 1 h TTL: in a meeting more than 5 minutes can go by between questions.
131
+ self.system = [{"type": "text", "text": system, "cache_control": {"type": "ephemeral", "ttl": "1h"}}]
132
+
133
+ def _params(self, user_text: str, max_tokens: int) -> dict:
134
+ return dict(
135
+ model=self.model,
136
+ max_tokens=max_tokens,
137
+ system=self.system,
138
+ thinking={"type": "disabled"},
139
+ output_config={"effort": "low"},
140
+ messages=[{"role": "user", "content": user_text}],
141
+ )
142
+
143
+ async def start(self):
144
+ # Warms the system prompt cache so the first question doesn't pay for it.
145
+ try:
146
+ await self.client.messages.create(**self._params("hi", 1))
147
+ except Exception:
148
+ pass # the real error shows up on the first question, with its message
149
+
150
+ async def stream(self, user_text: str):
151
+ import anthropic
152
+ try:
153
+ async with self.client.messages.stream(**self._params(user_text, 300)) as s:
154
+ async for text in s.text_stream:
155
+ yield text
156
+ except anthropic.AuthenticationError:
157
+ raise RuntimeError(tr("Invalid Anthropic API key."))
158
+ except anthropic.RateLimitError:
159
+ raise RuntimeError(tr("Anthropic API rate limit reached."))
160
+
161
+ async def close(self):
162
+ await self.client.close()
163
+
164
+
165
+ _SUB_ERRORS = {
166
+ "authentication_failed": "Claude Code isn't logged in: run «claude» and use /login.",
167
+ "rate_limit": "Claude usage limit reached.",
168
+ "billing_error": "Claude usage limit reached (billing).",
169
+ }
170
+
171
+
172
+ class SubscriptionEngine:
173
+ """One Claude Code process kept warm for the whole session (Claude Agent SDK), one conversation.
174
+
175
+ Reusing the conversation measured better than /clear per question (20/20 vs 18/20 within
176
+ 3 s; after /clear the first token took up to 2-4 s).
177
+ ponytail: the history grows with each question (~250 tokens); irrelevant in a one-hour
178
+ meeting. If it ever matters, /clear between questions once it passes some size.
179
+ """
180
+
181
+ def __init__(self, model: str, system: str):
182
+ import tempfile
183
+ from pathlib import Path
184
+ from claude_agent_sdk import ClaudeAgentOptions
185
+ # Through a file: a ~100 KB prompt doesn't fit on the Windows command line.
186
+ self.system_file = Path(tempfile.gettempdir()) / "voizce_system_prompt.md"
187
+ self.system_file.write_text(system, encoding="utf-8")
188
+ self.options = ClaudeAgentOptions(
189
+ system_prompt={"type": "file", "path": str(self.system_file)},
190
+ model=model,
191
+ tools=[],
192
+ setting_sources=[],
193
+ strict_mcp_config=True,
194
+ max_turns=1,
195
+ thinking={"type": "disabled"},
196
+ effort="low",
197
+ include_partial_messages=True,
198
+ # Otherwise the CLI would use an API key from the environment instead of the subscription.
199
+ env={"ANTHROPIC_API_KEY": ""},
200
+ )
201
+ self.client = None
202
+ self._lock = asyncio.Lock()
203
+ self._dirty = False # interrupted turn whose messages haven't been drained yet
204
+
205
+ async def start(self):
206
+ from claude_agent_sdk import ClaudeSDKClient
207
+ self.client = ClaudeSDKClient(self.options)
208
+ await self.client.connect()
209
+ # First call before the meeting: writes the prompt cache and warms up the process.
210
+ async for _ in self.stream(build_user([], "Hi, can you hear me?")):
211
+ pass
212
+
213
+ async def _drain(self):
214
+ async def run():
215
+ async for _ in self.client.receive_response():
216
+ pass
217
+ await asyncio.wait_for(run(), 5)
218
+
219
+ async def _tidy(self):
220
+ if self._dirty:
221
+ await self.client.interrupt()
222
+ await self._drain()
223
+ self._dirty = False
224
+
225
+ async def stream(self, user_text: str):
226
+ from claude_agent_sdk import AssistantMessage, ResultMessage, StreamEvent
227
+ async with self._lock:
228
+ await self._tidy() # usually the background task already did it
229
+ self._dirty = True
230
+ try:
231
+ await self.client.query(user_text)
232
+ async for msg in self.client.receive_response():
233
+ if isinstance(msg, StreamEvent):
234
+ ev = msg.event
235
+ if ev.get("type") == "content_block_delta" and ev["delta"].get("type") == "text_delta":
236
+ yield ev["delta"]["text"]
237
+ elif isinstance(msg, AssistantMessage) and msg.error:
238
+ raise RuntimeError(tr(_SUB_ERRORS[msg.error]) if msg.error in _SUB_ERRORS
239
+ else f"Claude Code: {msg.error}")
240
+ elif isinstance(msg, ResultMessage):
241
+ self._dirty = False
242
+ if msg.is_error:
243
+ raise RuntimeError("Claude Code: " + "; ".join(msg.errors or [msg.result or msg.subtype]))
244
+ finally:
245
+ if self._dirty: # cancelled halfway: interrupt and drain in the background
246
+ asyncio.get_running_loop().create_task(self._locked_tidy())
247
+
248
+ async def _locked_tidy(self):
249
+ async with self._lock:
250
+ try:
251
+ await self._tidy()
252
+ except Exception:
253
+ pass
254
+
255
+ async def close(self):
256
+ if self.client:
257
+ await self.client.disconnect()
voizce/app.py ADDED
@@ -0,0 +1,379 @@
1
+ """Startup and controller: capture → Parakeet → filter → Claude → window."""
2
+ import asyncio
3
+ import json
4
+ import os
5
+ import queue
6
+ import re
7
+ import subprocess
8
+ import sys
9
+ import threading
10
+ import time
11
+ import tomllib
12
+ from pathlib import Path
13
+
14
+ import numpy as np
15
+ from PySide6.QtCore import QObject, QTimer, Signal
16
+ from PySide6.QtWidgets import QApplication, QDialog
17
+
18
+ from .answer import ApiEngine, SkipGate, SubscriptionEngine, build_system, build_user, is_candidate
19
+ from .audio import Capture
20
+ from .i18n import tr
21
+ from .ui import SettingsDialog, Window
22
+
23
+ APP_DIR = Path(os.environ.get("APPDATA") or Path.home()) / "Voizce"
24
+ CONFIG = APP_DIR / "config.toml"
25
+ DEFAULTS = {"repo": "", "digest": "REPO_DIGEST.md", "extra_doc": "", "personal": "", "silence_ms": 250,
26
+ "model": "claude-haiku-5-5", "asr": "cpu-int8", "api_key": "", "timeout_s": 8}
27
+ # The design said ~30k, but with ~80k real tokens (digest + a thesis) 12/12 answers came within
28
+ # 3 s (median 1.6 s): with the prompt cached, its size barely matters.
29
+ TOKEN_BUDGET = 100_000
30
+
31
+
32
+ def resolve(p: str) -> Path:
33
+ path = Path(p).expanduser()
34
+ return path if path.is_absolute() else APP_DIR / path
35
+
36
+
37
+ def load_config() -> dict:
38
+ cfg = dict(DEFAULTS)
39
+ if CONFIG.exists():
40
+ cfg.update(tomllib.loads(CONFIG.read_text(encoding="utf-8")))
41
+ return cfg
42
+
43
+
44
+ def save_config(cfg: dict):
45
+ APP_DIR.mkdir(parents=True, exist_ok=True)
46
+ # The JSON form of a string or number is also valid TOML. Hand-written comments are lost.
47
+ CONFIG.write_text("".join(f"{k} = {json.dumps(v, ensure_ascii=False)}\n" for k, v in cfg.items()),
48
+ encoding="utf-8")
49
+
50
+
51
+ def git_head(repo: str) -> str:
52
+ try:
53
+ # safe.directory: Windows git rejects WSL repos (\\wsl.localhost\...) over ownership.
54
+ # CREATE_NO_WINDOW: without it a terminal flashes when started from a shortcut.
55
+ return subprocess.run(["git", "-c", "safe.directory=*", "-C", repo, "rev-parse", "HEAD"],
56
+ capture_output=True, text=True, timeout=5,
57
+ creationflags=subprocess.CREATE_NO_WINDOW).stdout.strip()
58
+ except Exception:
59
+ return ""
60
+
61
+
62
+ def read_docs(cfg: dict) -> tuple[str, str, list[str]]:
63
+ warnings = []
64
+ digest_path = resolve(cfg["digest"])
65
+ digest = digest_path.read_text(encoding="utf-8") if digest_path.is_file() else ""
66
+ if not digest:
67
+ warnings.append(tr("no repository summary yet (⚙)"))
68
+ extra = ""
69
+ if cfg["extra_doc"]:
70
+ extra_path = resolve(cfg["extra_doc"])
71
+ if extra_path.is_file():
72
+ extra = extra_path.read_text(encoding="utf-8")
73
+ else:
74
+ warnings.append(tr("extra document not found"))
75
+ if (m := re.search(r"<!-- voizce head=(\w+) -->", digest)) and cfg["repo"]:
76
+ if (head := git_head(cfg["repo"])) and head != m.group(1):
77
+ warnings.append(tr("the repository changed since its summary was made (⚙ to update it)"))
78
+ tokens = int((len(digest) + len(extra)) / 2.3) # measured ~2.3 chars per token in Spanish; English is ~4
79
+ if tokens > TOKEN_BUDGET:
80
+ warnings.append(tr("documents ~{k}k tokens (> {b}k): the first word will take longer",
81
+ k=tokens // 1000, b=TOKEN_BUDGET // 1000))
82
+ return digest, extra, warnings
83
+
84
+
85
+ def make_engine(cfg: dict, system: str):
86
+ api_key = cfg["api_key"] or os.environ.get("ANTHROPIC_API_KEY", "")
87
+ return ApiEngine(api_key, cfg["model"], system) if api_key else SubscriptionEngine(cfg["model"], system)
88
+
89
+
90
+ def create_shortcuts():
91
+ """Desktop and Start menu shortcuts that open Voizce without a console window."""
92
+ exe = str(Path(sys.executable).with_name("pythonw.exe")).replace("'", "''")
93
+ ps = ("$s = New-Object -ComObject WScript.Shell; foreach ($d in 'Desktop', 'Programs') { "
94
+ "$l = $s.CreateShortcut([Environment]::GetFolderPath($d) + '\\Voizce.lnk'); "
95
+ f"$l.TargetPath = '{exe}'; $l.Arguments = '-m voizce'; "
96
+ "$l.IconLocation = [Environment]::GetFolderPath('System') + '\\SndVol.exe,0'; $l.Save() }")
97
+ subprocess.run(["powershell", "-NoProfile", "-Command", ps], check=True, capture_output=True, timeout=30,
98
+ creationflags=subprocess.CREATE_NO_WINDOW)
99
+
100
+
101
+ class Controller(QObject):
102
+ # Signals emitted from other threads; Qt delivers them on the window's thread.
103
+ speech_start = Signal()
104
+ speech_end = Signal()
105
+ phrase = Signal(str, float) # text, t_end
106
+ asr_ready = Signal()
107
+ capture_failed = Signal(str)
108
+ chunk = Signal(int, str) # card, text
109
+ done = Signal(int, str, str) # card, kind (ok/skip/cancelled/error), note
110
+ digest_step = Signal(str)
111
+ digest_done = Signal(str) # error message, empty if it worked
112
+
113
+ def __init__(self, cfg: dict, win: Window, demo: bool):
114
+ super().__init__()
115
+ self.cfg, self.win, self.demo = cfg, win, demo
116
+ self.loop = asyncio.new_event_loop()
117
+ threading.Thread(target=self.loop.run_forever, daemon=True).start()
118
+ self.engine = None
119
+ self.engine_started = None # concurrent.futures.Future of engine.start()
120
+ self.warnings: list[str] = []
121
+ self.capture = None
122
+ self.asr_loaded = False
123
+ self.asr_q = queue.Queue()
124
+ self.lines: list[str] = []
125
+ self.speaking = False
126
+ self.pending = None # (text, row of its first piece): a question split in two
127
+ self.current = None # call in progress
128
+ self.card_rows: dict[int, int] = {}
129
+ self.card_seq = 0
130
+ self.error = ""
131
+ self.digesting = None # Future of the repository summary being written
132
+ self.digest_note = ""
133
+
134
+ self.speech_start.connect(self.on_speech_start)
135
+ self.speech_end.connect(lambda: setattr(self, "speaking", False))
136
+ self.phrase.connect(self.on_phrase)
137
+ self.asr_ready.connect(self.on_asr_ready)
138
+ self.capture_failed.connect(self.on_capture_failed)
139
+ self.chunk.connect(win.append_card)
140
+ self.done.connect(self.on_done)
141
+ self.digest_step.connect(self.on_digest_step)
142
+ self.digest_done.connect(self.on_digest_done)
143
+ win.listen_toggled.connect(self.on_listen)
144
+ win.answer_requested.connect(self.on_answer_requested)
145
+ win.settings_requested.connect(self.open_settings)
146
+ threading.Thread(target=self._asr_worker, daemon=True).start()
147
+ self.reload()
148
+
149
+ # --- status ---
150
+ def refresh(self):
151
+ if self.error:
152
+ kind, text = "error", self.error
153
+ elif self.digesting:
154
+ kind, text = "loading", tr("reading your repository… {step}", step=self.digest_note)
155
+ elif not self.asr_loaded:
156
+ kind, text = "loading", tr("loading Parakeet…")
157
+ elif self.current and not self.current["fut"].done():
158
+ kind, text = "thinking", tr("thinking…")
159
+ elif self.capture:
160
+ kind, text = "listening", tr("listening")
161
+ else:
162
+ kind, text = "paused", tr("paused")
163
+ warnings = list(self.warnings)
164
+ if self.demo:
165
+ warnings.append(tr("demo mode: the window shows up in screen recordings"))
166
+ elif self.win.isVisible() and not self.win.capture_hidden:
167
+ warnings.append(tr("couldn't hide the window from screen capture"))
168
+ if warnings:
169
+ text += " · ⚠ " + " · ".join(warnings)
170
+ self.win.set_status(kind, text)
171
+
172
+ # --- documents, engine and settings ---
173
+ def reload(self):
174
+ """(Re)builds the prompt and the engine from the current config and documents."""
175
+ digest, extra, self.warnings = read_docs(self.cfg)
176
+ if self.current and not self.current["fut"].done():
177
+ self.current["fut"].cancel()
178
+ old, started = self.engine, self.engine_started
179
+ self.engine = make_engine(self.cfg, build_system(digest, extra, self.cfg["personal"]))
180
+ self.engine_started = None
181
+ if old:
182
+ if started:
183
+ started.cancel()
184
+ asyncio.run_coroutine_threadsafe(old.close(), self.loop)
185
+ if self.capture:
186
+ self._ensure_engine()
187
+ self.refresh()
188
+
189
+ def open_settings(self):
190
+ digest_path = resolve(self.cfg["digest"])
191
+ needs_digest = bool(self.cfg["repo"]) and not digest_path.is_file()
192
+ dlg = SettingsDialog(self.win, self.cfg, digest_path, needs_digest or not CONFIG.exists(), create_shortcuts)
193
+ if dlg.exec() != QDialog.Accepted:
194
+ return
195
+ self.cfg.update(dlg.values())
196
+ save_config(self.cfg)
197
+ self.error = ""
198
+ if dlg.regen.isChecked() and self.cfg["repo"]:
199
+ self.start_digest()
200
+ self.reload()
201
+
202
+ def start_digest(self):
203
+ from .digest import generate # digest imports this module
204
+ if self.digesting:
205
+ return
206
+ self.digest_note = ""
207
+ self.digesting = asyncio.run_coroutine_threadsafe(generate(dict(self.cfg), self.digest_step.emit), self.loop)
208
+ self.digesting.add_done_callback(
209
+ lambda f: self.digest_done.emit("" if f.cancelled() or not f.exception() else str(f.exception())))
210
+ self.refresh()
211
+
212
+ def on_digest_step(self, step: str):
213
+ self.digest_note = re.split(r"[\\/]", step)[-1] # just the file name
214
+ self.refresh()
215
+
216
+ def on_digest_done(self, err: str):
217
+ self.digesting = None
218
+ if err:
219
+ self.error = tr("repository summary failed: {e}", e=err)
220
+ self.reload()
221
+
222
+ # --- transcription ---
223
+ def _asr_worker(self):
224
+ try:
225
+ import onnx_asr
226
+ kw = ({"providers": ["DmlExecutionProvider", "CPUExecutionProvider"]} if self.cfg["asr"] == "directml"
227
+ else {"providers": ["CPUExecutionProvider"], "quantization": "int8"})
228
+ asr = onnx_asr.load_model("nemo-parakeet-tdt-0.6b-v3", **kw)
229
+ asr.recognize(np.zeros(16000, np.float32)) # warm-up
230
+ except Exception as e:
231
+ self.capture_failed.emit(tr("couldn't load Parakeet: {e}", e=e))
232
+ return
233
+ self.asr_ready.emit()
234
+ while True:
235
+ audio, t_end = self.asr_q.get()
236
+ try:
237
+ text = asr.recognize(audio).strip()
238
+ except Exception:
239
+ text = ""
240
+ self.phrase.emit(text, t_end)
241
+
242
+ def on_asr_ready(self):
243
+ self.asr_loaded = True
244
+ self.refresh()
245
+
246
+ # --- listen / pause ---
247
+ def _ensure_engine(self):
248
+ f = self.engine_started
249
+ if f is None or (f.done() and (f.cancelled() or f.exception())):
250
+ self.engine_started = asyncio.run_coroutine_threadsafe(self.engine.start(), self.loop)
251
+ self.engine_started.add_done_callback(self._engine_start_done)
252
+
253
+ def _engine_start_done(self, f):
254
+ if not f.cancelled() and f.exception():
255
+ self.capture_failed.emit(f"Claude: {f.exception()}") # informative only; transcription goes on
256
+
257
+ def on_listen(self, on: bool):
258
+ self.error = ""
259
+ if on and not self.capture:
260
+ self._ensure_engine()
261
+ self.capture = Capture(self.cfg["silence_ms"], self.speech_start.emit, self._on_utterance,
262
+ self.capture_failed.emit)
263
+ self.capture.start()
264
+ elif not on and self.capture:
265
+ self.capture.stop()
266
+ self.capture = None
267
+ self.refresh()
268
+
269
+ def _on_utterance(self, audio, t_end): # audio thread
270
+ self.speech_end.emit()
271
+ self.asr_q.put((audio, t_end))
272
+
273
+ def on_capture_failed(self, msg: str):
274
+ self.error = msg
275
+ if msg.startswith("audio:") and self.capture:
276
+ self.capture = None
277
+ self.win.set_listening(False)
278
+ self.refresh()
279
+
280
+ # --- questions ---
281
+ def on_speech_start(self):
282
+ self.speaking = True
283
+ c = self.current
284
+ if c and not c["fut"].done() and not c["manual"]:
285
+ # Question split in two: cancel it and resend it joined with the phrase starting now.
286
+ c["fut"].cancel()
287
+ self.pending = (c["phrase"], c["first_row"])
288
+
289
+ def on_phrase(self, text: str, t_end: float):
290
+ if not text: # noise with no words
291
+ if self.pending and not self.speaking:
292
+ (prev, first), self.pending = self.pending, None
293
+ self._send(prev, first, first, t_end, manual=False)
294
+ return
295
+ row = self.win.add_line(text)
296
+ self.lines.append(text)
297
+ if self.pending:
298
+ (prev, first), self.pending = self.pending, None
299
+ self._send(f"{prev} {text}", row, first, t_end, manual=False)
300
+ elif is_candidate(text):
301
+ if self.speaking: # another phrase already started: they'll be sent together
302
+ self.pending = (text, row)
303
+ else:
304
+ self._send(text, row, row, t_end, manual=False)
305
+
306
+ def on_answer_requested(self, row: int):
307
+ c = self.current
308
+ if c and c["manual"] and c["first_row"] == row and not c["fut"].done():
309
+ return # double click: that line is already being answered
310
+ self._send(self.win.line_text(row), row, row, time.monotonic(), manual=True)
311
+
312
+ def _send(self, phrase: str, row: int, first_row: int, t_end: float, manual: bool):
313
+ if self.current and not self.current["fut"].done():
314
+ self.current["fut"].cancel() # one call at a time: the latest wins
315
+ self._ensure_engine()
316
+ self.card_seq += 1
317
+ card = self.card_seq
318
+ self.card_rows[card] = row
319
+ self.win.new_card(card, phrase)
320
+ recent = self.lines[max(0, first_row - 10):first_row]
321
+ fut = asyncio.run_coroutine_threadsafe(self._ask(card, phrase, recent, manual, t_end), self.loop)
322
+ self.current = {"fut": fut, "phrase": phrase, "first_row": first_row, "manual": manual}
323
+ self.refresh()
324
+
325
+ async def _ask(self, card, phrase, recent, manual, t_end):
326
+ gate = SkipGate()
327
+ engine, started = self.engine, self.engine_started
328
+ try:
329
+ async with asyncio.timeout(self.cfg["timeout_s"]):
330
+ # shield: cancelling this question must not cancel the engine start-up
331
+ await asyncio.shield(asyncio.wrap_future(started))
332
+ async for piece in engine.stream(build_user(recent, phrase, manual)):
333
+ if out := gate.feed(piece):
334
+ self.chunk.emit(card, out)
335
+ if out := gate.finish():
336
+ self.chunk.emit(card, out)
337
+ if gate.skipped:
338
+ self.done.emit(card, "skip", tr("Claude doesn't see it as a question") if manual else "")
339
+ else:
340
+ self.done.emit(card, "ok", f"{time.monotonic() - t_end:.1f} s")
341
+ except asyncio.CancelledError:
342
+ self.done.emit(card, "cancelled", tr("interrupted"))
343
+ raise
344
+ except TimeoutError:
345
+ self.done.emit(card, "error", tr("no answer: over {s} s", s=self.cfg["timeout_s"]))
346
+ except Exception as e:
347
+ self.done.emit(card, "error", tr("no answer: {e}", e=e))
348
+
349
+ def on_done(self, card: int, kind: str, note: str):
350
+ self.win.finish_card(card, note, keep_empty=kind in ("ok", "error") or bool(note and kind == "skip"))
351
+ if kind == "error":
352
+ self.win.mark_line(self.card_rows[card], tr("⚠ no answer"))
353
+ self.error = note
354
+ elif kind == "ok":
355
+ self.error = ""
356
+ self.refresh()
357
+
358
+ def shutdown(self):
359
+ if self.capture:
360
+ self.capture.stop()
361
+ if self.digesting:
362
+ self.digesting.cancel()
363
+ try:
364
+ asyncio.run_coroutine_threadsafe(self.engine.close(), self.loop).result(timeout=3)
365
+ except Exception:
366
+ pass
367
+
368
+
369
+ def main():
370
+ demo = "--demo" in sys.argv[1:] # visible in screen recordings, to record the README GIF
371
+ app = QApplication(sys.argv)
372
+ win = Window(hide_from_capture=not demo)
373
+ ctl = Controller(load_config(), win, demo)
374
+ app.aboutToQuit.connect(ctl.shutdown)
375
+ win.show()
376
+ ctl.refresh()
377
+ if not CONFIG.exists(): # first run
378
+ QTimer.singleShot(0, ctl.open_settings)
379
+ sys.exit(app.exec())