[本地唤醒模型]:完成KWS模型下载和检查,包含manifest、配置和model-check
This commit is contained in:
@@ -16,6 +16,9 @@ if str(SRC_DIR) not in sys.path:
|
||||
sys.path.insert(0, str(SRC_DIR))
|
||||
|
||||
from owner_voice_pet.speech_models import (
|
||||
DEFAULT_KWS_DIR,
|
||||
DEFAULT_KWS_KEYWORDS,
|
||||
DEFAULT_KWS_URL,
|
||||
DEFAULT_STT_DIR,
|
||||
DEFAULT_STT_URL,
|
||||
DEFAULT_VAD_URL,
|
||||
@@ -32,6 +35,7 @@ def main(argv: list[str] | None = None) -> int:
|
||||
|
||||
target = Path(args.dir)
|
||||
target.mkdir(parents=True, exist_ok=True)
|
||||
download_kws(target, force=args.force)
|
||||
download_vad(target, force=args.force)
|
||||
download_stt(target, force=args.force)
|
||||
manifest = write_default_manifest(target)
|
||||
@@ -42,6 +46,38 @@ def main(argv: list[str] | None = None) -> int:
|
||||
return 0 if not status.missing_files else 1
|
||||
|
||||
|
||||
def download_kws(models_dir: Path, *, force: bool = False) -> Path:
|
||||
output_dir = models_dir / "wake" / DEFAULT_KWS_DIR
|
||||
if output_dir.exists() and any(output_dir.iterdir()) and not force:
|
||||
print(f"skip existing {output_dir}")
|
||||
else:
|
||||
output_dir.parent.mkdir(parents=True, exist_ok=True)
|
||||
last_error: Exception | None = None
|
||||
for attempt in range(1, 4):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
archive = Path(tmp) / f"{DEFAULT_KWS_DIR}.tar.bz2"
|
||||
try:
|
||||
download_file(DEFAULT_KWS_URL, archive)
|
||||
extract_tar_bz2(archive, output_dir.parent)
|
||||
last_error = None
|
||||
break
|
||||
except Exception as exc:
|
||||
last_error = exc
|
||||
print(f"download/extract KWS attempt {attempt} failed: {exc}")
|
||||
if output_dir.exists():
|
||||
shutil.rmtree(output_dir)
|
||||
if last_error is not None:
|
||||
raise last_error
|
||||
keywords = models_dir / "wake" / "keywords.txt"
|
||||
if force or not keywords.exists():
|
||||
keywords.parent.mkdir(parents=True, exist_ok=True)
|
||||
keywords.write_text(DEFAULT_KWS_KEYWORDS, encoding="utf-8")
|
||||
print(f"wrote {keywords}")
|
||||
else:
|
||||
print(f"skip existing {keywords}")
|
||||
return output_dir
|
||||
|
||||
|
||||
def download_vad(models_dir: Path, *, force: bool = False) -> Path:
|
||||
output = models_dir / "vad" / "silero_vad.onnx"
|
||||
if output.exists() and not force:
|
||||
|
||||
Reference in New Issue
Block a user