"""Use an anonymous mirror transport; hashes come from the official HF API.""" import fnmatch import argparse import json import time import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT)) from settings import MODELS, WEIGHTS from huggingface_hub import HfApi from range_download import download from download_core import download_imagebind if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument('--only', choices=['nextgpt','llama','vicuna_delta']) parser.add_argument('--skip-imagebind', action='store_true') args = parser.parse_args() api = HfApi() 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"]), ]: if args.only and key != args.only: continue repo, revision = MODELS[key] manifest=destination/'download-manifest.json' destination.mkdir(parents=True,exist_ok=True) cached=json.loads(manifest.read_text()) if manifest.is_file() else None if cached and cached.get('revision')==revision and cached.get('repo')==repo: entries=cached['files'] else: for attempt in range(12): try: info=api.model_info(repo,revision=revision,files_metadata=True) break except Exception as exc: print(f'Official metadata retry {attempt+1}: {type(exc).__name__}',flush=True) if attempt==11: raise time.sleep(min(20,2*(attempt+1))) entries=[dict(name=f.rfilename,size=f.size,sha256=f.lfs.sha256 if f.lfs else None, blob_id=f.blob_id) for f in info.siblings] manifest.write_text(json.dumps(dict(repo=repo,revision=revision,files=entries),indent=2)) for file in entries: name=file['name'] if not any(fnmatch.fnmatch(name, pattern) for pattern in patterns): continue download(f"https://hf-mirror.com/{repo}/resolve/{revision}/{name}", destination/name,file['size'],sha256=file['sha256'], git_sha1=file['blob_id'] if not file['sha256'] else None, fallback_url=f"https://huggingface.co/{repo}/resolve/{revision}/{name}") if not args.skip_imagebind: download_imagebind() print("Core model downloads verified.", flush=True)