transcription_whisper_cpp.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. """Managed whisper.cpp command and first-use, verified model acquisition."""
  2. from __future__ import annotations
  3. import logging
  4. import shlex
  5. from pathlib import Path
  6. from hermes_constants import get_hermes_home
  7. logger = logging.getLogger("tools.transcription_tools")
  8. # Keys include the profile's absolute file path; a served profile never inherits
  9. # another profile's verification. Changed/replaced files must be checked again.
  10. _verified_files: dict[Path, tuple] = {}
  11. # Model artifacts stay outside PM's bundled tool closure: only first transcription
  12. # downloads them, into writable profile state even when the program is in WindowsApps.
  13. _MODEL_FILES = (
  14. ("ggml-base.bin",
  15. "https://huggingface.co/ggerganov/whisper.cpp/resolve/5359861c739e955e79d9a303bcbc70fb988958b1/ggml-base.bin",
  16. "60ed5bc3dd14eea856493d334349b405782ddcaf0028d4b5df4088345fba2efe"),
  17. ("ggml-silero-v6.2.0.bin",
  18. "https://huggingface.co/ggml-org/whisper-vad/resolve/9ffd54a1e1ee413ddf265af9913beaf518d1639b/ggml-silero-v6.2.0.bin",
  19. "2aa269b785eeb53a82983a20501ddf7c1d9c48e33ab63a41391ac6c9f7fb6987"),
  20. )
  21. def _model_dir() -> Path:
  22. return get_hermes_home() / "cache" / "whisper.cpp"
  23. def _file_signature(path: Path, digest: str) -> tuple | None:
  24. try:
  25. stat = path.stat()
  26. except FileNotFoundError:
  27. return None
  28. return (digest, stat.st_ino, stat.st_size, stat.st_mtime_ns, stat.st_ctime_ns)
  29. def whisper_cpp_command() -> str | None:
  30. import pm
  31. # Passive: an engine the install does not carry is absent, never fetched here.
  32. installed = pm.installed_package("whispercpp-cpu")
  33. if installed is None:
  34. return None
  35. binary = installed.binary
  36. models = _model_dir()
  37. # The existing command adapter tokenizes with shlex, including on Windows.
  38. quote = lambda path: shlex.quote(path.as_posix())
  39. return (f"{quote(binary)} -m {quote(models / 'ggml-base.bin')} "
  40. "-f {input_path} -l {language} -t 4 -bs 5 -bo 5 -ng -nt -otxt "
  41. "-of {output_dir}/transcript --vad "
  42. f"-vm {quote(models / 'ggml-silero-v6.2.0.bin')} -vsd 500")
  43. def ensure_whisper_cpp_models(model_name: str) -> None:
  44. if model_name != "base":
  45. raise ValueError("Windows ARM64 whisper.cpp currently supports stt.local.model: base; "
  46. f"requested {model_name!r}")
  47. from pm.downloader import Download, Source
  48. destination = _model_dir()
  49. sources = [Source(url, destination / name, digest) for name, url, digest in _MODEL_FILES]
  50. pending = [source for source in sources
  51. if (signature := _file_signature(source.dest, source.sha256)) is None
  52. or _verified_files.get(source.dest) != signature]
  53. if not pending:
  54. return
  55. logger.info("Preparing whisper.cpp Base and VAD models (first use downloads about 149 MB)")
  56. # PM owns resumable partials, concurrent-download locking, SHA256 verification,
  57. # and atomic publication. A complete cache is checked without network access.
  58. Download(pending, partials_dir=destination / "partials").run()
  59. for source in pending:
  60. signature = _file_signature(source.dest, source.sha256)
  61. if signature is not None:
  62. _verified_files[source.dest] = signature