"""Download only core weights first; diffusion decoders can be fetched on demand.""" import sys import time import urllib.request from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT)) from settings import MODELS, WEIGHTS, IMAGEBIND_URL, weight_paths from huggingface_hub import snapshot_download def download_imagebind(): target = weight_paths()["imagebind"] if target.is_file(): return target.parent.mkdir(parents=True, exist_ok=True) partial = target.with_suffix(".part") for attempt in range(6): offset = partial.stat().st_size if partial.exists() else 0 request = urllib.request.Request(IMAGEBIND_URL, headers={"Range": f"bytes={offset}-"} if offset else {}) try: with urllib.request.urlopen(request, timeout=90) as response: append = offset > 0 and response.status == 206 if not append: offset = 0 remaining = int(response.headers.get("Content-Length", "0")) total = offset + remaining if remaining else None last_log = offset with partial.open("ab" if append else "wb") as stream: while True: chunk = response.read(8 * 1024 * 1024) if not chunk: break stream.write(chunk) offset += len(chunk) if offset-last_log > 256 * 1024 * 1024: print(f"ImageBind {offset/2**30:.2f} GiB / {total/2**30 if total else 0:.2f}", flush=True) last_log = offset if total is not None and offset != total: raise IOError("Incomplete ImageBind transfer") if offset < 1_000_000_000: raise IOError("Unexpected ImageBind file size") partial.replace(target) return except Exception as exc: print(f"ImageBind retry {attempt+1}: {type(exc).__name__}: {exc}", flush=True) if attempt == 5: raise time.sleep(2) if __name__ == "__main__": for key, destination, patterns in [ ("nextgpt", WEIGHTS / "nextgpt", ["*.json", "*.model", "pytorch_model.pt"]), ("llama", WEIGHTS / "sources" / "llama", ["*.json", "*.model", "*.safetensors"]), ("vicuna_delta", WEIGHTS / "sources" / "vicuna_delta", ["*.json", "*.model", "pytorch_model*.bin"]), ]: repo, revision = MODELS[key] print(f"Downloading {key}: {repo}@{revision}", flush=True) snapshot_download(repo, revision=revision, local_dir=str(destination), allow_patterns=patterns, max_workers=2) download_imagebind() print("Core downloads complete. Restore Vicuna with scripts/merge_vicuna_streaming.py", flush=True)