Automation foundation model for tiny devices: 2-bit, 8-29 MB, tool calls, ASR, structured extraction and embeddings on phones, wearables, smart homes, robots, cars and microcontrollers.
feat: register downloads for cached models in fetch_weights and add tests for download counting
HenryNdubuakuaf2654dffa49
diff --git a/needle/__init__.py b/needle/__init__.pyindex 133ff10..5993c45 100644--- a/needle/__init__.py+++ b/needle/__init__.py@@ -114,6 +114,7 @@ def _base_weights_path(generation):local = os.path.join(fetch.cache_dir(generation), fetch.base_weights(generation))if os.path.exists(local):+ fetch._register_download(generation)return localreturn fetch.fetch_weights(generation)diff --git a/needle/agent/fetch.py b/needle/agent/fetch.pyindex 583bfe9..6fdaf96 100644--- a/needle/agent/fetch.py+++ b/needle/agent/fetch.py@@ -226,7 +226,8 @@ def fetch_weights(generation=2, dest_dir=None, force=False):"""Download the base .cact archive of a generation (cached next to its engine).``force`` fetches it again even when the cache holds a copy, so every build- is a download of the published model.+ is a download of the published model. The download is registered before the+ cache is consulted, so a warm cache still counts as a use of the model."""import shutilfrom huggingface_hub import hf_hub_download@@ -234,9 +235,9 @@ def fetch_weights(generation=2, dest_dir=None, force=False):name = base_weights(generation)dest_dir = dest_dir or cache_dir(generation)out = os.path.join(dest_dir, name)+ _register_download(generation)if os.path.exists(out) and not force:return out- _register_download(generation)cached = hf_hub_download(repo_id=engine_repo(generation), filename=name, repo_type="model",force_download=force)os.makedirs(dest_dir, exist_ok=True)diff --git a/tests/test_whistle.py b/tests/test_whistle.pyindex 7bbc722..1b85df7 100644--- a/tests/test_whistle.py+++ b/tests/test_whistle.py@@ -54,6 +54,51 @@ def test_speech_weights_come_from_their_own_repo(tmp_path, monkeypatch):assert whistle._weights_path() == "/opt/whistle.cact"+def test_a_cached_model_still_counts_as_a_download(tmp_path, monkeypatch):+ """Hugging Face counts a model by requests for its config, so a warm cache must+ still ask for it: otherwise only a visitor's first ever load is ever counted."""+ import needle+ from needle.agent import whistle+ from needle.agent import fetch++ monkeypatch.setattr(os.path, "expanduser", lambda path: str(tmp_path))+ monkeypatch.setattr(needle, "__file__", str(tmp_path / "package" / "__init__.py"))+ monkeypatch.delenv("NEEDLE_WHISTLE_WEIGHTS", raising=False)++ cache = tmp_path / ".cache" / "cactus-needle" / "whistle" / fetch.ENGINE_VERSIONS[fetch.WHISTLE]+ cache.mkdir(parents=True)+ (cache / "whistle.cact").write_bytes(b"weights")++ asked = []+ monkeypatch.setattr("huggingface_hub.hf_hub_download",+ lambda **kwargs: asked.append((kwargs["repo_id"], kwargs["filename"],+ kwargs.get("force_download"))) or str(cache / "whistle.cact"))+ assert whistle._weights_path() == str(cache / "whistle.cact")+ assert asked == [("Cactus-Compute/whistle", "config.json", True)]++ whistle._weights_path()+ assert len(asked) == 2, "every load counts, not just the first"+++def test_counting_a_download_never_breaks_an_offline_load(tmp_path, monkeypatch):+ import needle+ from needle.agent import whistle+ from needle.agent import fetch++ monkeypatch.setattr(os.path, "expanduser", lambda path: str(tmp_path))+ monkeypatch.setattr(needle, "__file__", str(tmp_path / "package" / "__init__.py"))+ monkeypatch.delenv("NEEDLE_WHISTLE_WEIGHTS", raising=False)+ cache = tmp_path / ".cache" / "cactus-needle" / "whistle" / fetch.ENGINE_VERSIONS[fetch.WHISTLE]+ cache.mkdir(parents=True)+ (cache / "whistle.cact").write_bytes(b"weights")++ def offline(**kwargs):+ raise OSError("no network")++ monkeypatch.setattr("huggingface_hub.hf_hub_download", offline)+ assert whistle._weights_path() == str(cache / "whistle.cact")++def test_module_level_transcribe_reuses_one_model(monkeypatch):import needlefrom needle.agent import whistle