"""Machine-sound diagnosis from the whisper_ft adapter -> a sentence the agent can speak. The whisper_ft main adapter (whisper_ft/out/whisper-lora/main) was trained to describe non-speech audio with one of three templates (whisper_ft/configs/templates.yaml): anomaly: "machine: fan; condition: high load; status: anomalous; fault: overheating; noise: clean" engine: "engine sound; rpm mean 2020 range 920 to 3860; torque mean 467 nm" ego: "activity: ...; task: ...; view: ...[; notes: ...]" parse_diagnosis() turns the decoded text into fields (None if it is not a complete template), describe() turns the fields into plain English for TTS, e.g. "Sound check. This is a fan under high load. It sounds abnormal. Possible fault: overheating." Wording was chosen by synthesizing with MMS-TTS and transcribing back with Whisper (2026-11-12): MMS cannot say "engine" ("Insan", "Ed Sheik") or "idling" ("piling"); "machine sound check" came back as "matching sound check". "motor", "at idle" and "Sound check. This is ..." survive. The diagnosis is the model's estimate (anomaly F1 1.0, field accuracy 0.60 on validation, whisper_ft train_summary.json): describe() says "possible" fault, never a certainty. """ from __future__ import annotations import re from dataclasses import dataclass, field @dataclass class Diagnosis: kind: str # "anomaly" | "engine" | "activity" fields: dict = field(default_factory=dict) raw: str = "" _ENGINE = re.compile( r"^\s*engine sound\s*;\s*rpm mean\s+(-?\d+)\s+range\s+(-?\d+)\s+to\s+(-?\d+)\s*(?:;(.*))?$", re.IGNORECASE | re.DOTALL) _TORQUE = re.compile(r"^\s*torque mean\s+(-?\d+)\s*nm\s*\.?\s*$", re.IGNORECASE) def _kv(text: str) -> dict: out = {} for part in text.split(";"): if ":" in part: k, v = part.split(":", 1) out[k.strip().lower()] = v.strip().strip(".").lower() return out def parse_diagnosis(text: str) -> Diagnosis | None: text = (text or "").strip() m = _ENGINE.match(text) if m: mean, lo, hi = (int(g) for g in m.groups()[:3]) f = {"rpm_mean": mean, "rpm_min": lo, "rpm_max": hi} t = _TORQUE.match(m.group(4) or "") if t: # a garbled torque ("6-7-0-8 .", seen 2026-11-12) is dropped, not guessed f["torque_mean"] = int(t.group(1)) if not lo <= mean <= hi: # inconsistent numbers: not a usable diagnosis return None return Diagnosis("engine", f, text) f = _kv(text) if f.get("machine") and f.get("condition") and f.get("status") in {"normal", "anomalous"}: return Diagnosis("anomaly", f, text) if f.get("activity") and f.get("task"): return Diagnosis("activity", f, text) return None def _article(noun: str) -> str: return ("an " if noun[:1] in "aeiou" else "a ") + noun def _condition(c: str) -> str: if c == "idle": return "at idle" if c.endswith("load"): return "under " + c return "in " + c + " condition" def _num(n: int) -> str: return ("minus " + str(-n)) if n < 0 else str(n) def describe(d: Diagnosis) -> str: f = d.fields if d.kind == "anomaly": s = f"Sound check. This is {_article(f['machine'])} {_condition(f['condition'])}." if f["status"] == "anomalous": s += " It sounds abnormal." fault = f.get("fault", "none") if fault and fault != "none": s += f" Possible fault: {fault}." else: s += " It sounds normal." noise = f.get("noise", "") if noise and noise not in {"clean", "none"}: s += f" The recording has {noise.replace(' noise', '')} background noise." return s if d.kind == "engine": # "estimated": the adapter's numbers can be far off (505_Engine-C: said mean 4660 RPM, label 2870) s = (f"Sound check. This is a motor. The estimated average speed is {_num(f['rpm_mean'])} RPM, " f"ranging from {_num(f['rpm_min'])} to {_num(f['rpm_max'])} RPM.") if "torque_mean" in f: s += f" The estimated average torque is {_num(f['torque_mean'])} newton metres." return s s = f"Sound check. Workshop activity: {f['activity']}. Task: {f['task']}." if f.get("view"): s += f" View: {f['view']}." if f.get("notes"): s += f" Notes: {f['notes']}." return s