Download level3/scripts/materialize_level3.py from PhoebeCC/RoboSteer-Preprocessing: direct link, hf CLI and curl.
- Browser
- Download file 4.96 kB
-
https://huggingface.co/datasets/PhoebeCC/RoboSteer-Preprocessing/resolve/main/level3/scripts/materialize_level3.py
- Command line
-
hf download hf://datasets/PhoebeCC/RoboSteer-Preprocessing/level3/scripts/materialize_level3.py
-
curl -L -o materialize_level3.py https://huggingface.co/datasets/PhoebeCC/RoboSteer-Preprocessing/resolve/main/level3/scripts/materialize_level3.py
4.96 kB
| #!/usr/bin/env python3 | |
| """Restore or verify the three released Level3 assets. Requires pyarrow.""" | |
| import argparse | |
| import hashlib | |
| import json | |
| import os | |
| import shutil | |
| from pathlib import Path | |
| import pyarrow.parquet as pq | |
| def sha(path): | |
| h=hashlib.sha256() | |
| with path.open('rb') as f: | |
| for b in iter(lambda:f.read(4*1024*1024),b''):h.update(b) | |
| return h.hexdigest() | |
| def under(root, relative): | |
| p=Path(relative) | |
| if p.is_absolute() or '..' in p.parts:raise ValueError('Unsafe relative path: '+str(p)) | |
| return root/p | |
| def main(): | |
| p=argparse.ArgumentParser(description=__doc__) | |
| p.add_argument('--release-root',type=Path,default=Path(__file__).resolve().parents[1]) | |
| p.add_argument('--public-root',type=Path,required=True,help='Directory containing Data_Processed/Level3/Video_Slices') | |
| p.add_argument('--output-root',type=Path,required=True) | |
| p.add_argument('--video-mode',choices=['symlink','copy'],default='symlink') | |
| p.add_argument('--verify-only',action='store_true') | |
| a=p.parse_args();r=a.release_root.resolve();out=a.output_root.resolve();public=a.public_root.resolve() | |
| for line in (r/'SHA256SUMS').read_text().splitlines(): | |
| expected,name=line.split(' ',1) | |
| if sha(under(r,name))!=expected:raise SystemExit('Release checksum mismatch: '+name) | |
| texts=pq.read_table(r/'text_audio_instructions/data.parquet').to_pylist() | |
| videos=pq.read_table(r/'video_symlinks/data.parquet').to_pylist() | |
| items=pq.read_table(r/'interleaved_input_list_for_GEM/items.parquet').to_pylist() | |
| source_text={x['item_id']:x['text'].encode('utf-8') for x in texts} | |
| gemtext=dict(source_text) | |
| overrides=r/'interleaved_input_list_for_GEM/text_overrides.parquet' | |
| if overrides.exists():gemtext.update({x['item_id']:x['text'].encode('utf-8') for x in pq.read_table(overrides).to_pylist()}) | |
| video_by_id={x['item_id']:x for x in videos};bad=[] | |
| for x in videos: | |
| path=under(public,x['source_relpath']) | |
| if not path.is_file():bad.append({'item_id':x['item_id'],'reason':'missing','path':x['source_relpath']}) | |
| elif path.stat().st_size!=x['source_size'] or sha(path)!=x['source_sha256']: | |
| bad.append({'item_id':x['item_id'],'reason':'size_or_sha256_mismatch','path':x['source_relpath']}) | |
| if bad: | |
| print(json.dumps({'ok':False,'invalid_source_videos':bad},ensure_ascii=False,indent=2)) | |
| raise SystemExit(2) | |
| # Complete preflight before creating output. Never overwrite different existing content. | |
| plans=[] | |
| for x in texts: | |
| b=source_text[x['item_id']] | |
| if hashlib.sha256(b).hexdigest()!=x['sha256']:raise ValueError('Text checksum mismatch') | |
| plans.append((under(out/'text_audio_instructions',x['file_name']),b,None)) | |
| for x in videos:plans.append((under(out/'video_symlinks',x['file_name']),None,under(public,x['source_relpath']))) | |
| for x in items: | |
| dest=under(out/'interleaved_input_list_for_GEM',x['file_name']) | |
| if x['kind']=='text': | |
| b=gemtext[x['item_id']] | |
| if hashlib.sha256(b).hexdigest()!=x['sha256']:raise ValueError('GEM text checksum mismatch') | |
| plans.append((dest,b,None)) | |
| else: | |
| v=video_by_id[x['item_id']] | |
| if v['source_relpath']!=x['source_relpath'] or v['source_sha256']!=x['sha256']:raise ValueError('Video reference mismatch') | |
| plans.append((dest,None,under(public,v['source_relpath']))) | |
| failures=[] | |
| for dest,b,source in plans: | |
| if dest.exists() or dest.is_symlink(): | |
| valid=(not dest.is_symlink() and dest.is_file() and dest.read_bytes()==b) if b is not None else ((dest.is_symlink() and dest.resolve()==source.resolve() and dest.is_file()) or (not dest.is_symlink() and dest.is_file() and sha(dest)==sha(source))) | |
| if not valid:failures.append(str(dest.relative_to(out))) | |
| elif a.verify_only:failures.append(str(dest.relative_to(out))) | |
| # Existing symlink parents must not route output outside output-root. | |
| if out not in dest.parent.resolve().parents and dest.parent.resolve()!=out:raise ValueError('Output parent escapes output-root') | |
| if failures: | |
| print(json.dumps({'ok':False,'missing_or_conflicting_outputs':failures},indent=2));raise SystemExit(3) | |
| if not a.verify_only: | |
| for dest,b,source in plans: | |
| if dest.exists() or dest.is_symlink():continue | |
| dest.parent.mkdir(parents=True,exist_ok=True) | |
| if b is not None: | |
| with dest.open('xb') as f:f.write(b) | |
| elif a.video_mode=='copy':shutil.copyfile(source,dest) | |
| else:dest.symlink_to(source) | |
| print(json.dumps({'ok':True,'mode':'verify' if a.verify_only else 'materialize','tasks':len({x['task_id'] for x in items}),'grouped_texts':len(texts),'videos':len(videos),'gem_items':len(items),'expected_output_entries':len(plans),'source_video_sha256':'passed'},indent=2)) | |
| if __name__=='__main__':main() | |