FWeindel's picture
Rename v2 selection assets folders to metadata
4b7635e verified
Raw History Blame
2.18 kB
"""Reproduce the frozen v2 selection from the unchanged, pinned v1 payloads."""
import argparse,json,hashlib
from pathlib import Path
import pyarrow as pa,pyarrow.parquet as pq
from huggingface_hub import hf_hub_download
p=argparse.ArgumentParser(description=__doc__);p.add_argument('--out',type=Path,required=True);p.add_argument('--source',type=Path);a=p.parse_args();a.out.mkdir(parents=True,exist_ok=True)
assert not (a.out/'tasks.parquet').exists()
m=json.loads((Path(__file__).parent/'metadata/selection.json').read_text());f=a.source or Path(hf_hub_download(m['repo_id'],m['source_folder']+'/tasks.parquet',revision=m['source_revision'],repo_type='dataset',cache_dir=a.out/'.source-cache'));assert hashlib.sha256(f.read_bytes()).hexdigest()==m['source_sha256']
src=pq.ParquetFile(f);schema=src.schema_arrow.remove_metadata();columns=[model+'_'+metric for model in m['models'] for metric in ['successes','graded_attempts','missing_attempts']]
for name in columns+['target_attempts_per_model']:schema=schema.append(pa.field(name,pa.int64()))
rows=[]
for batch in src.iter_batches(batch_size=32):
for r in batch.to_pylist():
t=m['tasks'][r['path']];assert r['content_sha256']==t['content_sha256']
if not t['kept']:continue
for k in columns:r[k]=t[k]
r['target_attempts_per_model']=4;rows.append(r)
assert len(rows)==m['summary']['solved'];names={r['path'] for r in rows};im=json.loads(src.schema_arrow.metadata[b'ot.images.v1']);im['tasks']={k:v for k,v in im['tasks'].items() if k in names};bundles={e['bundle'] for v in im['tasks'].values() for e in v['environments']};im['bundles']={k:v for k,v in im['bundles'].items() if k in bundles}
if 'uncached_task_ids' in im:im['uncached_task_ids']=sorted(names-set(im['tasks']));im['coverage']={'cached_tasks':len(im['tasks']),'total_tasks':len(names)}
else:assert set(im['tasks'])==names
meta={b'ot.images.v1':json.dumps(im).encode(),b'ot.release.v2':json.dumps({k:v for k,v in m.items() if k!='tasks'}).encode()};pq.write_table(pa.Table.from_pylist(rows,schema=schema).replace_schema_metadata(meta),a.out/'tasks.parquet',compression='zstd');print('Retained',len(rows),'cached tasks',len(im['tasks']),'bundles',len(bundles))