Datasets:
Download scripts/prepare.py from psychemme/PsycheMME-Training: direct link, hf CLI and curl.
- Browser
- Download file 2.51 kB
-
https://huggingface.co/datasets/psychemme/PsycheMME-Training/resolve/main/scripts/prepare.py
- Command line
-
hf download hf://datasets/psychemme/PsycheMME-Training/scripts/prepare.py
-
curl -L -o prepare.py https://huggingface.co/datasets/psychemme/PsycheMME-Training/resolve/main/scripts/prepare.py
2.51 kB
| #!/usr/bin/env python3 | |
| """Extract the training frames and prepare local SFT/GRPO input paths.""" | |
| from pathlib import Path | |
| import argparse,json,tarfile | |
| def read(path): | |
| return [json.loads(line) for line in path.read_text().splitlines() if line.strip()] | |
| def main(): | |
| parser=argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument('--root',type=Path,default=Path(__file__).resolve().parents[1]) | |
| parser.add_argument('--output',type=Path,default=None) | |
| args=parser.parse_args();root=args.root.resolve();out=(args.output or root/'prepared').resolve() | |
| sft=read(root/'sft/train.jsonl');grpo=read(root/'grpo/train.jsonl') | |
| if len(sft)!=3657 or len(grpo)!=2842:raise ValueError('Unexpected training population') | |
| sids={r['id'] for r in sft};gids={r['id'] for r in grpo} | |
| if len(sids)!=len(sft) or len(gids)!=len(grpo) or not gids<=sids:raise ValueError('Question ID mismatch') | |
| frames={p for row in sft for p in row['images']} | |
| if len(frames)!=61152 or any(len(r['images'])!=32 for r in sft+grpo):raise ValueError('Unexpected frame population') | |
| out.mkdir(parents=True,exist_ok=True) | |
| with tarfile.open(root/'media/frames.tar','r:') as archive: | |
| members=archive.getmembers();files=[m for m in members if m.isfile()] | |
| if len(files)!=len(frames) or {m.name for m in files}!=frames:raise ValueError('Frame archive population mismatch') | |
| if any(not m.isfile() or not m.name.startswith('media/frames/') or not (out/m.name).resolve().is_relative_to(out) for m in members):raise ValueError('Unexpected frame archive member') | |
| missing=[m for m in files if not (out/m.name).is_file() or (out/m.name).stat().st_size!=m.size] | |
| if missing: | |
| archive.extractall(out,members=missing,filter='data') | |
| for name,rows in [('sft',sft),('grpo',grpo)]: | |
| output=[] | |
| for row in rows: | |
| new=dict(row);new['images']=[str((out/p).resolve()) for p in row['images']] | |
| if any(not Path(p).is_file() for p in new['images']):raise FileNotFoundError(row['id']) | |
| if 'path' in row:new['path']=str((root/row['path']).resolve()) | |
| output.append(new) | |
| target=out/(name+'.jsonl');temporary=target.with_suffix('.jsonl.tmp') | |
| temporary.write_text(''.join(json.dumps(r,ensure_ascii=False)+'\n' for r in output));temporary.replace(target) | |
| print(json.dumps({'sft':len(sft),'grpo_candidates':len(grpo),'frames':len(frames),'output':str(out)},indent=2)) | |
| if __name__=='__main__':main() | |