RefSeg-CA / source /legacy_project /code /prepare_assets.py
nmsofficial's picture
Add verified public availability and human-evaluation guide
9126e0d verified
Raw History Blame Contribute Delete
2.19 kB
"""Download pinned public model weights and official gRefCOCO annotations.
No model execution, private tokens, or changes to other projects.
"""
from pathlib import Path
import urllib.request,urllib.parse,json,hashlib,time,concurrent.futures
ROOT=Path(__file__).resolve().parents[1]
ASSETS=ROOT/'assets';ASSETS.mkdir(parents=True,exist_ok=True)
def get_json(url):
with urllib.request.urlopen(url,timeout=45) as r:return json.load(r)
def download(url,path):
path=Path(path);path.parent.mkdir(parents=True,exist_ok=True)
if path.exists() and path.stat().st_size>0:return str(path)
for attempt in range(3):
try:
with urllib.request.urlopen(url,timeout=90) as r,open(str(path)+'.part','wb') as f:
while True:
b=r.read(1024*1024)
if not b:break
f.write(b)
Path(str(path)+'.part').replace(path);print('downloaded',str(path),path.stat().st_size,flush=True);return str(path)
except Exception:
if attempt==2:raise
time.sleep(2)
def main():
# Reproduction uses the executed lock, never the current upstream HEAD.
lock=ROOT/'protocol/asset_manifest.json';manifest=json.loads(lock.read_text())
jobs=[]
for rel,expected in manifest['file_sha256'].items():
p=Path(rel);assert p.parts[0]=='assets' and '..' not in p.parts
if p.parts[1]=='models':
meta=manifest['models'][p.parts[2]];prefix='https://huggingface.co/'+meta['repo'];name='/'.join(p.parts[3:])
else:
assert p.parts[1]=='grefcoco';meta=manifest['dataset'];prefix='https://huggingface.co/datasets/'+meta['repo'];name='/'.join(p.parts[2:])
jobs.append((prefix+'/resolve/'+meta['revision']+'/'+urllib.parse.quote(name),ROOT/p))
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as ex:
for result in ex.map(lambda args:download(*args),jobs):pass
for _,path in jobs:
h=hashlib.sha256()
with path.open('rb') as f:
for b in iter(lambda:f.read(1024*1024),b''):h.update(b)
assert h.hexdigest()==manifest['file_sha256'][str(path.relative_to(ROOT))],'Asset checksum mismatch: '+str(path)
(ASSETS/'asset_manifest.json').write_text(lock.read_text())
print(json.dumps({'status':'ASSETS_VERIFIED','files':len(jobs),'lock':str(lock)}),flush=True)
if __name__=='__main__':main()