#!/usr/bin/env python3 # Periodically persist caption and telemetry state while the GPU worker runs. import hashlib,json,os,time from pathlib import Path from huggingface_hub import HfApi,CommitOperationAdd R=Path('/marimo/disef_cars');G=R/'runs/generation_seed_1';CACHE=R/'cache/cars_llava_captions_seed1.json' api=HfApi(token=os.environ.get('HF_TOKEN'));repo='DanielNguyen121/DisEF-datasets';prior=None while True: try: s=json.loads((G/'state/cars_disef_state.json').read_text());run=s['run_id'];ops=[] if CACHE.exists(): digest=hashlib.sha256(CACHE.read_bytes()).hexdigest() if digest!=prior: ops.append(CommitOperationAdd(path_in_repo=f'cars/_progress/{run}/current/cars_llava_captions_seed1.json',path_or_fileobj=str(CACHE))) metric=G/'metadata/runtime_metrics.csv';heartbeat=G/'state/heartbeat.json' for p in [metric,heartbeat]: if p.exists():ops.append(CommitOperationAdd(path_in_repo=f'cars/_progress/{run}/current/{p.name}',path_or_fileobj=str(p))) if ops: c=api.create_commit(repo_id=repo,repo_type='dataset',operations=ops,commit_message=f'Cars generation telemetry and caption cache: {s.get("completed",0)}/12544') if CACHE.exists():prior=hashlib.sha256(CACHE.read_bytes()).hexdigest() print('SYNC',c.oid,'captions',len(json.loads(CACHE.read_text())) if CACHE.exists() else 0,'images',s.get('completed',0),flush=True) if s.get('status') in ['COMPLETED','PARTIAL','BLOCKED','STOPPED'] and s.get('completed',0)==12544:break except Exception as e:print('SYNC_DEFERRED',type(e).__name__,str(e)[:150],flush=True) time.sleep(300)