VideoObjectMover / code /training_admin /download_weights.py
FrontRear's picture
Upload training code and detailed handoff
42cd39c verified
Raw History Blame
2.52 kB
"""Fetch identical official model files directly on AutoDL, verify source SHA.
Keeps partial downloads resumable and never replaces an existing file until
the complete source hash matches. No tokens/passwords are stored here.
"""
import concurrent.futures, hashlib, json, os, subprocess, time
from pathlib import Path
ROOT=Path('/root/autodl-tmp/LampEditTraining')
ADMIN=ROOT/'training_admin'
MODEL=ROOT/'models/Wan2.2-TI2V-5B'
def sha(p):
h=hashlib.sha256()
with p.open('rb') as f:
for b in iter(lambda:f.read(8<<20),b''):h.update(b)
return h.hexdigest()
def one(item):
expected,relative=item; name=relative.removeprefix('models/Wan2.2-TI2V-5B/')
assert '..' not in Path(name).parts and not Path(name).is_absolute()
dst=MODEL/name;dst.parent.mkdir(parents=True,exist_ok=True)
if dst.is_file() and sha(dst)==expected:return {'file':name,'sha256':expected,'verified':True,'reused':True}
partial=dst.with_name(dst.name+'.hf-download')
for host in ('https://hf-mirror.com','https://huggingface.co'):
url=host+'/Wan-AI/Wan2.2-TI2V-5B/resolve/main/'+name
result=subprocess.run(['curl','--fail','--location','--retry','3','--retry-delay','3',
'--connect-timeout','30','--max-time','3600','--continue-at','-',
'--output',str(partial),url],capture_output=True,text=True)
if result.returncode==0 and partial.is_file() and sha(partial)==expected:
os.replace(partial,dst);return {'file':name,'sha256':expected,'verified':True,'source':host}
print('DOWNLOAD_RETRY',name,result.returncode,result.stderr[-250:],flush=True)
return {'file':name,'verified':False,'error':'Download or exact source checksum failed; partial retained'}
def main():
start=time.time();items=[]
for line in (ADMIN/'MODEL_SHA256SUMS').read_text().splitlines():
expected,relative=line.split(maxsplit=1);items.append((expected,relative.strip()))
rows=[]
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as pool:
for r in pool.map(one,items):rows.append(r);print(json.dumps(r),flush=True)
receipt={'finished_unix':time.time(),'elapsed_s':time.time()-start,'files':rows,
'all_models_verified':all(r['verified'] for r in rows),'data_training_ready':False}
(ROOT/'transfer_receipts/WEIGHTS_VERIFIED.json').write_text(json.dumps(receipt,indent=2)+'\n')
if not receipt['all_models_verified']:raise SystemExit(1)
if __name__=='__main__':main()