"""Rebuild the verified AraSTEM configs from the public source file on GitHub. Usage: python apply_filter.py [output_dir] Writes one parquet file per config, /_.parquet. """ import hashlib import json import os import sys import urllib.request import pandas as pd from huggingface_hub import hf_hub_download SOURCE = "AI-DSCHubAUB/AraSTEM-Dataset" SOURCE_REVISION = "d8f8fdd4e420df45737df1105a60a34b9b4aecd2" SOURCE_URL = f"https://raw.githubusercontent.com/{SOURCE}/{SOURCE_REVISION}/AraSTEMv5.json" THIS_REPO = "IFM/AraSTEM_verified" COLUMNS = ["question", "option_0", "option_1", "option_2", "option_3", "option_4", "correct_option", "level", "subject", "resource"] EXPECTED_KEPT = { "college_biology": 567, "college_chemistry": 292, "college_dentistry": 603, "college_information_technology": 331, "college_medicine": 690, "college_pharmacy": 408, "college_physics": 133, "primary_math": 2774, "primary_science": 3802, "secondary_biology": 313, "secondary_chemistry": 228, "secondary_math": 81, "secondary_physics": 173, "secondary_science": 99, } def fingerprint(row): return hashlib.sha256("\x1f".join(str(row[c]) for c in COLUMNS).encode()).hexdigest()[:16] ids = pd.read_csv(hf_hub_download(THIS_REPO, "ids.csv", repo_type="dataset")) with urllib.request.urlopen(SOURCE_URL) as f: source = pd.DataFrame(json.load(f), columns=COLUMNS) assert len(source) == len(ids), f"expected {len(ids)} source rows, got {len(source)}" mismatch = [i for i, row in source.iterrows() if fingerprint(row) != ids.at[i, "fingerprint"]] assert not mismatch, f"source rows differ from the audited version: {mismatch[:10]}" config = (source["level"] + "_" + source["subject"]).str.replace(" ", "_") assert config.equals(ids["config"]), "config labels differ from ids.csv" kept = ids["status"].eq("kept").to_numpy() out = sys.argv[1] if len(sys.argv) > 1 else "arastem_verified" os.makedirs(out, exist_ok=True) for name, expected in EXPECTED_KEPT.items(): verified = source[kept & config.eq(name).to_numpy()].reset_index(drop=True) assert len(verified) == expected, f"{name}: expected {expected} rows, got {len(verified)}" verified.to_parquet(os.path.join(out, f"{name}.parquet")) total = sum(EXPECTED_KEPT.values()) assert total == int(kept.sum()) == 10494 print(f"Wrote {total} rows in {len(EXPECTED_KEPT)} configs to {out}/")