File size: 49,409 Bytes
f94be80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
#!/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 上限;给 "<case>_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 的 <script src=...> 没被改动。"
        )
    tpl = tpl.replace(old, 'src="./_data.js"')
    out = os.path.join(d, "_view.html")
    with open(out, "w", encoding="utf-8") as f:
        f.write(tpl)
    return out


def render_mock(cfg, bench, tag, B):
    """把某次运行导出到 ./mock_data,供 view_template.html 直接读取"""
    src = os.path.join(bench_dir(cfg, bench), tag)
    dst = os.path.join(ROOT, "mock_data")
    if os.path.isdir(dst):
        shutil.rmtree(dst)
    os.makedirs(dst)

    n = 0
    for f in sorted(os.listdir(src)):
        keep = (f == "_meta.json"
                or f.startswith("_report")
                or f.lower().endswith(IMG_EXT))
        if keep and not (f.startswith("_") and not f.startswith("_report") and f != "_meta.json"):
            shutil.copy2(os.path.join(src, f), os.path.join(dst, f))
            n += 1

    data = build_data(cfg, bench, tag, B, dst, img_prefix="mock_data/")
    if not data:
        raise SystemExit("[!] mock_data 里没有可用图片")
    write_data_js(data, os.path.join(dst, "data.js"))
    return dst, n



# ======================================================================
# 报告
# ======================================================================
def build_report(cfg, bench, tag, P, cases, cells, ct, idv, run_dir, sheet_path, lang):
    en = (lang == "en")
    L = lambda cn, e: e if en else cn
    B = BENCHES[bench]
    Ls = []
    A = Ls.append

    A(f"{L('时间', 'time')}: {datetime.datetime.now():%Y-%m-%d %H:%M:%S}")

    A("\n[1] Pipeline\n")
    _sz = P.get("size") or ["?", "?"]
    _fix = ", ".join(f"{e['name']} @ {e['strength']}"
                     for e in (P.get("extra_loras") or [])) or "-"
    for k, v in [
        ("UNET",        P.get("unet")),
        ("TE",          str(P.get("te")) + (f"  ({P.get('clip_type')})" if P.get("clip_type") else "")),
        ("VAE",         P.get("vae")),
        ("Steps/CFG",   f"{P.get('steps')} / {P.get('cfg')} / {P.get('sampler')} / {P.get('scheduler')}"),
        ("Size",        f"{_sz[0]} x {_sz[1]}"),
        ("Fixed LoRA",  _fix),
        ("Target LoRA", P.get("target_lora")),
        ("Trigger",     str(P.get("trigger", "")).strip().rstrip(",")),
        ("Seeds",       ", ".join(str(s) for s in (cfg.get("seeds") or []))),
    ]:
        A(f"  {k:<12} {'-' if v is None else v}")

    A(f"\n[2] {L('自动指标', 'Auto metrics')}\n")
    if ct:
        A(L("  强度    色调偏移   锐度比     结论",
            "  strength  tone     sharp     reading"))
        for s, (hd, hr) in ct.items():
            if hd < 0.25:
                t = L("几乎不改色调", "tone unchanged")
            elif hd < 0.45:
                t = L("轻微改色调", "tone slightly shifted")
            else:
                t = L("明显改色调", "tone clearly shifted")
            if hr > 1.5:
                t += L(",且明显加锐/加细节", ", sharper / more detail")
            elif hr < 0.85:
                t += L(",且变得更柔/更平", ", softer / flatter")
            A(f"  {s:.2f}      {hd:<9}{hr:<10}{t}")
        A("")
        A(L("  色调偏移 = 0 表示和原始模型色调完全一致;0.5 以上说明这条 LoRA 会明显改变整体观感。",
            "  tone  = 0 means identical tone to base; above 0.5 the LoRA visibly changes the overall look."))
        A(L("  锐度比   = 1.0 表示细节量没变;1.5 表示高频细节比原始模型多 50%。",
            "  sharp = 1.0 same detail level; 1.5 means 50% more high-frequency detail than base."))
    else:
        A(L("  跳过:缺少 PB6@0.00(原始模型对照)或其他强度。用 --profile lite/full/probe 跑完整网格。",
            "  Skipped: needs PB6@0.00 (base reference) plus other strengths. Run --profile lite/full/probe."))

    if idv:
        modes = {m for m, _ in idv.values()}
        A("")
        A(L("  跨 seed 一致性(同一格换种子,出图还像不像)",
            "  Cross-seed consistency (same cell, different seed)"))
        A("    " + L("计算方式:", "method: ") +
          (L("人脸身份", "face identity") if modes == {"脸"}
           else L("结构相似度(画面里没有人脸)", "structural similarity (no face detected)")))
        for (case, s), (m, v) in list(idv.items())[:24]:
            A(f"    {case}@{s:.2f}   {v:<6} ({'face' if m == '脸' else 'struct'})")
        A("    " + L("参考:>0.85 很稳;0.5~0.85 有变化;<0.2 基本每次都不一样。",
                    "reference: >0.85 very stable; 0.5-0.85 some variation; <0.2 basically random."))

    A(f"\n[3] {L('需人工判定', 'Manual judgement')}\n")
    A(L("  1) 下界:从 0.40 往上,哪一档开始「一眼认得出是这个东西」      -> ____",
        "  1) Lower bound: scanning up from 0.40, where is it obviously this thing?   -> ____"))
    A(L(f"  2) 上界:看 {B['upper']} 那一行,哪一档开始失效/被拽回训练分布      -> ____",
        f"  2) Upper bound: on the {B['upper']} row, where does it break or snap back?   -> ____"))
    A(L("  3) WW  = 上界 - 下界                                         -> ____",
        "  3) WW = upper - lower                                       -> ____"))
    _judge = [c["id"] for c in cases if c["id"] not in ("PB0", "PB6")]
    A(L(f"  4) PAS@0.75:这些用例的约束有几个成立(共 {len(_judge)} 个)",
        f"  4) PAS@0.75: how many of these cases keep their constraints ({len(_judge)} total)"))
    A(f"     {' / '.join(_judge)}")
    A(L("  5) 分档:WW>=0.15 且 PAS>=0.6 -> A;WW>=0.10 -> B;否则 C",
        "  5) Grade: WW>=0.15 and PAS>=0.6 -> A; WW>=0.10 -> B; else C"))

    A(f"\n[4] {L('建议', 'Recommendations')}\n")
    tips = []
    if ct:
        worst = max(ct.items(), key=lambda kv: kv[1][0])
        if worst[1][0] > 0.45:
            tips.append(L(
                f"· 色调偏移偏大({worst[1][0]} @ {worst[0]:.2f})。发布时建议写明「会改变整体色调」,或提供低强度用法。",
                f"· Tone drift is high ({worst[1][0]} @ {worst[0]:.2f}). Say so in the model card, or recommend a lower strength."))
        if worst[1][1] > 1.5:
            tips.append(L(
                f"· 锐度明显增强({worst[1][1]}x @ {worst[0]:.2f})。这是训练集锐化的痕迹:想保留原始柔和感,把强度压到 0.60 以下。",
                f"· Sharpness clearly boosted ({worst[1][1]}x @ {worst[0]:.2f}) — the dataset's sharpening shows through. Stay below 0.60 for the base model's softness."))
        if worst[1][1] < 0.85:
            tips.append(L(
                f"· 出图比原始模型更柔({worst[1][1]}x)。如果训练集是高清实拍,说明数据锐度不够。",
                f"· Softer than base ({worst[1][1]}x) — if the dataset was hi-res photography, it may be under-sharp."))
        low = min(ct.items(), key=lambda kv: kv[1][0])
        if low[1][0] < 0.2:
            tips.append(L(
                f"· {low[0]:.2f} 档几乎不改色调,适合「只要形状、不要风格」的用法。",
                f"· At {low[0]:.2f} tone barely changes — good for shape-only use."))
    if idv:
        vals = [v for _, v in idv.values()]
        avg = sum(vals) / len(vals)
        if avg > 0.85:
            tips.append(L("· 跨种子很稳,说明它学的是稳定结构,不是随机拼贴。",
                          "· Very stable across seeds — it learned structure, not random collage."))
        elif avg < 0.4:
            tips.append(L("· 跨种子差异大:没有触发词或强度过低时,它自己不知道要画什么。",
                          "· High seed variance — without the trigger, or at low strength, it does not know what to draw."))
    if (P.get("cfg") or 2) <= 1.2 and (P.get("steps") or 99) <= 12:
        tips.append(L("· 蒸馏管线(低步数 / CFG≈1):风格迁移和长句服从天然弱,测出来的瓶颈别算在 LoRA 头上。",
                      "· Distilled pipeline (low steps / CFG~1): style transfer and long-prompt adherence are inherently weak — do not blame the LoRA."))
    if P.get("extra_loras"):
        nm = ", ".join(e["name"] for e in P["extra_loras"])
        tips.append(L(f"· 管线里还有固定 LoRA({nm})。它和 Target 共享强度预算 —— 记录 WW 时务必一起写。",
                      f"· Pipeline also loads fixed LoRA(s): {nm}. They share the strength budget — always record them with the WW."))
    if not tips:
        tips.append(L("· 指标正常,按上面三节人工判定即可。",
                      "· Metrics normal; proceed with the manual judgement above."))
    for t in tips:
        A(t)

    A(f"\n[5] {L('产出', 'Output')}")
    A(f"  {L('图片目录', 'images')} : {run_dir}")
    for p in (sheet_path if isinstance(sheet_path, list) else [sheet_path]):
        if p:
            A(f"  {L('对照图', 'sheet')}   : {p}")
    _v = os.path.join(run_dir, "_view.html")
    if os.path.exists(_v):
        A(f"  {L('网页版', 'viewer')}   : {_v}")
    A("=" * 68)
    return "\n".join(Ls)


def write_reports(cfg, bench, tag, P, cases, cells, multi, run_dir, sheet_path):
    """一次算指标,写中英两份 _report_cn.txt / _report_en.txt"""
    ct = score_cont(cells)
    idv = score_ids(multi)
    out = {}
    for lang in ("cn", "en"):
        txt = build_report(cfg, bench, tag, P, cases, cells, ct, idv, run_dir, sheet_path, lang)
        p = os.path.join(run_dir, f"_report_{lang}.txt")
        with open(p, "w", encoding="utf-8") as f:
            f.write(txt + "\n")
        out[lang] = (txt, p)
    return out


# ======================================================================
def main():
    ap = argparse.ArgumentParser(
        prog="bench.py",
        description=T(
            "LoRA Bench —— 跑一次 = 一个文件夹(图片 + 总览图 + 中英报告 + 可交互页面)",
            "LoRA Bench — one run produces one folder (images + grids + CN/EN reports + web viewer)",
        ),
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog=T(
            "常用组合:\n"
            "  python bench.py --object                    物体类 lite(15 张)\n"
            "  python bench.py --object --full             物体类全量(48 张)\n"
            "  python bench.py --character --probe         角色类标定 WW\n"
            "  python bench.py --object --view-only 00008  只重生成网页\n"
            "  python bench.py --object --mock             导出 ./mock_data 供改模板\n"
            "\n帮助语言: python bench.py --cn --help",
            "Common usage:\n"
            "  python bench.py --object                    object, lite (15 imgs)\n"
            "  python bench.py --object --full             object, full (48 imgs)\n"
            "  python bench.py --character --probe         character, calibrate WW\n"
            "  python bench.py --object --view-only 00008  rebuild viewer only\n"
            "  python bench.py --object --mock             export ./mock_data for template work\n"
            "\nHelp language: python bench.py --cn --help",
        ),
    )

    ap.add_argument("--cn", action="store_true", help=argparse.SUPPRESS)
    ap.add_argument("--list", action="store_true",
                    help=T("列出所有类别", "list all bench categories"))

    g = ap.add_argument_group(T("类别", "Bench categories"))
    for k, v in BENCHES.items():
        g.add_argument(f"--{k}", action="store_true", help=T(v["cn"], v["en"]))

    g = ap.add_argument_group(T("别名", "Aliases"))
    for k, v in ALIAS.items():
        g.add_argument(f"--{k}", action="store_true",
                       help=T(f"同 --{v}", f"same as --{v}"))

    g = ap.add_argument_group(T("采样", "Sampling"))
    g.add_argument("--profile", default=None, choices=list(PROFILES),
                   help=T("抽样档 lite / full / probe", "sampling profile: lite / full / probe"))
    for p, d in PROFILES.items():
        n = T("全部", "all") if d["cases"] is None else str(len(d["cases"]))
        g.add_argument(f"--{p}", action="store_true",
                       help=T(f"{n} 用例 × {len(d['strengths'])} 强度(= --profile {p})",
                              f"{n} cases × {len(d['strengths'])} strengths (= --profile {p})"))
    g.add_argument("--case", default=None,
                   help=T("逗号分隔,覆盖 profile 的用例", "comma separated, overrides profile cases"))
    g.add_argument("--strength", default=None,
                   help=T("逗号分隔,仅本次生效", "comma separated, this run only"))
    g.add_argument("--seed", default=None,
                   help=T("逗号分隔,仅本次生效", "comma separated, this run only"))

    g = ap.add_argument_group(T("输出与维护", "Output / maintenance"))
    g.add_argument("--report-only", default=None, metavar="NNNNN",
                   help=T("按已有图片重出中英报告", "rebuild _report_cn/en.txt from existing images"))
    g.add_argument("--view-only", default=None, metavar="NNNNN",
                   help=T("只重生成 _view.html(改了模板用这个)", "rebuild _view.html only"))
    g.add_argument("--mock", nargs="?", const="latest", default=None, metavar="NNNNN",
                   help=T("导出某次运行到 ./mock_data,供 view_template.html 读取(默认最新)",
                          "export a run into ./mock_data for view_template.html (default: latest)"))
    g.add_argument("--dry", action="store_true",
                   help=T("只打印计划,不跑", "print the plan only"))
    g.add_argument("--timeout", type=int, default=180,
                   help=T("单张超时秒数", "per-image timeout in seconds"))

    a = ap.parse_args()

    cats = list(BENCHES) + list(ALIAS)

    if a.list:
        for k, v in BENCHES.items():
            print(T(f"--{k:<12} {T(v['cn'], v['en']):<34}{len(v['cases']):>2} 用例   上界 {v['upper']}",
                    f"--{k:<12} {T(v['cn'], v['en']):<34}{len(v['cases']):>2} cases  upper {v['upper']}"))
        return

    cfg = json.load(open(CFG_PATH, encoding="utf-8"))
    base = cfg["comfy_url"]

    picked = [c for c in cats if getattr(a, c, False)]
    if picked:
        bench = ALIAS.get(picked[0], picked[0])
    elif (a.report_only or a.view_only or a.mock) and cfg.get("active_bench"):
        bench = cfg["active_bench"]          # 维护命令可省略类别
    else:
        raise SystemExit("[!] 指定一个类别,如 --object;用 --list 查看全部")
    B = BENCHES[bench]
    # ---------- 只重生成网页(模板改动后) ----------
    if a.view_only:
        d = bench_dir(cfg, bench)
        if not os.path.isdir(d):
            raise SystemExit(f"[!] 类别目录不存在: {d}")
        cand = [e for e in os.listdir(d) if e.endswith("_" + a.view_only)]
        if not cand:
            raise SystemExit(f"[!] 找不到运行 {a.view_only}")
        run_dir = os.path.join(d, cand[0])
        out = render_html(cfg, bench, cand[0], B, run_dir)
        if not out:
            raise SystemExit("[!] 缺少 _meta.json 或图片,无法生成页面")
        print(f"✅ 已重生成: {out}")
        return

    # ---------- 导出 mock_data 给模板调试 ----------
    if a.mock:
        d = bench_dir(cfg, bench)
        if a.mock == "latest":
            cands = sorted([e for e in os.listdir(d) if re.search(r"_\d{5}$", e)], reverse=True)
            if not cands:
                raise SystemExit("[!] 没有可用的运行")
            tag = cands[0]
        else:
            c = [e for e in os.listdir(d) if e.endswith("_" + a.mock)]
            if not c:
                raise SystemExit(f"[!] 找不到运行 {a.mock}")
            tag = c[0]
        dst, n = render_mock(cfg, bench, tag, B)
        print(f"✅ mock_data: {n} 个文件 → {dst}\n   打开 view_template.html 即可预览")
        return

    # ---------- 只重出报告 ----------
    if a.report_only:
        d = bench_dir(cfg, bench)
        cand = [e for e in os.listdir(d) if e.endswith("_" + a.report_only)]
        if not cand:
            raise SystemExit(f"[!] 找不到运行 {a.report_only}")
        run_dir = os.path.join(d, cand[0])
        cfg["seeds"] = (cfg.get("sampling") or {}).get("seeds") or [12345]
        wf = load_prompt(os.path.join(ROOT, cfg["workflow"]))
        P = read_workflow(wf, base)
        P["trigger"] = cfg["trigger"]
        cells, multi = collect(run_dir)
        sheets = render_sheets(cfg, bench, cand[0], P, B["cases"], cells, run_dir)
        dump_meta(run_dir, cfg, bench, cand[0], P)
        rep = write_reports(cfg, bench, cand[0], P, B["cases"], cells, multi, run_dir, sheets)
        render_html(cfg, bench, cand[0], B, run_dir)
        print(rep["cn"][0])
        print(f"\n✅ 已重写:\n   {rep['cn'][1]}\n   {rep['en'][1]}")
        return

    # ---------- 正常跑 ----------
    wf = load_prompt(os.path.join(ROOT, cfg["workflow"]))
    P = read_workflow(wf, base)
    P["trigger"] = cfg["trigger"]

    _pname = (a.profile
              or next((p for p in PROFILES if getattr(a, p, False)), None)
              or (cfg.get("sampling") or {}).get("profile")
              or "lite")
    prof = PROFILES[_pname]
    ids = [c["id"] for c in B["cases"]]
    if a.case:
        want = [x.strip() for x in a.case.split(",") if x.strip()]
        bad = [x for x in want if x not in ids]
        if bad:
            raise SystemExit(f"[!] 未知用例 {bad};本类可用 {ids}")
        case_ids = want
    else:
        case_ids = ids if prof["cases"] is None else [x for x in prof["cases"] if x in ids]

    strengths = ([round(float(x), 3) for x in a.strength.split(",") if x.strip()]
                 if a.strength else list(prof["strengths"]))
    seeds = ([int(x) for x in a.seed.split(",") if x.strip()]
             if a.seed else (cfg.get("sampling") or {}).get("seeds") or [12345])
    cfg["seeds"] = seeds
    cases = [c for c in B["cases"] if c["id"] in case_ids]

    lora_stem = os.path.splitext(P["target_lora"].replace("\\", "/").split("/")[-1])[0]
    tag = next_tag(cfg, bench, lora_stem)
    run_dir = os.path.join(bench_dir(cfg, bench), tag)
    os.makedirs(run_dir, exist_ok=True)      # 提前建,全失败也能写报告

    total = len(cases) * len(strengths) * len(seeds)
    print(f"运行 {tag}")
    print(f"  目标 LoRA : {P['target_lora']}")
    _sz = P.get("size") or ["?", "?"]
    print(f"  管线      : {P.get('unet')} · {P.get('steps')}步 · CFG {P.get('cfg')} · "
          f"{_sz[0]}×{_sz[1]}")
    print(f"  网格      : {len(cases)} 用例 × {len(strengths)} 强度 × {len(seeds)} 种子 = {total} 张")
    print(f"  输出      : {run_dir}\n")

    if a.dry:
        for c in cases:
            for s in strengths:
                for sd in seeds:
                    print("  " + prefix_for(cfg, bench, tag, c["id"], s, sd))
        return

    done = 0
    for c in cases:
        for s in strengths:
            for sd in seeds:
                pre = prefix_for(cfg, bench, tag, c["id"], s, sd)
                w = build(wf, P, c, s, sd, pre)
                try:
                    pid = api(base, "/prompt", {"prompt": w})["prompt_id"]
                except urllib.error.HTTPError as e:
                    print(f"  [x] {c['id']} s={s} HTTP {e.code}: "
                          f"{e.read().decode('utf-8', 'ignore')[:200]}")
                    continue
                except Exception as e:
                    print(f"  [x] {c['id']} s={s}: {e}")
                    continue
                t0 = time.time()
                while True:
                    if time.time() - t0 > a.timeout:
                        print(f"  [!] 超时 {c['id']} s={s}")
                        break
                    try:
                        h = api(base, f"/history/{pid}")
                    except Exception:
                        h = {}
                    if pid in h:
                        ok = (h[pid].get("status", {}).get("status_str") != "error")
                        print(f"  {'ok ' if ok else '错误'} {c['id']} s={s} seed={sd} "
                              f"({time.time() - t0:.1f}s)")
                        done += 1
                        break
                    time.sleep(1.0)

    print(f"\n出图完成 {done}/{total}")

    if (cfg.get("image_format") or "png").lower() == "webp":
        k = to_webp(run_dir, int(cfg.get("webp_quality") or 90))
        print(f"  已转 WebP(q{cfg.get('webp_quality') or 90}): {k} 张")

    cells, multi = collect(run_dir)
    sheets = render_sheets(cfg, bench, tag, P, B["cases"], cells, run_dir)
    dump_meta(run_dir, cfg, bench, tag, P)
    rep = write_reports(cfg, bench, tag, P, B["cases"], cells, multi, run_dir, sheets)
    render_html(cfg, bench, tag, B, run_dir)
    print("\n" + rep["cn"][0])
    print(f"\n✅ 报告:\n   {rep['cn'][1]}\n   {rep['en'][1]}")


if __name__ == "__main__":
    main()