Smaug + Mesmer AI image check, WebGPU
Browse files- .gitattributes +6 -0
- README.md +31 -5
- app.js +464 -0
- examples/ai-dflow2.png +3 -0
- examples/ai-gpt-image-2.png +3 -0
- examples/ai-midjourney-7.png +3 -0
- examples/photo-real-1.jpg +3 -0
- examples/photo-real-2.jpg +3 -0
- examples/photo-real-3.jpg +3 -0
- examples/thumb/ai-dflow2.jpg +0 -0
- examples/thumb/ai-gpt-image-2.jpg +0 -0
- examples/thumb/ai-midjourney-7.jpg +0 -0
- examples/thumb/photo-real-1.jpg +0 -0
- examples/thumb/photo-real-2.jpg +0 -0
- examples/thumb/photo-real-3.jpg +0 -0
- index.html +52 -17
- style.css +68 -22
.gitattributes
CHANGED
|
@@ -33,3 +33,9 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
examples/ai-dflow2.png filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
examples/ai-gpt-image-2.png filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
examples/ai-midjourney-7.png filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
examples/photo-real-1.jpg filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
examples/photo-real-2.jpg filter=lfs diff=lfs merge=lfs -text
|
| 41 |
+
examples/photo-real-3.jpg filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -1,10 +1,36 @@
|
|
| 1 |
---
|
| 2 |
-
title: AI Image
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: static
|
|
|
|
| 7 |
pinned: false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: AI Image Check
|
| 3 |
+
emoji: 🔍
|
| 4 |
+
colorFrom: gray
|
| 5 |
+
colorTo: red
|
| 6 |
sdk: static
|
| 7 |
+
app_file: index.html
|
| 8 |
pinned: false
|
| 9 |
+
license: cc-by-nc-sa-4.0
|
| 10 |
+
short_description: Two AI image detectors in your browser, WebGPU
|
| 11 |
+
models:
|
| 12 |
+
- abacusai/Smaug-AI-Image-Detector
|
| 13 |
+
- 21amd21/smaug-ai-image-detector-web
|
| 14 |
+
- mesmertech/mesmer-ai-image-detector
|
| 15 |
+
- WeReCooking/Mesmer-AI-Image-Detector-ONNX-WebGPU
|
| 16 |
+
tags:
|
| 17 |
+
- webgpu
|
| 18 |
+
- onnxruntime-web
|
| 19 |
+
- ai-image-detection
|
| 20 |
---
|
| 21 |
|
| 22 |
+
Drop, paste or pick a photo and two detectors check it on your own GPU with ONNX Runtime Web. Nothing is uploaded.
|
| 23 |
+
|
| 24 |
+
- **Smaug** ([abacusai/Smaug-AI-Image-Detector](https://huggingface.co/abacusai/Smaug-AI-Image-Detector), Apache-2.0),
|
| 25 |
+
run from the 181 MB web build [21amd21/smaug-ai-image-detector-web](https://huggingface.co/21amd21/smaug-ai-image-detector-web).
|
| 26 |
+
- **Mesmer** ([mesmertech/mesmer-ai-image-detector](https://huggingface.co/mesmertech/mesmer-ai-image-detector), CC-BY-NC-SA-4.0),
|
| 27 |
+
converted for the browser in [WeReCooking/Mesmer-AI-Image-Detector-ONNX-WebGPU](https://huggingface.co/WeReCooking/Mesmer-AI-Image-Detector-ONNX-WebGPU).
|
| 28 |
+
|
| 29 |
+
The crops and resizes are a JavaScript port of the Pillow code both models were trained with, and give the same pixels
|
| 30 |
+
as Python. A score is a likelihood, not proof.
|
| 31 |
+
|
| 32 |
+
Examples: Unsplash photos by Andrew Sterling, Andrey Câmara and Ross Sneddon (Unsplash License); Midjourney 7 and
|
| 33 |
+
GPT-Image-2 images from [OpenFake](https://huggingface.co/datasets/ComplexDataLab/OpenFake) (CC BY-NC 4.0); a DFlow2 image
|
| 34 |
+
made in [DFlow2 WebGPU](https://huggingface.co/spaces/WeReCooking/DFlow2-WebGPU).
|
| 35 |
+
|
| 36 |
+
Non-commercial: the Mesmer weights are CC-BY-NC-SA-4.0, so this Space is too.
|
app.js
ADDED
|
@@ -0,0 +1,464 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// AI Image Check, single-file build of pilresize.js + views.js + main.js (see build.py).
|
| 2 |
+
// Port of Pillow's 8-bit resampling (libImaging/Resample.c) for RGB images, so the
|
| 3 |
+
// detectors see the same pixels in the browser as in Python: Image.resize(size,
|
| 4 |
+
// BICUBIC | LANCZOS) with no box and reducing_gap=None. Separable, horizontal pass first,
|
| 5 |
+
// coefficients normalised in double and rounded to 22-bit fixed point, as in C.
|
| 6 |
+
const PRECISION_BITS = 22;
|
| 7 |
+
const ONE = 1 << PRECISION_BITS;
|
| 8 |
+
|
| 9 |
+
const FILTERS = {
|
| 10 |
+
bicubic: { support: 2.0, f: (x) => {
|
| 11 |
+
const a = -0.5;
|
| 12 |
+
if (x < 0) x = -x;
|
| 13 |
+
if (x < 1) return ((a + 2) * x - (a + 3)) * x * x + 1;
|
| 14 |
+
if (x < 2) return (((x - 5) * x + 8) * x - 4) * a;
|
| 15 |
+
return 0;
|
| 16 |
+
} },
|
| 17 |
+
lanczos: { support: 3.0, f: (x) => {
|
| 18 |
+
const sinc = (v) => { if (v === 0) return 1; v *= Math.PI; return Math.sin(v) / v; };
|
| 19 |
+
return (x >= -3 && x < 3) ? sinc(x) * sinc(x / 3) : 0;
|
| 20 |
+
} },
|
| 21 |
+
};
|
| 22 |
+
|
| 23 |
+
function coeffs(inSize, outSize, filter) {
|
| 24 |
+
const scale = inSize / outSize;
|
| 25 |
+
const filterscale = scale < 1 ? 1 : scale;
|
| 26 |
+
const support = filter.support * filterscale;
|
| 27 |
+
const ksize = Math.ceil(support) * 2 + 1;
|
| 28 |
+
const kk = new Int32Array(outSize * ksize), bounds = new Int32Array(outSize * 2);
|
| 29 |
+
const pre = new Float64Array(ksize);
|
| 30 |
+
for (let xx = 0; xx < outSize; xx++) {
|
| 31 |
+
const center = (xx + 0.5) * scale, ss = 1 / filterscale;
|
| 32 |
+
let xmin = Math.trunc(center - support + 0.5); if (xmin < 0) xmin = 0;
|
| 33 |
+
let xmax = Math.trunc(center + support + 0.5); if (xmax > inSize) xmax = inSize;
|
| 34 |
+
xmax -= xmin;
|
| 35 |
+
let ww = 0;
|
| 36 |
+
for (let x = 0; x < xmax; x++) { const w = filter.f((x + xmin - center + 0.5) * ss); pre[x] = w; ww += w; }
|
| 37 |
+
for (let x = 0; x < xmax; x++) {
|
| 38 |
+
const v = ww !== 0 ? pre[x] / ww : pre[x];
|
| 39 |
+
kk[xx * ksize + x] = v < 0 ? Math.trunc(-0.5 + v * ONE) : Math.trunc(0.5 + v * ONE);
|
| 40 |
+
}
|
| 41 |
+
bounds[xx * 2] = xmin; bounds[xx * 2 + 1] = xmax;
|
| 42 |
+
}
|
| 43 |
+
return { kk, bounds, ksize };
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
const clip8 = (v) => (v >= (ONE * 256) ? 255 : v <= 0 ? 0 : Math.floor(v / ONE));
|
| 47 |
+
|
| 48 |
+
// img: { w, h, data: Uint8Array RGB } -> { w, h, data }
|
| 49 |
+
function resize(img, outW, outH, kind) {
|
| 50 |
+
if (outW === img.w && outH === img.h) return { w: img.w, h: img.h, data: img.data.slice() };
|
| 51 |
+
const filter = FILTERS[kind];
|
| 52 |
+
const H = coeffs(img.w, outW, filter), V = coeffs(img.h, outH, filter);
|
| 53 |
+
const needH = outW !== img.w, needV = outH !== img.h;
|
| 54 |
+
const yFirst = V.bounds[0], yLast = V.bounds[(outH - 1) * 2] + V.bounds[(outH - 1) * 2 + 1];
|
| 55 |
+
let src = img;
|
| 56 |
+
if (needH) {
|
| 57 |
+
const rows = yLast - yFirst, out = new Uint8Array(outW * rows * 3), d = img.data, W = img.w;
|
| 58 |
+
for (let yy = 0; yy < rows; yy++) {
|
| 59 |
+
const rowOff = (yy + yFirst) * W * 3;
|
| 60 |
+
for (let xx = 0; xx < outW; xx++) {
|
| 61 |
+
const xmin = H.bounds[xx * 2], xmax = H.bounds[xx * 2 + 1], k = xx * H.ksize;
|
| 62 |
+
let s0 = ONE / 2, s1 = ONE / 2, s2 = ONE / 2;
|
| 63 |
+
for (let x = 0; x < xmax; x++) {
|
| 64 |
+
const c = H.kk[k + x], p = rowOff + (x + xmin) * 3;
|
| 65 |
+
s0 += d[p] * c; s1 += d[p + 1] * c; s2 += d[p + 2] * c;
|
| 66 |
+
}
|
| 67 |
+
const o = (yy * outW + xx) * 3;
|
| 68 |
+
out[o] = clip8(s0); out[o + 1] = clip8(s1); out[o + 2] = clip8(s2);
|
| 69 |
+
}
|
| 70 |
+
}
|
| 71 |
+
src = { w: outW, h: rows, data: out };
|
| 72 |
+
}
|
| 73 |
+
if (!needV) return src;
|
| 74 |
+
const shift = needH ? yFirst : 0, W = src.w, d = src.data, out = new Uint8Array(W * outH * 3);
|
| 75 |
+
for (let yy = 0; yy < outH; yy++) {
|
| 76 |
+
const ymin = V.bounds[yy * 2] - shift, ymax = V.bounds[yy * 2 + 1], k = yy * V.ksize;
|
| 77 |
+
for (let xx = 0; xx < W; xx++) {
|
| 78 |
+
let s0 = ONE / 2, s1 = ONE / 2, s2 = ONE / 2;
|
| 79 |
+
for (let y = 0; y < ymax; y++) {
|
| 80 |
+
const c = V.kk[k + y], p = ((y + ymin) * W + xx) * 3;
|
| 81 |
+
s0 += d[p] * c; s1 += d[p + 1] * c; s2 += d[p + 2] * c;
|
| 82 |
+
}
|
| 83 |
+
const o = (yy * W + xx) * 3;
|
| 84 |
+
out[o] = clip8(s0); out[o + 1] = clip8(s1); out[o + 2] = clip8(s2);
|
| 85 |
+
}
|
| 86 |
+
}
|
| 87 |
+
return { w: W, h: outH, data: out };
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
function crop(img, x, y, w, h) {
|
| 91 |
+
const out = new Uint8Array(w * h * 3);
|
| 92 |
+
for (let r = 0; r < h; r++) out.set(img.data.subarray(((y + r) * img.w + x) * 3, ((y + r) * img.w + x + w) * 3), r * w * 3);
|
| 93 |
+
return { w, h, data: out };
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
// Python's round(): halves to even.
|
| 97 |
+
function pyRound(v) {
|
| 98 |
+
const f = Math.floor(v), d = v - f;
|
| 99 |
+
if (d > 0.5) return f + 1;
|
| 100 |
+
if (d < 0.5) return f;
|
| 101 |
+
return f % 2 === 0 ? f : f + 1;
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
// Detector inputs, ported line by line from the Python originals so the browser feeds the
|
| 105 |
+
// models the same pixels: mesmer inference.py (views, rank_normalise, display_p) and
|
| 106 |
+
// Smaug smaug_image_detect.py (views). Images are { w, h, data: Uint8Array RGB }.
|
| 107 |
+
|
| 108 |
+
// ---------------------------------------------------------------- mesmer
|
| 109 |
+
const S_PEL = 336, S_PEG = 448, CAP_PEL = 1024;
|
| 110 |
+
|
| 111 |
+
function capLongSide(px, maxSide) {
|
| 112 |
+
const { w, h } = px;
|
| 113 |
+
if (maxSide <= 0 || Math.max(h, w) <= maxSide) return px;
|
| 114 |
+
const k = maxSide / Math.max(h, w);
|
| 115 |
+
const [nw, nh] = w >= h ? [maxSide, Math.max(1, pyRound(h * k))] : [Math.max(1, pyRound(w * k)), maxSide];
|
| 116 |
+
return resize(px, nw, nh, 'lanczos');
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
function viewResizeCrop(im, S) {
|
| 120 |
+
const s = S / Math.min(im.w, im.h);
|
| 121 |
+
const r = resize(im, Math.max(S, pyRound(im.w * s)), Math.max(S, pyRound(im.h * s)), 'bicubic');
|
| 122 |
+
return crop(r, (r.w - S) >> 1, (r.h - S) >> 1, S, S);
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
function upscaleShort(im, S) {
|
| 126 |
+
if (Math.min(im.w, im.h) >= S) return im;
|
| 127 |
+
const s = S / Math.min(im.w, im.h);
|
| 128 |
+
return resize(im, Math.max(S, pyRound(im.w * s)), Math.max(S, pyRound(im.h * s)), 'bicubic');
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
function nativeCentre(im, S) {
|
| 132 |
+
im = upscaleShort(im, S);
|
| 133 |
+
return crop(im, (im.w - S) >> 1, (im.h - S) >> 1, S, S);
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
function spreadCrops(im, S, ts = [0, 0.25, 1]) {
|
| 137 |
+
im = upscaleShort(im, S);
|
| 138 |
+
return ts.map((t) => (im.w >= im.h
|
| 139 |
+
? crop(im, pyRound(t * (im.w - S)), (im.h - S) >> 1, S, S)
|
| 140 |
+
: crop(im, (im.w - S) >> 1, pyRound(t * (im.h - S)), S, S)));
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
// uint8 HWC views -> float32 NCHW, (x / 255 - mean) / std
|
| 144 |
+
function toTensor(views, S, mean = 0.5, std = 0.5) {
|
| 145 |
+
const plane = S * S, out = new Float32Array(views.length * 3 * plane);
|
| 146 |
+
views.forEach((v, i) => {
|
| 147 |
+
const d = v.data, o = i * 3 * plane;
|
| 148 |
+
for (let p = 0; p < plane; p++)
|
| 149 |
+
for (let c = 0; c < 3; c++) out[o + c * plane + p] = (Math.fround(d[p * 3 + c] / 255) - mean) / std;
|
| 150 |
+
});
|
| 151 |
+
return out;
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
function mesmerViews(px) {
|
| 155 |
+
const capped = capLongSide(px, CAP_PEL);
|
| 156 |
+
const peg = viewResizeCrop(px, S_PEG); // PE-G: from the image as is (capping only applies to PE-L)
|
| 157 |
+
const pel = [viewResizeCrop(capped, S_PEL), nativeCentre(capped, S_PEL), ...spreadCrops(capped, S_PEL)];
|
| 158 |
+
return { peg: toTensor([peg], S_PEG), pel: toTensor(pel, S_PEL), pegView: peg, pelViews: pel };
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
// rank_normalise: logit of the mid-rank ECDF of the sorted real reference scores + 1e-3 x robust z
|
| 162 |
+
function makeRanker(ref) {
|
| 163 |
+
const r = Float64Array.from(ref).sort(), n = r.length;
|
| 164 |
+
const pct = (q) => { const i = q / 100 * (n - 1), lo = Math.floor(i), hi = Math.ceil(i); return r[lo] + (r[hi] - r[lo]) * (i - lo); };
|
| 165 |
+
const med = n % 2 ? r[(n - 1) / 2] : (r[n / 2 - 1] + r[n / 2]) / 2;
|
| 166 |
+
const iqr = Math.max(pct(75) - pct(25), 1e-12);
|
| 167 |
+
const left = (x) => { let a = 0, b = n; while (a < b) { const m = (a + b) >> 1; if (r[m] < x) a = m + 1; else b = m; } return a; };
|
| 168 |
+
const right = (x) => { let a = 0, b = n; while (a < b) { const m = (a + b) >> 1; if (r[m] <= x) a = m + 1; else b = m; } return a; };
|
| 169 |
+
return (x) => {
|
| 170 |
+
let u = (left(x) + right(x)) / 2;
|
| 171 |
+
u = Math.min(Math.max((u + 0.5) / (n + 1), 0.5 / (n + 1)), 1 - 0.5 / (n + 1));
|
| 172 |
+
return Math.log(u / (1 - u)) + 1e-3 * (x - med) / iqr;
|
| 173 |
+
};
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
function mesmerVerdict(pelLogit, pegLogit, fusion) {
|
| 177 |
+
const zp = fusion.rank.pel(pelLogit), zg = fusion.rank.peg(pegLogit), w = fusion.weights;
|
| 178 |
+
const z = (w.pel * zp + w.peg * zg) / (w.pel + w.peg);
|
| 179 |
+
const { ai, unc } = fusion.thresholds;
|
| 180 |
+
const verdict = z >= ai ? 'likely_ai' : z >= unc ? 'uncertain' : 'likely_real';
|
| 181 |
+
const p = 1 / (1 + Math.exp(-Math.max(-60, Math.min(60, Math.log(9) * (z - unc) / (ai - unc)))));
|
| 182 |
+
return { verdict, score: z, p_ai: p, zp, zg };
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
+
// ---------------------------------------------------------------- smaug
|
| 186 |
+
const CROP = 528, SIDE = 576;
|
| 187 |
+
|
| 188 |
+
function smaugCrops(px) {
|
| 189 |
+
let im = px;
|
| 190 |
+
const m = Math.min(im.w, im.h);
|
| 191 |
+
if (m > SIDE) {
|
| 192 |
+
const s = SIDE / m;
|
| 193 |
+
im = resize(im, pyRound(im.w * s), pyRound(im.h * s), 'lanczos');
|
| 194 |
+
} else if (m < CROP) {
|
| 195 |
+
const s = CROP / m;
|
| 196 |
+
im = resize(im, Math.ceil(im.w * s), Math.ceil(im.h * s), 'bicubic');
|
| 197 |
+
}
|
| 198 |
+
const cx = (im.w - CROP) >> 1, cy = (im.h - CROP) >> 1;
|
| 199 |
+
const spots = im.w >= im.h ? [[0, cy], [cx, cy], [im.w - CROP, cy]] : [[cx, 0], [cx, cy], [cx, im.h - CROP]];
|
| 200 |
+
const views = spots.map(([x, y]) => crop(im, x, y, CROP, CROP));
|
| 201 |
+
return { tensor: toTensor(views, CROP, 0, 1), views };
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
const sigmoid = (v) => 1 / (1 + Math.exp(-v));
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
const q = new URLSearchParams(location.search);
|
| 208 |
+
// Test hooks: same-origin paths only, so a crafted link cannot load someone else's code or weights.
|
| 209 |
+
const local = (v) => (v && v.startsWith('/') && !v.startsWith('//') ? v : null);
|
| 210 |
+
const MESMER_REPO = 'WeReCooking/Mesmer-AI-Image-Detector-ONNX-WebGPU';
|
| 211 |
+
const MESMER = local(q.get('mesmer')) || `https://huggingface.co/${MESMER_REPO}/resolve/main`;
|
| 212 |
+
const SMAUG = local(q.get('smaug')) || 'https://huggingface.co/21amd21/smaug-ai-image-detector-web/resolve/1f7d2da952f86543c6c878450ee8fc82750bd14d/model.onnx';
|
| 213 |
+
const ORT_URL = local(q.get('ort')) || 'https://cdn.jsdelivr.net/npm/onnxruntime-web@1.31.1/dist/ort.webgpu.min.mjs';
|
| 214 |
+
const CACHE = 'ai-image-check-v1';
|
| 215 |
+
const EXAMPLES = [
|
| 216 |
+
['photo-real-1.jpg', 'Real photo: a puppy, by Andrew Sterling on Unsplash', 'https://unsplash.com/photos/xelC-x_Uvh0'],
|
| 217 |
+
['photo-real-2.jpg', 'Real photo: a couple at sunrise, by Andrey Câmara on Unsplash', 'https://unsplash.com/photos/80_6jJ97c-U'],
|
| 218 |
+
['photo-real-3.jpg', 'Real photo: a vintage car, by Ross Sneddon on Unsplash', 'https://unsplash.com/photos/65SjWi7gTao'],
|
| 219 |
+
['ai-midjourney-7.png', 'AI image: Midjourney 7, from the OpenFake dataset', 'https://huggingface.co/datasets/ComplexDataLab/OpenFake'],
|
| 220 |
+
['ai-gpt-image-2.png', 'AI image: GPT-Image-2, from the OpenFake dataset', 'https://huggingface.co/datasets/ComplexDataLab/OpenFake'],
|
| 221 |
+
['ai-dflow2.png', 'AI image: DFlow2 2B with the 4-step LoRA, made in our DFlow2 WebGPU Space', 'https://huggingface.co/spaces/WeReCooking/DFlow2-WebGPU'],
|
| 222 |
+
];
|
| 223 |
+
|
| 224 |
+
const $ = (id) => document.getElementById(id);
|
| 225 |
+
const ui = { drop: $('drop'), photo: $('photo'), file: $('file'), thumbs: $('thumbs'), store: $('store'), wipe: $('wipe') };
|
| 226 |
+
const rows = { smaug: $('smaug'), mesmer: $('mesmer') };
|
| 227 |
+
const mb = (b) => `${Math.round(b / 1e6)} MB`;
|
| 228 |
+
const tick = () => new Promise((r) => setTimeout(r, 0));
|
| 229 |
+
|
| 230 |
+
function show(name, cls, frac, html) {
|
| 231 |
+
const r = rows[name];
|
| 232 |
+
r.className = `res ${cls}`;
|
| 233 |
+
r.querySelector('.meter i').style.width = `${Math.round(Math.max(0, Math.min(1, frac)) * 100)}%`;
|
| 234 |
+
r.querySelector('.val').innerHTML = html;
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
// ---------------------------------------------------------------- downloads (Cache API, once)
|
| 238 |
+
async function openCache() { try { return await caches.open(CACHE); } catch { return null; } }
|
| 239 |
+
|
| 240 |
+
async function fetchBytes(url, onBytes) {
|
| 241 |
+
const cache = await openCache();
|
| 242 |
+
const hit = cache && await cache.match(url);
|
| 243 |
+
if (hit) return new Uint8Array(await hit.arrayBuffer());
|
| 244 |
+
const resp = await fetch(url);
|
| 245 |
+
if (!resp.ok) throw new Error(`HTTP ${resp.status} for ${url.split('/').pop()}`);
|
| 246 |
+
const total = Number(resp.headers.get('content-length')) || 0, reader = resp.body.getReader();
|
| 247 |
+
let bytes = new Uint8Array(total || 1 << 20), got = 0;
|
| 248 |
+
for (;;) { // one preallocated buffer: no Blob, no second copy
|
| 249 |
+
const { done, value } = await reader.read();
|
| 250 |
+
if (done) break;
|
| 251 |
+
if (got + value.length > bytes.length) { const g = new Uint8Array(Math.max(bytes.length * 2, got + value.length)); g.set(bytes.subarray(0, got)); bytes = g; }
|
| 252 |
+
bytes.set(value, got); got += value.length;
|
| 253 |
+
onBytes?.(got);
|
| 254 |
+
}
|
| 255 |
+
if (got !== bytes.length) bytes = bytes.slice(0, got);
|
| 256 |
+
if (cache) { try { await cache.put(url, new Response(bytes)); } catch (e) { console.warn(`not stored (${e.name}): ${url}`); } }
|
| 257 |
+
return bytes;
|
| 258 |
+
}
|
| 259 |
+
|
| 260 |
+
async function cachedAll(urls) {
|
| 261 |
+
const cache = await openCache();
|
| 262 |
+
if (!cache) return false;
|
| 263 |
+
for (const u of urls) if (!(await cache.match(u))) return false;
|
| 264 |
+
return true;
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
// ---------------------------------------------------------------- models
|
| 268 |
+
let ortP = null;
|
| 269 |
+
const getOrt = () => (ortP ||= (async () => {
|
| 270 |
+
if (!navigator.gpu || !(await navigator.gpu.requestAdapter())) throw new Error('needs WebGPU: Chrome or Edge 121+, Android 12+ or Safari 26');
|
| 271 |
+
const ort = await import(ORT_URL);
|
| 272 |
+
ort.env.logLevel = 'error';
|
| 273 |
+
return ort;
|
| 274 |
+
})());
|
| 275 |
+
|
| 276 |
+
async function download(name, files, total) { // files: [[url, bytes]], sequential, progress in the row
|
| 277 |
+
let done = 0;
|
| 278 |
+
const out = [];
|
| 279 |
+
const fresh = !(await cachedAll(files.map(([u]) => u)));
|
| 280 |
+
for (const [url, size] of files) {
|
| 281 |
+
out.push(await fetchBytes(url, (got) => {
|
| 282 |
+
show(name, 'busy', (done + got) / total, `downloading ${mb(done + got)} of ${mb(total)}, first time only`);
|
| 283 |
+
}));
|
| 284 |
+
done += size;
|
| 285 |
+
}
|
| 286 |
+
if (fresh) show(name, 'busy', 1, 'starting on your GPU…');
|
| 287 |
+
return out;
|
| 288 |
+
}
|
| 289 |
+
|
| 290 |
+
let smaugP = null, mesmerP = null, manifestP = null;
|
| 291 |
+
const getManifest = () => (manifestP ||= fetch(`${MESMER}/manifest.json`).then((r) => {
|
| 292 |
+
if (!r.ok) throw new Error(`model list: HTTP ${r.status}`);
|
| 293 |
+
return r.json();
|
| 294 |
+
}));
|
| 295 |
+
|
| 296 |
+
const getSmaug = () => (smaugP ||= (async () => {
|
| 297 |
+
const ort = await getOrt();
|
| 298 |
+
const [bytes] = await download('smaug', [[SMAUG, 181210404]], 181210404);
|
| 299 |
+
show('smaug', 'busy', 1, 'starting on your GPU…');
|
| 300 |
+
return ort.InferenceSession.create(bytes, { executionProviders: ['webgpu'], graphOptimizationLevel: 'disabled', logSeverityLevel: 3 });
|
| 301 |
+
})());
|
| 302 |
+
|
| 303 |
+
const getMesmer = () => (mesmerP ||= (async () => {
|
| 304 |
+
const ort = await getOrt();
|
| 305 |
+
const man = await getManifest();
|
| 306 |
+
const parts = ['pel', ...man.peg];
|
| 307 |
+
const files = parts.map((p) => [`${MESMER}/${man.files[p].path}`, man.files[p].bytes]);
|
| 308 |
+
const total = files.reduce((a, [, b]) => a + b, 0);
|
| 309 |
+
const [fusion, policy] = await Promise.all(['fusion.json', 'policy.json'].map((f) => fetchBytes(`${MESMER}/${f}`).then((b) => JSON.parse(new TextDecoder().decode(b)))));
|
| 310 |
+
const bytes = await download('mesmer', files, total);
|
| 311 |
+
const sessions = {};
|
| 312 |
+
for (const [i, p] of parts.entries()) {
|
| 313 |
+
show('mesmer', 'busy', 1, `starting on your GPU (${i + 1} of ${parts.length})…`);
|
| 314 |
+
sessions[p] = await ort.InferenceSession.create(bytes[i], { executionProviders: ['webgpu'], logSeverityLevel: 3 });
|
| 315 |
+
bytes[i] = null;
|
| 316 |
+
}
|
| 317 |
+
const th = policy.thresholds;
|
| 318 |
+
return { ort, man, sessions, fusion: { weights: fusion.weights, thresholds: { ai: th.likely_ai_at_or_above, unc: th.uncertain_at_or_above },
|
| 319 |
+
rank: { pel: makeRanker(fusion.reference.pel), peg: makeRanker(fusion.reference.peg) } } };
|
| 320 |
+
})());
|
| 321 |
+
|
| 322 |
+
// ---------------------------------------------------------------- decode
|
| 323 |
+
// Strips of at most 4 MP: phone browsers cap canvas area (16 MP on iOS), photos are often bigger.
|
| 324 |
+
// sRGB: EXIF orientation, ICC -> sRGB, alpha over white (mesmer's to_srgb).
|
| 325 |
+
// raw: EXIF orientation, colours as stored (Smaug's PIL .convert('RGB') does no colour management).
|
| 326 |
+
async function decode(blob, srgb) {
|
| 327 |
+
const bmp = await createImageBitmap(blob, { imageOrientation: 'from-image', colorSpaceConversion: srgb ? 'default' : 'none',
|
| 328 |
+
premultiplyAlpha: srgb ? 'default' : 'none' });
|
| 329 |
+
const w = bmp.width, h = bmp.height, step = Math.max(1, Math.min(h, Math.floor(4194304 / w)));
|
| 330 |
+
const cv = typeof OffscreenCanvas !== 'undefined' ? new OffscreenCanvas(w, step) : Object.assign(document.createElement('canvas'), { width: w, height: step });
|
| 331 |
+
const ctx = cv.getContext('2d', { willReadFrequently: true });
|
| 332 |
+
ctx.imageSmoothingEnabled = false;
|
| 333 |
+
const out = new Uint8Array(w * h * 3);
|
| 334 |
+
for (let y = 0; y < h; y += step) {
|
| 335 |
+
const n = Math.min(step, h - y);
|
| 336 |
+
if (srgb) { ctx.globalCompositeOperation = 'source-over'; ctx.fillStyle = '#fff'; ctx.fillRect(0, 0, w, step); }
|
| 337 |
+
else { ctx.globalCompositeOperation = 'copy'; }
|
| 338 |
+
ctx.drawImage(bmp, 0, y, w, n, 0, 0, w, n);
|
| 339 |
+
const d = ctx.getImageData(0, 0, w, n).data;
|
| 340 |
+
for (let i = 0, o = y * w * 3; i < w * n * 4; i += 4, o += 3) { out[o] = d[i]; out[o + 1] = d[i + 1]; out[o + 2] = d[i + 2]; }
|
| 341 |
+
await tick();
|
| 342 |
+
}
|
| 343 |
+
bmp.close();
|
| 344 |
+
return { w, h, data: out };
|
| 345 |
+
}
|
| 346 |
+
|
| 347 |
+
// ---------------------------------------------------------------- scan
|
| 348 |
+
let latest = 0, chain = Promise.resolve();
|
| 349 |
+
|
| 350 |
+
function onImage(blob, from = null) {
|
| 351 |
+
if (!blob || !/^image\//.test(blob.type || 'image/')) return;
|
| 352 |
+
const id = ++latest;
|
| 353 |
+
ui.photo.src = URL.createObjectURL(blob);
|
| 354 |
+
ui.photo.onload = () => URL.revokeObjectURL(ui.photo.src);
|
| 355 |
+
ui.drop.classList.add('has');
|
| 356 |
+
for (const b of ui.thumbs.children) b.classList.toggle('on', b === from);
|
| 357 |
+
for (const n of Object.keys(rows)) show(n, 'busy', 0, 'reading the photo…');
|
| 358 |
+
chain = chain.then(() => scan(blob, id)).catch((e) => console.error(e));
|
| 359 |
+
}
|
| 360 |
+
|
| 361 |
+
async function scan(blob, id) {
|
| 362 |
+
const stale = () => id !== latest;
|
| 363 |
+
if (stale()) return;
|
| 364 |
+
let smaugIn, mesmerIn;
|
| 365 |
+
try {
|
| 366 |
+
await tick();
|
| 367 |
+
smaugIn = smaugCrops(await decode(blob, false));
|
| 368 |
+
if (stale()) return;
|
| 369 |
+
mesmerIn = mesmerViews(await decode(blob, true));
|
| 370 |
+
} catch (e) {
|
| 371 |
+
for (const n of Object.keys(rows)) show(n, 'bad', 0, `cannot read this image (${e.message || e.name})`);
|
| 372 |
+
return;
|
| 373 |
+
}
|
| 374 |
+
if (stale()) return;
|
| 375 |
+
for (const n of Object.keys(rows)) show(n, 'busy', 0, 'waiting…');
|
| 376 |
+
getMesmer().catch(() => {}); // Mesmer downloads while Smaug runs: separate promise, starts once
|
| 377 |
+
|
| 378 |
+
try {
|
| 379 |
+
const s = await getSmaug();
|
| 380 |
+
if (stale()) return;
|
| 381 |
+
show('smaug', 'busy', 0, 'scanning…');
|
| 382 |
+
const t0 = performance.now(), { ort } = { ort: await getOrt() };
|
| 383 |
+
const r = await s.run({ pixels: new ort.Tensor('float32', smaugIn.tensor, [3, 3, 528, 528]) });
|
| 384 |
+
const lg = Array.from(r.logits.data), p = sigmoid(lg.reduce((a, b) => a + b, 0) / lg.length);
|
| 385 |
+
Object.values(r).forEach((t) => t.dispose?.());
|
| 386 |
+
if (!stale()) {
|
| 387 |
+
const ai = p >= 0.5;
|
| 388 |
+
show('smaug', ai ? 'ai' : 'real', p, `<b>${ai ? 'Looks AI-made' : 'Looks real'}</b> · ${(100 * p).toFixed(p > 0.995 || p < 0.005 ? 1 : 0)}% AI · ${((performance.now() - t0) / 1000).toFixed(1)} s`);
|
| 389 |
+
rows.smaug.title = `crop logits ${lg.map((v) => v.toFixed(2)).join(', ')}`;
|
| 390 |
+
}
|
| 391 |
+
} catch (e) { show('smaug', 'bad', 0, e.message || String(e)); }
|
| 392 |
+
|
| 393 |
+
if (stale()) return;
|
| 394 |
+
try {
|
| 395 |
+
const m = await getMesmer();
|
| 396 |
+
if (stale()) return;
|
| 397 |
+
show('mesmer', 'busy', 0, 'scanning…');
|
| 398 |
+
const { ort, sessions: S, fusion } = m, t0 = performance.now();
|
| 399 |
+
const pr = await S.pel.run({ pixels: new ort.Tensor('float32', mesmerIn.pel, [5, 3, 336, 336]) });
|
| 400 |
+
const pv = Array.from(pr.logit.data), pel = pv.reduce((a, b) => a + b, 0) / pv.length;
|
| 401 |
+
let x = new ort.Tensor('float32', mesmerIn.peg, [1, 3, 448, 448]), s25 = null, s38 = null, peg = null;
|
| 402 |
+
for (const [i, part] of m.man.peg.entries()) {
|
| 403 |
+
show('mesmer', 'busy', (i + 1) / (m.man.peg.length + 1), 'scanning…');
|
| 404 |
+
const sess = S[part], feed = {};
|
| 405 |
+
for (const n of sess.inputNames) feed[n] = n === 's25' ? s25 : n === 's38' ? s38 : x;
|
| 406 |
+
const o = await sess.run(feed);
|
| 407 |
+
if (o.logit) { peg = o.logit.data[0]; break; }
|
| 408 |
+
x = o.y || o.x;
|
| 409 |
+
if (o.s25) s25 = o.s25;
|
| 410 |
+
if (o.s38) s38 = o.s38;
|
| 411 |
+
}
|
| 412 |
+
const v = mesmerVerdict(pel, peg, fusion);
|
| 413 |
+
if (!stale()) {
|
| 414 |
+
const label = { likely_ai: ['ai', 'Likely AI'], uncertain: ['unsure', 'Uncertain'], likely_real: ['real', 'Likely real'] }[v.verdict];
|
| 415 |
+
show('mesmer', label[0], v.p_ai, `<b>${label[1]}</b> · ${(100 * v.p_ai).toFixed(v.p_ai > 0.995 || v.p_ai < 0.005 ? 1 : 0)}% · ${((performance.now() - t0) / 1000).toFixed(1)} s`);
|
| 416 |
+
rows.mesmer.title = `score ${v.score.toFixed(3)} (uncertain from ${fusion.thresholds.unc.toFixed(2)}, likely AI from ${fusion.thresholds.ai.toFixed(2)}); PE-L logit ${pel.toFixed(3)}, PE-G logit ${peg.toFixed(3)}`;
|
| 417 |
+
}
|
| 418 |
+
} catch (e) { show('mesmer', 'bad', 0, e.message || String(e)); }
|
| 419 |
+
storage();
|
| 420 |
+
}
|
| 421 |
+
|
| 422 |
+
// ---------------------------------------------------------------- input
|
| 423 |
+
ui.file.addEventListener('change', () => { onImage(ui.file.files[0]); ui.file.value = ''; });
|
| 424 |
+
addEventListener('paste', (e) => {
|
| 425 |
+
const f = [...(e.clipboardData?.files || [])].find((x) => x.type.startsWith('image/'));
|
| 426 |
+
if (f) { e.preventDefault(); onImage(f); }
|
| 427 |
+
});
|
| 428 |
+
addEventListener('dragover', (e) => { e.preventDefault(); ui.drop.classList.add('over'); });
|
| 429 |
+
addEventListener('dragleave', (e) => { if (!e.relatedTarget) ui.drop.classList.remove('over'); });
|
| 430 |
+
addEventListener('drop', (e) => {
|
| 431 |
+
e.preventDefault(); ui.drop.classList.remove('over');
|
| 432 |
+
onImage([...(e.dataTransfer?.files || [])].find((x) => x.type.startsWith('image/')));
|
| 433 |
+
});
|
| 434 |
+
|
| 435 |
+
for (const [file, alt, link] of EXAMPLES) {
|
| 436 |
+
const b = document.createElement('button');
|
| 437 |
+
b.type = 'button'; b.title = alt;
|
| 438 |
+
b.innerHTML = `<img src="examples/thumb/${file.replace(/\.\w+$/, '.jpg')}" alt="${alt}" loading="lazy">`;
|
| 439 |
+
b.addEventListener('click', async () => {
|
| 440 |
+
try {
|
| 441 |
+
const r = await fetch(`examples/${file}`);
|
| 442 |
+
if (!r.ok) throw new Error(`HTTP ${r.status}`);
|
| 443 |
+
onImage(new Blob([await r.arrayBuffer()], { type: file.endsWith('.png') ? 'image/png' : 'image/jpeg' }), b);
|
| 444 |
+
} catch (e) { show('smaug', 'bad', 0, `example: ${e.message}`); }
|
| 445 |
+
});
|
| 446 |
+
ui.thumbs.append(b);
|
| 447 |
+
}
|
| 448 |
+
$('credits').innerHTML = 'Examples: ' + EXAMPLES.map(([, alt, link]) => `<a href="${link}" target="_blank" rel="noopener">${alt.split(': ')[1]}</a>`).join('; ') +
|
| 449 |
+
'. Unsplash photos under the Unsplash License; OpenFake images under CC BY-NC 4.0.';
|
| 450 |
+
$('repo').href = `https://huggingface.co/${MESMER_REPO}`; $('repo').textContent = MESMER_REPO;
|
| 451 |
+
|
| 452 |
+
async function storage() {
|
| 453 |
+
try {
|
| 454 |
+
const e = await navigator.storage.estimate();
|
| 455 |
+
ui.store.textContent = `This site stores ${mb(e.usage || 0)} in this browser.`;
|
| 456 |
+
} catch { ui.store.textContent = ''; }
|
| 457 |
+
}
|
| 458 |
+
ui.wipe.addEventListener('click', async () => {
|
| 459 |
+
await caches.delete(CACHE).catch(() => {});
|
| 460 |
+
smaugP = mesmerP = null;
|
| 461 |
+
ui.store.textContent = 'Deleted. The models download again on the next photo.';
|
| 462 |
+
});
|
| 463 |
+
storage();
|
| 464 |
+
getManifest().then((m) => { $('sizes').textContent = `${((181210404 + Object.values(m.files).reduce((a, f) => a + f.bytes, 0)) / 1e9).toFixed(1)} GB`; }).catch(() => {});
|
examples/ai-dflow2.png
ADDED
|
Git LFS Details
|
examples/ai-gpt-image-2.png
ADDED
|
Git LFS Details
|
examples/ai-midjourney-7.png
ADDED
|
Git LFS Details
|
examples/photo-real-1.jpg
ADDED
|
Git LFS Details
|
examples/photo-real-2.jpg
ADDED
|
Git LFS Details
|
examples/photo-real-3.jpg
ADDED
|
Git LFS Details
|
examples/thumb/ai-dflow2.jpg
ADDED
|
|
examples/thumb/ai-gpt-image-2.jpg
ADDED
|
|
examples/thumb/ai-midjourney-7.jpg
ADDED
|
|
examples/thumb/photo-real-1.jpg
ADDED
|
|
examples/thumb/photo-real-2.jpg
ADDED
|
|
examples/thumb/photo-real-3.jpg
ADDED
|
|
index.html
CHANGED
|
@@ -1,19 +1,54 @@
|
|
| 1 |
<!doctype html>
|
| 2 |
-
<html>
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
</html>
|
|
|
|
| 1 |
<!doctype html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="utf-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
| 6 |
+
<title>AI Image Check</title>
|
| 7 |
+
<meta name="description" content="Two AI image detectors, Smaug and Mesmer, running on your own GPU with ONNX Runtime Web. Your photo never leaves this tab.">
|
| 8 |
+
<link rel="icon" href="data:,">
|
| 9 |
+
<link rel="stylesheet" href="style.css">
|
| 10 |
+
</head>
|
| 11 |
+
<body>
|
| 12 |
+
<main>
|
| 13 |
+
<p id="line"><b>Is it AI?</b> <a href="https://huggingface.co/abacusai/Smaug-AI-Image-Detector" target="_blank" rel="noopener">Smaug</a>
|
| 14 |
+
and <a href="https://huggingface.co/mesmertech/mesmer-ai-image-detector" target="_blank" rel="noopener">Mesmer</a> check your photo on your own GPU. It never leaves this tab.</p>
|
| 15 |
+
|
| 16 |
+
<label id="drop" for="file">
|
| 17 |
+
<img id="photo" alt="">
|
| 18 |
+
<span id="ask">Drop, paste or <u>pick a photo</u></span>
|
| 19 |
+
<input id="file" type="file" accept="image/*" class="sr">
|
| 20 |
+
</label>
|
| 21 |
+
|
| 22 |
+
<section id="results" aria-live="polite">
|
| 23 |
+
<div class="res" id="smaug"><a class="name" href="https://huggingface.co/abacusai/Smaug-AI-Image-Detector" target="_blank" rel="noopener">Smaug</a>
|
| 24 |
+
<span class="meter"><i></i></span><span class="val">waiting for a photo</span></div>
|
| 25 |
+
<div class="res" id="mesmer"><a class="name" href="https://huggingface.co/mesmertech/mesmer-ai-image-detector" target="_blank" rel="noopener">Mesmer</a>
|
| 26 |
+
<span class="meter"><i></i></span><span class="val">waiting for a photo</span></div>
|
| 27 |
+
</section>
|
| 28 |
+
|
| 29 |
+
<section id="examples">
|
| 30 |
+
<p>Or try one: <span class="k">real photos</span> then <span class="k">AI images</span></p>
|
| 31 |
+
<div id="thumbs"></div>
|
| 32 |
+
</section>
|
| 33 |
+
|
| 34 |
+
<details id="more">
|
| 35 |
+
<summary>About, storage and credits</summary>
|
| 36 |
+
<p>Both models download once into this browser (about <span id="sizes">1.3 GB</span>), then work offline. They need WebGPU
|
| 37 |
+
(Chrome or Edge 121+, Android 12+, Safari 26). Smaug answers first; Mesmer is bigger and follows.</p>
|
| 38 |
+
<p>A score is a likelihood, not proof. Detectors miss images from new generators, and heavy edits, screenshots or
|
| 39 |
+
recompression can fool them. Do not use them as the only evidence about a person.</p>
|
| 40 |
+
<p><b>Smaug</b>: <a href="https://huggingface.co/abacusai/Smaug-AI-Image-Detector" target="_blank" rel="noopener">abacusai/Smaug-AI-Image-Detector</a>
|
| 41 |
+
(Apache-2.0), run from the 4-bit web build <a href="https://huggingface.co/21amd21/smaug-ai-image-detector-web" target="_blank" rel="noopener">21amd21/smaug-ai-image-detector-web</a>.
|
| 42 |
+
The bar is sigmoid of the mean logit of three 528 px crops.</p>
|
| 43 |
+
<p><b>Mesmer</b>: <a href="https://huggingface.co/mesmertech/mesmer-ai-image-detector" target="_blank" rel="noopener">mesmertech/mesmer-ai-image-detector</a>
|
| 44 |
+
(CC-BY-NC-SA-4.0, non-commercial), on Meta's Perception Encoder backbones (Apache-2.0). ONNX conversion:
|
| 45 |
+
<a id="repo" target="_blank" rel="noopener">model repo</a>. Verdict lines and the bar come from the model's policy: 50% at "uncertain", 90% at "likely AI";
|
| 46 |
+
it is a display score, not a calibrated probability.</p>
|
| 47 |
+
<p id="store"></p>
|
| 48 |
+
<button id="wipe" type="button">Delete downloaded models</button>
|
| 49 |
+
<p id="credits"></p>
|
| 50 |
+
</details>
|
| 51 |
+
</main>
|
| 52 |
+
<script type="module" src="app.js"></script>
|
| 53 |
+
</body>
|
| 54 |
</html>
|
style.css
CHANGED
|
@@ -1,28 +1,74 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
| 4 |
}
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
}
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
}
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
padding: 16px;
|
| 22 |
-
border: 1px solid lightgray;
|
| 23 |
-
border-radius: 16px;
|
| 24 |
}
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
|
|
|
| 28 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
:root {
|
| 2 |
+
--bg: #f4f2ee; --panel: #ffffff; --ink: #1d1c1a; --muted: #6b6760; --line: #d8d4cc;
|
| 3 |
+
--accent: #b4441b; --accent-ink: #ffffff; --bad: #a3261b;
|
| 4 |
+
--real: #2f6f4f; --unsure: #a8741a; --ai: #b4441b;
|
| 5 |
+
color-scheme: light dark;
|
| 6 |
}
|
| 7 |
+
@media (prefers-color-scheme: dark) {
|
| 8 |
+
:root { --bg: #151413; --panel: #1f1d1b; --ink: #ece8e1; --muted: #a29d94; --line: #3a3732;
|
| 9 |
+
--accent: #e0703f; --accent-ink: #151413; --bad: #ff8a7a;
|
| 10 |
+
--real: #6cc097; --unsure: #e2b04f; --ai: #e0703f; }
|
| 11 |
}
|
| 12 |
+
* { box-sizing: border-box; }
|
| 13 |
+
html, body { margin: 0; background: var(--bg); color: var(--ink);
|
| 14 |
+
font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
|
| 15 |
+
main {
|
| 16 |
+
display: grid; gap: 10px; padding: 8px 12px 16px; max-width: 1100px; margin: 0 auto;
|
| 17 |
+
grid-template-columns: minmax(0, 1fr);
|
| 18 |
+
grid-template-areas: "line" "drop" "results" "examples" "more";
|
| 19 |
}
|
| 20 |
+
@media (min-width: 760px) {
|
| 21 |
+
main { grid-template-columns: minmax(0, 1.3fr) minmax(0, 1fr); align-items: start;
|
| 22 |
+
grid-template-rows: auto auto auto 1fr;
|
| 23 |
+
grid-template-areas: "line line" "drop results" "drop examples" "drop more"; }
|
|
|
|
|
|
|
|
|
|
| 24 |
}
|
| 25 |
+
#line { grid-area: line; margin: 0; font-size: 14px; color: var(--muted); }
|
| 26 |
+
#line b { color: var(--ink); font-weight: 600; font-size: 15px; }
|
| 27 |
|
| 28 |
+
#drop {
|
| 29 |
+
grid-area: drop; position: relative; display: grid; place-items: center; cursor: pointer;
|
| 30 |
+
min-height: 240px; background: var(--panel); border: 1.5px dashed var(--line); border-radius: 6px; overflow: hidden;
|
| 31 |
}
|
| 32 |
+
#drop.over { border-color: var(--accent); }
|
| 33 |
+
#drop img { display: none; max-width: 100%; max-height: 70vh; object-fit: contain; }
|
| 34 |
+
#drop.has img { display: block; }
|
| 35 |
+
#drop.has { border-style: solid; }
|
| 36 |
+
#ask { color: var(--muted); padding: 24px; text-align: center; }
|
| 37 |
+
#ask u { color: var(--accent); }
|
| 38 |
+
#drop.has #ask { position: absolute; bottom: 6px; right: 8px; padding: 2px 8px; font-size: 12px;
|
| 39 |
+
background: var(--panel); border-radius: 4px; opacity: .9; }
|
| 40 |
+
|
| 41 |
+
#results { grid-area: results; display: grid; gap: 8px; }
|
| 42 |
+
.res { display: grid; grid-template-columns: 4.8em minmax(0, 1fr); grid-template-areas: "name meter" "name val";
|
| 43 |
+
gap: 3px 10px; align-items: center; padding: 8px 10px; background: var(--panel); border: 1px solid var(--line); border-radius: 6px; }
|
| 44 |
+
.res .name { grid-area: name; font-weight: 600; color: var(--ink); text-decoration: none; align-self: start; padding-top: 1px; }
|
| 45 |
+
.res .name:hover { text-decoration: underline; }
|
| 46 |
+
.meter { grid-area: meter; height: 8px; background: var(--line); border-radius: 4px; overflow: hidden; }
|
| 47 |
+
.meter i { display: block; height: 100%; width: 0; background: var(--muted); transition: width .3s; }
|
| 48 |
+
.val { grid-area: val; font-size: 14px; color: var(--muted); font-variant-numeric: tabular-nums; min-height: 1.4em; }
|
| 49 |
+
.val b { font-weight: 600; }
|
| 50 |
+
.res.real .meter i { background: var(--real); } .res.real .val b { color: var(--real); }
|
| 51 |
+
.res.unsure .meter i { background: var(--unsure); } .res.unsure .val b { color: var(--unsure); }
|
| 52 |
+
.res.ai .meter i { background: var(--ai); } .res.ai .val b { color: var(--ai); }
|
| 53 |
+
.res.busy .meter i { background: var(--muted); opacity: .6; }
|
| 54 |
+
.res.bad .val { color: var(--bad); }
|
| 55 |
+
|
| 56 |
+
#examples { grid-area: examples; }
|
| 57 |
+
#examples p { margin: 0 0 6px; font-size: 13px; color: var(--muted); }
|
| 58 |
+
#thumbs { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 6px; }
|
| 59 |
+
@media (max-width: 420px) { #thumbs { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
|
| 60 |
+
#thumbs button { padding: 0; border: 2px solid transparent; border-radius: 5px; background: none; cursor: pointer; aspect-ratio: 1; overflow: hidden; }
|
| 61 |
+
#thumbs button.on { border-color: var(--accent); }
|
| 62 |
+
#thumbs img { width: 100%; height: 100%; object-fit: cover; display: block; border-radius: 3px; }
|
| 63 |
+
.k { color: var(--ink); }
|
| 64 |
+
|
| 65 |
+
#more { grid-area: more; color: var(--muted); font-size: 13px; }
|
| 66 |
+
#more p { margin: 6px 0; }
|
| 67 |
+
summary { cursor: pointer; }
|
| 68 |
+
#wipe { font: inherit; min-height: 30px; padding: 0 12px; cursor: pointer; color: var(--ink); background: transparent;
|
| 69 |
+
border: 1px solid var(--line); border-radius: 4px; }
|
| 70 |
+
a { color: var(--accent); }
|
| 71 |
+
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
|
| 72 |
+
:where(button, input, summary, a, label, [tabindex]):focus-visible { outline: 2px solid var(--accent) !important; outline-offset: 2px; }
|
| 73 |
+
#drop:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; }
|
| 74 |
+
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { transition-duration: 0.01ms !important; } }
|