Download scripts/validate_dataset.py from Simon-Liu/tw-finance-reasoning-instruct: direct link, hf CLI and curl.
- Browser
- Download file 4.22 kB
-
https://huggingface.co/datasets/Simon-Liu/tw-finance-reasoning-instruct/resolve/main/scripts/validate_dataset.py
- Command line
-
hf download hf://datasets/Simon-Liu/tw-finance-reasoning-instruct/scripts/validate_dataset.py
-
curl -L -o validate_dataset.py https://huggingface.co/datasets/Simon-Liu/tw-finance-reasoning-instruct/resolve/main/scripts/validate_dataset.py
4.22 kB
| # -*- coding: utf-8 -*- | |
| """資料集品質驗證(tw-finance-reasoning-instruct)。 | |
| 檢查項目: | |
| 1. 欄位與型別是否對齊 twinkle-ai/tw-reasoning-instruct-50k | |
| 2. conversations 是否可由 input / think / output 完整還原 | |
| 3. output 是否有未渲染的模板佔位符或格式殘留 | |
| 4. think / output 是否為繁體中文且長度合理 | |
| 5. train / test 是否有完全重複的樣本 | |
| """ | |
| import json | |
| import re | |
| import sys | |
| from collections import Counter | |
| SCHEMA = ["conversations", "input", "output", "think", "seed"] | |
| TYPES = {"conversations": list, "input": str, "output": str, "think": str, "seed": str} | |
| PLACEHOLDER = re.compile(r"\{[a-z_]+\}|%[sdf]|None|nan|\bnan\b") | |
| def load(p): | |
| with open(p, encoding="utf-8") as f: | |
| return [json.loads(l) for l in f if l.strip()] | |
| def check(rows, name, errors, stats): | |
| for i, r in enumerate(rows): | |
| tag = "%s#%d" % (name, i) | |
| if list(r.keys()) != SCHEMA: | |
| errors.append("%s 欄位不符:%s" % (tag, list(r.keys()))) | |
| continue | |
| for k, t in TYPES.items(): | |
| if not isinstance(r[k], t): | |
| errors.append("%s 欄位 %s 型別應為 %s" % (tag, k, t.__name__)) | |
| c = r["conversations"] | |
| if len(c) != 2 or [x["from"] for x in c] != ["human", "gpt"]: | |
| errors.append("%s conversations 不是 human/gpt 兩輪" % tag) | |
| continue | |
| if c[0]["value"] != r["input"]: | |
| errors.append("%s conversations[0] 與 input 不一致" % tag) | |
| expect = "<think>\n%s\n</think>\n\n%s" % (r["think"], r["output"]) | |
| if c[1]["value"] != expect: | |
| errors.append("%s conversations[1] 無法由 think + output 還原" % tag) | |
| for field in ("input", "output", "think"): | |
| mm = PLACEHOLDER.search(r[field]) | |
| if mm: | |
| errors.append("%s %s 有未渲染內容:%r" % (tag, field, mm.group(0))) | |
| if not r["seed"]: | |
| errors.append("%s seed 為空" % tag) | |
| if len(r["think"]) < 40: | |
| errors.append("%s think 過短(%d 字)" % (tag, len(r["think"]))) | |
| if len(r["output"]) < 80: | |
| errors.append("%s output 過短(%d 字)" % (tag, len(r["output"]))) | |
| # 簡體字檢查(應全為繁體) | |
| simp = set("们个东这来对时说过还没经买卖头资产权贷额费户际线") | |
| hit = simp & set(r["output"]) | |
| if hit: | |
| errors.append("%s output 含簡體字:%s" % (tag, "".join(hit))) | |
| stats["think_len"] += len(r["think"]) | |
| stats["out_len"] += len(r["output"]) | |
| stats["in_len"] += len(r["input"]) | |
| if "### 計算過程" in r["output"]: | |
| stats["calc"] += 1 | |
| stats["n"] += 1 | |
| def main(): | |
| tr = load(sys.argv[1] if len(sys.argv) > 1 else "data/train.jsonl") | |
| te = load(sys.argv[2] if len(sys.argv) > 2 else "data/test.jsonl") | |
| errors, stats = [], Counter() | |
| check(tr, "train", errors, stats) | |
| check(te, "test", errors, stats) | |
| tr_in = {r["input"] for r in tr} | |
| dup = [r["input"] for r in te if r["input"] in tr_in] | |
| if dup: | |
| errors.append("test 有 %d 筆 input 與 train 重複" % len(dup)) | |
| n = stats["n"] or 1 | |
| print("=" * 66) | |
| print("資料集驗證報告 — tw-finance-reasoning-instruct") | |
| print("=" * 66) | |
| print("train / test : %d / %d (%.1f%% : %.1f%%)" | |
| % (len(tr), len(te), 100 * len(tr) / (len(tr) + len(te)), | |
| 100 * len(te) / (len(tr) + len(te)))) | |
| print("計算型 / 觀念型 : %d / %d" % (stats["calc"], n - stats["calc"])) | |
| print("平均 input 長度 : %.0f 字" % (stats["in_len"] / n)) | |
| print("平均 think 長度 : %.0f 字" % (stats["think_len"] / n)) | |
| print("平均 output 長度 : %.0f 字" % (stats["out_len"] / n)) | |
| print("train/test input 重複 : %d 筆" % len(dup)) | |
| print("-" * 66) | |
| if errors: | |
| print("發現 %d 項錯誤:" % len(errors)) | |
| for e in errors[:25]: | |
| print(" ✗", e) | |
| sys.exit(1) | |
| print("結果:通過,無結構性錯誤。") | |
| print("(數字正確性另由 scripts/test_solvers.py 驗證)") | |
| if __name__ == "__main__": | |
| main() | |