voice_mode_transcript.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. """Pure-text helpers for voice mode: Whisper hallucination filter, voice-chat
  2. stop phrases, and the TTS self-echo guard. No audio dependencies."""
  3. import difflib
  4. import re
  5. from contextlib import suppress
  6. from typing import Optional
  7. def _voice_config() -> dict:
  8. """``voice`` section of config.yaml, or ``{}`` when missing, malformed, or the
  9. config system can't be imported (broken config mid-install)."""
  10. with suppress(Exception):
  11. from hermes_cli.config import load_config
  12. voice_cfg = load_config().get("voice", {})
  13. return voice_cfg if isinstance(voice_cfg, dict) else {}
  14. return {}
  15. # Whisper commonly hallucinates these phrases on silent/near-silent audio
  16. # (matched with trailing '.'/'!' stripped, so the bare form suffices).
  17. WHISPER_HALLUCINATIONS = {
  18. "thank you", "thanks for watching", "subscribe to my channel", "like and subscribe",
  19. "please subscribe", "thank you for watching", "bye", "you", "the end",
  20. # Non-English hallucinations (common on silence)
  21. "продолжение следует", "sous-titres", "sous-titres réalisés par la communauté d'amara.org",
  22. "sottotitoli creati dalla comunità amara.org", "untertitel von stephanie geiges",
  23. "amara.org", "www.mooji.org", "ご視聴ありがとうございました",
  24. }
  25. # Repetitive hallucinations (e.g. "Thank you. Thank you. Thank you.")
  26. _HALLUCINATION_REPEAT_RE = re.compile(r'^(?:thank you|thanks|bye|you|ok|okay|the end|\.|\s|,|!)+$',
  27. flags=re.IGNORECASE)
  28. def is_whisper_hallucination(transcript: str) -> bool:
  29. """Check if a transcript is a known Whisper hallucination on silence."""
  30. cleaned = transcript.strip().lower()
  31. return (not cleaned or cleaned.rstrip('.!') in WHISPER_HALLUCINATIONS
  32. or bool(_HALLUCINATION_REPEAT_RE.match(cleaned)))
  33. DEFAULT_VOICE_STOP_PHRASES = ("stop",)
  34. def _load_voice_stop_phrases() -> tuple:
  35. """Configured ``voice.stop_phrases`` (default ``("stop",)``); an empty tuple disables
  36. the feature. Malformed config (dict, list of non-strings) falls back to the default
  37. rather than crashing the voice loop."""
  38. with suppress(Exception):
  39. raw = _voice_config().get("stop_phrases", DEFAULT_VOICE_STOP_PHRASES)
  40. if isinstance(raw, str):
  41. raw = [raw]
  42. if isinstance(raw, (list, tuple)):
  43. return tuple(str(p).strip().lower() for p in raw if isinstance(p, (str, int, float)) and str(p).strip())
  44. return DEFAULT_VOICE_STOP_PHRASES
  45. def is_voice_stop_phrase(transcript: str, stop_phrases: Optional[tuple] = None) -> bool:
  46. """True when *transcript* is EXACTLY a configured stop phrase. Deliberately strict: the whole
  47. utterance — lowercased, surrounding punctuation stripped — must equal a phrase, so "stop doing
  48. that and try again" still reaches the agent. ``voice.stop_phrases: []`` disables."""
  49. cleaned = transcript.strip().lower().strip(".,!?;: \t\n\"'") if transcript else ""
  50. return bool(cleaned) and cleaned in (_load_voice_stop_phrases() if stop_phrases is None else stop_phrases)
  51. # Similarity ratio (difflib.SequenceMatcher) above which a playback-phase barge transcript
  52. # is treated as a self-capture of Hermes' own TTS: the full-duplex listener has no echo
  53. # cancellation, so speaker bleed can be transcribed near-verbatim (TTS -> STT -> TTS loop).
  54. DEFAULT_TTS_ECHO_SIMILARITY_THRESHOLD = 0.6
  55. # Minimum normalized-transcript length before the sliding-window fallback runs. Below
  56. # this a genuine one-word barge-in ("yes") landing verbatim inside a longer reply would
  57. # score a trivial 1.0; a real self-capture spans pre-roll plus time-to-silence, so it is longer.
  58. # See #75792.
  59. MIN_FRAGMENT_LENGTH_FOR_ECHO = 10
  60. def _normalize_for_echo_compare(text: str) -> str:
  61. return re.sub(r"\s+", " ", text).strip().lower()
  62. def is_tts_echo(transcript: str, spoken_text: str,
  63. threshold: float = DEFAULT_TTS_ECHO_SIMILARITY_THRESHOLD) -> bool:
  64. """True when *transcript* looks like a self-capture of *spoken_text*. Character-level similarity
  65. (language-agnostic): a genuine interjection rarely matches Hermes' own words, so a high ratio signals
  66. speaker-bleed (fail-closed guard for the playback-phase listener). Playback capture spans only pre-roll
  67. plus time-to-silence, so for long replies the transcript is a FRAGMENT and the whole-string ratio dilutes
  68. toward 0; when it misses, a transcript-sized window slides across `spoken_text`. Transcripts shorter than
  69. `MIN_FRAGMENT_LENGTH_FOR_ECHO` skip the fallback (a short interjection trivially matches a short window)."""
  70. a, b = _normalize_for_echo_compare(transcript or ""), _normalize_for_echo_compare(spoken_text or "")
  71. if not a or not b:
  72. return False
  73. def _similar(x: str, y: str) -> bool:
  74. return difflib.SequenceMatcher(None, x, y).ratio() >= threshold
  75. if _similar(a, b):
  76. return True
  77. if len(a) < MIN_FRAGMENT_LENGTH_FOR_ECHO or len(a) >= len(b):
  78. return False
  79. return any(_similar(a, b[start : start + len(a)]) for start in range(len(b) - len(a) + 1))
  80. def voice_stop_hint() -> str:
  81. """One-line 'Say "stop" to end the voice chat.' hint for voice-mode start, using the first
  82. ``voice.stop_phrases`` entry ("" when disabled). Every surface announcing voice-mode start
  83. (CLI, TUI, desktop) uses this one owner instead of hardcoding the wording."""
  84. phrases = _load_voice_stop_phrases()
  85. return f'Say "{phrases[0]}" to end the voice chat.' if phrases else ""