test_1 / scripts /publish_bundle.py
Loasaster's picture
Prepare ViCo test43 inference code and verified baseline assets
36b6923 verified
Raw History Blame Contribute Delete
4.56 kB
"""Add code/assets to the explicitly selected HF dataset, without deleting files.
Use a separately authenticated packaging environment. Credentials are never
read from this directory or written to commit receipts.
"""
import argparse
import hashlib
import json
from pathlib import Path
import time
import socket
from huggingface_hub import HfApi,set_client_factory
import httpx2
ROOT=Path(__file__).resolve().parents[1]
REPO='Loasaster/test_1'
def sha(path):
h=hashlib.sha256()
with path.open('rb') as f:
for b in iter(lambda:f.read(8*1024*1024),b''):h.update(b)
return h.hexdigest()
def inventory(api):
old=ROOT/'manifests/WEIGHTS.json'
cached={p['path']:p for p in json.loads(old.read_text())['files']} if old.is_file() else {}
base=json.loads((ROOT/'provenance/HUB_BASE_WEIGHTS_COMMIT.json').read_text())['files']
remote={} if all(p in cached for p in base) else {p.path:p for p in api.list_repo_tree(REPO,repo_type='dataset',recursive=True) if hasattr(p,'size')}
items={}
# These server-side copies were checked against source LFS object hashes.
for path in base:
if path in cached:items[path]=cached[path];continue
p=remote[path]
if p.lfs:digest=p.lfs.sha256
else:
import requests
for attempt in range(8):
try:
r=requests.get(f'https://huggingface.co/datasets/{REPO}/raw/main/{path}',timeout=(12,60))
r.raise_for_status();digest=hashlib.sha256(r.content).hexdigest();break
except Exception:
if attempt==7:raise
time.sleep(2)
items[path]=dict(path=path,bytes=p.size,sha256=digest)
for path in (ROOT/'weights').rglob('*'):
if path.is_file() and not path.name.endswith(('.part','.lock')):
key=str(path.relative_to(ROOT));items[key]=dict(path=key,bytes=path.stat().st_size,sha256=sha(path))
required=['weights/dystream/last.ckpt','weights/dystream/renderer.ckpt','weights/gpavatar/resources.tar']
required.extend(json.loads((ROOT/'provenance/TREVOR_EXTRACTED.json').read_text())['files'][i]['path'] for i in range(4))
large=ROOT/'provenance/DUALTALK_WAV2VEC_CONFIG.json'
if large.is_file():required.extend(r['path'] for r in json.loads(large.read_text())['files'])
else:required.append('weights/dualtalk/wav2vec2-large-960h-lv60-self/config.json')
required.append('weights/gpavatar/mask2former-swin-tiny-coco-instance/config.json')
missing=[p for p in required if p not in items]
complete=not missing
(ROOT/'manifests/WEIGHTS.json').write_text(json.dumps(dict(complete=complete,files=[items[p] for p in sorted(items)]),indent=2)+'\n')
(ROOT/'manifests/WEIGHTS_STATUS.json').write_text(json.dumps(dict(
repository=REPO,complete=False,asset_inventory_complete=complete,
phase='ready_for_upload' if complete else 'assembling',
actual_files=len(items),bytes=sum(v['bytes'] for v in items.values()),missing=missing,
source_code_pinned=True,model_inference_started=False,target_gpu_validation=False,
scope='ViCo paper test43 only',updated_at=time.time()),indent=2)+'\n')
return items,missing
def main():
parser=argparse.ArgumentParser();parser.add_argument('--code-only',action='store_true');args=parser.parse_args()
# Process-local TCP settings and connection retries. No system routing change.
set_client_factory(lambda:httpx2.Client(follow_redirects=True,timeout=120,
transport=httpx2.HTTPTransport(retries=8,socket_options=[(socket.IPPROTO_TCP,socket.TCP_MAXSEG,1200)])))
api=HfApi();items,missing=inventory(api)
excludes=['.cache/**','**/.cache/**','**/__pycache__/**','**/*.pyc','**/*.part','**/*.lock','.env','outputs/**']
if args.code_only:excludes.append('weights/**')
for attempt in range(8):
try:
result=api.upload_folder(repo_id=REPO,repo_type='dataset',folder_path=ROOT,
ignore_patterns=excludes,commit_message='Prepare ViCo test43 inference code and verified baseline assets')
break
except (httpx2.TransportError,TimeoutError,ConnectionError):
if attempt==7:raise
print('Retry interrupted upload',attempt+1,flush=True);time.sleep(3)
receipt=dict(commit=result.oid,code_only=args.code_only,missing=missing,at=time.time())
(ROOT/'provenance/LATEST_UPLOAD.json').write_text(json.dumps(receipt,indent=2)+'\n')
print(json.dumps(receipt),flush=True)
if __name__=='__main__':main()