[依赖、模型和配置]:完成本地语音模型准备,包含.env配置、模型下载脚本和model-check

This commit is contained in:
mkbk
2026-06-17 19:34:29 +08:00
parent 40510325d9
commit e877cca00f
10 changed files with 323 additions and 7 deletions
+142
View File
@@ -0,0 +1,142 @@
from __future__ import annotations
import importlib.util
import json
from dataclasses import dataclass
from pathlib import Path
from typing import Any
from .models import ErrorCode, ProviderError
DEFAULT_VAD_URL = "https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/silero_vad.onnx"
DEFAULT_STT_URL = (
"https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/"
"sherpa-onnx-streaming-zipformer-zh-14M-2023-02-23.tar.bz2"
)
DEFAULT_STT_DIR = "sherpa-onnx-streaming-zipformer-zh-14M-2023-02-23"
REQUIRED_MODEL_FILES = (
"vad/silero_vad.onnx",
f"stt/{DEFAULT_STT_DIR}/tokens.txt",
f"stt/{DEFAULT_STT_DIR}/encoder-epoch-99-avg-1.int8.onnx",
f"stt/{DEFAULT_STT_DIR}/decoder-epoch-99-avg-1.onnx",
f"stt/{DEFAULT_STT_DIR}/joiner-epoch-99-avg-1.int8.onnx",
)
@dataclass(frozen=True, slots=True)
class SpeechModelStatus:
models_dir: Path
manifest_path: Path
manifest_present: bool
required_files: tuple[str, ...]
missing_files: tuple[str, ...]
sherpa_onnx_available: bool
@property
def ok(self) -> bool:
return not self.missing_files and self.sherpa_onnx_available
def to_json(self) -> dict[str, Any]:
return {
"ok": self.ok,
"models_dir": str(self.models_dir),
"manifest_path": str(self.manifest_path),
"manifest_present": self.manifest_present,
"required_files": list(self.required_files),
"missing_files": list(self.missing_files),
"sherpa_onnx_available": self.sherpa_onnx_available,
}
def default_manifest() -> dict[str, Any]:
return {
"schema": "owner_voice_pet.speech_models.v1",
"sample_rate": 16000,
"sources": {
"vad": DEFAULT_VAD_URL,
"stt": DEFAULT_STT_URL,
},
"providers": {
"vad": {
"type": "silero-vad",
"path": "vad/silero_vad.onnx",
},
"stt": {
"type": "sherpa-onnx-streaming-transducer",
"model_dir": f"stt/{DEFAULT_STT_DIR}",
"tokens": f"stt/{DEFAULT_STT_DIR}/tokens.txt",
"encoder": f"stt/{DEFAULT_STT_DIR}/encoder-epoch-99-avg-1.int8.onnx",
"decoder": f"stt/{DEFAULT_STT_DIR}/decoder-epoch-99-avg-1.onnx",
"joiner": f"stt/{DEFAULT_STT_DIR}/joiner-epoch-99-avg-1.int8.onnx",
},
},
"required_files": list(REQUIRED_MODEL_FILES),
}
def write_default_manifest(models_dir: str | Path) -> Path:
root = Path(models_dir)
root.mkdir(parents=True, exist_ok=True)
manifest_path = root / "manifest.json"
manifest_path.write_text(
json.dumps(default_manifest(), ensure_ascii=False, indent=2) + "\n",
encoding="utf-8",
)
return manifest_path
def load_manifest(models_dir: str | Path) -> dict[str, Any]:
manifest_path = Path(models_dir) / "manifest.json"
if not manifest_path.exists():
return default_manifest()
return json.loads(manifest_path.read_text(encoding="utf-8"))
def required_model_files(models_dir: str | Path) -> tuple[str, ...]:
manifest = load_manifest(models_dir)
files = manifest.get("required_files", list(REQUIRED_MODEL_FILES))
return tuple(str(item) for item in files)
def check_speech_models(models_dir: str | Path, require_sherpa: bool = True) -> SpeechModelStatus:
root = Path(models_dir)
manifest_path = root / "manifest.json"
required = required_model_files(root)
missing = tuple(relative for relative in required if not (root / relative).exists())
sherpa_available = True
if require_sherpa:
sherpa_available = importlib.util.find_spec("sherpa_onnx") is not None
return SpeechModelStatus(
models_dir=root,
manifest_path=manifest_path,
manifest_present=manifest_path.exists(),
required_files=required,
missing_files=missing,
sherpa_onnx_available=sherpa_available,
)
def model_status_errors(status: SpeechModelStatus) -> list[ProviderError]:
errors: list[ProviderError] = []
if not status.sherpa_onnx_available:
errors.append(
ProviderError(
ErrorCode.STT_TRANSCRIBE_FAILED,
"sherpa_onnx is not installed in the active Python environment",
False,
"sherpa-onnx",
"model-check",
)
)
if status.missing_files:
errors.append(
ProviderError(
ErrorCode.STT_MODEL_MISSING,
"missing speech model files: " + ", ".join(status.missing_files),
False,
"speech-models",
"model-check",
)
)
return errors