Download alea/dataset.py from Akahsizrr/alea-runs: direct link, hf CLI and curl.
- Browser
- Download file 2.25 kB
-
https://huggingface.co/datasets/Akahsizrr/alea-runs/resolve/d1132dbb5c20aa59b02426266ce2b956813652a1/alea/dataset.py
- Command line
-
hf download hf://datasets/Akahsizrr/alea-runs@d1132dbb5c20aa59b02426266ce2b956813652a1/alea/dataset.py
-
curl -L -o dataset.py https://huggingface.co/datasets/Akahsizrr/alea-runs/resolve/d1132dbb5c20aa59b02426266ce2b956813652a1/alea/dataset.py
2.25 kB
| """JSONL rows -> Packed sequences.""" | |
| import json | |
| import torch | |
| from torch.utils.data import Dataset | |
| from .packing import QuestionSpec, pack_request | |
| def row_to_specs(row): | |
| """Return (state_text, list[QuestionSpec], targets: {qid: q}). | |
| For choice/score, q is a list aligned with option order; for noul a float.""" | |
| state = row["state"] | |
| if not isinstance(state, str): | |
| state = json.dumps(state) | |
| qs, targets = [], {} | |
| for qid, spec in row["questions"].items(): | |
| qtype = spec["type"] | |
| if qtype in ("choice", "score"): | |
| crit = spec.get("criteria") or spec.get("levels") or {} | |
| keys = list(crit.keys()) | |
| opts = [f"{k}: {v}" for k, v in crit.items()] | |
| t = row["targets"][qid] | |
| qvec = [float(x) for x in t] | |
| qs.append(QuestionSpec(qid, qtype, spec["instructions"], opts, keys)) | |
| targets[qid] = qvec | |
| elif qtype == "noul": | |
| instr = spec["instructions"] | |
| crit = spec.get("criteria") or {} | |
| if crit.get("true"): | |
| instr += f" (true means: {crit['true']})" | |
| if crit.get("false"): | |
| instr += f" (false means: {crit['false']})" | |
| qs.append(QuestionSpec(qid, "noul", instr, [], None)) | |
| targets[qid] = float(row["targets"][qid]) | |
| elif qtype == "regress": | |
| instr = spec["instructions"] | |
| if spec.get("unit"): | |
| instr += f" (unit: {spec['unit']})" | |
| qs.append(QuestionSpec(qid, "regress", instr, [], None)) | |
| targets[qid] = [float(x) for x in row["targets"][qid]] | |
| else: | |
| raise ValueError(qtype) | |
| return state, qs, targets | |
| class DecisionDataset(Dataset): | |
| def __init__(self, path, tokenizer, max_len=4096): | |
| self.rows = [json.loads(l) for l in open(path, encoding="utf-8")] | |
| self.tok = tokenizer | |
| self.max_len = max_len | |
| def __len__(self): | |
| return len(self.rows) | |
| def __getitem__(self, i): | |
| row = self.rows[i] | |
| state, qs, targets = row_to_specs(row) | |
| pk = pack_request(self.tok, state, qs, max_len=self.max_len) | |
| return pk, targets | |