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>
This commit is contained in:
@@ -6,10 +6,23 @@ class Settings(BaseSettings):
|
||||
|
||||
tatlock_base_url: str = "http://tatlock.schweitz.internal:8000"
|
||||
tatlock_model: str = "Tatlock"
|
||||
|
||||
# PCM rate of the device WebSocket contract (docs/architecture.md)
|
||||
sample_rate: int = 16000
|
||||
stt_model: str = "small"
|
||||
stt_device: str = "cuda"
|
||||
tts_voice: str = "en_GB-alan-medium"
|
||||
|
||||
# "speaches" (shared speech container) or "embedded" (in-process models)
|
||||
stt_backend: str = "speaches"
|
||||
tts_backend: str = "speaches"
|
||||
|
||||
speaches_base_url: str = "http://localhost:8601"
|
||||
stt_model: str = "Systran/faster-whisper-small"
|
||||
tts_model: str = "speaches-ai/Kokoro-82M-v1.0-ONNX"
|
||||
tts_voice: str = "bm_george"
|
||||
|
||||
# embedded fallback only (requires the [speech] extra)
|
||||
embedded_stt_model: str = "small"
|
||||
embedded_stt_device: str = "cuda"
|
||||
embedded_tts_voice: str = "en_GB-alan-medium"
|
||||
|
||||
model_config = {"env_prefix": "DESKLOCK_"}
|
||||
|
||||
|
||||
@@ -1,24 +1,61 @@
|
||||
"""Speech-to-text: faster-whisper on the tower-of-joy GPU.
|
||||
"""Speech-to-text: Speaches over HTTP (default) or embedded faster-whisper.
|
||||
|
||||
Import of faster_whisper is deferred so the gateway can run (health checks,
|
||||
protocol tests) without the heavy speech extras installed.
|
||||
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
|
||||
|
||||
_model = None
|
||||
_client: httpx.Client | None = None
|
||||
_embedded_model = None
|
||||
|
||||
|
||||
def transcribe(pcm: bytes, sample_rate: int | None = None) -> str:
|
||||
def transcribe(pcm: bytes, sample_rate: int) -> str:
|
||||
"""Transcribe raw s16le mono PCM to text."""
|
||||
global _model
|
||||
if _model is None:
|
||||
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
|
||||
|
||||
_model = WhisperModel(settings.stt_model, device=settings.stt_device)
|
||||
_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 = _model.transcribe(audio, language="en")
|
||||
segments, _info = _embedded_model.transcribe(audio, language="en")
|
||||
return " ".join(segment.text.strip() for segment in segments).strip()
|
||||
|
||||
@@ -1,23 +1,64 @@
|
||||
"""Text-to-speech: Piper, resampled to the device sample rate.
|
||||
"""Text-to-speech: Speaches/Kokoro over HTTP (default) or embedded Piper.
|
||||
|
||||
Import of piper is deferred so the gateway can run without the speech extras.
|
||||
Output is always s16le mono PCM at settings.sample_rate (the device WS contract).
|
||||
Kokoro synthesizes at 24 kHz, so the speaches path resamples via audioop —
|
||||
which pins the runtime to Python 3.12 (audioop is removed in 3.13).
|
||||
"""
|
||||
|
||||
import audioop
|
||||
import io
|
||||
import wave
|
||||
|
||||
import httpx
|
||||
|
||||
from .config import settings
|
||||
|
||||
_voice = None
|
||||
_client: httpx.Client | None = None
|
||||
_embedded_voice = None
|
||||
|
||||
|
||||
def synthesize(text: str) -> bytes:
|
||||
"""Synthesize text to raw s16le mono PCM at the configured sample rate."""
|
||||
global _voice
|
||||
if _voice is None:
|
||||
if settings.tts_backend == "speaches":
|
||||
return _synthesize_speaches(text)
|
||||
return _synthesize_embedded(text)
|
||||
|
||||
|
||||
def _synthesize_speaches(text: str) -> bytes:
|
||||
global _client
|
||||
if _client is None:
|
||||
_client = httpx.Client(base_url=settings.speaches_base_url, timeout=120.0)
|
||||
response = _client.post(
|
||||
"/v1/audio/speech",
|
||||
json={
|
||||
"model": settings.tts_model,
|
||||
"voice": settings.tts_voice,
|
||||
"input": text,
|
||||
"response_format": "wav",
|
||||
},
|
||||
)
|
||||
response.raise_for_status()
|
||||
|
||||
with wave.open(io.BytesIO(response.content), "rb") as w:
|
||||
rate, channels, width = w.getframerate(), w.getnchannels(), w.getsampwidth()
|
||||
frames = w.readframes(w.getnframes())
|
||||
if width != 2:
|
||||
frames = audioop.lin2lin(frames, width, 2)
|
||||
if channels == 2:
|
||||
frames = audioop.tomono(frames, 2, 0.5, 0.5)
|
||||
if rate != settings.sample_rate:
|
||||
frames, _state = audioop.ratecv(frames, 2, 1, rate, settings.sample_rate, None)
|
||||
return frames
|
||||
|
||||
|
||||
def _synthesize_embedded(text: str) -> bytes:
|
||||
global _embedded_voice
|
||||
if _embedded_voice is None:
|
||||
from piper import PiperVoice
|
||||
|
||||
_voice = PiperVoice.load(settings.tts_voice)
|
||||
_embedded_voice = PiperVoice.load(settings.embedded_tts_voice)
|
||||
|
||||
chunks = bytearray()
|
||||
for chunk in _voice.synthesize_stream_raw(text):
|
||||
for chunk in _embedded_voice.synthesize_stream_raw(text):
|
||||
chunks.extend(chunk)
|
||||
# TODO: resample from the Piper voice's native rate to settings.sample_rate
|
||||
return bytes(chunks)
|
||||
|
||||
Reference in New Issue
Block a user