Upload alea/gen_hf6.py with huggingface_hub
Browse files- alea/gen_hf6.py +78 -0
alea/gen_hf6.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""gen_hf6 — LegalBench contract/policy tasks -> Alea rows.
|
| 2 |
+
|
| 3 |
+
contract_nli_*: real contract clause + hypothesis -> yes/no (noul).
|
| 4 |
+
cuad_*: clause text -> does it contain provision type X (noul) and
|
| 5 |
+
clause-type routing (choice).
|
| 6 |
+
Small per-config but real legal text — complements synthetic policy data.
|
| 7 |
+
"""
|
| 8 |
+
import random
|
| 9 |
+
from datasets import load_dataset, get_dataset_config_names
|
| 10 |
+
|
| 11 |
+
from alea.genbase import emit, q_choice, q_noul, validate_row, write_jsonl
|
| 12 |
+
|
| 13 |
+
rng = random.Random(0)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def _slug2words(s):
|
| 17 |
+
return s.replace("_", " ").replace("-", " ")
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def gen_legalbench(out, max_rows=15000):
|
| 21 |
+
cfgs = get_dataset_config_names("nguha/legalbench")
|
| 22 |
+
keep = [c for c in cfgs if c.startswith(("contract_nli", "cuad"))]
|
| 23 |
+
rows, i = [], 0
|
| 24 |
+
for cfg in keep:
|
| 25 |
+
try:
|
| 26 |
+
ds = load_dataset("nguha/legalbench", cfg, split="train")
|
| 27 |
+
except Exception:
|
| 28 |
+
try:
|
| 29 |
+
ds = load_dataset("nguha/legalbench", cfg,
|
| 30 |
+
split="test")
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print("skip", cfg, str(e)[:60])
|
| 33 |
+
continue
|
| 34 |
+
topic = _slug2words(cfg.split("_", 2)[-1]
|
| 35 |
+
if cfg.startswith("cuad") else
|
| 36 |
+
cfg.replace("contract_nli_", ""))
|
| 37 |
+
got = 0
|
| 38 |
+
for ex in ds:
|
| 39 |
+
text = str(ex.get("text", ""))[:2500]
|
| 40 |
+
ans = str(ex.get("answer", "")).strip().lower()
|
| 41 |
+
if len(text) < 80 or ans not in ("yes", "no"):
|
| 42 |
+
continue
|
| 43 |
+
if cfg.startswith("contract_nli"):
|
| 44 |
+
state = ("Contract clause under review (NDA excerpt):\n"
|
| 45 |
+
+ text)
|
| 46 |
+
instr = (f"Based only on the clause text, is it accurate "
|
| 47 |
+
f"that the agreement includes obligations related "
|
| 48 |
+
f"to: {topic}?")
|
| 49 |
+
qs = [q_noul("holds", instr, 1.0 if ans == "yes" else 0.0)]
|
| 50 |
+
else:
|
| 51 |
+
state = ("Contract clause under review:\n" + text)
|
| 52 |
+
instr = (f"Does this clause contain a provision about "
|
| 53 |
+
f"{topic}?")
|
| 54 |
+
qs = [q_noul("contains", instr,
|
| 55 |
+
1.0 if ans == "yes" else 0.0)]
|
| 56 |
+
row = emit(f"lb_{cfg[:24]}", i, state, qs)
|
| 57 |
+
try:
|
| 58 |
+
validate_row(row)
|
| 59 |
+
rows.append(row)
|
| 60 |
+
i += 1
|
| 61 |
+
got += 1
|
| 62 |
+
except AssertionError:
|
| 63 |
+
continue
|
| 64 |
+
if got:
|
| 65 |
+
print(cfg, got)
|
| 66 |
+
rng.shuffle(rows)
|
| 67 |
+
write_jsonl(rows[:max_rows], out)
|
| 68 |
+
print("legalbench:", len(rows[:max_rows]))
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def main():
|
| 72 |
+
import sys
|
| 73 |
+
gen_legalbench("data/hf6_legal.jsonl",
|
| 74 |
+
int(sys.argv[1]) if len(sys.argv) > 1 else 15000)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
main()
|