Skip to content

model_integrity

diapason.speech.model_integrity

Model download integrity and the local-only download policy.

Two gaps this closes:

  • Diapason pulls models with huggingface-cli download / ollama pull and never checks what arrived. Diapason's fetch-model.js pins a sha256 and deletes the file on mismatch. verify_file_sha256 brings that here.

  • faster-whisper downloads its model on first transcription — silently, over the network. Under [privacy] local_only a silent fetch is exactly what the user asked not to happen. should_allow_download encodes the policy: an implicit download is refused, an explicit diapason model pull is 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.

ImplicitDownloadBlocked

Bases: RuntimeError

Raised when local-only refuses a silent model download.

faster_whisper_cached(model_size)

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.

guard_implicit_download(model_name, *, local_only, already_cached, explicit=False)

Raise :class:ImplicitDownloadBlocked when a download must not happen.

sha256_file(path)

Return the hex sha256 of a file, read in chunks (models are large).

should_allow_download(*, local_only, explicit, already_cached)

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.

verify_file_sha256(path, expected)

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.