#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ LoRA Bench —— 跑一次 = 一个文件夹 + 一张对照图 + 一份报告 python bench.py --object 物体(默认 lite,15 张) python bench.py --object --full 全量(48 张) python bench.py --object --probe 标定 WW(14 张,含 0.00 对照) python bench.py --report-only 00005 只按已有图片重出图+报告 python bench.py --list 列出所有类别 python bench.py --object --dry 只打印计划,不跑 依赖:pip install pillow numpy 可选:pip install insightface onnxruntime (人物类才需要,用来算跨 seed 一致性) """ import os import re import sys import copy import json import time import argparse import itertools import datetime import urllib.request import urllib.error import shutil import numpy as np from PIL import Image, ImageDraw, ImageFont CN_HELP = ("--cn" in sys.argv) or (os.environ.get("BENCH_LANG", "").lower() == "cn") def T(cn, en): return cn if CN_HELP else en ROOT = os.path.dirname(os.path.abspath(__file__)) CFG_PATH = os.path.join(ROOT, "bench_config.json") TPL_PATH = os.path.join(ROOT, "view_template.html") # ====================================================================== # 类别定义:PB0–PB7 是固定槽位,prompt 按类别重写 # ====================================================================== # 用例库在 benches.json,改 prompt 不用动代码 BENCH_PATH = os.path.join(ROOT, "bench_prompts.json") def load_benches(): if not os.path.exists(BENCH_PATH): raise SystemExit(f"[!] 缺少 {BENCH_PATH}\n 用例库必须是 bench_prompts.json(见模板)") with open(BENCH_PATH, encoding="utf-8") as f: return json.load(f) BENCHES = load_benches() ALIAS = {"shoes": "object", "cars": "vehicle", "girls": "character", "monster": "creature", "building": "scene", "art": "style"} # 抽样档:每档都含 0.00(原始模型对照),因为每次运行是独立文件夹,不能借用上次的 0.00 PROFILES = { "lite": {"cases": ["PB0", "PB6", "PB7"], "strengths": [0.0, 0.40, 0.60, 0.75, 0.85]}, "full": {"cases": None, # None = 本类别全部用例 "strengths": [0.0, 0.40, 0.60, 0.75, 0.85, 1.00]}, "probe": {"cases": ["PB6", "PB7"], "strengths": [0.0, 0.40, 0.60, 0.75, 0.80, 0.85, 0.90]}, } TURBO_RE = r"dmd2|turbo|accelerat|lightning|hyper|\blcm\b|\d+[-_ ]?steps" # ====================================================================== # ComfyUI 通信 # ====================================================================== def api(base, path, obj=None, timeout=60): data = json.dumps(obj).encode() if obj is not None else None req = urllib.request.Request(base.rstrip("/") + path, data=data, headers={"Content-Type": "application/json"} if data else {}) with urllib.request.urlopen(req, timeout=timeout) as r: return json.loads(r.read()) _OPTS = {} def comfy_options(base, cls, inp): """查 ComfyUI 真实可选值列表(用于修正 / 与 \\ 差异)""" key = (cls, inp) if key in _OPTS: return _OPTS[key] try: info = api(base, f"/object_info/{cls}") spec = (info.get(cls) or {}).get("input", {}) opts = None for sec in ("required", "optional"): e = spec.get(sec, {}).get(inp) if isinstance(e, list) and e and isinstance(e[0], list): opts = list(e[0]); break except Exception: opts = None _OPTS[key] = opts return opts def fix_name(base, cls, inp, want): opts = comfy_options(base, cls, inp) if opts is None or want in opts: return want n = str(want).replace("\\", "/").strip().lower() for o in opts: if str(o).replace("\\", "/").strip().lower() == n: return o base_n = n.split("/")[-1] cand = [o for o in opts if str(o).replace("\\", "/").split("/")[-1].lower() == base_n] if len(cand) == 1: return cand[0] if len(cand) > 1: raise SystemExit(f"[!] {cls}.{inp}: '{want}' 对应多个文件 {cand}") raise SystemExit( f"[!] {cls}.{inp}: '{want}' 在 ComfyUI 里不存在。\n" f" 候选(前 25):\n" + "\n".join(" " + str(o) for o in opts[:25])) # ====================================================================== # 读 workflow:按节点标题找 Turbo / Target,其余参数直接读 # ====================================================================== def load_prompt(path): """读取 workflow,返回 ComfyUI 的 API 格式({节点ID: {class_type, inputs}})""" with open(path, encoding="utf-8") as f: raw = json.load(f) def is_api(d): return isinstance(d, dict) and bool(d) and all( isinstance(v, dict) and "class_type" in v for v in d.values()) if is_api(raw): return raw for key in ("prompt", "workflow", "api"): # 常见包装格式 if isinstance(raw.get(key), dict) and is_api(raw[key]): print(f"[i] 已从 '{key}' 字段取出 API 格式") return raw[key] found = [v for v in raw.values() if is_api(v)] if isinstance(raw, dict) else [] if len(found) == 1: print("[i] 已自动定位文件内的 API 节点表") return found[0] keys = list(raw)[:12] if isinstance(raw, dict) else type(raw).__name__ # UI 格式:把节点清单打出来,方便确认标题改对没有 ui = raw.get("nodes") if isinstance(raw, dict) else None if isinstance(ui, list) and ui: print(f"\n [诊断] 这是 UI 格式,里面共 {len(ui)} 个节点:") for n in ui: t = n.get("type") or "?" ti = (n.get("title") or "").strip() mark = " ← LoRA 节点(这个要标标题)" \ if t in ("LoraLoader", "LoraLoaderModelOnly") else "" print(f" #{n.get('id')} {t:<26} 标题: {ti or '(未命名)'}{mark}") print(" 提示:只给『目标 LoRA』那个节点把标题写成 Target 即可,其余 LoRA 不用管。\n") raise SystemExit( f"[!] {os.path.basename(path)} 不是 API 格式\n" f" 顶层键: {keys}\n\n" f" 这是 UI 格式(含 nodes / links / last_node_id),脚本读不了。\n" f" 请在 ComfyUI 里用 File → Export (API) 重新导出,覆盖这个文件。" ) def ntitle(node): return str(((node.get("_meta") or {}).get("title")) or "").strip() def up(wf, nid, field): v = (wf.get(str(nid)) or {}).get("inputs", {}).get(field) return (str(v[0]), v[1]) if isinstance(v, list) and len(v) == 2 else None def one(wf, cls, need=True): hits = [k for k, v in wf.items() if v.get("class_type") == cls] if len(hits) == 1: return hits[0] if not hits and not need: return None raise SystemExit(f"[!] 需要恰好 1 个 {cls},实际 {len(hits)} 个") def read_workflow(wf, base_url): """从 workflow 里把一切读出来;返回参数字典""" P = {} ks = one(wf, "KSampler") P["ks_node"] = ks ki = wf[ks]["inputs"] P["steps"] = ki.get("steps") P["cfg"] = ki.get("cfg") P["sampler"] = ki.get("sampler_name") P["scheduler"] = ki.get("scheduler") # 正向文本节点 cur, pos = up(wf, ks, "positive"), None for _ in range(10): if not cur: break if (wf.get(cur[0]) or {}).get("class_type") == "CLIPTextEncode": pos = cur[0]; break cur = up(wf, cur[0], "conditioning") or up(wf, cur[0], "positive") if not pos: raise SystemExit("[!] 找不到正向提示词节点(KSampler.positive 上游无 CLIPTextEncode)") P["pos_node"] = pos # 尺寸 lt = up(wf, ks, "latent_image") if lt: P["latent_node"] = lt[0] P["size"] = [wf[lt[0]]["inputs"].get("width"), wf[lt[0]]["inputs"].get("height")] # 保存节点(必须是 SaveImage:PreviewImage 不落盘、也没有 filename_prefix) sv = one(wf, "SaveImage", need=False) if not sv: raise SystemExit("[!] workflow 里没有 Save Image 节点。\n" " PreviewImage 不会把图写进 output,也没有 filename_prefix。\n" " 请加一个 Save Image(VAEDecode.images → SaveImage)后重新 Export (API)。") P["save_node"] = sv # 主模型 for k, v in wf.items(): if v.get("class_type") == "UNETLoader": P["unet_node"] = k P["unet"] = os.path.basename(str(v["inputs"].get("unet_name")).replace("\\", "/")) break if v.get("class_type") == "CheckpointLoaderSimple": P["unet_node"] = k P["unet"] = os.path.basename(str(v["inputs"].get("ckpt_name")).replace("\\", "/")) break # 读词模型 for k, v in wf.items(): if v.get("class_type") in ("CLIPLoader", "DualCLIPLoader"): P["clip_node"] = k P["clip_type"] = v["inputs"].get("type") names = [v["inputs"].get(x) for x in ("clip_name", "clip_name1", "clip_name2") if v["inputs"].get(x)] P["te"] = " + ".join(os.path.basename(str(n).replace("\\", "/")) for n in names) break # 解码器 for k, v in wf.items(): if v.get("class_type") == "VAELoader": P["vae_node"] = k P["vae"] = os.path.basename(str(v["inputs"].get("vae_name")).replace("\\", "/")) break # ---- LoRA:只需要认出 Target,其余原样不动 ---- lora_nodes = [k for k, v in wf.items() if v.get("class_type") in ("LoraLoaderModelOnly", "LoraLoader")] if not lora_nodes: raise SystemExit("[!] workflow 里没有 LoRA 节点") tgt = None for k in lora_nodes: if "target" in ntitle(wf[k]).lower(): tgt = k break if tgt is None: # 没标标题 → 取模型链上最靠近采样的那个 chain, cur = [], up(wf, ks, "model") for _ in range(20): if not cur: break if (wf.get(cur[0]) or {}).get("class_type") in ("LoraLoaderModelOnly", "LoraLoader"): chain.append(cur[0]) cur = up(wf, cur[0], "model") if not chain: raise SystemExit("[!] KSampler.model 上游没有 LoRA 节点") tgt = chain[0] print("⚠️ 节点标题未标注,已按顺序推断;建议标题写 『Load LoRA - Target』") P["target_node"] = tgt P["target_lora"] = fix_name(base_url, "LoraLoaderModelOnly", "lora_name", wf[tgt]["inputs"].get("lora_name")) # 其余 LoRA(加速补丁/细节/风格)一律不动,只在报告里列出 P["extra_loras"] = [ {"name": os.path.basename(str(wf[k]["inputs"].get("lora_name", "")).replace("\\", "/")), "strength": wf[k]["inputs"].get("strength_model")} for k in lora_nodes if k != tgt ] # 模型名也顺手对齐(Windows 反斜杠) for key, cls, inp in [("unet", "UNETLoader", "unet_name"), ("vae", "VAELoader", "vae_name")]: if P.get(key + "_node"): try: P[key] = os.path.basename(fix_name(base_url, cls, inp, wf[P[key + "_node"]]["inputs"].get(inp)) .replace("\\", "/")) except SystemExit: pass return P def set_lora(wf, nid, name, strength): i = wf[nid]["inputs"] i["lora_name"] = name if "strength_model" in i: i["strength_model"] = strength if "strength_clip" in i: i["strength_clip"] = strength def build(wf, P, case, strength, seed, prefix): w = copy.deepcopy(wf) trg = str(P["trigger"]).strip().rstrip(",").strip() w[P["pos_node"]]["inputs"]["text"] = case["prompt"].replace("{t}", trg) set_lora(w, P["target_node"], P["target_lora"], strength) w[P["ks_node"]]["inputs"]["seed"] = int(seed) if P.get("latent_node") and P.get("size"): w[P["latent_node"]]["inputs"]["width"] = int(P["size"][0]) w[P["latent_node"]]["inputs"]["height"] = int(P["size"][1]) w[P["latent_node"]]["inputs"]["batch_size"] = 1 w[P["save_node"]]["inputs"]["filename_prefix"] = prefix return w # ====================================================================== # 目录 / 编号 # ====================================================================== def bench_dir(cfg, bench): return os.path.join(cfg["comfy_output_dir"], cfg.get("prefix", "bench"), bench) def next_tag(cfg, bench, lora_stem): d = bench_dir(cfg, bench) os.makedirs(d, exist_ok=True) mx = 0 for e in os.listdir(d): m = re.search(r"_(\d{5})$", e) if m and os.path.isdir(os.path.join(d, e)): mx = max(mx, int(m.group(1))) # Windows 总路径 260 上限;给 "_s###_sd###_00001_.png" 留 40 字符余量 budget = max(24, 240 - len(os.path.abspath(d)) - 6 - 40) return f"{lora_stem[:budget]}_{mx + 1:05d}" def prefix_for(cfg, bench, tag, case_id, strength, seed): return (f"{cfg.get('prefix', 'bench')}/{bench}/{tag}" f"/{case_id}_s{int(round(strength * 100)):03d}_sd{seed}") PAT = re.compile(r"((?:PB|CX-)[\w-]+)_s(\d{3})_sd(\d+)") IMG_EXT = (".png", ".webp", ".jpg", ".jpeg") def to_webp(run_dir, quality=90, delete_png=True): """ PNG → WebP(q90, method=4) 同时把 ComfyUI 写在 PNG tEXt 里的 workflow / prompt 搬进 EXIF: ImageDescription(0x0110) ← "Prompt:" + prompt Make(0x010F) ← "Workflow:" + workflow """ PROMPT_TAG = 0x0110 EXTRA_TAG = 0x010F n = 0 for f in sorted(os.listdir(run_dir)): if f.startswith("_") or not f.lower().endswith(".png"): continue src = os.path.join(run_dir, f) dst = os.path.join(run_dir, os.path.splitext(f)[0] + ".webp") try: with Image.open(src) as img: ex = img.getexif() tag = EXTRA_TAG for k, v in img.info.items(): if k == "prompt": ex[PROMPT_TAG] = "Prompt:" + str(v) elif k == "workflow": ex[tag] = "Workflow:" + str(v) tag -= 1 if img.mode in ("RGBA", "P"): img = img.convert("RGB") kw = {"exif": ex.tobytes()} if len(ex) else {} img.save(dst, "WEBP", lossless=False, quality=quality, method=4, **kw) if delete_png: os.remove(src) n += 1 except Exception as e: print(f" [!] webp 转换失败 {f}: {type(e).__name__}: {e}") return n def collect(run_dir): """{(case, strength): (seed, path)} 与 {(case,strength): [(seed,path)]}""" one_, all_ = {}, {} if not os.path.isdir(run_dir): return one_, all_ for f in sorted(os.listdir(run_dir)): if not f.lower().endswith(IMG_EXT): continue m = PAT.match(f) if not m: continue case, s, seed = m.group(1), int(m.group(2)) / 100, int(m.group(3)) p = os.path.join(run_dir, f) all_.setdefault((case, s), []).append((seed, p)) prev = one_.get((case, s)) if prev is None or seed < prev[0]: one_[(case, s)] = (seed, p) return one_, all_ # ====================================================================== # 打分 # ====================================================================== def _stat(p): a = np.asarray(Image.open(p).convert("RGB").resize((256, 256)), dtype=np.float32) h = np.concatenate([np.histogram(a[:, :, c], bins=32, range=(0, 255))[0] for c in range(3)]).astype(np.float32) h /= max(h.sum(), 1) g = a.mean(2) return h, float(np.abs(np.diff(g, axis=0)).mean() + np.abs(np.diff(g, axis=1)).mean()) def score_cont(cells): """裸触发词 vs 原始模型:LoRA 到底加了多少色调/锐度""" res = {} for s in sorted({k[1] for k in cells}): if s == 0.0: continue a, b = cells.get(("PB6", 0.0)), cells.get(("PB6", s)) if not a or not b: continue h1, e1 = _stat(a[1]); h2, e2 = _stat(b[1]) res[s] = (round(float(0.5 * np.abs(h1 - h2).sum()), 3), round(float(e2 / max(e1, 1e-6)), 3)) return res def _sig(p, n=64): im = Image.open(p).convert("L").resize((n, n), Image.LANCZOS) a = np.asarray(im, dtype=np.float32).flatten() a -= a.mean() return a / (np.linalg.norm(a) + 1e-6) def score_ids(multi): app = None try: from insightface.app import FaceAnalysis app = FaceAnalysis(name="buffalo_l", providers=["CPUExecutionProvider"]) app.prepare(ctx_id=-1, det_size=(640, 640)) except Exception: app = None def face(p): if not app: return None a = np.array(Image.open(p).convert("RGB"))[:, :, ::-1] f = app.get(a) return f[0].normed_embedding if f else None res = {} for (case, s), pairs in multi.items(): if len(pairs) < 2: continue ps = [p for _, p in pairs] em = [e for e in (face(p) for p in ps) if e is not None] if len(em) >= 2: sims = [float(np.dot(x, y)) for x, y in itertools.combinations(em, 2)] res[(case, s)] = ("脸", round(sum(sims) / len(sims), 3)) else: vs = [_sig(p) for p in ps] sims = [float(np.dot(x, y)) for x, y in itertools.combinations(vs, 2)] res[(case, s)] = ("结构", round(sum(sims) / len(sims), 3)) return res # ====================================================================== # 出图 # ====================================================================== COL, ROWH, PAD, LABEL, GROUP, LEFT = 148, 196, 8, 20, 14, 214 def font(sz): for p in ("C:/Windows/Fonts/msyh.ttc", "C:/Windows/Fonts/simhei.ttf", "C:/Windows/Fonts/arial.ttf", "/System/Library/Fonts/PingFang.ttc", "/usr/share/fonts/truetype/noto/NotoSansCJK-Regular.ttc"): if os.path.exists(p): try: return ImageFont.truetype(p, sz) except Exception: pass return ImageFont.load_default() def render_sheet(cfg, bench, tag, P, cases, cells, run_dir, lang="cn"): en = (lang == "en") Lb = lambda cn, e: e if en else cn Bx = BENCHES[bench] strg = sorted({s for _, s in cells}) if not strg: return None f13, f12 = font(13), font(12) _sz = P.get("size") or ["?", "?"] _distill = (P.get("cfg") or 2) <= 1.2 and (P.get("steps") or 99) <= 12 head = [ ("LoRA Bench", f"[{bench}] {Lb(Bx['cn'], Bx['en'])} {tag}"), ("UNET", str(P.get("unet", "?"))), ("TE", f"{P.get('te','?')} (type={P.get('clip_type','?')})"), ("VAE", str(P.get("vae", "?"))), ("Steps / CFG", f"{P.get('steps')} · {P.get('cfg')} · " f"{P.get('sampler')}/{P.get('scheduler')}" + (Lb(" ← 低步数+CFG≈1:distilled", " ← distilled (low steps, CFG≈1)") if _distill else "")), ("Size", f"{_sz[0]} × {_sz[1]}"), ("Fixed LoRA", "、".join(f"{e['name']} @ {e['strength']}" for e in P.get("extra_loras") or []) or "—"), ("Target LoRA", str(P.get("target_lora", "?"))), ("Trigger", f'"{str(P["trigger"]).strip().rstrip(",")}"'), ("Seeds", f"{cfg['seeds']}"), ] head_h = 18 * len(head) + 26 used = [c for c in cases if any(k[0] == c["id"] for k in cells)] idline = " " + " / ".join(f"{c['id']} {Lb(c['cn'], c['en'])}" for c in used) foot = [ Lb("怎么读这张图", "How to read this grid"), Lb(" 列 = LoRA 强度。0.00 = 不加载 LoRA 的原始模型,是「它到底改了什么」的基准。", " Columns = LoRA strength. 0.00 = base model without the LoRA."), idline, Lb(" WW(可用强度区间)= 下界(还认得出)~ 上界(还听 prompt)。越宽越好用。", " WW = usable strength window: lower bound (still recognisable) .. upper bound (prompt still obeyed)."), Lb(" CONT 污染指纹 = 和原始模型比,色调偏了多少、锐度变了多少。越小越干净。", " CONT = tone / sharpness drift vs the base model. Lower is cleaner."), Lb(f" 本类别判断「上界」看 {Bx['upper']} 那一行。", f" Upper bound for this category: read the {Bx['upper']} row."), ] foot_h = 20 * len(foot) + 16 gh = GROUP + LABEL + len(used) * (ROWH + LABEL + PAD) W = PAD * 2 + LEFT + len(strg) * (COL + PAD) H = head_h + 30 + gh + foot_h img = Image.new("RGB", (W, H), (16, 16, 16)) dr = ImageDraw.Draw(img) y = 10 for k, v in head: dr.text((PAD, y), f"{k:<9}", fill=(110, 190, 250), font=f12) dr.text((PAD + 110, y), v[:118], fill=(225, 225, 225), font=f12) y += 18 y = head_h dr.line([(0, y), (W, y)], fill=(60, 60, 60), width=2) y += 8 for ci, s in enumerate(strg): x = PAD * 2 + LEFT + ci * (COL + PAD) dr.text((x, y), f"{s:.2f}", fill=(120, 230, 150) if s == 0.0 else (210, 210, 210), font=f13) y += LABEL for c in used: dr.text((PAD * 2, y + 6), c["id"], fill=(210, 210, 210), font=f13) dr.text((PAD * 2, y + 26), Lb(c["cn"], c["en"]), fill=(125, 125, 125), font=f12) for ci, s in enumerate(strg): x = PAD * 2 + LEFT + ci * (COL + PAD) v = cells.get((c["id"], s)) if not v: dr.rectangle([x, y, x + COL, y + ROWH], outline=(50, 50, 50)) continue im = Image.open(v[1]).convert("RGB") im.thumbnail((COL, ROWH)) img.paste(im, (x, y)) y += ROWH + LABEL + PAD dr.line([(0, y), (W, y)], fill=(60, 60, 60), width=2) y += 8 for ln in foot: dr.text((PAD, y), ln, fill=(135, 135, 135), font=f12) y += 20 out = os.path.join(run_dir, f"_sheet_{lang}.jpg") img.save(out, "JPEG", quality=90, optimize=True, progressive=True) return out def render_sheets(cfg, bench, tag, P, cases, cells, run_dir): out = [] for lang in ("cn", "en"): p = render_sheet(cfg, bench, tag, P, cases, cells, run_dir, lang) if p: out.append(p) return out # ====================================================================== # 每轮回写元数据 + 生成 HTML 查看器 # ====================================================================== def dump_meta(run_dir, cfg, bench, tag, P): m = { "tag": tag, "bench": bench, "time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M"), "unet": P.get("unet"), "te": P.get("te"), "clip_type": P.get("clip_type"), "vae": P.get("vae"), "steps": P.get("steps"), "cfg": P.get("cfg"), "sampler": P.get("sampler"), "scheduler": P.get("scheduler"), "size": P.get("size"), "extra": P.get("extra_loras") or [], "target": P.get("target_lora"), "trigger": str(P.get("trigger", "")).strip().rstrip(","), "seeds": cfg.get("seeds"), } with open(os.path.join(run_dir, "_meta.json"), "w", encoding="utf-8") as f: json.dump(m, f, ensure_ascii=False, indent=2) return m def load_template(): if not os.path.exists(TPL_PATH): raise SystemExit(f"[!] 缺少模板 {TPL_PATH}\n 从仓库拷一份 view_template.html 过来") with open(TPL_PATH, encoding="utf-8") as f: return f.read() TERMS_I18N = [ {"k": "WW", "cn": "可用强度区间。下界(还认得出)到上界(还听 prompt 话)之间的宽度,越宽越好用。", "en": "Usable strength window: width between the lower bound (still recognisable) and upper bound (prompt still obeyed). Wider is better."}, {"k": "0.00", "cn": "原始模型对照列。不加载 LoRA,用来判断这条 LoRA 到底改了什么。", "en": "Base reference column. No LoRA loaded, used to see what the LoRA actually changes."}, {"k": "PB", "cn": "8 个固定探针,每个只隔离一个变量(场景/尺度/视角/背景/风格/裸词/构图)。", "en": "8 fixed probes, each isolating one variable (scene / scale / angle / background / style / bare trigger / framing)."}, {"k": "CONT", "cn": "污染指纹。和原始模型比,色调偏了多少、锐度变了多少。越小越干净。", "en": "Contamination: how much tone and sharpness drift from the base model. Lower is cleaner."}, {"k": "IDS", "cn": "跨种子一致性。同一格换随机种子,出图还像不像。", "en": "Seed consistency: whether different seeds in the same cell still look alike."}, ] def build_data(cfg, bench, tag, B, run_dir, img_prefix=""): """收集一次运行的全部数据;img_prefix 用于「HTML 与图片不同目录」的场景""" mp = os.path.join(run_dir, "_meta.json") if not os.path.exists(mp): return None with open(mp, encoding="utf-8") as f: m = json.load(f) cells, _ = collect(run_dir) if not cells: return None m["cells"] = {} for (case, s), (seed, path) in cells.items(): m["cells"].setdefault(case, {})[f"{s:.2f}"] = img_prefix + os.path.basename(path) rep = {} for lang in ("cn", "en"): p = os.path.join(run_dir, f"_report_{lang}.txt") rep[lang] = open(p, encoding="utf-8").read() if os.path.exists(p) else "" return { "bench": bench, "bench_cn": B["cn"], "bench_en": B["en"], "cases": [{"id": c["id"], "cn": c["cn"], "en": c["en"], "prompt": c["prompt"]} for c in B["cases"]], "terms": TERMS_I18N, "report": rep, "runs": [m], } def write_data_js(data, path): with open(path, "w", encoding="utf-8") as f: f.write("window.BENCH_DATA = ") f.write(json.dumps(data, ensure_ascii=False)) f.write(";\n") def render_html(cfg, bench, tag, B, run_dir=None): """在运行目录生成 _view.html + _data.js(自包含,双击可用)""" d = run_dir or os.path.join(bench_dir(cfg, bench), tag) data = build_data(cfg, bench, tag, B, d) if not data: return None write_data_js(data, os.path.join(d, "_data.js")) tpl = load_template() old = 'src="./mock_data/data.js"' if old not in tpl: raise SystemExit( "[!] 模板里找不到 " + old + "\n" " 生成 _view.html 需要把这一行改成 src=\"./_data.js\",\n" " 请确认 view_template.html 的