File size: 50,612 Bytes
6fc11b7
 
 
 
 
 
b415bb6
 
6fc11b7
b415bb6
 
 
 
 
6fc11b7
 
b415bb6
6fc11b7
b415bb6
 
6fc11b7
b415bb6
 
 
 
 
 
 
 
 
6fc11b7
 
 
 
b415bb6
6fc11b7
 
 
 
 
 
b415bb6
6fc11b7
 
b415bb6
6fc11b7
 
b415bb6
6fc11b7
 
 
 
b415bb6
6fc11b7
b415bb6
6fc11b7
b415bb6
6fc11b7
 
 
b415bb6
 
 
6fc11b7
 
 
 
 
 
 
b415bb6
 
6fc11b7
 
b415bb6
 
6fc11b7
 
b415bb6
 
 
 
6fc11b7
 
 
 
 
 
 
 
 
 
 
 
 
b415bb6
 
 
 
 
 
 
6fc11b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b415bb6
 
 
 
 
 
 
 
 
 
 
 
 
6fc11b7
 
 
 
 
 
 
 
 
 
b415bb6
6fc11b7
b415bb6
6fc11b7
 
 
 
b415bb6
6fc11b7
 
 
 
 
b415bb6
 
 
 
 
 
 
 
6fc11b7
b415bb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fc11b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b415bb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fc11b7
 
 
 
 
b415bb6
6fc11b7
 
b415bb6
6fc11b7
 
 
 
 
 
b415bb6
6fc11b7
 
b415bb6
6fc11b7
 
 
 
b415bb6
6fc11b7
b415bb6
6fc11b7
 
b415bb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fc11b7
b415bb6
 
6fc11b7
b415bb6
 
6fc11b7
 
 
 
 
 
 
 
 
b415bb6
 
 
 
6fc11b7
 
 
b415bb6
 
6fc11b7
b415bb6
 
6fc11b7
 
b415bb6
 
 
 
6fc11b7
 
 
 
b415bb6
 
 
 
 
 
 
 
 
 
 
 
6fc11b7
 
 
b415bb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fc11b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b415bb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fc11b7
 
 
 
 
 
b415bb6
6fc11b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b415bb6
6fc11b7
 
 
b415bb6
6fc11b7
b415bb6
6fc11b7
b415bb6
 
6fc11b7
 
 
 
 
 
 
 
 
 
b415bb6
 
 
6fc11b7
 
 
 
 
 
 
b415bb6
6fc11b7
 
 
b415bb6
 
6fc11b7
 
 
 
b415bb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fc11b7
 
 
 
 
b415bb6
 
6fc11b7
 
b415bb6
 
 
 
 
6fc11b7
 
 
 
 
 
 
 
b415bb6
 
 
 
6fc11b7
 
 
 
b415bb6
 
6fc11b7
 
 
 
 
 
 
b415bb6
6fc11b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b415bb6
 
 
6fc11b7
b415bb6
 
 
 
 
6fc11b7
 
b415bb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fc11b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b415bb6
 
6fc11b7
 
 
b415bb6
6fc11b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b415bb6
 
6fc11b7
 
 
b415bb6
 
 
 
 
6fc11b7
 
b415bb6
6fc11b7
 
b415bb6
 
 
 
 
 
 
 
 
6fc11b7
b415bb6
 
 
 
6fc11b7
b415bb6
 
6fc11b7
b415bb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fc11b7
 
b415bb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fc11b7
b415bb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fc11b7
 
b415bb6
6fc11b7
 
b415bb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fc11b7
 
 
b415bb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fc11b7
 
 
 
 
 
 
 
 
 
b415bb6
6fc11b7
 
b415bb6
 
6fc11b7
 
b415bb6
 
 
 
 
 
6fc11b7
b415bb6
6fc11b7
b415bb6
 
 
6fc11b7
b415bb6
 
 
 
 
6fc11b7
 
b415bb6
6fc11b7
 
 
 
 
 
 
 
 
b415bb6
 
 
 
 
 
 
 
 
6fc11b7
 
 
 
b415bb6
 
 
 
 
6fc11b7
b415bb6
 
 
6fc11b7
b415bb6
6fc11b7
 
 
b415bb6
 
 
 
 
 
6fc11b7
b415bb6
6fc11b7
b415bb6
 
 
 
 
 
6fc11b7
 
 
 
b415bb6
 
 
 
 
 
 
 
 
 
6fc11b7
b415bb6
6fc11b7
 
 
 
b415bb6
 
 
 
 
 
6fc11b7
 
 
 
b415bb6
 
 
 
 
 
 
 
 
 
 
 
 
6fc11b7
 
b415bb6
 
6fc11b7
b415bb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fc11b7
 
 
 
 
b415bb6
 
6fc11b7
 
b415bb6
 
 
 
 
 
6fc11b7
 
 
 
 
 
 
 
 
 
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
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
#!/usr/bin/env python3
# /// script
# requires-python = ">=3.11,<3.15"
# dependencies = [
#     "remove-ai-watermarks[source-classify]>=0.62.1",
#     "huggingface-hub>=0.26.0",
#     "onnxruntime>=1.19.0",
#     "peft>=0.14.0",
#     "pyarrow>=17.0.0",
#     "safetensors>=0.4.5",
#     "sentence-transformers>=6.1.0",
#     "timm>=1.0.9",
#     "torch>=2.5.0",
#     "transformers>=5.20.0",
# ]
# ///
"""Reproducible open benchmark: `wiltodelta/raiw-source-classify` vs 2025-2026 frontier and legacy HF AI detectors.

Streams pinned, un-gated datasets directly from Hugging Face Hub at immutable commit SHAs
(no private files, no manual dataset setup) across three evaluation regimes:
1. Non-AI false-positive traps (camera photos, UI screenshots, science diagrams,
   scanned receipts and invoices).
2. Untouched native AI generator exports across FLUX.1-dev, OpenAI GPT-Image-1,
   Google Gemini Nano Banana Pro, Stable Diffusion XL, and Midjourney v6.1.
3. Transcoded crowd-arena AI outputs across multi-provider preference shards re-encoded
   into square JPEG/WebP/PNG arena containers.

Evaluates open-weights configurations across artifact size, license, runtime stack,
warm single-image CPU latency, non-AI FPR by slice, binary recall/precision, and exact
generator-family attribution accuracy.

Run directly with `uv`:

    uv run https://huggingface.co/wiltodelta/raiw-source-classify/resolve/main/benchmark.py
    uv run benchmark.py --tier frontier --json-out results.json
    uv run benchmark.py --raiw-only
"""

from __future__ import annotations

import argparse
import gc
import hashlib
import io
import itertools
import json
import logging
import math
import statistics
import sys
import time
from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass, field
from pathlib import Path
from typing import TYPE_CHECKING, Any, Literal

import numpy as np
from PIL import Image, ImageOps

if TYPE_CHECKING:
    from collections.abc import Callable

    from huggingface_hub import HfApi, HfFileSystem

_REPO_SRC = Path(__file__).resolve().parents[2] / "src"
if (_REPO_SRC / "remove_ai_watermarks").is_dir() and str(_REPO_SRC) not in sys.path:
    sys.path.insert(0, str(_REPO_SRC))

log = logging.getLogger(__name__)

Regime = Literal["non_ai", "native", "arena"]

MIN_SIDE_PX = 256
MAX_AREA_PX = 9_000_000
LATENCY_SAMPLE_COUNT = 30
SMAUG_CPU_LATENCY_COUNT = 10
EXIF_ORIENTATION_TAG = 274
TRANSPOSED_ORIENTATIONS = frozenset({5, 6, 7, 8})
IMAGE_SUFFIXES = (".png", ".jpg", ".jpeg", ".webp")

IMAGENET_MEAN = np.array([0.485, 0.456, 0.406], dtype=np.float32).reshape(3, 1, 1)
IMAGENET_STD = np.array([0.229, 0.224, 0.225], dtype=np.float32).reshape(3, 1, 1)


@dataclass(frozen=True)
class SampleItem:
    """One deduplicated benchmark image in original encoded bytes."""

    sha256: str
    category: str
    expected_provider: str | None
    container_format: str
    width: int
    height: int
    raw_bytes: bytes
    regime: Regime = "arena"

    def __post_init__(self) -> None:
        if (self.regime == "non_ai") != (self.expected_provider is None):
            raise ValueError(
                f"Inconsistent SampleItem regime={self.regime!r} for expected_provider={self.expected_provider!r}"
            )

    @property
    def is_ai(self) -> bool:
        return self.expected_provider is not None


@dataclass(frozen=True)
class ParquetSliceSpec:
    """Pinned Hugging Face single-image Parquet slice."""

    category: str
    dataset_id: str
    revision: str
    parquet_path: str
    limit: int = 50
    expected_provider: str | None = None
    regime: Regime = "non_ai"


@dataclass(frozen=True)
class RawRepoSliceSpec:
    """Pinned Hugging Face dataset tree slice containing native generator export files."""

    category: str
    dataset_id: str
    revision: str
    path_in_repo: str | None
    limit: int
    expected_provider: str


@dataclass(frozen=True)
class PreferenceSliceSpec:
    """Pinned Hugging Face pairwise-preference Parquet slice."""

    dataset_id: str
    revision: str
    parquet_path: str
    per_provider_cap: int
    provider_map: dict[str, str]
    max_row_groups: int | None = None
    substring_match: bool = False


@dataclass(frozen=True)
class OpenDetectorSpec:
    """Pinned Hugging Face `transformers` binary AI-image detector checkpoint."""

    repo_id: str
    revision: str
    weights_mb: float
    license_id: str
    ai_labels: frozenset[str] = field(default_factory=frozenset)
    tier: str = "legacy"
    use_sigmoid: bool = False


@dataclass(frozen=True)
class TimmViTSpec:
    """Pinned `vit_small_patch16_384` checkpoint evaluated at native CommFor preprocessing."""

    repo_id: str
    revision: str
    weights_mb: float
    license_id: str
    logit_threshold: float


RAPIDATA_4O_PROVIDER_MAP: dict[str, str] = {
    "4o-26-3-25": "openai",
    "dalle-3": "openai",
    "imagen-3": "google",
    "nano-banana": "google",
    "flux-1-pro": "flux",
    "flux-1.1-pro": "flux",
    "midjourney-5.2": "midjourney",
    "stable-diffusion-3": "stable_diffusion",
    "ideogram": "ideogram",
    "aurora-20-1-25": "xai",
    "seedream-3": "bytedance",
}

SEEDREAM_PROVIDER_MAP: dict[str, str] = {
    "seedream": "bytedance",
}

NON_AI_SLICES: tuple[ParquetSliceSpec, ...] = (
    ParquetSliceSpec(
        category="camera_photo",
        dataset_id="visual-layer/imagenet-1k-vl-enriched",
        revision="ac6afcdeb3be31c5ff6a7ff579874b3d372b7074",
        parquet_path="data/validation-00000-of-00008.parquet",
        limit=50,
    ),
    ParquetSliceSpec(
        category="ui_screenshot",
        dataset_id="rootsautomation/ScreenSpot",
        revision="0be08781e2e188582f6131625ae1598d443b4d5d",
        parquet_path="data/test-00000-of-00003.parquet",
        limit=50,
    ),
    ParquetSliceSpec(
        category="science_diagram",
        dataset_id="lmms-lab/ai2d",
        revision="c83a9b9692933aff8349157c88a413df9d02c4e5",
        parquet_path="data/test-00000-of-00002.parquet",
        limit=50,
    ),
    ParquetSliceSpec(
        category="receipt_invoice",
        dataset_id="mychen76/invoices-and-receipts_ocr_v1",
        revision="83835c87346de32ac9223bdce5264e69ef3366ad",
        parquet_path="data/test-00000-of-00001-af2d92d1cee28514.parquet",
        limit=50,
    ),
)

NATIVE_RAW_AI_SLICES: tuple[RawRepoSliceSpec, ...] = (
    RawRepoSliceSpec(
        category="native_flux",
        dataset_id="LukasT9/Flux-1-Dev-Images-1k",
        revision="ade8147d5f10bac016d691fd2b564e877e6315f7",
        path_in_repo="images",
        limit=25,
        expected_provider="flux",
    ),
    RawRepoSliceSpec(
        category="native_openai",
        dataset_id="Tungtom2004/Output_dataset_gptImage1",
        revision="ef327a0e5f089b34e27f25d2a7700c5fba811bf7",
        path_in_repo="10000-20000",
        limit=25,
        expected_provider="openai",
    ),
    RawRepoSliceSpec(
        category="native_stable_diffusion",
        dataset_id="aziksh/animals_with_objects_sdxl",
        revision="478f6a267926671d01d468e9211b5f5132d90aa3",
        path_in_repo="cat_blue boat",
        limit=25,
        expected_provider="stable_diffusion",
    ),
    RawRepoSliceSpec(
        category="native_midjourney",
        dataset_id="saq1b/midjourney-v6.1",
        revision="97a3ff2aea504384145af73d77df7f124613e9c6",
        path_in_repo=None,
        limit=25,
        expected_provider="midjourney",
    ),
)

NATIVE_GOOGLE_PARQUET_SPEC = ParquetSliceSpec(
    category="native_google",
    dataset_id="kaupane/nano-banana-pro-gen",
    revision="5c61390a0307e6c38788ab85a5280fbc9893415b",
    parquet_path="data/train-00000-of-00005.parquet",
    limit=25,
    expected_provider="google",
    regime="native",
)

RAPIDATA_4O_SPEC = PreferenceSliceSpec(
    dataset_id="Rapidata/OpenAI-4o_t2i_human_preference",
    revision="ca6c084c0fa0021d66bbdd44d6e61fbeb8e1198a",
    parquet_path="data/train-00000-of-00022.parquet",
    per_provider_cap=35,
    provider_map=RAPIDATA_4O_PROVIDER_MAP,
)

NANO_BANANA_ARENA_SPEC = ParquetSliceSpec(
    category="ai_google",
    dataset_id="bitmind/nano-banana",
    revision="9ea8da32a5be03f4946e6cb10c2d2f8e90f0a0a4",
    parquet_path="data/train-00000-of-00031.parquet",
    limit=25,
    expected_provider="google",
    regime="arena",
)

SEEDREAM_ARENA_SPEC = PreferenceSliceSpec(
    dataset_id="Rapidata/Seedream-3_t2i_human_preference",
    revision="0867c34e5583d63376e3289b938e6a2cb6f43751",
    parquet_path="data/train-00000-of-00101.parquet",
    per_provider_cap=25,
    provider_map=SEEDREAM_PROVIDER_MAP,
    max_row_groups=3,
    substring_match=True,
)

FRONTIER_VIT_DETECTORS: tuple[TimmViTSpec, ...] = (
    TimmViTSpec(
        repo_id="TensorFold/detectra-v1",
        revision="2bbc31161daa46932e05ee6f96e7016ee817bbde",
        weights_mb=41.7,
        license_id="mit",
        logit_threshold=0.6190392,  # logit(0.65)
    ),
    TimmViTSpec(
        repo_id="Thermostatic/community-forensics-frontier-detector-2026-08",
        revision="16db135220b318d811b207db576d90368980b595",
        weights_mb=41.8,
        license_id="mit",
        logit_threshold=1.359375,
    ),
)

COMMFOR_DEEPFAKEDET_SPEC = OpenDetectorSpec(
    repo_id="buildborderless/CommunityForensics-DeepfakeDet-ViT",
    revision="ac6ee457bea904a373065754107451793b56db00",
    weights_mb=83.2,
    license_id="mit",
    tier="frontier",
    use_sigmoid=True,
)

SMAUG_REPO = "abacusai/Smaug-AI-Image-Detector"
SMAUG_REVISION = "c7c95951e04a87b884a4ad479401b1128c3903bd"

DETECTRA_OPENFAKE_REPO = "s-gehring/detectra-openfake"
DETECTRA_OPENFAKE_REVISION = "c84565e5bf8969d95908e12844f551e77ae87105"

OPENFAKE_BASE_REPO = "bendavidsteel/openfake-swinv2-base"
OPENFAKE_BASE_REVISION = "90837e19c45b9a49af435d35071114ba1448c239"
OPENFAKE_ATTR_REPO = "bendavidsteel/openfake-swinv2-base-attribution-head"
OPENFAKE_ATTR_REVISION = "000944122ba497638fc88eea671dbe998fe635da"
OPENFAKE_LORA_REPO = "bendavidsteel/openfake-swinv2-base-textgraphics-lora"
OPENFAKE_LORA_REVISION = "b319ab4689286a620c3ef4a678709f4d04f25793"

LEGACY_BINARY_DETECTORS: tuple[OpenDetectorSpec, ...] = (
    OpenDetectorSpec(
        repo_id="haywoodsloan/ai-image-detector-deploy",
        revision="4ae1822603cb1ec980c217cfde1ef1b8e0477a16",
        ai_labels=frozenset({"artificial"}),
        weights_mb=744.7,
        license_id="apache-2.0",
    ),
    OpenDetectorSpec(
        repo_id="Organika/sdxl-detector",
        revision="b37fede8562cb72b89ec201c0987f96ba21b518a",
        ai_labels=frozenset({"artificial"}),
        weights_mb=331.4,
        license_id="cc-by-nc-3.0",
    ),
    OpenDetectorSpec(
        repo_id="dima806/ai_vs_human_generated_image_detection",
        revision="341594f2b003d185db789becf48a2286c177b926",
        ai_labels=frozenset({"ai-generated", "artificial", "fake"}),
        weights_mb=327.3,
        license_id="apache-2.0",
    ),
    OpenDetectorSpec(
        repo_id="umm-maybe/AI-image-detector",
        revision="c7e223baf11bc40528af364ba7bdea030ef42f9e",
        ai_labels=frozenset({"artificial"}),
        weights_mb=331.5,
        license_id="cc-by-4.0",
    ),
    OpenDetectorSpec(
        repo_id="Ateeqq/ai-vs-human-image-detector",
        revision="60e82406916921b823616bee33397baab38af3f0",
        ai_labels=frozenset({"ai"}),
        weights_mb=354.4,
        license_id="apache-2.0",
    ),
)

OPENFAKE_FAMILY_MAP: dict[str, str] = {
    "DALLE3": "openai",
    "GPT-Image": "openai",
    "Imagen": "google",
    "FLUX1": "flux",
    "Midjourney": "midjourney",
    "SD1": "stable_diffusion",
    "SD2": "stable_diffusion",
    "SD3": "stable_diffusion",
    "SDXL": "stable_diffusion",
    "Ideogram": "ideogram",
    "Grok": "xai",
}


def _sigmoid(x: float) -> float:
    if x >= 0.0:
        z = math.exp(-x)
        return 1.0 / (1.0 + z)
    z = math.exp(x)
    return z / (1.0 + z)


def _release_memory() -> None:
    gc.collect()
    try:
        import torch

        if torch.cuda.is_available():
            torch.cuda.empty_cache()
        if torch.backends.mps.is_available():
            torch.mps.empty_cache()
    except ImportError:
        return


def _inspect_image_bytes(raw_bytes: bytes) -> tuple[str, int, int] | None:
    try:
        with Image.open(io.BytesIO(raw_bytes)) as im:
            w, h = im.size
            if min(w, h) < MIN_SIDE_PX or (w * h) > MAX_AREA_PX:
                return None
            fmt = str(im.format or "UNKNOWN")
            orientation = im.getexif().get(EXIF_ORIENTATION_TAG, 1)
            im.load()
            if orientation in TRANSPOSED_ORIENTATIONS:
                w, h = h, w
            return fmt, w, h
    except Exception:
        return None


def _decode_rgb(raw_bytes: bytes) -> Image.Image:
    with Image.open(io.BytesIO(raw_bytes)) as im:
        ImageOps.exif_transpose(im, in_place=True)
        return im.convert("RGB") if im.mode != "RGB" else im.copy()


def _preprocess_commfor_chw(rgb: Image.Image, short_edge: int = 440, crop: int = 384) -> np.ndarray:
    w, h = rgb.size
    scale = short_edge / min(w, h)
    nw, nh = round(w * scale), round(h * scale)
    resized = rgb.resize((nw, nh), Image.Resampling.BILINEAR)
    left = (nw - crop) // 2
    top = (nh - crop) // 2
    cropped = resized.crop((left, top, left + crop, top + crop))
    arr = np.asarray(cropped, dtype=np.float32) / 255.0
    chw = np.transpose(arr, (2, 0, 1))
    return (chw - IMAGENET_MEAN) / IMAGENET_STD


def _smaug_views(rgb: Image.Image) -> list[Image.Image]:
    crop_px, side_px = 528, 576
    w, h = rgb.size
    if min(w, h) > side_px:
        scale = side_px / min(w, h)
        rgb = rgb.resize((round(w * scale), round(h * scale)), Image.Resampling.LANCZOS)
    elif min(w, h) < crop_px:
        scale = crop_px / min(w, h)
        rgb = rgb.resize((math.ceil(w * scale), math.ceil(h * scale)), Image.Resampling.BICUBIC)
    w, h = rgb.size
    cx, cy = (w - crop_px) // 2, (h - crop_px) // 2
    if w >= h:
        return [rgb.crop((x, cy, x + crop_px, cy + crop_px)) for x in (0, cx, w - crop_px)]
    return [rgb.crop((cx, y, cx + crop_px, y + crop_px)) for y in (0, cy, h - crop_px)]


def _try_build_sample(
    cell: Any,
    local_seen: set[str],
    *,
    category: str,
    expected_provider: str | None,
    regime: Regime,
) -> SampleItem | None:
    raw = cell["bytes"] if isinstance(cell, dict) else cell
    if not isinstance(raw, (bytes, bytearray)):
        return None
    raw_bytes = bytes(raw)
    sha = hashlib.sha256(raw_bytes).hexdigest()
    if sha in local_seen:
        return None
    meta = _inspect_image_bytes(raw_bytes)
    if meta is None:
        return None
    fmt, w, h = meta
    local_seen.add(sha)
    return SampleItem(
        sha256=sha,
        category=category,
        expected_provider=expected_provider,
        container_format=fmt,
        width=w,
        height=h,
        raw_bytes=raw_bytes,
        regime=regime,
    )


def _latency_indices(total: int, count: int = LATENCY_SAMPLE_COUNT) -> list[int]:
    """Deterministic stratified indices across the full sample list for CPU timing."""
    if total <= count:
        return list(range(total))
    step = max(1, total // count)
    return [i * step for i in range(count)]


def _read_single_image_slice(fs: HfFileSystem, spec: ParquetSliceSpec) -> list[SampleItem]:
    import pyarrow.parquet as pq

    items: list[SampleItem] = []
    local_seen: set[str] = set()
    uri = f"datasets/{spec.dataset_id}/{spec.parquet_path}"
    with fs.open(uri, "rb", revision=spec.revision) as handle:
        pf = pq.ParquetFile(handle)
        for batch in pf.iter_batches(batch_size=spec.limit + 16, columns=["image"]):
            col = batch["image"]
            for row_idx in range(len(col)):
                if len(items) >= spec.limit:
                    break
                sample = _try_build_sample(
                    col[row_idx].as_py(),
                    local_seen,
                    category=spec.category,
                    expected_provider=spec.expected_provider,
                    regime=spec.regime,
                )
                if sample is not None:
                    items.append(sample)
            if len(items) >= spec.limit:
                break
    log.info("Loaded %s images from %s (%s)", len(items), spec.dataset_id, spec.category)
    return items


def _read_raw_repo_slice(api: HfApi, spec: RawRepoSliceSpec) -> list[SampleItem]:
    from huggingface_hub import hf_hub_download

    tree_iter = api.list_repo_tree(
        spec.dataset_id,
        path_in_repo=spec.path_in_repo,
        repo_type="dataset",
        revision=spec.revision,
    )
    candidate_paths = list(
        itertools.islice(
            (
                str(entry.path)
                for entry in tree_iter
                if str(getattr(entry, "path", "")).lower().endswith(IMAGE_SUFFIXES)
            ),
            spec.limit + 8,
        )
    )

    def _download_bytes(rel_path: str) -> bytes:
        local_path = Path(
            hf_hub_download(
                spec.dataset_id,
                rel_path,
                repo_type="dataset",
                revision=spec.revision,
            )
        )
        return local_path.read_bytes()

    items: list[SampleItem] = []
    local_seen: set[str] = set()
    offset = 0
    with ThreadPoolExecutor(max_workers=8) as pool:
        while len(items) < spec.limit and offset < len(candidate_paths):
            needed = spec.limit - len(items)
            batch_paths = candidate_paths[offset : offset + needed]
            offset += len(batch_paths)
            for blob in pool.map(_download_bytes, batch_paths):
                sample = _try_build_sample(
                    blob,
                    local_seen,
                    category=spec.category,
                    expected_provider=spec.expected_provider,
                    regime="native",
                )
                if sample is not None:
                    items.append(sample)
                    if len(items) >= spec.limit:
                        break
    log.info("Loaded %s native images from %s (%s)", len(items), spec.dataset_id, spec.category)
    return items


def _read_preference_slice(fs: HfFileSystem, spec: PreferenceSliceSpec) -> list[SampleItem]:
    import pyarrow.parquet as pq

    items: list[SampleItem] = []
    local_seen: set[str] = set()
    counts: dict[str, int] = {}
    norm_map = {k.lower(): v for k, v in spec.provider_map.items()}
    target_providers = set(norm_map.values())
    uri = f"datasets/{spec.dataset_id}/{spec.parquet_path}"

    def _resolve_provider(raw_tag: Any) -> str | None:
        tag_lc = str(raw_tag).lower()
        if spec.substring_match:
            return next((prov for needle, prov in norm_map.items() if needle in tag_lc), None)
        return norm_map.get(tag_lc)

    with fs.open(uri, "rb", revision=spec.revision) as handle:
        pf = pq.ParquetFile(handle)
        rg_limit = pf.num_row_groups if spec.max_row_groups is None else min(spec.max_row_groups, pf.num_row_groups)
        for rg_idx in range(rg_limit):
            if all(counts.get(p, 0) >= spec.per_provider_cap for p in target_providers):
                break
            meta_rg = pf.read_row_group(rg_idx, columns=["model1", "model2"])
            p1_list = [_resolve_provider(t) for t in meta_rg["model1"].to_pylist()]
            p2_list = [_resolve_provider(t) for t in meta_rg["model2"].to_pylist()]
            need_col1 = any(p is not None and counts.get(p, 0) < spec.per_provider_cap for p in p1_list)
            need_col2 = any(p is not None and counts.get(p, 0) < spec.per_provider_cap for p in p2_list)
            img_cols = [c for c, flag in (("image1", need_col1), ("image2", need_col2)) if flag]
            if not img_cols:
                continue
            img_rg = pf.read_row_group(rg_idx, columns=img_cols)
            for row_idx, (p1, p2) in enumerate(zip(p1_list, p2_list, strict=True)):
                for prov, icol in ((p1, "image1"), (p2, "image2")):
                    if prov is None or counts.get(prov, 0) >= spec.per_provider_cap or icol not in img_cols:
                        continue
                    sample = _try_build_sample(
                        img_rg[icol][row_idx].as_py(),
                        local_seen,
                        category=f"ai_{prov}",
                        expected_provider=prov,
                        regime="arena",
                    )
                    if sample is not None:
                        counts[prov] = counts.get(prov, 0) + 1
                        items.append(sample)
    log.info("Loaded %s preference AI images from %s", len(items), spec.dataset_id)
    return items


def _dedup_ordered(batches: list[list[SampleItem]], seen_shas: set[str]) -> list[SampleItem]:
    merged: list[SampleItem] = []
    for batch in batches:
        for item in batch:
            if item.sha256 not in seen_shas:
                seen_shas.add(item.sha256)
                merged.append(item)
    return merged


def load_all_samples(fs: HfFileSystem, api: HfApi) -> list[SampleItem]:
    """Stream all pinned non-AI, native-export AI, and crowd-arena AI slices in deterministic order."""
    with ThreadPoolExecutor(max_workers=6) as pool:
        non_ai_futures = [pool.submit(_read_single_image_slice, fs, spec) for spec in NON_AI_SLICES]
        native_raw_futures = [pool.submit(_read_raw_repo_slice, api, spec) for spec in NATIVE_RAW_AI_SLICES]
        fut_native_google = pool.submit(_read_single_image_slice, fs, NATIVE_GOOGLE_PARQUET_SPEC)
        fut_4o = pool.submit(_read_preference_slice, fs, RAPIDATA_4O_SPEC)
        fut_nb = pool.submit(_read_single_image_slice, fs, NANO_BANANA_ARENA_SPEC)
        fut_sd = pool.submit(_read_preference_slice, fs, SEEDREAM_ARENA_SPEC)

        seen_shas: set[str] = set()
        return _dedup_ordered(
            [
                *[f.result() for f in non_ai_futures],
                *[f.result() for f in native_raw_futures],
                fut_native_google.result(),
                fut_4o.result(),
                fut_nb.result(),
                fut_sd.result(),
            ],
            seen_shas,
        )


def _select_torch_accelerator() -> Any:
    import torch

    if torch.cuda.is_available():
        return torch.device("cuda")
    if torch.backends.mps.is_available():
        return torch.device("mps")
    return torch.device("cpu")


def _evaluate_rgb_detector(
    samples: list[SampleItem],
    rgb_cache: list[Image.Image] | None,
    *,
    score_cpu_batch: Callable[[list[Image.Image]], list[dict[str, Any]]],
    make_batch_scorer: Callable[[], Callable[[list[Image.Image]], list[dict[str, Any]]]] | None = None,
    batch_size: int = 16,
    latency_count: int = LATENCY_SAMPLE_COUNT,
    model_id: str,
    revision: str,
    runtime: str,
    weights_mb: float,
    license_id: str,
    tier: str = "frontier",
) -> dict[str, Any]:
    """Shared harness: warm up on CPU, measure single-image CPU latency from raw bytes, then batch untimed items."""
    rgb0 = rgb_cache[0] if rgb_cache is not None else _decode_rgb(samples[0].raw_bytes)
    _ = score_cpu_batch([rgb0])

    lat_idxs = _latency_indices(len(samples), latency_count)
    lat_idx_set = set(lat_idxs)
    latencies_ms: list[float] = []
    preds_by_idx: dict[int, dict[str, Any]] = {}

    for idx in lat_idxs:
        t0 = time.perf_counter()
        rgb = _decode_rgb(samples[idx].raw_bytes)
        pred = score_cpu_batch([rgb])[0]
        latencies_ms.append((time.perf_counter() - t0) * 1000.0)
        preds_by_idx[idx] = pred

    batch_scorer = make_batch_scorer() if make_batch_scorer is not None else score_cpu_batch
    untimed_indices = [idx for idx in range(len(samples)) if idx not in lat_idx_set]
    for start in range(0, len(untimed_indices), batch_size):
        chunk_idxs = untimed_indices[start : start + batch_size]
        rgbs = [rgb_cache[idx] if rgb_cache is not None else _decode_rgb(samples[idx].raw_bytes) for idx in chunk_idxs]
        for sample_idx, pred in zip(chunk_idxs, batch_scorer(rgbs), strict=True):
            preds_by_idx[sample_idx] = pred

    return _summarize_predictions(
        model_id=model_id,
        revision=revision,
        runtime=runtime,
        weights_mb=weights_mb,
        license_id=license_id,
        median_cpu_ms=round(statistics.median(latencies_ms), 1),
        samples=samples,
        preds=[preds_by_idx[i] for i in range(len(samples))],
        tier=tier,
    )


def evaluate_raiw(samples: list[SampleItem]) -> dict[str, Any]:
    """Evaluate `wiltodelta/raiw-source-classify` in pure NumPy on CPU."""
    from remove_ai_watermarks import source_classify as sc

    weights_file = sc.resolve_weights_path()
    weights_mb = round(weights_file.stat().st_size / (1024 * 1024), 2)

    def _classify_one(raw_bytes: bytes) -> dict[str, Any]:
        with Image.open(io.BytesIO(raw_bytes)) as im:
            res = sc.classify_source(im, weights_path=weights_file)
        return {
            "pred_ai": res.label != "unknown",
            "pred_provider": None if res.label == "unknown" else res.label,
        }

    _classify_one(samples[0].raw_bytes)

    lat_idxs = _latency_indices(len(samples))
    lat_idx_set = set(lat_idxs)
    latencies_ms: list[float] = []
    preds_by_idx: dict[int, dict[str, Any]] = {}

    for idx in lat_idxs:
        t0 = time.perf_counter()
        preds_by_idx[idx] = _classify_one(samples[idx].raw_bytes)
        latencies_ms.append((time.perf_counter() - t0) * 1000.0)

    untimed_indices = [idx for idx in range(len(samples)) if idx not in lat_idx_set]
    with ThreadPoolExecutor(max_workers=4) as pool:
        untimed_preds = list(pool.map(lambda i: _classify_one(samples[i].raw_bytes), untimed_indices))
    for idx, pred in zip(untimed_indices, untimed_preds, strict=True):
        preds_by_idx[idx] = pred

    return _summarize_predictions(
        model_id=sc.WEIGHTS_REPO,
        revision=sc.WEIGHTS_REVISION,
        runtime="NumPy + OpenCV (CPU)",
        weights_mb=weights_mb,
        license_id="apache-2.0",
        median_cpu_ms=round(statistics.median(latencies_ms), 1),
        samples=samples,
        preds=[preds_by_idx[i] for i in range(len(samples))],
        tier="raiw",
    )


def evaluate_hf_binary_detector(
    spec: OpenDetectorSpec,
    samples: list[SampleItem],
    rgb_cache: list[Image.Image] | None = None,
) -> dict[str, Any]:
    """Evaluate an open Hugging Face `transformers` binary AI-image detector."""
    import torch
    from transformers import AutoImageProcessor, AutoModelForImageClassification, ViTForImageClassification

    processor = AutoImageProcessor.from_pretrained(spec.repo_id, revision=spec.revision)
    ai_indices: list[int] = []
    if spec.use_sigmoid:
        model = ViTForImageClassification.from_pretrained(spec.repo_id, revision=spec.revision)
    else:
        model = AutoModelForImageClassification.from_pretrained(spec.repo_id, revision=spec.revision)
        id2label = {int(k): str(v).lower() for k, v in model.config.id2label.items()}
        ai_indices = [idx for idx, lbl in id2label.items() if lbl in spec.ai_labels]
        if not ai_indices:
            raise RuntimeError(f"Could not map AI label for {spec.repo_id}: {id2label}")

    def logits_to_probs(logits: Any) -> Any:
        if spec.use_sigmoid:
            return torch.sigmoid(logits.reshape(-1))
        return torch.softmax(logits, dim=-1)[:, ai_indices].sum(dim=-1)

    model.train(False)
    model.to("cpu")

    def _score_on(dev: Any) -> Callable[[list[Image.Image]], list[dict[str, Any]]]:
        def _run(rgbs: list[Image.Image]) -> list[dict[str, Any]]:
            inputs = {k: v.to(dev) for k, v in processor(images=rgbs, return_tensors="pt").items()}
            with torch.inference_mode():
                probs = logits_to_probs(model(**inputs).logits).cpu().tolist()
            return [{"pred_ai": float(p_val) >= 0.5, "pred_provider": None} for p_val in probs]

        return _run

    def _move_to_acc() -> Callable[[list[Image.Image]], list[dict[str, Any]]]:
        dev = _select_torch_accelerator()
        model.to(dev)
        return _score_on(dev)

    return _evaluate_rgb_detector(
        samples,
        rgb_cache,
        score_cpu_batch=_score_on(torch.device("cpu")),
        make_batch_scorer=_move_to_acc,
        model_id=spec.repo_id,
        revision=spec.revision,
        runtime="PyTorch + Transformers",
        weights_mb=spec.weights_mb,
        license_id=spec.license_id,
        tier=spec.tier,
    )


def evaluate_timm_vit_detector(
    spec: TimmViTSpec,
    samples: list[SampleItem],
    rgb_cache: list[Image.Image] | None = None,
) -> dict[str, Any]:
    """Evaluate a `vit_small_patch16_384` frontier detector at official 440->384 preprocessing."""
    import timm
    import torch
    from huggingface_hub import hf_hub_download
    from safetensors.torch import load_file

    raw_state = load_file(hf_hub_download(spec.repo_id, "model.safetensors", revision=spec.revision))
    clean_state = {(k[4:] if k.startswith("vit.") else k): v for k, v in raw_state.items()}
    del raw_state
    model = timm.create_model("vit_small_patch16_384", pretrained=False, num_classes=1)
    model.load_state_dict(clean_state, strict=True)
    del clean_state
    model.train(False)
    model.to("cpu")

    def _score_on(dev: Any) -> Callable[[list[Image.Image]], list[dict[str, Any]]]:
        def _run(rgbs: list[Image.Image]) -> list[dict[str, Any]]:
            batch_np = np.stack([_preprocess_commfor_chw(rgb) for rgb in rgbs])
            with torch.inference_mode():
                logits = model(torch.from_numpy(batch_np).to(dev)).reshape(-1).cpu().tolist()
            return [{"pred_ai": float(val) >= spec.logit_threshold, "pred_provider": None} for val in logits]

        return _run

    def _move_to_acc() -> Callable[[list[Image.Image]], list[dict[str, Any]]]:
        dev = _select_torch_accelerator()
        model.to(dev)
        return _score_on(dev)

    return _evaluate_rgb_detector(
        samples,
        rgb_cache,
        score_cpu_batch=_score_on(torch.device("cpu")),
        make_batch_scorer=_move_to_acc,
        model_id=spec.repo_id,
        revision=spec.revision,
        runtime="PyTorch + timm / ONNX",
        weights_mb=spec.weights_mb,
        license_id=spec.license_id,
        tier="frontier",
    )


def evaluate_detectra_openfake(
    samples: list[SampleItem],
    rgb_cache: list[Image.Image] | None = None,
) -> dict[str, Any]:
    """Evaluate `s-gehring/detectra-openfake` INT8 ONNX with its documented two-pass Platt rule."""
    import onnxruntime as ort
    from huggingface_hub import hf_hub_download

    onnx_path = Path(hf_hub_download(DETECTRA_OPENFAKE_REPO, "model.onnx", revision=DETECTRA_OPENFAKE_REVISION))
    weights_mb = round(onnx_path.stat().st_size / (1024 * 1024), 2)
    sess_opts = ort.SessionOptions()
    sess_opts.intra_op_num_threads = 4
    sess = ort.InferenceSession(str(onnx_path), sess_options=sess_opts, providers=["CPUExecutionProvider"])
    in_name = sess.get_inputs()[0].name

    def _platt(logit_val: float) -> float:
        return _sigmoid(0.552 * logit_val + 0.1933)

    def _score_batch(rgbs: list[Image.Image]) -> list[dict[str, Any]]:
        x_batch = np.stack([_preprocess_commfor_chw(rgb, short_edge=440, crop=384) for rgb in rgbs])
        logits = np.asarray(sess.run(None, {in_name: x_batch})[0]).reshape(-1)
        probs = [_platt(float(val)) for val in logits]

        borderline = [idx for idx, p_val in enumerate(probs) if 0.5 <= p_val < 0.95]
        if borderline:
            hi_crops = np.stack(
                [_preprocess_commfor_chw(rgbs[idx], short_edge=se, crop=384) for idx in borderline for se in (576, 768)]
            )
            hi_logits = np.asarray(sess.run(None, {in_name: hi_crops})[0]).reshape(len(borderline), 2)
            for b_pos, idx in enumerate(borderline):
                probs[idx] = max(probs[idx], _platt(float(hi_logits[b_pos, 0])), _platt(float(hi_logits[b_pos, 1])))

        return [{"pred_ai": p_val >= 0.95, "pred_provider": None} for p_val in probs]

    return _evaluate_rgb_detector(
        samples,
        rgb_cache,
        score_cpu_batch=_score_batch,
        model_id=DETECTRA_OPENFAKE_REPO,
        revision=DETECTRA_OPENFAKE_REVISION,
        runtime="ONNX Runtime INT8 (CPU)",
        weights_mb=weights_mb,
        license_id="cc-by-nc-4.0",
        tier="frontier",
    )


def evaluate_openfake_attribution(
    samples: list[SampleItem],
    rgb_cache: list[Image.Image] | None = None,
) -> dict[str, Any]:
    """Evaluate OpenFake SwinV2-base paired with its 15-family sub-center attribution head."""
    import torch
    import torch.nn as nn
    import torch.nn.functional as f_nn
    from huggingface_hub import hf_hub_download
    from safetensors.torch import load_file
    from transformers import AutoImageProcessor, AutoModelForImageClassification

    base_st = Path(hf_hub_download(OPENFAKE_BASE_REPO, "model.safetensors", revision=OPENFAKE_BASE_REVISION))
    attr_cfg_path = Path(hf_hub_download(OPENFAKE_ATTR_REPO, "config.json", revision=OPENFAKE_ATTR_REVISION))
    attr_st_path = Path(hf_hub_download(OPENFAKE_ATTR_REPO, "model.safetensors", revision=OPENFAKE_ATTR_REVISION))
    attr_mb = round((base_st.stat().st_size + attr_st_path.stat().st_size) / (1024 * 1024), 2)

    attr_cfg = json.loads(attr_cfg_path.read_text(encoding="utf-8"))
    attr_weights = load_file(str(attr_st_path))
    families: list[str] = [str(x) for x in attr_cfg["families"]]

    processor = AutoImageProcessor.from_pretrained(OPENFAKE_BASE_REPO, revision=OPENFAKE_BASE_REVISION)
    detector = AutoModelForImageClassification.from_pretrained(OPENFAKE_BASE_REPO, revision=OPENFAKE_BASE_REVISION)
    detector.train(False)
    detector.to("cpu")

    head = nn.Sequential(
        nn.Linear(int(attr_cfg["in_dim"]), int(attr_cfg["emb_dim"])),
        nn.BatchNorm1d(int(attr_cfg["emb_dim"])),
    )
    head.load_state_dict({k[len("head.") :]: v for k, v in attr_weights.items() if k.startswith("head.")})
    head.train(False)
    head.to("cpu")
    family_protos_cpu = attr_weights["family_prototypes"].float()
    del attr_weights

    def _score_on(dev: Any, protos: Any) -> Callable[[list[Image.Image]], list[dict[str, Any]]]:
        def _run(rgbs: list[Image.Image]) -> list[dict[str, Any]]:
            px = processor(images=rgbs, return_tensors="pt")["pixel_values"].to(dev)
            with torch.inference_mode():
                pooled = detector.swinv2(pixel_values=px).pooler_output
                p_fake = (detector.classifier(pooled) / 2.0).softmax(-1)[:, 1]
                emb = f_nn.normalize(head(f_nn.normalize(pooled)))
                fam_idx = (emb @ protos.T).argmax(dim=-1)
            return [
                {
                    "pred_ai": float(p_val) >= 0.5,
                    "pred_provider": (
                        OPENFAKE_FAMILY_MAP.get(families[int(f_i)], f"other:{families[int(f_i)]}")
                        if float(p_val) >= 0.5
                        else None
                    ),
                }
                for p_val, f_i in zip(p_fake.cpu().tolist(), fam_idx.cpu().tolist(), strict=True)
            ]

        return _run

    def _move_to_acc() -> Callable[[list[Image.Image]], list[dict[str, Any]]]:
        dev = _select_torch_accelerator()
        detector.to(dev)
        head.to(dev)
        return _score_on(dev, family_protos_cpu.to(dev))

    return _evaluate_rgb_detector(
        samples,
        rgb_cache,
        score_cpu_batch=_score_on(torch.device("cpu"), family_protos_cpu),
        make_batch_scorer=_move_to_acc,
        model_id="bendavidsteel/openfake-swinv2-base + attribution-head",
        revision=f"{OPENFAKE_BASE_REVISION[:10]}+{OPENFAKE_ATTR_REVISION[:10]}",
        runtime="PyTorch + Transformers",
        weights_mb=attr_mb,
        license_id="cc-by-nc-4.0",
        tier="frontier",
    )


def evaluate_openfake_textgraphics_lora(
    samples: list[SampleItem],
    rgb_cache: list[Image.Image] | None = None,
) -> dict[str, Any]:
    """Evaluate OpenFake SwinV2-base with the McGill text-graphics false-positive LoRA adapter."""
    import torch
    from huggingface_hub import hf_hub_download
    from peft import PeftModel
    from transformers import AutoImageProcessor, AutoModelForImageClassification

    base_st = Path(hf_hub_download(OPENFAKE_BASE_REPO, "model.safetensors", revision=OPENFAKE_BASE_REVISION))
    lora_st = Path(hf_hub_download(OPENFAKE_LORA_REPO, "adapter_model.safetensors", revision=OPENFAKE_LORA_REVISION))
    lora_mb = round((base_st.stat().st_size + lora_st.stat().st_size) / (1024 * 1024), 2)

    processor = AutoImageProcessor.from_pretrained(OPENFAKE_BASE_REPO, revision=OPENFAKE_BASE_REVISION)
    base_model = AutoModelForImageClassification.from_pretrained(OPENFAKE_BASE_REPO, revision=OPENFAKE_BASE_REVISION)
    lora_model = PeftModel.from_pretrained(base_model, OPENFAKE_LORA_REPO, revision=OPENFAKE_LORA_REVISION)
    lora_model.train(False)
    lora_model.to("cpu")

    def _score_on(dev: Any) -> Callable[[list[Image.Image]], list[dict[str, Any]]]:
        def _run(rgbs: list[Image.Image]) -> list[dict[str, Any]]:
            inputs = {k: v.to(dev) for k, v in processor(images=rgbs, return_tensors="pt").items()}
            with torch.inference_mode():
                probs = (lora_model(**inputs).logits / 2.0).softmax(-1)[:, 1].cpu().tolist()
            return [{"pred_ai": float(p_val) >= 0.5, "pred_provider": None} for p_val in probs]

        return _run

    def _move_to_acc() -> Callable[[list[Image.Image]], list[dict[str, Any]]]:
        dev = _select_torch_accelerator()
        lora_model.to(dev)
        return _score_on(dev)

    return _evaluate_rgb_detector(
        samples,
        rgb_cache,
        score_cpu_batch=_score_on(torch.device("cpu")),
        make_batch_scorer=_move_to_acc,
        model_id="bendavidsteel/openfake-swinv2-base + textgraphics-lora",
        revision=f"{OPENFAKE_BASE_REVISION[:10]}+{OPENFAKE_LORA_REVISION[:10]}",
        runtime="PyTorch + PEFT",
        weights_mb=lora_mb,
        license_id="cc-by-nc-4.0",
        tier="frontier",
    )


def evaluate_smaug(
    samples: list[SampleItem],
    rgb_cache: list[Image.Image] | None = None,
) -> dict[str, Any]:
    """Evaluate `abacusai/Smaug-AI-Image-Detector` (EmbeddingGemma-2 439M, 3x 528px crops)."""
    import torch
    from huggingface_hub import snapshot_download
    from sentence_transformers import SentenceTransformer

    snap = Path(snapshot_download(SMAUG_REPO, revision=SMAUG_REVISION))
    weights_bytes = sum(p.stat().st_size for p in snap.rglob("*.safetensors") if p.is_file())
    weights_mb = round(weights_bytes / (1024 * 1024), 2)

    st = SentenceTransformer(
        str(snap),
        device="cpu",
        config_kwargs={"audio_config": None},
        model_kwargs={"torch_dtype": torch.float32},
    )
    ip = st[0].processor.image_processor
    ip.max_soft_tokens = ip.image_seq_length = 140

    def _score_batch(rgbs: list[Image.Image]) -> list[dict[str, Any]]:
        flat = [{"image": crop} for rgb in rgbs for crop in _smaug_views(rgb)]
        with torch.inference_mode():
            logits = st.encode(flat, batch_size=16, convert_to_tensor=True, show_progress_bar=False).float()
        probs = torch.sigmoid(logits.view(len(rgbs), 3).mean(dim=1)).cpu().tolist()
        return [{"pred_ai": float(p_val) >= 0.5, "pred_provider": None} for p_val in probs]

    def _move_to_acc() -> Callable[[list[Image.Image]], list[dict[str, Any]]]:
        dev = _select_torch_accelerator()
        if dev.type == "cuda":
            st.to(device=dev, dtype=torch.bfloat16)
        elif dev.type != "cpu":
            st.to(device=dev)
        return _score_batch

    return _evaluate_rgb_detector(
        samples,
        rgb_cache,
        score_cpu_batch=_score_batch,
        make_batch_scorer=_move_to_acc,
        batch_size=8,
        latency_count=SMAUG_CPU_LATENCY_COUNT,
        model_id=SMAUG_REPO,
        revision=SMAUG_REVISION,
        runtime="PyTorch + SentenceTransformers",
        weights_mb=weights_mb,
        license_id="apache-2.0",
        tier="frontier",
    )


def _summarize_ai_subset(
    ai_pairs: list[tuple[SampleItem, dict[str, Any]]],
    *,
    non_ai_fp: int,
) -> dict[str, Any]:
    per_prov: dict[str, dict[str, int]] = {}
    tp = 0
    exact_provider_hits = 0
    total = len(ai_pairs)

    for item, pred in ai_pairs:
        flagged = bool(pred["pred_ai"])
        prov = item.expected_provider or "unknown"
        bucket = per_prov.setdefault(prov, {"count": 0, "detected_ai": 0, "exact_provider": 0})
        bucket["count"] += 1
        if flagged:
            tp += 1
            bucket["detected_ai"] += 1
        if pred["pred_provider"] is not None and pred["pred_provider"] == item.expected_provider:
            exact_provider_hits += 1
            bucket["exact_provider"] += 1

    precision = (tp / (tp + non_ai_fp) * 100.0) if (tp + non_ai_fp) else 0.0
    recall = (tp / total * 100.0) if total else 0.0
    attr_acc = (exact_provider_hits / total * 100.0) if total else 0.0
    attr_when_classified = (exact_provider_hits / tp * 100.0) if tp else 0.0
    per_provider_summary = {
        prov: {
            "count": b["count"],
            "detected_ai": b["detected_ai"],
            "exact_provider": b["exact_provider"],
            "recall_pct": round(b["detected_ai"] / b["count"] * 100.0, 1),
            "attribution_pct": round(b["exact_provider"] / b["count"] * 100.0, 1),
        }
        for prov, b in sorted(per_prov.items())
    }
    return {
        "total": total,
        "tp": tp,
        "recall_pct": round(recall, 1),
        "precision_pct": round(precision, 1),
        "provider_attribution_pct": round(attr_acc, 1),
        "provider_attribution_when_classified_pct": round(attr_when_classified, 1),
        "per_provider": per_provider_summary,
    }


def _summarize_predictions(
    *,
    model_id: str,
    revision: str,
    runtime: str,
    weights_mb: float,
    license_id: str,
    median_cpu_ms: float,
    samples: list[SampleItem],
    preds: list[dict[str, Any]],
    tier: str = "frontier",
) -> dict[str, Any]:
    non_ai_by_cat: dict[str, list[bool]] = {}
    native_ai_pairs: list[tuple[SampleItem, dict[str, Any]]] = []
    arena_ai_pairs: list[tuple[SampleItem, dict[str, Any]]] = []

    for item, pred in zip(samples, preds, strict=True):
        if item.regime == "non_ai":
            non_ai_by_cat.setdefault(item.category, []).append(bool(pred["pred_ai"]))
        elif item.regime == "native":
            native_ai_pairs.append((item, pred))
        elif item.regime == "arena":
            arena_ai_pairs.append((item, pred))
        else:
            raise ValueError(f"Unsupported sample regime: {item.regime!r}")

    non_ai_total = sum(len(flags) for flags in non_ai_by_cat.values())
    fp = sum(sum(flags) for flags in non_ai_by_cat.values())
    fpr = (fp / non_ai_total * 100.0) if non_ai_total else 0.0
    slice_fprs = {cat: round(sum(flags) / len(flags) * 100.0, 1) for cat, flags in non_ai_by_cat.items()}

    all_ai = _summarize_ai_subset(native_ai_pairs + arena_ai_pairs, non_ai_fp=fp)
    native_ai = _summarize_ai_subset(native_ai_pairs, non_ai_fp=fp)
    arena_ai = _summarize_ai_subset(arena_ai_pairs, non_ai_fp=fp)

    return {
        "model_id": model_id,
        "tier": tier,
        "revision": revision,
        "license": license_id,
        "weights_mb": weights_mb,
        "runtime": runtime,
        "median_cpu_ms": median_cpu_ms,
        "non_ai_total": non_ai_total,
        "non_ai_fp": fp,
        "non_ai_fpr_pct": round(fpr, 2),
        "slice_fpr_pct": slice_fprs,
        "ai_total": all_ai["total"],
        "ai_tp": all_ai["tp"],
        "ai_recall_pct": all_ai["recall_pct"],
        "precision_pct": all_ai["precision_pct"],
        "provider_attribution_pct": all_ai["provider_attribution_pct"],
        "provider_attribution_when_classified_pct": all_ai["provider_attribution_when_classified_pct"],
        "per_provider": all_ai["per_provider"],
        "native_ai": native_ai,
        "arena_ai": arena_ai,
    }


def format_markdown_table(results: list[dict[str, Any]]) -> str:
    """Format head-to-head results across non-AI traps, native exports, and transcoded arena images."""
    n_non_ai = results[0]["non_ai_total"] if results else 0
    n_native = results[0]["native_ai"]["total"] if results else 0
    n_arena = results[0]["arena_ai"]["total"] if results else 0

    header = (
        f"| Model | License | Size | CPU ms | Non-AI FPR (`{n_non_ai}`) | Photo / UI / Diag / Rcpt FPR "
        f"| Native AI Recall (`{n_native}`) | Native Attribution (`{n_native}`) "
        f"| Arena AI Recall (`{n_arena}`) | Arena Attribution (`{n_arena}`) | Precision |"
    )
    sep = "|---|---|---:|---:|---:|---|---:|---:|---:|---:|---:|"
    lines = [header, sep]
    for r in results:
        sf = r["slice_fpr_pct"]
        nat = r["native_ai"]
        arn = r["arena_ai"]
        slice_str = (
            f"{sf.get('camera_photo', 0.0):.0f}% / {sf.get('ui_screenshot', 0.0):.0f}% / "
            f"{sf.get('science_diagram', 0.0):.0f}% / {sf.get('receipt_invoice', 0.0):.0f}%"
        )
        lines.append(
            f"| `{r['model_id']}` | `{r['license']}` | {r['weights_mb']:.1f} MB | {r['median_cpu_ms']:.0f} ms "
            f"| {r['non_ai_fpr_pct']:.1f}% ({r['non_ai_fp']}/{r['non_ai_total']}) "
            f"| {slice_str} "
            f"| {nat['recall_pct']:.1f}% "
            f"| {nat['provider_attribution_pct']:.1f}% "
            f"| {arn['recall_pct']:.1f}% "
            f"| {arn['provider_attribution_pct']:.1f}% "
            f"| {r['precision_pct']:.1f}% |"
        )
    return "\n".join(lines)


def _format_buckets(items: list[SampleItem]) -> dict[str, dict[str, int]]:
    out: dict[str, dict[str, int]] = {}
    for item in items:
        prov = item.expected_provider or item.category
        fmap = out.setdefault(prov, {})
        key = f"{item.container_format}:{item.width}x{item.height}"
        fmap[key] = fmap.get(key, 0) + 1
    return out


def main() -> int:
    from huggingface_hub import HfApi, HfFileSystem

    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--json-out", type=Path, help="Optional path to write full JSON benchmark report.")
    parser.add_argument("--raiw-only", action="store_true", help="Run only wiltodelta/raiw-source-classify.")
    parser.add_argument(
        "--tier",
        choices=("all", "frontier", "legacy"),
        default="all",
        help="Baseline tier to run alongside wiltodelta/raiw-source-classify (default: all).",
    )
    args = parser.parse_args()

    logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
    fs = HfFileSystem()
    api = HfApi()

    all_samples = load_all_samples(fs, api)
    non_ai = [s for s in all_samples if s.regime == "non_ai"]
    native_ai = [s for s in all_samples if s.regime == "native"]
    arena_ai = [s for s in all_samples if s.regime == "arena"]
    log.info(
        "Evaluating %s total images (%s non-AI + %s native AI + %s transcoded arena AI)",
        len(all_samples),
        len(non_ai),
        len(native_ai),
        len(arena_ai),
    )

    results: list[dict[str, Any]] = [evaluate_raiw(all_samples)]
    _release_memory()

    if not args.raiw_only:
        with ThreadPoolExecutor(max_workers=8) as pool:
            rgb_cache = list(pool.map(lambda s: _decode_rgb(s.raw_bytes), all_samples))

        evaluators: list[tuple[str, Callable[[], dict[str, Any]]]] = []
        if args.tier in ("all", "frontier"):
            evaluators.extend(
                [
                    (SMAUG_REPO, lambda: evaluate_smaug(all_samples, rgb_cache)),
                    (
                        COMMFOR_DEEPFAKEDET_SPEC.repo_id,
                        lambda: evaluate_hf_binary_detector(COMMFOR_DEEPFAKEDET_SPEC, all_samples, rgb_cache),
                    ),
                    *[
                        (
                            vit_spec.repo_id,
                            lambda vs=vit_spec: evaluate_timm_vit_detector(vs, all_samples, rgb_cache),
                        )
                        for vit_spec in FRONTIER_VIT_DETECTORS
                    ],
                    (DETECTRA_OPENFAKE_REPO, lambda: evaluate_detectra_openfake(all_samples, rgb_cache)),
                    (
                        "bendavidsteel/openfake-swinv2-base + attribution-head",
                        lambda: evaluate_openfake_attribution(all_samples, rgb_cache),
                    ),
                    (
                        "bendavidsteel/openfake-swinv2-base + textgraphics-lora",
                        lambda: evaluate_openfake_textgraphics_lora(all_samples, rgb_cache),
                    ),
                ]
            )
        if args.tier in ("all", "legacy"):
            evaluators.extend(
                [
                    (spec.repo_id, lambda sp=spec: evaluate_hf_binary_detector(sp, all_samples, rgb_cache))
                    for spec in LEGACY_BINARY_DETECTORS
                ]
            )

        for label, run_eval in evaluators:
            log.info("Evaluating %s ...", label)
            results.append(run_eval())
            _release_memory()
        del rgb_cache
        _release_memory()

    md_table = format_markdown_table(results)
    sys.stdout.write("\n" + md_table + "\n")

    if args.json_out is not None:
        native_formats = _format_buckets(native_ai)
        arena_formats = _format_buckets(arena_ai)
        payload = {
            "non_ai_count": len(non_ai),
            "native_ai_count": len(native_ai),
            "arena_ai_count": len(arena_ai),
            "native_ai_providers": {p: sum(m.values()) for p, m in sorted(native_formats.items())},
            "native_ai_formats": native_formats,
            "arena_ai_providers": {p: sum(m.values()) for p, m in sorted(arena_formats.items())},
            "arena_ai_formats": arena_formats,
            "results": results,
        }
        args.json_out.parent.mkdir(parents=True, exist_ok=True)
        args.json_out.write_text(json.dumps(payload, indent=2) + "\n", encoding="utf-8")
        log.info("Wrote JSON report to %s", args.json_out)
    return 0


if __name__ == "__main__":
    raise SystemExit(main())