Files
Jiao Huifeng fdafebd47f feat: add STT voice transcription for all messaging channels (#28)
* feat: add STT voice transcription for all channels

Automatically transcribes audio/voice messages (Telegram, WeChat, Slack,
etc.) into text before the agent sees them. Enabled via config, off by default.

Changes:
- EvoScientist/stt.py: new STT engine using faster-whisper with lazy
  model loading and per-language model selection (zh/en/auto)
- EvoScientist/channels/base.py: hook in _enqueue_raw() to transcribe
  audio files and prepend transcript to message text; removes the raw
  [voice: ...] annotation after successful transcription so the agent
  does not attempt further audio processing
- EvoScientist/config/settings.py: stt_enabled (default False),
  stt_language (default "auto")
- pyproject.toml: optional [stt] dependency group (faster-whisper>=1.0)
- tests/test_stt.py: unit tests covering all backends and channel integration

Usage:
  pip install 'EvoScientist[stt]'
  EvoSci config set stt_enabled true
  EvoSci config set stt_language zh   # zh / en / auto

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix: remove unused imports (ruff F401)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix: address PR #28 reviewer feedback

Changes per SemiGlassFace review (CHANGES_REQUESTED):

1. Cache config at channel __init__ — no longer calls load_config() on
   every incoming message; STT settings stored as instance attributes
   (_stt_enabled, _stt_language, _stt_model, _stt_device,
   _stt_compute_type) set once during Channel.__init__().

2. Replace deprecated asyncio.get_event_loop() with get_running_loop()
   to avoid DeprecationWarning on Python 3.12+.

3. Annotation removal now uses exact path matching instead of substring
   search — checks fp == a or a.endswith(f": {fp}]") so only the
   correct annotation is removed after transcription.

4. Expose stt_model, stt_device, stt_compute_type as config fields so
   users can override the HuggingFace model id, inference device, and
   quantisation without touching code. transcribe_file() forwards all
   three to the engine.

Also: _engines dict replaced with single _engine + _engine_key tuple
(model_id, device, compute_type) — reuses cached model unless settings
change, simpler than a dict.

Tests: 19 STT-specific tests all pass; total 1105 tests green, ruff clean.

* fix: resolve ruff lint errors (UP037, I001, PT006)

* style: apply ruff format

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-20 10:59:30 +01:00

136 lines
4.4 KiB
Python

"""Speech-to-text transcription.
Default language → model mapping (all via faster-whisper):
"zh" → Systran/faster-whisper-small (language=zh, ~250MB)
"en" → Systran/faster-whisper-small.en (~250MB, en-only)
"auto" → Systran/faster-whisper-small (~250MB, multilingual auto-detect)
All three parameters can be overridden via config:
stt_model — any HuggingFace faster-whisper model id
stt_device — "cpu" (default) or "cuda"
stt_compute_type — "int8" (default), "float16", "float32", etc.
Install:
pip install 'EvoScientist[stt]'
"""
from __future__ import annotations
import asyncio
import logging
from pathlib import Path
logger = logging.getLogger(__name__)
_AUDIO_EXTS = frozenset(
{
".ogg",
".mp3",
".m4a",
".wav",
".flac",
".opus",
".weba",
".webm",
}
)
# Default language → HuggingFace model id
STT_MODELS: dict[str, str] = {
"zh": "Systran/faster-whisper-small",
"en": "Systran/faster-whisper-small.en",
"auto": "Systran/faster-whisper-small",
}
# Single cached engine — re-created only when settings change
_engine: _WhisperEngine | None = None
_engine_key: tuple[str, str, str] | None = None # (model_id, device, compute_type)
# ── Engine ────────────────────────────────────────────────────────────
class _WhisperEngine:
"""faster-whisper transcription engine."""
def __init__(self, model_id: str, device: str, compute_type: str) -> None:
from faster_whisper import WhisperModel # type: ignore[import]
self._model = WhisperModel(model_id, device=device, compute_type=compute_type)
def transcribe(self, file_path: str, language: str | None) -> str:
segments, _ = self._model.transcribe(
file_path,
language=language,
beam_size=5,
vad_filter=True,
vad_parameters={"min_silence_duration_ms": 500},
)
# Skip segments where the model is not confident there is real speech
parts = [s.text.strip() for s in segments if s.no_speech_prob < 0.6]
return " ".join(parts).strip()
def _get_engine(
model_id: str,
device: str = "cpu",
compute_type: str = "int8",
) -> _WhisperEngine:
global _engine, _engine_key
key = (model_id, device, compute_type)
if _engine is None or _engine_key != key:
logger.info(
f"[STT] Loading model '{model_id}' device={device} compute={compute_type}"
)
_engine = _WhisperEngine(model_id, device, compute_type)
_engine_key = key
return _engine
# ── Public API ────────────────────────────────────────────────────────
def is_audio_file(file_path: str) -> bool:
"""Return True if *file_path* has an audio extension."""
return Path(file_path).suffix.lower() in _AUDIO_EXTS
async def transcribe_file(
file_path: str,
language: str = "auto",
model: str = "",
device: str = "cpu",
compute_type: str = "int8",
) -> str | None:
"""Transcribe an audio file asynchronously.
Args:
file_path: Path to the audio file.
language: Language hint — ``"zh"``, ``"en"``, or ``"auto"`` (default).
model: Override the HuggingFace model id. Empty = use STT_MODELS mapping.
device: Inference device — ``"cpu"`` (default) or ``"cuda"``.
compute_type: Quantisation — ``"int8"`` (default), ``"float16"``, etc.
Returns the transcript string, or ``None`` on error / silence.
"""
if not is_audio_file(file_path):
return None
try:
model_id = model or STT_MODELS.get(language, STT_MODELS["auto"])
lang: str | None = None if language == "auto" else language
engine = _get_engine(model_id, device, compute_type)
loop = asyncio.get_running_loop()
result: str = await loop.run_in_executor(
None, engine.transcribe, file_path, lang
)
return result or None
except ImportError as e:
logger.warning(
f"[STT] Missing dependency: {e}. "
"Install with: pip install 'EvoScientist[stt]'"
)
return None
except Exception as e:
logger.error(f"[STT] Transcription failed for {file_path}: {e}")
return None