Spaces:
Sleeping
Sleeping
Download scripts/lib/build_config.py from defenestrate/asa: direct link, hf CLI and curl.
- Browser
- Download file 3 kB
-
https://huggingface.co/spaces/defenestrate/asa/resolve/main/scripts/lib/build_config.py
- Command line
-
hf download hf://spaces/defenestrate/asa/scripts/lib/build_config.py
-
curl -L -o build_config.py https://huggingface.co/spaces/defenestrate/asa/resolve/main/scripts/lib/build_config.py
3 kB
| """Build-config fingerprinting. | |
| Captures every input that affects the catalog. The sha256 of the canonicalized | |
| build-config dict (excluding the timestamp and the fingerprint field itself) is | |
| the build_config_fingerprint, which the manifest uses (alongside per-doc | |
| source_hash) to decide whether to reuse cached embeddings. | |
| """ | |
| from __future__ import annotations | |
| import hashlib | |
| import json | |
| from datetime import datetime, timezone | |
| from pathlib import Path | |
| from scripts.lib.concept_profile import PROFILE_SCHEMA_VERSION | |
| CATALOG_SCHEMA_VERSION = 1 | |
| MANIFEST_SCHEMA_VERSION = 1 | |
| RETRIEVAL_SCHEMA_VERSION = 1 | |
| BUILD_CONFIG_SCHEMA_VERSION = 1 | |
| def _sha256_file(path: Path) -> str: | |
| if not path.is_file(): | |
| raise FileNotFoundError(f"build-config input not found: {path}") | |
| h = hashlib.sha256() | |
| with open(path, "rb") as f: | |
| for chunk in iter(lambda: f.read(8192), b""): | |
| h.update(chunk) | |
| return "sha256:" + h.hexdigest() | |
| def build_config_dict( | |
| *, | |
| model: str, | |
| dims: int, | |
| registry_path: Path, | |
| topic_link_path: Path, | |
| classification_rules_path: Path, | |
| index_schema_version: int, | |
| ) -> dict: | |
| """Build the dict that gets serialized to build-config.json. | |
| The build_config_fingerprint field is computed AFTER all other fields, | |
| over the canonicalized JSON of the dict-without-fingerprint AND | |
| without the timestamp (so the fingerprint is stable across builds with | |
| identical inputs). | |
| """ | |
| cfg = { | |
| "schema_version": BUILD_CONFIG_SCHEMA_VERSION, | |
| "embedding_model": model, | |
| "embedding_dimensions": dims, | |
| "profile_schema_version": PROFILE_SCHEMA_VERSION, | |
| "retrieval_schema_version": RETRIEVAL_SCHEMA_VERSION, | |
| "catalog_schema_version": CATALOG_SCHEMA_VERSION, | |
| "manifest_schema_version": MANIFEST_SCHEMA_VERSION, | |
| "index_schema_version": index_schema_version, | |
| "term_registry_sha256": _sha256_file(registry_path), | |
| "topic_link_map_sha256": _sha256_file(topic_link_path), | |
| "classification_rules_sha256": _sha256_file(classification_rules_path), | |
| "build_timestamp_utc": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"), | |
| } | |
| fp_input = {k: v for k, v in cfg.items() if k != "build_timestamp_utc"} | |
| canonical = json.dumps(fp_input, sort_keys=True, separators=(",", ":")) | |
| cfg["build_config_fingerprint"] = "sha256:" + hashlib.sha256(canonical.encode("utf-8")).hexdigest() | |
| return cfg | |
| def compute_fingerprint( | |
| *, | |
| model: str, | |
| dims: int, | |
| registry_path: Path, | |
| topic_link_path: Path, | |
| classification_rules_path: Path, | |
| index_schema_version: int, | |
| ) -> str: | |
| """Convenience: just compute the fingerprint string.""" | |
| return build_config_dict( | |
| model=model, dims=dims, registry_path=registry_path, | |
| topic_link_path=topic_link_path, | |
| classification_rules_path=classification_rules_path, | |
| index_schema_version=index_schema_version, | |
| )["build_config_fingerprint"] | |