File size: 1,127 Bytes
1c9b5b6 | 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 | """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)
|