Datasets:
Download scripts/evaluate_json.py from bloksbgt/SQL-SFT-V1-all-rows: direct link, hf CLI and curl.
- Browser
- Download file 1.72 kB
-
https://huggingface.co/datasets/bloksbgt/SQL-SFT-V1-all-rows/resolve/main/scripts/evaluate_json.py
- Command line
-
hf download hf://datasets/bloksbgt/SQL-SFT-V1-all-rows/scripts/evaluate_json.py
-
curl -L -o evaluate_json.py https://huggingface.co/datasets/bloksbgt/SQL-SFT-V1-all-rows/resolve/main/scripts/evaluate_json.py
1.72 kB
| # Hitung akurasi jawaban (\boxed{...} vs gtruth_answer). Contoh: | |
| # python scripts/evaluate_json.py --json_path data/pajak-eval.json --gtruth_answer gtruth_answer --generated_text message_content | |
| import argparse, json, re | |
| def extract_boxed(text): | |
| i = text.rfind("\\boxed{") | |
| if i < 0: return None | |
| j, depth, out = i + 7, 1, [] | |
| while j < len(text) and depth: | |
| c = text[j] | |
| if c == "{": depth += 1 | |
| elif c == "}": depth -= 1 | |
| if depth: out.append(c) | |
| j += 1 | |
| return "".join(out) if depth == 0 else None | |
| def to_num(s): | |
| m = re.search(r"-?\d[\d.,]*", s.replace("\\,", "")) | |
| if not m: return None | |
| g = m.group(0).rstrip(".,") | |
| if re.fullmatch(r"\d{1,3}(\.\d{3})+(,\d+)?", g): g = g.replace(".", "").replace(",", ".") | |
| elif re.fullmatch(r"\d{1,3}(,\d{3})+(\.\d+)?", g): g = g.replace(",", "") | |
| else: g = g.replace(",", ".") | |
| try: return float(g) | |
| except ValueError: return None | |
| def norm(s): return re.sub(r"\W+", " ", s.lower()).strip() | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--json_path", required=True) | |
| parser.add_argument("--gtruth_answer", default="gtruth_answer") | |
| parser.add_argument("--generated_text", default="message_content") | |
| args = parser.parse_args() | |
| data = json.load(open(args.json_path, encoding="utf-8")) | |
| correct = 0 | |
| for d in data: | |
| pred, gt = extract_boxed(d[args.generated_text]), d[args.gtruth_answer] | |
| if pred is None: continue | |
| a, b = to_num(gt), to_num(pred) | |
| if a is not None and b is not None: correct += abs(a - b) < 0.5 | |
| else: correct += norm(gt) == norm(pred) or norm(gt) in norm(pred) or norm(pred) in norm(gt) | |
| print(f"Answer accuracy: {correct/len(data):.1%} ({correct}/{len(data)})") | |