Files

33 lines
1.4 KiB
Python

"""Explicit worker-only preparation, separate from network-isolated inference."""
import argparse, hashlib, json, urllib.request
from pathlib import Path
from assets import COMMIT, ARCHIVE_SHA, WEIGHTS
def digest(path):
h=hashlib.sha256()
with path.open('rb') as f:
for chunk in iter(lambda:f.read(8388608),b''):h.update(chunk)
return h.hexdigest()
def acquire(path, expected, download):
path.parent.mkdir(parents=True,exist_ok=True)
if path.exists() and digest(path)==expected:return
part=path.with_suffix(path.suffix+'.partial')
download(part)
if digest(part)!=expected:
part.unlink(missing_ok=True);raise ValueError('Downloaded artifact hash mismatch')
part.replace(path)
if __name__=='__main__':
p=argparse.ArgumentParser();p.add_argument('--source',type=Path);p.add_argument('--weights',type=Path);a=p.parse_args()
if a.source:
import shutil
a.source.mkdir(parents=True,exist_ok=True)
shutil.copy2(Path(__file__).resolve().parents[2]/'ai-polygon/terrain.py',a.source/'terrain.py')
acquire(a.source/'vma-source.tar.gz',ARCHIVE_SHA,lambda path:urllib.request.urlretrieve('https://codeload.github.com/hustvl/VMA/tar.gz/'+COMMIT,path))
if a.weights:
import gdown
for name, item in WEIGHTS.items():
acquire(a.weights/name,item['sha256'],lambda path:gdown.download(id=item['drive_id'],output=str(path),quiet=False))
(a.weights/'manifest.json').write_text(json.dumps(WEIGHTS,indent=2))