File size: 2,713 Bytes
15c46ce
 
1c9b5b6
37a409d
 
15c46ce
 
 
 
 
 
 
 
 
 
 
1c9b5b6
 
 
 
15c46ce
 
 
 
 
 
1c9b5b6
 
15c46ce
37a409d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15c46ce
37a409d
 
 
 
1c9b5b6
 
15c46ce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""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)