AndroidFlux_RL_Train / androidflux_tools /export_repaired_interchange.py
Gyubeum's picture
Repair all four AndroidFlux contexts: history screens, readable actions and source references
198b302 verified
Raw History Blame Contribute Delete
2.65 kB
"""Convert a repaired AndroidFlux Parquet slice to af_recovery_prompt_v1.
Usage: python export_repaired_interchange.py DOWNLOADED_SLICE NEW_PACKAGE_DIR
No access to the original machine or source trajectories is required.
"""
import argparse
import hashlib
import json
from pathlib import Path
import pyarrow.parquet as pq
def export(source, output):
output.mkdir(parents=True,exist_ok=False)
(output/'images').mkdir()
rows=pq.read_table(source/'samples.parquet').to_pylist()
required=set(); records=[]
for row in rows:
k=row['generation_step_idx'];screens=row['screen_image_ids']
required.update(screens[max(0,k-4):])
records.append(dict(schema_version='af_recovery_prompt_v1',sample_id=row['sample_id'],
source=dict(dataset='Gyubeum/AndroidFlux_RL_Train',revision=None,domain=row['source_config'],
trajectory_id=row['trajectory_id'],checkpoint_step=k),goal=row['goal'],
history=[dict(step=i+1,summary=s) for i,s in enumerate(json.loads(row['qwen_history_entries_json']))],
screens=[f'images/{h}.png' if i>=max(0,k-4) else None for i,h in enumerate(screens)],
annotations={key:row[key] for key in ('reference_action_type','reference_action_json',
'reference_action_source','reference_action_semantics','reference_action_coordinate_space',
'reference_action_available','history_summary_complete','first_error_step_idx','source_model')}))
found=set()
for batch in pq.ParquetFile(source/'images.parquet').iter_batches(batch_size=8):
for im in batch.to_pylist():
h=im['image_id']
if h in required:
assert hashlib.sha256(im['encoded_bytes']).hexdigest()==h
(output/'images'/f'{h}.png').write_bytes(im['encoded_bytes']);found.add(h)
assert found==required
with (output/'samples.jsonl').open('w') as f:
for r in records:f.write(json.dumps(r,ensure_ascii=False)+'\n')
report=dict(samples=len(rows),images=len(found),source_slice=str(source),
source_samples_sha256=hashlib.sha256((source/'samples.parquet').read_bytes()).hexdigest(),
unavailable_reference=sum(not r['reference_action_available'] for r in rows),
incomplete_history=sum(not r['history_summary_complete'] for r in rows))
(output/'BUILD_REPORT.json').write_text(json.dumps(report,indent=2)+'\n')
return report
if __name__=='__main__':
ap=argparse.ArgumentParser();ap.add_argument('source',type=Path);ap.add_argument('output',type=Path)
args=ap.parse_args();print(json.dumps(export(args.source,args.output),indent=2))