Samarth043 commited on
Commit
def45de
路
verified 路
1 Parent(s): f1ef9e0

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +16 -13
index.html CHANGED
@@ -35,14 +35,14 @@ small{color:#93a3b1}
35
  </div>
36
  </main>
37
  <script type="module">
38
- import { pipeline } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3';
39
  const $ = id => document.getElementById(id);
40
  const MODEL = 'buildborderless/CommunityForensics-DeepfakeDet-ViT';
41
- let clf;
42
  async function load(){
43
- if(clf) return clf;
44
- clf = await pipeline('image-classification', MODEL, { dtype: 'fp32' });
45
- return clf;
46
  }
47
  const TOOLS = [[/midjourney/i,'Midjourney'],[/dall[\s\-路]?e|openai/i,'OpenAI / DALL路E'],[/stable\s?diffusion|sdxl|automatic1111/i,'Stable Diffusion'],[/firefly|generative ?fill/i,'Adobe Firefly'],[/imagen|gemini|synthid/i,'Google AI'],[/leonardo\.?ai/i,'Leonardo AI'],[/comfyui/i,'ComfyUI'],[/ideogram/i,'Ideogram']];
48
  function markers(bytes){
@@ -62,20 +62,23 @@ $('f').onchange = async e => {
62
  $('pct').textContent = ''; $('b').style.width = '0'; $('m').innerHTML = ''; $('raw').textContent = '';
63
  try {
64
  const mk = markers(new Uint8Array(await file.arrayBuffer()));
65
- const c = await load();
66
  $('v').textContent = 'Checking...';
67
- const r = await c(url, { top_k: 5 });
68
- const hit = r.find(x => /fake|ai|synth|generat|artificial/i.test(x.label));
69
- let ai = hit ? hit.score : null;
70
- if(ai === null && r.length === 2){ ai = r[0].score < 0.5 ? r[0].score : 1 - r[1].score; }
71
- $('raw').textContent = 'Model output: ' + r.map(x => x.label + ' ' + Math.round(x.score*100) + '%').join(', ');
 
 
 
72
  mk.forEach(t => { const li = document.createElement('li'); li.textContent = t; $('m').appendChild(li); });
73
  if(mk.length) $('v').textContent = 'AI markers found';
74
- else if(ai === null) $('v').textContent = 'Result below';
75
  else if(ai >= 0.85) $('v').textContent = 'Likely AI-generated';
76
  else if(ai >= 0.5) $('v').textContent = 'Possibly AI';
77
  else $('v').textContent = 'Likely a real photo';
78
- if(ai !== null){ $('pct').textContent = 'Chance it is AI: ' + Math.round(ai*100) + '%'; $('b').style.width = Math.round(ai*100) + '%'; }
 
79
  } catch(err){
80
  $('v').textContent = 'Could not analyze this image.';
81
  $('raw').textContent = String(err);
 
35
  </div>
36
  </main>
37
  <script type="module">
38
+ import { AutoProcessor, AutoModelForImageClassification, RawImage } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3';
39
  const $ = id => document.getElementById(id);
40
  const MODEL = 'buildborderless/CommunityForensics-DeepfakeDet-ViT';
41
+ let processor, model;
42
  async function load(){
43
+ if(model) return;
44
+ processor = await AutoProcessor.from_pretrained(MODEL);
45
+ model = await AutoModelForImageClassification.from_pretrained(MODEL, { dtype: 'fp32' });
46
  }
47
  const TOOLS = [[/midjourney/i,'Midjourney'],[/dall[\s\-路]?e|openai/i,'OpenAI / DALL路E'],[/stable\s?diffusion|sdxl|automatic1111/i,'Stable Diffusion'],[/firefly|generative ?fill/i,'Adobe Firefly'],[/imagen|gemini|synthid/i,'Google AI'],[/leonardo\.?ai/i,'Leonardo AI'],[/comfyui/i,'ComfyUI'],[/ideogram/i,'Ideogram']];
48
  function markers(bytes){
 
62
  $('pct').textContent = ''; $('b').style.width = '0'; $('m').innerHTML = ''; $('raw').textContent = '';
63
  try {
64
  const mk = markers(new Uint8Array(await file.arrayBuffer()));
65
+ await load();
66
  $('v').textContent = 'Checking...';
67
+ const image = await RawImage.read(url);
68
+ const inputs = await processor(image);
69
+ const { logits } = await model(inputs);
70
+ const z = Array.from(logits.data);
71
+ let ai;
72
+ if(z.length === 1){ ai = 1 / (1 + Math.exp(-z[0])); }
73
+ else { const m = Math.max(...z), ex = z.map(v => Math.exp(v - m)), s = ex.reduce((a,b) => a + b, 0); ai = ex[1] / s; }
74
+ $('raw').textContent = 'Raw model score: ' + z.map(v => v.toFixed(2)).join(', ');
75
  mk.forEach(t => { const li = document.createElement('li'); li.textContent = t; $('m').appendChild(li); });
76
  if(mk.length) $('v').textContent = 'AI markers found';
 
77
  else if(ai >= 0.85) $('v').textContent = 'Likely AI-generated';
78
  else if(ai >= 0.5) $('v').textContent = 'Possibly AI';
79
  else $('v').textContent = 'Likely a real photo';
80
+ $('pct').textContent = 'Chance it is AI: ' + Math.round(ai*100) + '%';
81
+ $('b').style.width = Math.round(ai*100) + '%';
82
  } catch(err){
83
  $('v').textContent = 'Could not analyze this image.';
84
  $('raw').textContent = String(err);