model_integrity
model_integrity
¶
Model download integrity and the local-only download policy.
Two gaps this closes:
-
Diapason pulls models with
huggingface-cli download/ollama pulland never checks what arrived. Diapason'sfetch-model.jspins a sha256 and deletes the file on mismatch.verify_file_sha256brings that here. -
faster-whisper downloads its model on first transcription — silently, over the network. Under
[privacy] local_onlya silent fetch is exactly what the user asked not to happen.should_allow_downloadencodes the policy: an implicit download is refused, an explicitdiapason model pullis allowed, and an already-cached model is always fine (nothing leaves).
Both are pure functions so the policy is unit-tested, not left to a live pull.
Classes¶
ImplicitDownloadBlocked
¶
Bases: RuntimeError
Raised when local-only refuses a silent model download.
Functions:¶
sha256_file
¶
Return the hex sha256 of a file, read in chunks (models are large).
Source code in src/diapason/speech/model_integrity.py
verify_file_sha256
¶
True when the file's digest matches expected (case-insensitive).
A missing or unreadable file is a failure, not an exception: the caller's job is to react (re-download, delete), not to crash.
Source code in src/diapason/speech/model_integrity.py
should_allow_download
¶
Decide whether a model download may proceed.
- Already cached → always allowed: using a model that is already on disk sends nothing.
- Explicit (the user ran
diapason model pull) → allowed even in local-only: a foreground request is consent. - Implicit under local-only → refused: a background fetch triggered by the first dictation is the silent network access local-only forbids.
Source code in src/diapason/speech/model_integrity.py
guard_implicit_download
¶
guard_implicit_download(
model_name: str,
*,
local_only: bool,
already_cached: bool,
explicit: bool = False,
) -> None
Raise :class:ImplicitDownloadBlocked when a download must not happen.
Source code in src/diapason/speech/model_integrity.py
faster_whisper_cached
¶
Best-effort check: is this faster-whisper model already in the HF cache?
Uses huggingface_hub's cache probe when available; on any uncertainty it returns False, which fails safe — the caller then refuses an implicit download rather than risking a silent one.