Files
gogo-telefon/gogo/voice/stt.py
Senad Uka 714aa1782b M4: voice pipeline (software-only telephony)
- AudioSocket server (asyncio TCP, Asterisk wire protocol) bridging calls
  into the same M2 agent used by chat
- Call session engine: greeting → energy-VAD utterance collection → STT →
  agent turn → TTS playback with barge-in (120ms of caller speech stops
  playback); inactivity + max-duration guards; unintelligible audio reaches
  the agent as '[nerazumljivo]' so the two-attempt rule stays in the prompt
- STTProvider (faster-whisper, lang hint 'sr', GPU/CPU via env) and
  TTSProvider (Azure Neural raw-8k PCM / ElevenLabs Flash) + test fakes
- Recording (mixed caller+agent WAV), transcripts, per-turn latency trace,
  metering via record_agent_call (§12)
- Internal dialplan API: /internal/calls/register (ring targets computed from
  working hours + ring settings §5.2), /answered (human outcome, no minutes),
  /hangup (abandoned → missed-call SMS §5.5); shared-token auth
- Asterisk config generator (pjsip.conf + extensions.conf from DB): worker
  endpoints, per-tenant test-caller endpoint, register→Dial→AudioSocket
  dialplan with h-extension reporting — validated against a real Asterisk 20
  container (modules, dialplan, endpoints all load)
- docker-compose 'voice' profile (voice server + Asterisk), Dockerfile.voice,
  docs/VOICE_TESTING.md softphone runbook
- 16 new tests incl. full fake-call e2e and a real-TCP AudioSocket wire test

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-11 11:05:56 +02:00

82 lines
2.5 KiB
Python

"""STTProvider interface — faster-whisper implementation + test fake (§4).
Language hint 'sr' covers spoken Bosnian/Serbian/Croatian (§6.1). The model
runs on GPU in the founder's DC (large-v3); CPU with a small model for dev.
"""
from __future__ import annotations
import asyncio
import logging
import os
from typing import Protocol
from gogo.voice.audio import SAMPLE_RATE, resample
log = logging.getLogger("gogo.voice.stt")
class STTProvider(Protocol):
async def transcribe(self, pcm_8k: bytes) -> str:
"""SLIN 8 kHz PCM → text (empty string when nothing intelligible)."""
...
class WhisperSTT:
"""faster-whisper; model size/device via env (GOGO_WHISPER_MODEL, GOGO_WHISPER_DEVICE)."""
def __init__(self, model_name: str | None = None, device: str | None = None):
from faster_whisper import WhisperModel # heavy import, voice extra
model_name = model_name or os.environ.get("GOGO_WHISPER_MODEL", "small")
device = device or os.environ.get("GOGO_WHISPER_DEVICE", "auto")
compute = "float16" if device == "cuda" else "int8"
log.info("loading whisper model=%s device=%s", model_name, device)
self._model = WhisperModel(model_name, device=device, compute_type=compute)
self._lock = asyncio.Lock()
async def transcribe(self, pcm_8k: bytes) -> str:
import numpy as np
pcm_16k = resample(pcm_8k, SAMPLE_RATE, 16000)
audio = np.frombuffer(pcm_16k, dtype=np.int16).astype(np.float32) / 32768.0
def run() -> str:
segments, _info = self._model.transcribe(
audio,
language="sr", # covers bs/sr/hr as one spoken language (§6.1)
beam_size=5,
vad_filter=True,
)
return " ".join(s.text.strip() for s in segments).strip()
async with self._lock: # one decode at a time per worker process
return await asyncio.get_running_loop().run_in_executor(None, run)
class FakeSTT:
"""Test fake: returns queued texts, one per transcribe() call."""
def __init__(self, texts: list[str] | None = None):
self.texts = list(texts or [])
self.received: list[bytes] = []
async def transcribe(self, pcm_8k: bytes) -> str:
self.received.append(pcm_8k)
return self.texts.pop(0) if self.texts else ""
_stt: STTProvider | None = None
def get_stt() -> STTProvider:
global _stt
if _stt is None:
_stt = WhisperSTT()
return _stt
def set_stt(provider: STTProvider | None) -> None:
global _stt
_stt = provider