Download convert_openjev.py from Akahsizrr/alea-runs: direct link, hf CLI and curl.
- Browser
- Download file 5.66 kB
-
https://huggingface.co/datasets/Akahsizrr/alea-runs/resolve/main/convert_openjev.py
- Command line
-
hf download hf://datasets/Akahsizrr/alea-runs/convert_openjev.py
-
curl -L -o convert_openjev.py https://huggingface.co/datasets/Akahsizrr/alea-runs/resolve/main/convert_openjev.py
5.66 kB
| import hashlib | |
| import json | |
| from collections import Counter, defaultdict | |
| from datasets import load_dataset | |
| from alea.genbase import emit, q_choice, q_noul, q_score, validate_row, write_jsonl | |
| CONFIGS = { | |
| "amount-extraction-control-v1": 10000, | |
| "citation-control-v1": 10000, | |
| "entity-alignment-control-v1": 10000, | |
| "email-selection-control-v1": 10000, | |
| "phone-extraction-control-v1": 10000, | |
| "context-retention-control-v1": 10000, | |
| "silent-failure-control-v1": 10000, | |
| "ir-control-v1": 10000, | |
| "mailroom-control-v1": 12000, | |
| "sponsor-segment-control-v1": 8000, | |
| } | |
| MAX_OPTIONS = 16 | |
| def parse_json_field(value): | |
| if not isinstance(value, str): | |
| return value | |
| try: | |
| return json.loads(value) | |
| except Exception: | |
| return value | |
| def fingerprint(row): | |
| raw = json.dumps(row["state"], sort_keys=True, ensure_ascii=False) + json.dumps(row["questions"], sort_keys=True, ensure_ascii=False) | |
| return hashlib.sha256(raw.encode()).hexdigest() | |
| def convert(ex, config, idx, split): | |
| state = parse_json_field(ex["state_json"]) | |
| if isinstance(state, str): | |
| state = state.strip() | |
| question = ex["question"] | |
| options = ex["options"] | |
| target = ex["target"] | |
| kind = str(ex["kind"]).lower() | |
| if not isinstance(options, list) or not isinstance(target, list): | |
| return None | |
| if len(options) < 2 or len(options) > MAX_OPTIONS or len(options) != len(target): | |
| return None | |
| if any(not isinstance(o, (str, dict, list)) for o in options): | |
| return None | |
| if kind == "choice": | |
| q = q_choice("answer", question, options, [float(x) for x in target]) | |
| elif kind == "score": | |
| q = q_score("score", question, options, [float(x) for x in target]) | |
| elif kind == "noul": | |
| if len(target) != 2: | |
| return None | |
| q = q_noul("answer", question, float(target[1])) | |
| elif kind in ("multilabel", "multi_label"): | |
| qs = [q_noul(f"label_{i}", | |
| f"Based on the state, {question} Is this criterion true: {opt}?", | |
| float(target[i])) for i, opt in enumerate(options)] | |
| row = emit(f"openjev_{config}", idx, state, qs) | |
| row["id"] = str(ex.get("id", row["id"])) | |
| row["provenance"] = {"dataset": "ZefanCai/Open-Jev", "config": config, | |
| "group_id": ex.get("group_id"), "split": split, | |
| "source": ex.get("source")} | |
| validate_row(row) | |
| return row | |
| else: | |
| return None | |
| row = emit(f"openjev_{config}", idx, state, [q]) | |
| row["id"] = str(ex.get("id", row["id"])) | |
| row["provenance"] = {"dataset": "ZefanCai/Open-Jev", "config": config, | |
| "group_id": ex.get("group_id"), "split": split, | |
| "source": ex.get("source")} | |
| validate_row(row) | |
| return row | |
| def load_split(split): | |
| rows, seen, groups = [], set(), defaultdict(set) | |
| stats = Counter() | |
| next_idx = 0 | |
| for config, limit in CONFIGS.items(): | |
| ds = load_dataset("ZefanCai/Open-Jev", config, split=split, streaming=True) | |
| cfg_rows = 0 | |
| for ex in ds: | |
| stats["raw"] += 1 | |
| kind = str(ex.get("kind", "")).lower() | |
| stats[f"kind:{kind}"] += 1 | |
| row = convert(ex, config, next_idx, split) | |
| next_idx += 1 | |
| if row is None: | |
| stats["unsupported_or_invalid"] += 1 | |
| continue | |
| group = row["provenance"].get("group_id") | |
| if group is not None: | |
| groups[config].add(str(group)) | |
| key = fingerprint(row) | |
| if key in seen: | |
| stats["duplicate_input"] += 1 | |
| continue | |
| seen.add(key) | |
| rows.append(row) | |
| cfg_rows += 1 | |
| if cfg_rows >= limit: | |
| break | |
| stats[f"kept:{config}"] = cfg_rows | |
| return rows, groups, stats | |
| def main(): | |
| train, train_groups, train_stats = load_split("train") | |
| validation, val_groups, val_stats = load_split("validation") | |
| overlap = {c: len(train_groups[c] & val_groups[c]) for c in CONFIGS} | |
| if any(overlap.values()): | |
| raise RuntimeError(f"group leakage across train/validation: {overlap}") | |
| train_keys = {fingerprint(r) for r in train} | |
| before = len(validation) | |
| validation = [r for r in validation if fingerprint(r) not in train_keys] | |
| write_jsonl(train, "data/openjev_train.jsonl") | |
| write_jsonl(validation, "data/openjev_validation.jsonl") | |
| report = { | |
| "dataset": "ZefanCai/Open-Jev", | |
| "configs": CONFIGS, | |
| "used_splits": ["train", "validation"], | |
| "excluded_splits": ["calibration", "test", "ood"], | |
| "excluded_overlapping_configs": ["release-v2-redistributable", "browser-drone-expansion-v1-redistributable"], | |
| "train_stats": dict(train_stats), | |
| "validation_stats": dict(val_stats), | |
| "groups_shared_by_config": overlap, | |
| "validation_exact_duplicates_removed": before-len(validation), | |
| "converted_train_rows": len(train), | |
| "converted_validation_rows": len(validation), | |
| "model_inputs": ["state", "question", "options"], | |
| "privileged_metadata_excluded_from_model_input": ["metadata_json", "record_json", "target", "group_id", "split", "source"], | |
| } | |
| json.dump(report, open("data/openjev_conversion_report.json", "w"), indent=2) | |
| print(json.dumps({"train": len(train), "validation": len(validation), | |
| "overlap": overlap, "train_kinds": dict(train_stats), | |
| "dev_kinds": dict(val_stats)}, indent=2)) | |
| if __name__ == "__main__": | |
| main() | |