Upload convert_openjev.py with huggingface_hub
Browse files- convert_openjev.py +145 -0
convert_openjev.py
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import hashlib
|
| 2 |
+
import json
|
| 3 |
+
from collections import Counter, defaultdict
|
| 4 |
+
from datasets import load_dataset
|
| 5 |
+
from alea.genbase import emit, q_choice, q_noul, q_score, validate_row, write_jsonl
|
| 6 |
+
|
| 7 |
+
CONFIGS = {
|
| 8 |
+
"amount-extraction-control-v1": 10000,
|
| 9 |
+
"citation-control-v1": 10000,
|
| 10 |
+
"entity-alignment-control-v1": 10000,
|
| 11 |
+
"email-selection-control-v1": 10000,
|
| 12 |
+
"phone-extraction-control-v1": 10000,
|
| 13 |
+
"context-retention-control-v1": 10000,
|
| 14 |
+
"silent-failure-control-v1": 10000,
|
| 15 |
+
"ir-control-v1": 10000,
|
| 16 |
+
"mailroom-control-v1": 12000,
|
| 17 |
+
"sponsor-segment-control-v1": 8000,
|
| 18 |
+
}
|
| 19 |
+
MAX_OPTIONS = 16
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def parse_json_field(value):
|
| 23 |
+
if not isinstance(value, str):
|
| 24 |
+
return value
|
| 25 |
+
try:
|
| 26 |
+
return json.loads(value)
|
| 27 |
+
except Exception:
|
| 28 |
+
return value
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def fingerprint(row):
|
| 32 |
+
raw = json.dumps(row["state"], sort_keys=True, ensure_ascii=False) + json.dumps(row["questions"], sort_keys=True, ensure_ascii=False)
|
| 33 |
+
return hashlib.sha256(raw.encode()).hexdigest()
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def convert(ex, config, idx, split):
|
| 37 |
+
state = parse_json_field(ex["state_json"])
|
| 38 |
+
if isinstance(state, str):
|
| 39 |
+
state = state.strip()
|
| 40 |
+
question = ex["question"]
|
| 41 |
+
options = ex["options"]
|
| 42 |
+
target = ex["target"]
|
| 43 |
+
kind = str(ex["kind"]).lower()
|
| 44 |
+
if not isinstance(options, list) or not isinstance(target, list):
|
| 45 |
+
return None
|
| 46 |
+
if len(options) < 2 or len(options) > MAX_OPTIONS or len(options) != len(target):
|
| 47 |
+
return None
|
| 48 |
+
if any(not isinstance(o, (str, dict, list)) for o in options):
|
| 49 |
+
return None
|
| 50 |
+
if kind == "choice":
|
| 51 |
+
q = q_choice("answer", question, options, [float(x) for x in target])
|
| 52 |
+
elif kind == "score":
|
| 53 |
+
q = q_score("score", question, options, [float(x) for x in target])
|
| 54 |
+
elif kind == "noul":
|
| 55 |
+
if len(target) != 2:
|
| 56 |
+
return None
|
| 57 |
+
q = q_noul("answer", question, float(target[1]))
|
| 58 |
+
elif kind in ("multilabel", "multi_label"):
|
| 59 |
+
qs = [q_noul(f"label_{i}",
|
| 60 |
+
f"Based on the state, {question} Is this criterion true: {opt}?",
|
| 61 |
+
float(target[i])) for i, opt in enumerate(options)]
|
| 62 |
+
row = emit(f"openjev_{config}", idx, state, qs)
|
| 63 |
+
row["id"] = str(ex.get("id", row["id"]))
|
| 64 |
+
row["provenance"] = {"dataset": "ZefanCai/Open-Jev", "config": config,
|
| 65 |
+
"group_id": ex.get("group_id"), "split": split,
|
| 66 |
+
"source": ex.get("source")}
|
| 67 |
+
validate_row(row)
|
| 68 |
+
return row
|
| 69 |
+
else:
|
| 70 |
+
return None
|
| 71 |
+
row = emit(f"openjev_{config}", idx, state, [q])
|
| 72 |
+
row["id"] = str(ex.get("id", row["id"]))
|
| 73 |
+
row["provenance"] = {"dataset": "ZefanCai/Open-Jev", "config": config,
|
| 74 |
+
"group_id": ex.get("group_id"), "split": split,
|
| 75 |
+
"source": ex.get("source")}
|
| 76 |
+
validate_row(row)
|
| 77 |
+
return row
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def load_split(split):
|
| 81 |
+
rows, seen, groups = [], set(), defaultdict(set)
|
| 82 |
+
stats = Counter()
|
| 83 |
+
next_idx = 0
|
| 84 |
+
for config, limit in CONFIGS.items():
|
| 85 |
+
ds = load_dataset("ZefanCai/Open-Jev", config, split=split, streaming=True)
|
| 86 |
+
cfg_rows = 0
|
| 87 |
+
for ex in ds:
|
| 88 |
+
stats["raw"] += 1
|
| 89 |
+
kind = str(ex.get("kind", "")).lower()
|
| 90 |
+
stats[f"kind:{kind}"] += 1
|
| 91 |
+
row = convert(ex, config, next_idx, split)
|
| 92 |
+
next_idx += 1
|
| 93 |
+
if row is None:
|
| 94 |
+
stats["unsupported_or_invalid"] += 1
|
| 95 |
+
continue
|
| 96 |
+
group = row["provenance"].get("group_id")
|
| 97 |
+
if group is not None:
|
| 98 |
+
groups[config].add(str(group))
|
| 99 |
+
key = fingerprint(row)
|
| 100 |
+
if key in seen:
|
| 101 |
+
stats["duplicate_input"] += 1
|
| 102 |
+
continue
|
| 103 |
+
seen.add(key)
|
| 104 |
+
rows.append(row)
|
| 105 |
+
cfg_rows += 1
|
| 106 |
+
if cfg_rows >= limit:
|
| 107 |
+
break
|
| 108 |
+
stats[f"kept:{config}"] = cfg_rows
|
| 109 |
+
return rows, groups, stats
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
def main():
|
| 113 |
+
train, train_groups, train_stats = load_split("train")
|
| 114 |
+
validation, val_groups, val_stats = load_split("validation")
|
| 115 |
+
overlap = {c: len(train_groups[c] & val_groups[c]) for c in CONFIGS}
|
| 116 |
+
if any(overlap.values()):
|
| 117 |
+
raise RuntimeError(f"group leakage across train/validation: {overlap}")
|
| 118 |
+
train_keys = {fingerprint(r) for r in train}
|
| 119 |
+
before = len(validation)
|
| 120 |
+
validation = [r for r in validation if fingerprint(r) not in train_keys]
|
| 121 |
+
write_jsonl(train, "data/openjev_train.jsonl")
|
| 122 |
+
write_jsonl(validation, "data/openjev_validation.jsonl")
|
| 123 |
+
report = {
|
| 124 |
+
"dataset": "ZefanCai/Open-Jev",
|
| 125 |
+
"configs": CONFIGS,
|
| 126 |
+
"used_splits": ["train", "validation"],
|
| 127 |
+
"excluded_splits": ["calibration", "test", "ood"],
|
| 128 |
+
"excluded_overlapping_configs": ["release-v2-redistributable", "browser-drone-expansion-v1-redistributable"],
|
| 129 |
+
"train_stats": dict(train_stats),
|
| 130 |
+
"validation_stats": dict(val_stats),
|
| 131 |
+
"groups_shared_by_config": overlap,
|
| 132 |
+
"validation_exact_duplicates_removed": before-len(validation),
|
| 133 |
+
"converted_train_rows": len(train),
|
| 134 |
+
"converted_validation_rows": len(validation),
|
| 135 |
+
"model_inputs": ["state", "question", "options"],
|
| 136 |
+
"privileged_metadata_excluded_from_model_input": ["metadata_json", "record_json", "target", "group_id", "split", "source"],
|
| 137 |
+
}
|
| 138 |
+
json.dump(report, open("data/openjev_conversion_report.json", "w"), indent=2)
|
| 139 |
+
print(json.dumps({"train": len(train), "validation": len(validation),
|
| 140 |
+
"overlap": overlap, "train_kinds": dict(train_stats),
|
| 141 |
+
"dev_kinds": dict(val_stats)}, indent=2))
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
if __name__ == "__main__":
|
| 145 |
+
main()
|