nextgpt-chapter6-report / code /scripts /download_imagebind_parallel.py
CollinMa's picture
Publish NExT-GPT course code and actual execution records
1c9b5b6 verified
Raw History Blame Contribute Delete
1.13 kB
"""Fetch ImageBind directly from Meta and check its official multipart ETag."""
import hashlib
import json
import sys
import urllib.request
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
from settings import IMAGEBIND_URL, weight_paths
from range_download import download
if __name__ == '__main__':
with urllib.request.urlopen(urllib.request.Request(IMAGEBIND_URL,method='HEAD'),timeout=30) as response:
size = int(response.headers['Content-Length'])
etag = response.headers['ETag'].strip('"')
target = weight_paths()['imagebind']
partial = target.with_suffix('.part')
download(IMAGEBIND_URL,target,size,s3_etag=etag,seed_prefix=partial if partial.exists() else None)
digest = hashlib.sha256()
with target.open('rb') as source:
for block in iter(lambda: source.read(8*1024*1024),b''):
digest.update(block)
target.with_suffix('.source.json').write_text(json.dumps({'url':IMAGEBIND_URL,'etag':etag,
'size':size,'sha256':digest.hexdigest()},indent=2))
print('Official ImageBind verified.',flush=True)