Files
desklock/gateway/src/desklock_gateway/stt.py
T
jpmschweitzerandClaude Fable 5 f5df5049db
Test, Build and Push / test-gateway (push) Successful in 1m2s
Test, Build and Push / release (push) Skipped
Test, Build and Push / build-gateway (push) Skipped
Add rage table-flip state; wire gateway to live Speaches; add CI pipeline
- Face: new 'rage' state — 3-frame kaomoji loop (stare, flip the table,
  put it back) for in-flight request failures; 'error' stays the quiet
  persistent face for a dead link. Sim + artifact + design doc updated.
- Gateway: stt.py/tts.py are now pluggable backends. Default 'speaches'
  talks OpenAI-format HTTP to the live container on :8601
  (faster-whisper-small STT, Kokoro bm_george TTS with 24->16 kHz
  audioop resample); 'embedded' fallback kept behind the [speech] extra.
  Verified with a live TTS->STT round trip (warm: STT 0.27s, TTS 1.9s).
  Docker image is now slim (no CUDA/ML deps). Python pinned to 3.12
  (system 3.8 too old, audioop gone in 3.13).
- CI: .gitea/workflows/build.yml — lint+test on main pushes; on v* tags
  test, build gateway image, push to registry, release, and trigger
  Watchtower (tatlock pattern; needs REGISTRY_USER/REGISTRY_PASSWORD/
  WATCHTOWER_TOKEN secrets). Runtime stack in deploy/desklock-gateway.yml.
- architecture.md: measured speech latencies, deployed-Speaches status,
  CI & deployment section.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-14 17:54:30 +02:00

62 lines
1.8 KiB
Python

"""Speech-to-text: Speaches over HTTP (default) or embedded faster-whisper.
The embedded import is deferred so the gateway runs without the heavy
[speech] extras installed.
"""
import io
import wave
import httpx
from .config import settings
_client: httpx.Client | None = None
_embedded_model = None
def transcribe(pcm: bytes, sample_rate: int) -> str:
"""Transcribe raw s16le mono PCM to text."""
if settings.stt_backend == "speaches":
return _transcribe_speaches(pcm, sample_rate)
return _transcribe_embedded(pcm)
def _wav_bytes(pcm: bytes, sample_rate: int) -> bytes:
buf = io.BytesIO()
with wave.open(buf, "wb") as w:
w.setnchannels(1)
w.setsampwidth(2)
w.setframerate(sample_rate)
w.writeframes(pcm)
return buf.getvalue()
def _transcribe_speaches(pcm: bytes, sample_rate: int) -> str:
global _client
if _client is None:
_client = httpx.Client(base_url=settings.speaches_base_url, timeout=60.0)
response = _client.post(
"/v1/audio/transcriptions",
files={"file": ("utterance.wav", _wav_bytes(pcm, sample_rate), "audio/wav")},
data={"model": settings.stt_model, "language": "en"},
)
response.raise_for_status()
return response.json()["text"].strip()
def _transcribe_embedded(pcm: bytes) -> str:
global _embedded_model
if _embedded_model is None:
from faster_whisper import WhisperModel
_embedded_model = WhisperModel(
settings.embedded_stt_model, device=settings.embedded_stt_device
)
import numpy as np
audio = np.frombuffer(pcm, dtype=np.int16).astype(np.float32) / 32768.0
segments, _info = _embedded_model.transcribe(audio, language="en")
return " ".join(segment.text.strip() for segment in segments).strip()