Spaces:
Paused
Paused
Download models/weights.py from fracture-bench/playground: direct link, hf CLI and curl.
- Browser
- Download file 3.06 kB
-
https://huggingface.co/spaces/fracture-bench/playground/resolve/main/models/weights.py
- Command line
-
hf download hf://spaces/fracture-bench/playground/models/weights.py
-
curl -L -o weights.py https://huggingface.co/spaces/fracture-bench/playground/resolve/main/models/weights.py
3.06 kB
| """Fetch trained weights, which are too large to keep in the git repository. | |
| checkpoints/weights_manifest.json lists every weight file with its size and | |
| MD5. A file missing from checkpoints/ is taken from the weights source: | |
| * the FRACTURE_BENCH_WEIGHTS environment variable, or else | |
| * "base_url" in the manifest. | |
| The source is either a URL to a folder with the same layout as checkpoints/ | |
| (for example https://huggingface.co/<user>/<repo>/resolve/main), from which the | |
| file is downloaded and checked against its MD5, or a local folder, from which | |
| the file is symlinked. | |
| Run `python -m models.weights` to fetch everything up front, for example when | |
| deploying. | |
| """ | |
| from __future__ import annotations | |
| import hashlib | |
| import json | |
| import os | |
| import shutil | |
| import sys | |
| import tempfile | |
| import urllib.request | |
| CHECKPOINT_ROOT = os.path.join(os.path.dirname(__file__), "..", "checkpoints") | |
| MANIFEST = os.path.join(CHECKPOINT_ROOT, "weights_manifest.json") | |
| def _manifest() -> dict: | |
| with open(MANIFEST) as fh: | |
| return json.load(fh) | |
| def weights_source() -> str: | |
| return os.environ.get("FRACTURE_BENCH_WEIGHTS") or _manifest().get("base_url", "") | |
| def _md5(path: str) -> str: | |
| h = hashlib.md5() | |
| with open(path, "rb") as fh: | |
| for block in iter(lambda: fh.read(16 * 2**20), b""): | |
| h.update(block) | |
| return h.hexdigest() | |
| def _fetch(rel: str, meta: dict, source: str) -> None: | |
| dest = os.path.join(CHECKPOINT_ROOT, rel) | |
| os.makedirs(os.path.dirname(dest), exist_ok=True) | |
| if "://" not in source: # local folder: link, don't copy | |
| src = os.path.abspath(os.path.join(source, rel)) | |
| if os.path.exists(src): | |
| os.symlink(src, dest) | |
| return | |
| url = source.rstrip("/") + "/" + rel | |
| fd, tmp = tempfile.mkstemp(dir=os.path.dirname(dest), suffix=".part") | |
| try: | |
| with urllib.request.urlopen(url) as resp, os.fdopen(fd, "wb") as out: | |
| shutil.copyfileobj(resp, out, length=16 * 2**20) | |
| if _md5(tmp) != meta["md5"]: | |
| raise IOError(f"checksum mismatch for {url}") | |
| os.replace(tmp, dest) | |
| finally: | |
| if os.path.exists(tmp): | |
| os.remove(tmp) | |
| def fetch_missing(subdir: str | None = None) -> None: | |
| """Fetch the missing weight files (all of them, or those under `subdir`). | |
| Does nothing when no weights source is configured; models whose weights | |
| are absent are then simply unavailable in the app. | |
| """ | |
| source = weights_source() | |
| if not source: | |
| return | |
| for rel, meta in _manifest()["files"].items(): | |
| if subdir and not rel.startswith(subdir.rstrip("/") + "/"): | |
| continue | |
| if not os.path.exists(os.path.join(CHECKPOINT_ROOT, rel)): | |
| _fetch(rel, meta, source) | |
| if __name__ == "__main__": | |
| if not weights_source(): | |
| sys.exit("No weights source: set FRACTURE_BENCH_WEIGHTS or base_url in " | |
| "checkpoints/weights_manifest.json") | |
| fetch_missing() | |
| print("all weights present in", os.path.abspath(CHECKPOINT_ROOT)) | |