#!/usr/bin/env python3 from __future__ import annotations import argparse import json from pathlib import Path import pyarrow.parquet as pq import polars as pl from datasets import Dataset, Features, Image, Sequence, Value GMAIL_COLUMNS = [ "id", "domain", "episode_id", "step_id", "split", "instruction", "current_activity", "image", "target_json", "action_text", "ex_action_type", "touch_x", "touch_y", "lift_x", "lift_y", "image_height", "image_width", ] INDEX_COLUMNS = [ "domain", "split", "episode_id", "step_id", "package", "activity", "app_label", "label_source", "goal_info", "action_text", "action_family", "ex_action_type", "touch_x", "touch_y", "lift_x", "lift_y", "image_height", "image_width", "tar_member", ] def _parse_target(target_json: str) -> tuple[str, list[int], list[int]]: try: obj = json.loads(target_json) args = obj.get("arguments", {}) action = str(args.get("action", "unknown")) coord = args.get("coordinate") or [] coord2 = args.get("coordinate2") or [] return action, [int(x) for x in coord], [int(x) for x in coord2] except Exception: return "unknown", [], [] def build_gmail_examples(root: Path, out_dir: Path, n: int) -> None: src = root / "parquet/aitw_gmail_google_apps_train.parquet" table = pq.ParquetFile(src).read_row_group(0, columns=GMAIL_COLUMNS) rows = [] for row in table.slice(0, min(n, table.num_rows)).to_pylist(): action, coord, coord2 = _parse_target(row["target_json"] or "") rows.append( { "id": row["id"], "domain": row["domain"], "split": row["split"], "episode_id": row["episode_id"], "step_id": int(row["step_id"]), "instruction": row["instruction"], "current_activity": row["current_activity"], "image": {"bytes": row["image"], "path": None}, "target_json": row["target_json"], "action": action, "coordinate": coord, "coordinate2": coord2, "action_text": row["action_text"], "ex_action_type": int(row["ex_action_type"]) if row["ex_action_type"] is not None else -1, "touch_x": float(row["touch_x"]) if row["touch_x"] is not None else None, "touch_y": float(row["touch_y"]) if row["touch_y"] is not None else None, "lift_x": float(row["lift_x"]) if row["lift_x"] is not None else None, "lift_y": float(row["lift_y"]) if row["lift_y"] is not None else None, "image_height": int(row["image_height"]), "image_width": int(row["image_width"]), } ) features = Features( { "id": Value("string"), "domain": Value("string"), "split": Value("string"), "episode_id": Value("string"), "step_id": Value("int64"), "instruction": Value("string"), "current_activity": Value("string"), "image": Image(), "target_json": Value("string"), "action": Value("string"), "coordinate": Sequence(Value("int64")), "coordinate2": Sequence(Value("int64")), "action_text": Value("string"), "ex_action_type": Value("int64"), "touch_x": Value("float64"), "touch_y": Value("float64"), "lift_x": Value("float64"), "lift_y": Value("float64"), "image_height": Value("int64"), "image_width": Value("int64"), } ) ds = Dataset.from_list(rows, features=features) ds.to_parquet(out_dir / "gmail_mobile_use_examples.parquet") def build_app_index_sample(root: Path, out_dir: Path, n_per_app: int) -> None: src = root / "app_index/aitw_app_index.parquet" apps = [ "Gmail", "Chrome", "Android Settings", "Google Calendar", "Google Maps", "Clock", "Google Play Store", "Google Photos", ] app_order = {app: i for i, app in enumerate(apps)} out = ( pl.scan_parquet(str(src)) .select(INDEX_COLUMNS) .filter(pl.col("app_label").is_in(apps)) .with_columns( pl.col("app_label").cum_count().over("app_label").alias("_rank"), pl.col("app_label") .replace_strict(app_order, default=len(apps), return_dtype=pl.Int64) .alias("_app_order"), ) .filter(pl.col("_rank") <= n_per_app) .sort(["_app_order", "episode_id", "step_id"]) .drop(["_rank", "_app_order"]) .collect(engine="streaming") ) out.write_parquet(out_dir / "app_index_sample.parquet") def main() -> None: p = argparse.ArgumentParser() p.add_argument("--root", default="/data/gmail_aitw") p.add_argument("--out-dir", default="/data/gmail_aitw/hf_upload/aitw_processed_labeled_full/viewer_examples") p.add_argument("--gmail-examples", type=int, default=16) p.add_argument("--index-examples-per-app", type=int, default=5) args = p.parse_args() root = Path(args.root) out_dir = Path(args.out_dir) out_dir.mkdir(parents=True, exist_ok=True) build_gmail_examples(root, out_dir, args.gmail_examples) build_app_index_sample(root, out_dir, args.index_examples_per_app) print(out_dir) if __name__ == "__main__": main()