Datasets:
File size: 2,507 Bytes
3b80a47 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #!/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()
|