Files
natural-memory-site/tools/make_ledger.py
T

101 lines
3.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
从磁盘上的真实评测 JSON 生成站点用的台账数据 assets/js/ledger.js。
⚠️ 生成物不要手改;改数据请改上游 JSON 后重跑本脚本。
来源(两份都是工程里保存的正式运行结果,逐字复制,不做任何改写或换算):
A. H:\\Memory\\V2_dpskw\\runtime_score_bands.json 60 episodes
B. H:\\Memory\\V2_dpskw\\nm2_1_final_runtime_e2e.json 48 episodes
用法:
python tools/make_ledger.py
"""
from __future__ import annotations
import io
import json
import os
SOURCES = [
{
"key": "bands",
"label": "运行时评分带扫描",
"path": r"H:\Memory\V2_dpskw\runtime_score_bands.json",
"config": "0.00",
"note": "60 用例 · 24 同形候选 · prior=1.00 blend=0.00",
},
{
"key": "e2e",
"label": "NM2.1 最终运行时",
"path": r"H:\Memory\V2_dpskw\nm2_1_final_runtime_e2e.json",
"config": "prior=1.00 blend=0.50",
"note": "48 用例 · 24 同形候选 · prior=1.00 blend=0.50",
},
]
OUT = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
"assets", "js", "ledger.js")
def rows_of(path: str, key: str):
with open(path, encoding="utf-8") as fh:
j = json.load(fh)
rows = j.get("rows") or {}
if key not in rows:
raise SystemExit("在 %s 里找不到 rows[%r]" % (path, key))
return rows[key]
def main() -> int:
out, total = [], 0
for src in SOURCES:
rows = rows_of(src["path"], src["config"])
total += len(rows)
out.append({
"key": src["key"],
"label": src["label"],
"note": src["note"],
"n": len(rows),
"items": [{
"id": r.get("id"),
"attr": r.get("attribute"),
"ans": bool(r.get("answerable")),
"exp": r.get("expected"),
"reply": r.get("reply"),
"ok": bool(r.get("correct")),
"wrong": bool(r.get("wrong_attribute")),
"leak": bool(r.get("leaked")),
"stop": r.get("stop_reason"),
"sel": r.get("records_selected"),
"score": r.get("top_score"),
"active": r.get("records_active"),
"need": r.get("need_memory"),
} for r in rows],
})
body = io.StringIO()
body.write("/* ==================================================================\n")
body.write(" Natural Memory v2 — ledger.js\n")
body.write(" 「真实评测台账」的数据层。\n\n")
body.write(" ⚠️ 本文件由 tools/make_ledger.py 从磁盘上的原始评测 JSON 生成,\n")
body.write(" 请勿手改。下面是全部 %d 条 episode,字段逐字复制、未做改写或换算。\n\n" % total)
for s in SOURCES:
body.write(" %s\n" % os.path.basename(s["path"]))
body.write(" ================================================================== */\n")
body.write("window.NM_LEDGER = ")
body.write(json.dumps(out, ensure_ascii=False, separators=(",", ":"), indent=0)
.replace("\n", ""))
body.write(";\n")
os.makedirs(os.path.dirname(OUT), exist_ok=True)
with open(OUT, "w", encoding="utf-8", newline="\n") as fh:
fh.write(body.getvalue())
print("写出 %s(%d 条 episode / %d 字节)" % (OUT, total, os.path.getsize(OUT)))
return 0
if __name__ == "__main__":
raise SystemExit(main())