add voice gateway (wake word, speech-to-text, text-to-speech)
This commit is contained in:
1 parent
4496b8eceb
commit
d25b3259e4
10 files changed
+1522
-24
No files matched your search
+51
-2
@@ -32,7 +32,7 @@
|
||||
#
|
||||
# Usage:
|
||||
# scripts/bootstrap.sh [--llm-url URL] [--codegen-url URL] [--llm-model MODEL]
|
||||
# [--codegen-model MODEL] [--copy-data SRC] [-h]
|
||||
# [--codegen-model MODEL] [--copy-data SRC] [--voice] [-h]
|
||||
#
|
||||
# Run as the human user; sudo is used internally for system bits.
|
||||
set -euo pipefail
|
||||
@@ -42,6 +42,7 @@ CODEGEN_URL=""
|
||||
LLM_MODEL=""
|
||||
CODEGEN_MODEL=""
|
||||
COPY_DATA=""
|
||||
VOICE=0
|
||||
|
||||
usage() {
|
||||
sed -n '2,35p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'
|
||||
@@ -51,6 +52,7 @@ usage() {
|
||||
echo " --llm-model MODEL override llm.model from config.json for this run (pulled if the local ollama lacks it)"
|
||||
echo " --codegen-model MODEL override codegen.model from config.json for this run"
|
||||
echo " --copy-data SRC rsync SRC (e.g. user@host:/path/to/semif-agent/data) to data/ — opt-in"
|
||||
echo " --voice install the optional voice-gateway stack + download its models"
|
||||
echo " -h this help"
|
||||
exit "${1:-0}"
|
||||
}
|
||||
@@ -62,6 +64,7 @@ while [[ $# -gt 0 ]]; do
|
||||
--llm-model) LLM_MODEL="$2"; shift 2 ;;
|
||||
--codegen-model) CODEGEN_MODEL="$2"; shift 2 ;;
|
||||
--copy-data) COPY_DATA="$2"; shift 2 ;;
|
||||
--voice) VOICE=1; shift ;;
|
||||
-h|--help) usage 0 ;;
|
||||
*) echo "unknown option: $1" >&2; usage 1 ;;
|
||||
esac
|
||||
@@ -178,7 +181,7 @@ echo "== pins: semif @ $SEMIF_REF | gguf sha256 ${GGUF_SHA256:0:12}… | simplex
|
||||
echo "== llm $LLM_URL (local, model $LLM_MODEL) | codegen $CODEGEN_URL"
|
||||
echo "== runtime tree: $RUNTIME"
|
||||
|
||||
mkdir -p "$RUNTIME" "$MODELS" "$HF_CACHE" "$TOOLDIR" "$SIMPLEX_DB" "$RUNTIME/simplex-forward" "$RUNTIME/lxmf" "$UNITS" "$USER_UNITS"
|
||||
mkdir -p "$RUNTIME" "$MODELS" "$HF_CACHE" "$TOOLDIR" "$SIMPLEX_DB" "$RUNTIME/simplex-forward" "$RUNTIME/lxmf" "$RUNTIME/voice" "$UNITS" "$USER_UNITS"
|
||||
|
||||
# --- stage 0: system prereqs + linger ----------------------------------------
|
||||
PKGS=(ca-certificates curl git rsync build-essential python3-dev python3-venv pkg-config cmake util-linux)
|
||||
@@ -241,6 +244,23 @@ echo "== installing engine deps into $VENV"
|
||||
CMAKE_BUILD_PARALLEL_LEVEL=6 MAKEFLAGS=-j6 "$PIP" install -r "$REPO_ROOT/requirements/staging.txt"
|
||||
"$PIP" install -e "$REPO_ROOT" --no-deps
|
||||
|
||||
# --- stage 3b: optional voice-gateway stack (--voice) --------------------------
|
||||
# Hardware-dependent: only the host with a microphone/speaker needs it. The
|
||||
# stack is lazy-imported, so a host without it still runs every other front end.
|
||||
if [[ "$VOICE" = 1 ]]; then
|
||||
if ! dpkg -s libportaudio2 >/dev/null 2>&1; then
|
||||
echo "== installing system PortAudio (libportaudio2)"
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y libportaudio2
|
||||
fi
|
||||
echo "== installing voice deps into $VENV"
|
||||
"$PIP" install -r "$REPO_ROOT/requirements/voice.txt"
|
||||
# openWakeWord's Linux metadata hard-requires tflite-runtime (no py3.12+
|
||||
# wheel); the onnxruntime backend does not need it, so install it --no-deps
|
||||
# (its own runtime deps are pinned in voice.txt).
|
||||
"$PIP" install --no-deps openwakeword==0.6.0
|
||||
fi
|
||||
|
||||
# --- stage 4: GGUF --------------------------------------------------------------
|
||||
GGUF_NAME="$(basename "$GGUF_URL")"
|
||||
GGUF="$MODELS/$GGUF_NAME"
|
||||
@@ -264,6 +284,18 @@ from transformers import AutoTokenizer
|
||||
AutoTokenizer.from_pretrained(sys.argv[1], revision=sys.argv[2])
|
||||
PY
|
||||
|
||||
# --- stage 5b: optional voice models (--voice) ----------------------------------
|
||||
# Downloads the openWakeWord models (into the venv) + the Piper voice (into
|
||||
# .runtime/voice/tts) and warms the faster-whisper model into the HF cache.
|
||||
# Reads gateway.voice from config.json
|
||||
# so the models match the configured names. Best-effort: a download failure
|
||||
# warns (the gateway reports the missing model at startup) rather than aborting.
|
||||
if [[ "$VOICE" = 1 ]]; then
|
||||
echo "== downloading voice models (openWakeWord + Piper + faster-whisper)"
|
||||
HF_HOME="$HF_CACHE" "$PYTHON" "$REPO_ROOT/scripts/voice-models.py" --config "$CONFIG" \
|
||||
|| echo "WARN: voice model download failed; re-run scripts/voice-models.py" >&2
|
||||
fi
|
||||
|
||||
# --- stage 6: simplex-chat daemon binary -------------------------------------------
|
||||
SIMPLEX_BIN="$TOOLDIR/simplex-chat"
|
||||
simplex_ok() {
|
||||
@@ -498,3 +530,20 @@ LLM bridge (generation for skills):
|
||||
model (configured once) and serves a generic POST /chat; skills call it via
|
||||
the top-level llm_bridge_url (default http://127.0.0.1:5229).
|
||||
EOF
|
||||
|
||||
if [[ "$VOICE" = 1 ]]; then
|
||||
cat <<EOF
|
||||
|
||||
Voice command gateway (wake word + speech-to-text + text-to-speech):
|
||||
- The stack is installed and its models downloaded (openWakeWord into the
|
||||
venv, the Piper voice into .runtime/voice/tts, faster-whisper into .runtime/hf).
|
||||
Enable it in config.json with gateway.voice.enabled = true, then run it in
|
||||
the foreground (it needs your audio session; it is NOT a systemd unit):
|
||||
HF_HOME="$HF_CACHE" "$PYTHON" -m semif_agent.cli gateway --platform voice
|
||||
- Say the wake word (gateway.voice.wake.model, default 'hey_mycroft') and
|
||||
speak; the reply is spoken back. After each reply the next utterance needs
|
||||
no wake word for gateway.voice.follow_up_window_s.
|
||||
- Re-download / switch models any time with:
|
||||
HF_HOME="$HF_CACHE" "$PYTHON" scripts/voice-models.py --config config.json
|
||||
EOF
|
||||
fi
|
||||
@@ -0,0 +1,113 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Download the voice-gateway models into the checkout's `.runtime/voice` tree.
|
||||
|
||||
Run after installing `requirements/voice.txt` (bootstrap does this with
|
||||
`--voice`). Idempotent: a model already on disk is skipped. Reads
|
||||
`gateway.voice` from config.json so the downloaded models match the configured
|
||||
names, or take explicit flags.
|
||||
|
||||
- **Wake word** (openWakeWord): downloaded by `openwakeword.utils.download_models`
|
||||
into the venv's package resources dir (inside `.runtime/venv/`, so still
|
||||
contained in the checkout). The detector resolves the model by name there.
|
||||
- **TTS** (Piper): the voice `.onnx` + `.onnx.json` are fetched from the
|
||||
`rhasspy/piper-voices` HF repo into `.runtime/voice/tts/`.
|
||||
- **STT** (faster-whisper): instantiating the model warms it into the HF cache
|
||||
(`.runtime/hf`); the runner reuses that cache at runtime.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
REPO_ROOT = Path(__file__).resolve().parent.parent
|
||||
RUNTIME = REPO_ROOT / ".runtime" / "voice"
|
||||
PIPER_REPO = "rhasspy/piper-voices"
|
||||
|
||||
|
||||
def piper_hf_path(voice: str) -> str:
|
||||
"""Map a Piper voice id to its file path in the `rhasspy/piper-voices` repo.
|
||||
|
||||
`en_US-lessac-medium` -> `en/en_US/lessac/medium/en_US-lessac-medium.onnx`
|
||||
(the voice name may itself contain dashes).
|
||||
"""
|
||||
parts = voice.split("-")
|
||||
if len(parts) < 3:
|
||||
raise ValueError(f"unrecognized piper voice id {voice!r}")
|
||||
locale, quality = parts[0], parts[-1]
|
||||
name = "-".join(parts[1:-1])
|
||||
lang = locale.split("_")[0]
|
||||
return f"{lang}/{locale}/{name}/{quality}/{voice}.onnx"
|
||||
|
||||
|
||||
def download_wake(name: str) -> None:
|
||||
import openwakeword.utils as utils
|
||||
|
||||
print(f"== wake word model: {name}")
|
||||
# Default target: the venv's package resources dir (inside .runtime/venv).
|
||||
# openWakeWord resolves the model (and its shared mel/embedding models) by
|
||||
# name from there.
|
||||
utils.download_models(model_names=[name])
|
||||
|
||||
|
||||
def download_voice(voice: str, target: Path) -> None:
|
||||
from huggingface_hub import hf_hub_download
|
||||
|
||||
target.mkdir(parents=True, exist_ok=True)
|
||||
rel = piper_hf_path(voice)
|
||||
for suffix in ("", ".json"):
|
||||
dest = target / f"{voice}.onnx{suffix}"
|
||||
if dest.is_file():
|
||||
print(f"== tts voice: {dest.name} (present)")
|
||||
continue
|
||||
print(f"== tts voice: {voice}{suffix or '.onnx'} -> {dest}")
|
||||
cached = hf_hub_download(repo_id=PIPER_REPO, filename=rel + suffix)
|
||||
shutil.copyfile(cached, dest)
|
||||
|
||||
|
||||
def download_stt(model: str) -> None:
|
||||
from faster_whisper import WhisperModel
|
||||
|
||||
print(f"== stt model: {model} (warming HF cache)")
|
||||
WhisperModel(model, device="cpu", compute_type="int8")
|
||||
|
||||
|
||||
def _config_voice(config_path: Path) -> dict:
|
||||
try:
|
||||
config = json.loads(config_path.read_text())
|
||||
except Exception:
|
||||
return {}
|
||||
return (config.get("gateway", {}) or {}).get("voice", {}) or {}
|
||||
|
||||
|
||||
def main(argv: list[str] | None = None) -> int:
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--config", default=str(REPO_ROOT / "config.json"))
|
||||
parser.add_argument("--wake", default=None, help="openWakeWord model name")
|
||||
parser.add_argument("--stt", default=None, help="faster-whisper model size/path")
|
||||
parser.add_argument("--tts", default=None, help="Piper voice id")
|
||||
parser.add_argument("--tts-dir", default=str(RUNTIME / "tts"))
|
||||
parser.add_argument("--skip-wake", action="store_true")
|
||||
parser.add_argument("--skip-stt", action="store_true")
|
||||
parser.add_argument("--skip-tts", action="store_true")
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
voice_cfg = _config_voice(Path(args.config))
|
||||
wake = args.wake or (voice_cfg.get("wake", {}) or {}).get("model") or "hey_mycroft"
|
||||
stt = args.stt or (voice_cfg.get("stt", {}) or {}).get("model") or "base.en"
|
||||
tts = args.tts or (voice_cfg.get("tts", {}) or {}).get("voice") or "en_US-lessac-medium"
|
||||
|
||||
if not args.skip_wake:
|
||||
download_wake(wake)
|
||||
if not args.skip_tts:
|
||||
download_voice(tts, Path(args.tts_dir))
|
||||
if not args.skip_stt:
|
||||
download_stt(stt)
|
||||
print("== voice models ready")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in new issue
Block a user