feat(voice_worker): finals are also decoded with the recent conversation as Whisper's prompt

The words of the topic were the ones misheard live (화면공이 for 화면 공유, 계산지 for 계산기), and
five models made the same mistakes. Each final is decoded as before and again with the last 180 s
of the channel's conversation (everyone's finals and Jarvis's speech, at most context_chars=200
characters) as the previous-text prompt; the prompted text is used only when the plain one was kept,
stays within half its letters and keeps every hotword count, as for the hint.

Measured on the game PC (RTX 5070 Ti, 100 recordings of the user, 16 with their own text): CER
12.0% -> 7.5%, empty results unchanged, noise clips with text unchanged (16/108), final decode
p95 170 -> 253 ms. Finals with a context are decoded one clip at a time (ctranslate2 needs the
start token at one position in a batch).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-19 19:38:10 +09:00
parent 478968949c
commit 628d89c4f1
10 changed files with 234 additions and 17 deletions

View File

@@ -32,7 +32,8 @@ class SttResult:
class Transcriber(Protocol):
def transcribe(self, clips: list[np.ndarray], opts: DecodeOptions) -> list[str]: ...
def transcribe(self, clips: list[np.ndarray], opts: DecodeOptions,
contexts: list[str] | None = None) -> list[str]: ...
Callback = Callable[[SttResult], None]
@@ -44,6 +45,7 @@ class _Job:
audio: np.ndarray
opts: DecodeOptions
callback: Callback
context: str = "" # finals only: the recent conversation, as the model's prompt
class SttScheduler:
@@ -74,8 +76,9 @@ class SttScheduler:
pass
self._executor.shutdown(wait=False, cancel_futures=True)
def submit_final(self, key: Hashable, audio: np.ndarray, opts: DecodeOptions, callback: Callback) -> None:
self._finals.append(_Job(key, audio, opts, callback))
def submit_final(self, key: Hashable, audio: np.ndarray, opts: DecodeOptions, callback: Callback,
context: str = "") -> None:
self._finals.append(_Job(key, audio, opts, callback, context))
self._wake.set()
def submit_partial(self, key: Hashable, audio: np.ndarray, opts: DecodeOptions, callback: Callback) -> None:
@@ -121,7 +124,7 @@ class SttScheduler:
t0 = time.perf_counter()
try:
texts = await loop.run_in_executor(self._executor, self.transcriber.transcribe, clips,
batch[0].opts)
batch[0].opts, [j.context for j in batch])
error = None
except Exception as e: # the model failing must not stop every speaker's STT
texts, error = [""] * len(batch), f"{type(e).__name__}: {e}"

View File

@@ -10,6 +10,7 @@ import itertools
import json
import secrets
import time
from collections import deque
from dataclasses import dataclass
from typing import Callable
@@ -28,6 +29,7 @@ from .tts import TtsEngine, TtsRequest, TtsService
CLOSE_BAD_HELLO = 4400
CLOSE_BAD_TOKEN = 4401
MAX_SPEAKERS = 32
CONTEXT_SECONDS = 180 # how far back the STT's conversation context reaches
def is_loopback(host: str) -> bool:
@@ -274,6 +276,9 @@ class Session:
self.chat_waiting: dict[str, asyncio.Future] = {}
self._chat_ids = itertools.count(1)
self.names: dict[str, str] = {}
# What was said and answered lately in Jarvis's voice channel: (time, text), the STT's
# conversation context (voice_worker.spec.md, "Conversation context"). Never logged.
self.lines: deque[tuple[float, str]] = deque(maxlen=50)
self.speakers: dict[str, _Speaker] = {}
self.tts_requests: dict[str, TtsRequest] = {}
# With recordings on: each spoken answer's text and mono samples, kept until it is made and the
@@ -364,6 +369,15 @@ class Session:
samples = st.converter.push(pcm)
self._handle_events(speaker, st, st.segmenter.push(samples, time.monotonic()))
def _context(self) -> str:
"""The recent conversation for a final's prompt: the channel's lines of the last
CONTEXT_SECONDS, at most `context_chars` characters from the end."""
n = self.settings.context_chars
if not n or not self.voice_channel:
return ""
since = time.monotonic() - CONTEXT_SECONDS
return " ".join(t for at, t in self.lines if at >= since)[-n:].strip()
def _opts(self, beam: int) -> DecodeOptions:
return DecodeOptions(beam=beam, language=self.settings.language, hotwords=self.settings.hotwords)
@@ -388,7 +402,8 @@ class Session:
else:
sched.submit_final((self.sid, speaker), loudness_equalise(ev.audio, ev.voiced),
self._opts(self.settings.final_beam),
lambda res, ev=ev, said=said: self._send_final(speaker, ev, res, said))
lambda res, ev=ev, said=said: self._send_final(speaker, ev, res, said),
context=self._context())
def _partial_cb(self, speaker: str, st: _Speaker, utt: int):
def cb(res: SttResult) -> None:
@@ -402,6 +417,8 @@ class Session:
store = self.worker.history
if store is not None and channel and res.text.strip():
store.add_voice(channel, speaker, name, res.text, ts)
if channel and channel == self.voice_channel and res.text.strip():
self.lines.append((time.monotonic(), res.text.strip()))
if self.worker.recorder is not None:
self.worker.recorder.said(ts, speaker, name, channel, res.text, ev.audio, SR)
self.send({"type": "final", "speaker": speaker, "utt": ev.utt, "text": res.text,
@@ -499,6 +516,8 @@ class Session:
rec["text"] += text
while len(self.tts_recorded) > 50: # never reported as played: not kept forever
self.tts_recorded.pop(next(iter(self.tts_recorded)))
if self.voice_channel and text.strip():
self.lines.append((time.monotonic(), text.strip()))
tts.feed(req, text, more)
def _on_tts_cancel(self, msg: dict) -> None:
@@ -523,6 +542,7 @@ class Session:
if self.voice_channel:
debug_log(f"session {self.sid} left voice channel {self.voice_channel}", "voice_worker")
self.voice_channel = None
self.lines.clear()
def _on_voice_join(self, msg: dict) -> None:
got = self._strings(msg, "channel")

View File

@@ -13,6 +13,7 @@ class SttSettings:
final_beam: int = 2
language: str | None = "ko"
hotwords: tuple[str, ...] = ()
context_chars: int = 200
def update(self, changes: dict) -> None:
"""Apply ``changes``; raises ValueError and changes nothing if any value is bad."""
@@ -52,6 +53,8 @@ def _check(key: str, value):
if value is not None and (not isinstance(value, str) or not 2 <= len(value) <= 8):
raise ValueError("language must be a language code or null")
return value
if key == "context_chars":
return _int_in(key, value, 0, 500)
if key == "hotwords":
if (not isinstance(value, (list, tuple)) or len(value) > 50
or not all(isinstance(w, str) and 0 < len(w) <= 64 for w in value)):

View File

@@ -73,6 +73,25 @@ def choose_with_hint(hint_text: str, hint_lp: float, plain_text: str, plain_lp:
return plain_text
def choose_with_context(heard: str, prompted: str, hotwords: tuple[str, ...]) -> str:
"""Pick between the decode without the conversation context (``heard``, already through the hint
choice) and the one that had it as Whisper's previous-text prompt (``prompted``).
The context fixes words of the topic the model misheard, but on noise or unsure audio it can write
words of the prompt, so the decode without it decides, as for the hint: nothing heard stays
nothing, and the prompted text is used only when it stays close to what was heard and holds every
hotword exactly as often.
"""
if not heard or not prompted:
return heard
h, p = _letters(heard), _letters(prompted)
if _edit_distance(p, h) > len(h) // 2:
return heard
if any(_mentions(prompted, w) != _mentions(heard, w) for w in hotwords):
return heard
return prompted
def add_cuda_dll_dirs() -> None:
"""On Windows ctranslate2 loads cuBLAS/cuDNN from the PATH; the pip wheels put them in nvidia/*/bin."""
if not hasattr(os, "add_dll_directory"):
@@ -104,8 +123,25 @@ class WhisperTranscriber:
def warm_up(self) -> None:
self.transcribe([np.zeros(SR, dtype=np.float32)], DecodeOptions(beam=1, language="en", hotwords=()))
def transcribe(self, clips: list[np.ndarray], opts: DecodeOptions) -> list[str]:
"""Clips arrive loudness-equalised by the session."""
def transcribe(self, clips: list[np.ndarray], opts: DecodeOptions,
contexts: list[str] | None = None) -> list[str]:
"""Clips arrive loudness-equalised by the session. ``contexts``: per clip, the recent
conversation to prompt with ("" for none); see ``choose_with_context``."""
if contexts and any(contexts):
# Prompts of different lengths cannot share one generate call (the start token must sit
# at the same position): one clip at a time. The encoder costs the same per clip anyway.
return [self._with_context(c, opts, ctx) for c, ctx in zip(clips, contexts)]
enc, langs = self._encode(clips, opts)
return self._choose(enc, langs, opts, None)
def _with_context(self, clip: np.ndarray, opts: DecodeOptions, context: str) -> str:
enc, langs = self._encode([clip], opts)
heard = self._choose(enc, langs, opts, None)[0]
if not context:
return heard
return choose_with_context(heard, self._choose(enc, langs, opts, [context])[0], opts.hotwords)
def _encode(self, clips: list[np.ndarray], opts: DecodeOptions):
m = self.model
n_frames = m.feature_extractor.nb_max_frames
feats = []
@@ -118,16 +154,22 @@ class WhisperTranscriber:
langs = [opts.language] * len(clips)
else:
langs = [r[0][0][2:-2] for r in m.model.detect_language(enc)]
plain = self._decode(enc, langs, opts.beam, None)
return enc, langs
def _choose(self, enc, langs: list[str], opts: DecodeOptions, contexts: list[str] | None) -> list[str]:
plain = self._decode(enc, langs, opts.beam, None, contexts)
if not opts.hotwords:
return [text if keep_transcript(text, lp) else "" for text, lp in plain]
hinted = self._decode(enc, langs, opts.beam, " ".join(opts.hotwords))
hinted = self._decode(enc, langs, opts.beam, " ".join(opts.hotwords), contexts)
return [choose_with_hint(ht, hlp, pt, plp, opts.hotwords) for (ht, hlp), (pt, plp) in zip(hinted, plain)]
def _decode(self, enc, langs: list[str], beam: int, hotwords: str | None) -> list[tuple[str, float]]:
def _decode(self, enc, langs: list[str], beam: int, hotwords: str | None,
contexts: list[str] | None = None) -> list[tuple[str, float]]:
m = self.model
prompts = [m.get_prompt(self._tokenizer(lang), [], without_timestamps=True, hotwords=hotwords)
for lang in langs]
prompts = [m.get_prompt(self._tokenizer(lang),
self._tokenizer(lang).encode(" " + ctx.strip()) if ctx else [],
without_timestamps=True, hotwords=hotwords)
for lang, ctx in zip(langs, contexts or [""] * len(langs))]
results = m.model.generate(enc, prompts, beam_size=beam, max_length=224, suppress_blank=True,
suppress_tokens=[-1], return_scores=True)
# ``scores`` is the sequence log-probability divided by its length: the mean per token.

View File

@@ -80,6 +80,7 @@ STT settings (all optional in `hello` and `config`):
| `end_silence_ms` | 300 | Trailing VAD silence that ends an utterance. Range 100-2000. |
| `final_beam` | 2 | Beam size for finals (partials always use 1). Range 1-5. |
| `language` | `"ko"` | Whisper language code, or `null` to detect per utterance. |
| `context_chars` | 200 | Finals are also decoded with the recent conversation as Whisper's previous-text prompt, at most this many characters (Conversation context). `0` turns it off. Range 0-500. |
| `hotwords` | `[]` | Words (wake words, names) passed to Whisper as a hint. The hint alone made Whisper write the wake word on laughter, so with hotwords every clip is also decoded without them and that plain decode decides (see Hallucination gate). |
### Utterance rules
@@ -152,6 +153,35 @@ text is empty:
`no_speech_prob` is not used: large-v3-turbo reports 0.0 for silence and noise alike.
### Conversation context
People talk about one thing for a while, and the words of that topic ("디코", "화면 공유", "계산기")
were the ones misheard (live 2026-11-19): Whisper heard a casual, unclear "화면 공유" as "화면공이"
and "계산기" as "계산지", and five models (large-v3-turbo, large-v3 and three Korean fine-tunes) made
the same mistakes. Whisper was trained to continue from the text before a clip, so a final is also
decoded with the recent conversation as its previous-text prompt:
- The context is what was said (the session's non-empty finals, everyone's) and what Jarvis said
(the text of every `tts` request) in the voice channel Jarvis is in, over the last 180 s, joined
by spaces, at most `context_chars` characters from the end. Joining another channel or leaving
clears it. It lives in memory only, is never logged and goes nowhere but the model's prompt.
- Partials have none (they stay one cheap decode).
- The clip is decoded as before (with the hotword choice above) and again with the context (the
same choice, each decode prompted). The prompted text is used only when the one without context
was kept, it stays within half that text's letters of it, and it holds every hotword exactly as
often; otherwise the text without context is used. On noise or unsure audio the prompt makes
Whisper write words of the conversation; this rule keeps that out, as for the hint.
- Prompts of different lengths cannot share one decoder call, so finals with a context are decoded
one clip at a time (the encoder costs the same per clip).
Measured (2026-11-19, the game PC's RTX 5070 Ti with Strata loaded, large-v3-turbo int8_float16,
beam 2, the 100 recordings of the user's speech from 2026-11-16 to 19, 16 of them with the user's own
text): character error rate 12.0% without and 7.5% with the context (wrong sentences 10 to 8), no more
empty results (17 of 100 both ways), final decode 134 ms to 182 ms on average (p95 170 to 253 ms).
On the 108 noise clips decoded straight (no VAD), as many came out with text with the context as
without it (16 each); without the rule above it was 22 with turbo, and with large-v3 63, which wrote
whole sentences of the prompt. Evidence and the model comparison: `docs/v2/stt-accuracy-plan.md`.
Measured (2026-11-11, RTX 5050) with 54 real noise recordings from ESC-50 (typing, mouse, breathing,
coughing, laughing, crying, vacuum, ...), each at its own level and at -25 dBFS: of the 108 noise-only
clips streamed through the worker with the wake word hint on, 103 never reached the model (the VAD