alea-runs / alea /gen_hf6.py
Akahsizrr's picture
Upload alea/gen_hf6.py with huggingface_hub
6207807 verified
Raw History Blame
2.82 kB
"""gen_hf6 — LegalBench contract/policy tasks -> Alea rows.
contract_nli_*: real contract clause + hypothesis -> yes/no (noul).
cuad_*: clause text -> does it contain provision type X (noul) and
clause-type routing (choice).
Small per-config but real legal text — complements synthetic policy data.
"""
import random
from datasets import load_dataset, get_dataset_config_names
from alea.genbase import emit, q_choice, q_noul, validate_row, write_jsonl
rng = random.Random(0)
def _slug2words(s):
return s.replace("_", " ").replace("-", " ")
def gen_legalbench(out, max_rows=15000):
cfgs = get_dataset_config_names("nguha/legalbench")
keep = [c for c in cfgs if c.startswith(("contract_nli", "cuad"))]
rows, i = [], 0
for cfg in keep:
try:
ds = load_dataset("nguha/legalbench", cfg, split="train")
except Exception:
try:
ds = load_dataset("nguha/legalbench", cfg,
split="test")
except Exception as e:
print("skip", cfg, str(e)[:60])
continue
topic = _slug2words(cfg.split("_", 2)[-1]
if cfg.startswith("cuad") else
cfg.replace("contract_nli_", ""))
got = 0
for ex in ds:
text = str(ex.get("text", ""))[:2500]
ans = str(ex.get("answer", "")).strip().lower()
if len(text) < 80 or ans not in ("yes", "no"):
continue
if cfg.startswith("contract_nli"):
state = ("Contract clause under review (NDA excerpt):\n"
+ text)
instr = (f"Based only on the clause text, is it accurate "
f"that the agreement includes obligations related "
f"to: {topic}?")
qs = [q_noul("holds", instr, 1.0 if ans == "yes" else 0.0)]
else:
state = ("Contract clause under review:\n" + text)
instr = (f"Does this clause contain a provision about "
f"{topic}?")
qs = [q_noul("contains", instr,
1.0 if ans == "yes" else 0.0)]
row = emit(f"lb_{cfg[:24]}", i, state, qs)
try:
validate_row(row)
rows.append(row)
i += 1
got += 1
except AssertionError:
continue
if got:
print(cfg, got)
rng.shuffle(rows)
write_jsonl(rows[:max_rows], out)
print("legalbench:", len(rows[:max_rows]))
def main():
import sys
gen_legalbench("data/hf6_legal.jsonl",
int(sys.argv[1]) if len(sys.argv) > 1 else 15000)
if __name__ == "__main__":
main()