Commands: corrections inside a sentence, conditions are no order, Korean speech levels, missions on any map in any word order, never half done

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-21 23:50:17 +09:00
co-authored by Claude Opus 5.5
parent 334b5f5b2b
commit c31bee4d2f
18 changed files with 1298 additions and 53 deletions
+55 -6
View File
@@ -24,6 +24,8 @@ STT_URL = "http://127.0.0.1:8096"
WHISPER_SERVER = ROOT / "whisper.cpp/build/bin/whisper-server"
WHISPER_MODEL = ROOT / "models/ggml-large-v3-turbo-q5_0.bin"
PROMPT = "Hi Lumi. Hey Lumi. 루미야." # helps whisper spell the robot's name
LANG_PROMPT = {"en": "Hi Lumi. Hey Lumi.", "ko": "안녕하세요, 루미야."} # when asked again in one language: the
# mixed prompt made whisper write Korean in Latin letters ("Natarawa" for 나 따라와)
last = {} # levels of the last listen() call, for the log: noise, threshold, peak
@@ -81,9 +83,23 @@ def louder(audio, target=0.7):
LANGS = {"english": "en", "korean": "ko"}
MIN_LOGPROB = -1.0 # below this average token log-probability whisper was guessing
NAME_ONLY = re.compile(r"\W*(lumi|루미야?)\W*", re.I)
KO_BONUS = 0.3 # another language named: asked in Korean and in English, a Hangul answer's average
# log-probability counts this much more (tests/asr_check.py: +3 of 898 cases)
ODD_KO_BONUS = 0.3 # English text with a word no dictionary has: Korean asked too, taken when written in
# Hangul and its log-probability + this beats the English one (tests/asr_check.py)
WORDS_FILE = pathlib.Path("/usr/share/dict/words")
OUR_WORDS = {"lumi", "cutshion", "okay", "ok", "hmm", "um", "uh"}
SLIPS = [ # whisper's spellings of our names (tests/asr_check.py, logs/lumi.log 2026-10-21) -> the name
(r"\b(?:[kc]ut(?:s|t)?ch[aei]n|katchen|kutch and)(?= company|\b)", "Cutshion"),
(r"\b(?:coaching|cushion|kitchen) (?=company)", "Cutshion "),
(r"\bcushion(?=[은는이가을를의에])|커션|컷션", "쿳션"),
]
_words = set()
last_stt = {} # language / confidence of the last transcription (see understood())
BEAM_SIZE = 0 # whisper beam search width (0: the server's greedy decoding)
HEARD_DIR = ROOT / "logs" / "heard" # the last HEARD_KEEP utterances as wav, to check what Lumi really heard
HEARD_KEEP = 50
@@ -91,7 +107,8 @@ HEARD_KEEP = 50
def _infer(wav, prompt, language=None):
"""One whisper-server call: (text, language, language probability, average token log-probability)."""
boundary = uuid.uuid4().hex
fields = {"response_format": "verbose_json", "prompt": prompt, **({"language": language} if language else {})}
fields = {"response_format": "verbose_json", "prompt": prompt, **({"language": language} if language else {}),
**({"beam_size": BEAM_SIZE} if BEAM_SIZE else {})}
body = "".join(f"--{boundary}\r\nContent-Disposition: form-data; name=\"{k}\"\r\n\r\n{v}\r\n"
for k, v in fields.items()).encode()
body += (f"--{boundary}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"a.wav\"\r\n"
@@ -106,6 +123,23 @@ def _infer(wav, prompt, language=None):
return out.get("text", "").strip(), lang, out.get("detected_language_probability"), logprob
def names(text):
"""Our names as whisper wrote them (SLIPS) -> the names; "Lumi야" -> 루미야."""
text = re.sub(r"\blumi(?=[야아])", "루미", text, flags=re.I)
for slip, said in SLIPS:
text = re.sub(slip, said, text, flags=re.I)
return text
def not_english(text):
"""A word in English text that no dictionary has (our names, p-numbers and contractions aside)."""
if not _words and WORDS_FILE.exists():
_words.update(w.strip().lower() for w in WORDS_FILE.open() if w.strip().isalpha())
words = [re.sub(r"'.*", "", w) for w in re.findall(r"[a-z']+", text.lower())]
return bool(_words) and any(w and w not in _words and w not in OUR_WORDS and not re.fullmatch(r"p\d*", w)
for w in words)
def transcribe(audio, prompt=PROMPT):
"""int16 mono 16 kHz -> text (empty string if nothing understood). Also fills last_stt.
Whisper sometimes names another language (Japanese) for Korean said from afar and writes only the prompt's
@@ -128,13 +162,28 @@ def transcribe(audio, prompt=PROMPT):
except OSError:
pass
text, lang, prob, logprob = _infer(wav, prompt)
only_name = re.fullmatch(r"\W*(lumi|루미야?)\W*", text, re.I) and len(audio) > 1.5 * RATE
text = names(text)
asked = {}
def ask(code): # transcribed with the language fixed, once per language
if code not in asked:
t2, _, _, lp2 = _infer(wav, LANG_PROMPT.get(code, prompt), code)
asked[code] = (names(t2), {"en": "english", "ko": "korean"}[code], None, lp2)
return asked[code]
only_name = NAME_ONLY.fullmatch(text) and len(audio) > 1.5 * RATE
if lang not in LANGS or only_name:
tries = [(text, lang, prob, logprob)] if lang in LANGS and not only_name else []
for code, name in (("ko", "korean"), ("en", "english")):
t2, _, _, lp2 = _infer(wav, prompt, code)
tries.append((t2, name, None, lp2))
text, lang, prob, logprob = max(tries, key=lambda r: r[3])
tries += [ask("ko"), ask("en")]
more = [r for r in tries if not NAME_ONLY.fullmatch(r[0])] # "Lumi, 이리 와" came back "Lumi." in English
# forced to English, Korean speech comes back translated and sure of itself: Hangul gets KO_BONUS
text, lang, prob, logprob = max(more if only_name and more else tries,
key=lambda r: r[3] + (KO_BONUS if re.search(r"[가-힣]", r[0]) else 0.0))
if lang == "english" and not_english(text): # "Natarawa", "Lumi. Iriwa.": Korean written in Latin letters
t2 = ask("ko")
if re.search(r"[가-힣]", t2[0]) and t2[3] + ODD_KO_BONUS > logprob:
text, lang, prob, logprob = t2
if lang == "english" and re.search(r"[가-힣]", text):
lang = "korean" # "Red cup. 집어줘." tagged English: the Korean is real
last_stt.clear()
last_stt.update(text=text, language=lang, lang_prob=prob, logprob=round(logprob, 2))
# whisper writes things like "[BLANK_AUDIO]" or "(music)" for non-speech