Upload convert_rlcd.py with huggingface_hub
Browse files- convert_rlcd.py +63 -0
convert_rlcd.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import hashlib, json, random
|
| 2 |
+
from huggingface_hub import hf_hub_download
|
| 3 |
+
|
| 4 |
+
random.seed(42)
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def conv(path, cap):
|
| 8 |
+
rows = []
|
| 9 |
+
for line in open(path):
|
| 10 |
+
r = json.loads(line)
|
| 11 |
+
qs, tgts = [], []
|
| 12 |
+
for qid, q in r["questions"].items():
|
| 13 |
+
t = q["type"]
|
| 14 |
+
if t == "noul":
|
| 15 |
+
p = float(q["reference"])
|
| 16 |
+
if not 0.0 <= p <= 1.0:
|
| 17 |
+
continue
|
| 18 |
+
qs.append({"kind": "noul", "options": ["false", "true"],
|
| 19 |
+
"text": q["instructions"], "unknown_allowed": False})
|
| 20 |
+
tgts.append([1.0 - p, p])
|
| 21 |
+
elif t == "choice":
|
| 22 |
+
crit = q["criteria"]
|
| 23 |
+
keys = list(crit.keys())
|
| 24 |
+
ref = q["reference"]
|
| 25 |
+
if any(k not in ref for k in keys) or len(keys) < 2:
|
| 26 |
+
continue
|
| 27 |
+
qs.append({"kind": "choice", "options": [f"{k}: {crit[k]}" for k in keys],
|
| 28 |
+
"text": q["instructions"], "unknown_allowed": False})
|
| 29 |
+
tgts.append([float(ref[k]) for k in keys])
|
| 30 |
+
elif t == "score":
|
| 31 |
+
crit, ref = q["criteria"], q["reference"]
|
| 32 |
+
if len(crit) != len(ref):
|
| 33 |
+
continue
|
| 34 |
+
qs.append({"kind": "score", "options": [f"{i} - {c}" for i, c in enumerate(crit)],
|
| 35 |
+
"text": q["instructions"], "unknown_allowed": False})
|
| 36 |
+
tgts.append([float(x) for x in ref])
|
| 37 |
+
if not qs:
|
| 38 |
+
continue
|
| 39 |
+
st = r["state"] if isinstance(r["state"], str) else json.dumps(r["state"], sort_keys=True)
|
| 40 |
+
gid = hashlib.sha256(st.encode()).hexdigest()[:16]
|
| 41 |
+
rows.append({"state": st, "id": f"rlcd:{r.get('domain','?')}:{gid}:{len(rows)}",
|
| 42 |
+
"questions": qs, "targets": tgts,
|
| 43 |
+
"source": f"rlcd:{r.get('domain','?')}", "group_id": f"rlcd:{gid}"})
|
| 44 |
+
if len(rows) >= cap:
|
| 45 |
+
break
|
| 46 |
+
return rows
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
tr = conv(hf_hub_download("soyrsoyr/openjev-rlcd-v0", "train.jsonl", repo_type="dataset"), 26000)
|
| 50 |
+
dv = conv(hf_hub_download("soyrsoyr/openjev-rlcd-v0", "val.jsonl", repo_type="dataset"), 4000)
|
| 51 |
+
trg = {r["group_id"] for r in tr}
|
| 52 |
+
dv = [r for r in dv if r["group_id"] not in trg]
|
| 53 |
+
random.shuffle(tr)
|
| 54 |
+
with open("/root/alea/data/rlcd_train.jsonl", "w") as f:
|
| 55 |
+
for r in tr:
|
| 56 |
+
f.write(json.dumps(r) + "\n")
|
| 57 |
+
with open("/root/alea/data/rlcd_dev.jsonl", "w") as f:
|
| 58 |
+
for r in dv:
|
| 59 |
+
f.write(json.dumps(r) + "\n")
|
| 60 |
+
print({"train": len(tr), "dev": len(dv),
|
| 61 |
+
"train_qs": sum(len(r["questions"]) for r in tr),
|
| 62 |
+
"kinds": {k: sum(1 for r in tr for q in r["questions"] if q["kind"] == k)
|
| 63 |
+
for k in ("choice", "noul", "score")}})
|