File size: 13,730 Bytes
928b74f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e03466a
 
 
 
 
928b74f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * app.js β€” ImageForensics-Detect Frontend
 * Handles file upload, API calls, result rendering, animations.
 */

// ──// Use current hostname to avoid CORS/Localhost resolution issues
// For deployment (Vercel/HuggingFace), empty string "" uses current host/port
const API_HOST = window.location.hostname;
const API_BASE = (API_HOST === "" || API_HOST === "localhost" || API_HOST === "127.0.0.1")
  ? "http://localhost:8000"
  : "";

console.log("[Forensics] Host:", API_HOST || "local-file");
console.log("[Forensics] API Base:", API_BASE || "relative-root");

// ── State ────────────────────────────────────────────────────────
let selectedFile = null;
let stepTimer = null;
const STEPS = ["spectral", "edge", "cnn", "vit", "diffusion", "fusion"];

// ── DOM refs ─────────────────────────────────────────────────────
const uploadZone = document.getElementById("uploadZone");
const fileInput = document.getElementById("fileInput");
const selectBtn = document.getElementById("selectBtn");
const previewArea = document.getElementById("previewArea");
const previewImg = document.getElementById("previewImg");
const fileNameEl = document.getElementById("fileName");
const fileSizeEl = document.getElementById("fileSize");
const analyzeBtn = document.getElementById("analyzeBtn");
const clearBtn = document.getElementById("clearBtn");
const analyzingState = document.getElementById("analyzingState");
const resultsSection = document.getElementById("resultsSection");
const errorBanner = document.getElementById("errorBanner");
const errorMsg = document.getElementById("errorMsg");
const serverStatus = document.getElementById("serverStatus");

// ── UI Helpers ───────────────────────────────────────────────────
function showError(msg) {
  errorMsg.textContent = msg;
  errorBanner.style.display = "flex";
}

function hideError() {
  errorBanner.style.display = "none";
}

// Fixed for hoisting: define resetUI as a window property but also a function
window.resetUI = function resetUI() {
  selectedFile = null;
  if (fileInput) fileInput.value = "";
  if (previewArea) previewArea.style.display = "none";
  if (resultsSection) resultsSection.style.display = "none";
  if (analyzingState) analyzingState.style.display = "none";
  if (errorBanner) errorBanner.style.display = "none";
  if (uploadZone) uploadZone.scrollIntoView({ behavior: "smooth" });
};

// ── Server health check ──────────────────────────────────────────
async function checkServerHealth() {
  try {
    const controller = new AbortController();
    const timeoutId = setTimeout(() => controller.abort(), 3000);
    const r = await fetch(`${API_BASE}/health`, { signal: controller.signal });
    clearTimeout(timeoutId);
    if (r.ok) {
      serverStatus.textContent = "● API Online";
      serverStatus.classList.remove("offline");
    } else {
      throw new Error("not ok");
    }
  } catch (err) {
    if (err.name === 'AbortError') {
      // Silent timeout
    } else {
      console.warn("[Forensics] Health check failed:", err.message);
    }
    serverStatus.textContent = "● API Offline";
    serverStatus.classList.add("offline");
  }
}

// ── Analysis Steps Animation ──────────────────────────────────────
function animateSteps() {
  let i = 0;
  console.log("[Forensics] Starting analysis animations...");
  STEPS.forEach(s => {
    const el = document.getElementById(`step-${s}`);
    if (el) {
      el.className = "step-item";
      const iconMatch = el.textContent.match(/^[βœ”β—‹γ€‡]\s*/);
      if (iconMatch) {
        el.textContent = "β—‹ " + el.textContent.substring(iconMatch[0].length);
      }
    }
  });

  stepTimer = setInterval(() => {
    if (i > 0) {
      const prev = document.getElementById(`step-${STEPS[i - 1]}`);
      if (prev) {
        prev.classList.remove("active");
        prev.classList.add("done");
        prev.textContent = "βœ” " + prev.textContent.slice(2);
      }
    }
    if (i < STEPS.length) {
      const cur = document.getElementById(`step-${STEPS[i]}`);
      if (cur) cur.classList.add("active");
      i++;
    } else {
      clearInterval(stepTimer);
    }
  }, 600);
}

function stopStepAnimation() {
  if (stepTimer) clearInterval(stepTimer);
  STEPS.forEach(s => {
    const el = document.getElementById(`step-${s}`);
    if (el) el.classList.remove("active");
  });
}

// ── Viz Loading Helper ───────────────────────────────────────────
function setVizImg(id, b64) {
  const el = document.getElementById(id);
  if (!el) return;
  if (b64) {
    el.src = `data:image/jpeg;base64,${b64}`;
    el.classList.add("loaded");
  } else {
    el.classList.remove("loaded");
    el.src = "";
  }
}

// ── Result Rendering ─────────────────────────────────────────────
function renderResults(data) {
  const isFake = data.prediction === "AI-Generated";
  const conf = data.confidence;
  const probFake = data.prob_fake;
  const probReal = 1 - probFake;

  // ── Verdict Card ──────────────────────────────────────────────
  const verdictCard = document.getElementById("verdictCard");
  if (verdictCard) verdictCard.className = `verdict-card ${isFake ? "fake" : "real"}`;

  const iconEl = document.getElementById("verdictIcon");
  if (iconEl) iconEl.textContent = isFake ? "πŸ€–" : "πŸ“·";

  const vtEl = document.getElementById("verdictText");
  if (vtEl) {
    vtEl.textContent = data.prediction;
    vtEl.className = `verdict-text ${isFake ? "fake" : "real"}`;
  }

  const vsEl = document.getElementById("verdictSub");
  if (vsEl) {
    vsEl.textContent = `Overall confidence: ${conf.toFixed(1)}% Β· ${isFake
      ? "Characteristics of AI-generated content detected"
      : "Characteristics of a real camera photograph detected"}`;
  }

  // Probability Bar
  const pctFake = Math.round(probFake * 100);
  const pctReal = Math.round(probReal * 100);
  const pbReal = document.getElementById("probBarReal");
  const pbFake = document.getElementById("probBarFake");
  if (pbReal) pbReal.style.width = `${pctReal}%`;
  if (pbFake) pbFake.style.width = `${pctFake}%`;

  const prPct = document.getElementById("probRealPct");
  const pfPct = document.getElementById("probFakePct");
  if (prPct) prPct.textContent = `${pctReal}%`;
  if (pfPct) pfPct.textContent = `${pctFake}%`;

  // Confidence Ring
  const ring = document.getElementById("ringFill");
  const confVal = document.getElementById("confValue");
  if (ring && confVal) {
    const circumf = 2 * Math.PI * 50;
    ring.style.strokeDashoffset = circumf * (1 - conf / 100);
    ring.className = `ring-fill ${isFake ? "fake" : "real"}`;
    confVal.textContent = `${Math.round(conf)}%`;
    confVal.style.color = isFake ? "var(--fake)" : "var(--real)";
  }

  // ── Branch Cards ──────────────────────────────────────────────
  const fused = data.fused_weights || {};

  Object.entries(data.branches).forEach(([name, info]) => {
    const probFakeB = info.prob_fake;
    const conf_b = info.confidence;
    const weight = fused[name] || 0;
    const isFakeB = info.label === "AI-Generated";
    const color = isFakeB ? "var(--fake)" : "var(--real)";

    const card = document.getElementById(`bc-${name}`);
    if (card) card.style.borderLeft = `3px solid ${color}`;

    const bar = document.getElementById(`bar-${name}`);
    if (bar) {
      setTimeout(() => { bar.style.width = `${probFakeB * 100}%`; }, 100);
      bar.style.background = color;
    }

    const probEl = document.getElementById(`prob-${name}`);
    const confEl = document.getElementById(`conf-${name}`);
    const weightEl = document.getElementById(`weight-${name}`);
    if (probEl) {
      probEl.textContent = `${(probFakeB * 100).toFixed(1)}% fake`;
      probEl.style.color = color;
    }
    if (confEl) confEl.textContent = `conf: ${(conf_b * 100).toFixed(0)}%`;
    if (weightEl) {
      weightEl.textContent = `weight: ${(weight * 100).toFixed(1)}%`;
      weightEl.style.color = weight > 0.25 ? "var(--accent)" : "var(--text3)";
    }
  });

  // ── Visualizations ────────────────────────────────────────────
  const gradcamImg = document.getElementById("img-gradcam");
  const gradcamUnavail = document.getElementById("gradcam-unavail");
  const gradcamLabel = document.getElementById("gradcam-label");
  const gradcamSub = document.getElementById("gradcam-sub");

  if (gradcamImg) {
    if (data.gradcam_available && data.gradcam_b64) {
      gradcamImg.src = `data:image/jpeg;base64,${data.gradcam_b64}`;
      gradcamImg.classList.add("loaded");
      if (gradcamUnavail) gradcamUnavail.style.display = "none";
      if (gradcamLabel) gradcamLabel.textContent = "Saliency / Grad-CAM Heatmap";
      if (gradcamSub) gradcamSub.textContent = "Suspicious regions highlighted (JET colormap)";
    } else {
      gradcamImg.classList.remove("loaded");
      if (gradcamUnavail) gradcamUnavail.style.display = "flex";
      if (gradcamLabel) gradcamLabel.textContent = "Grad-CAM Heatmap";
      if (gradcamSub) gradcamSub.textContent = "Unavailable β€” CNN weights not loaded";
    }
  }

  setVizImg("img-spectrum", data.spectrum_b64);
  setVizImg("img-spectrum-ann", data.spectrum_annotated_b64);
  setVizImg("img-noise", data.noise_map_b64);
  setVizImg("img-edge", data.edge_map_b64);

  const lcBanner = document.getElementById("lowCertaintyBanner");
  if (lcBanner) lcBanner.style.display = data.low_certainty ? "block" : "none";
}

// ── Main Analysis Logic ──────────────────────────────────────────
async function runAnalysis() {
  console.log("[Forensics] runAnalysis() started for file:", selectedFile ? selectedFile.name : "none");
  if (!selectedFile) { showError("Please select an image first."); return; }

  previewArea.style.display = "none";
  resultsSection.style.display = "none";
  errorBanner.style.display = "none";
  analyzingState.style.display = "block";
  animateSteps();

  const formData = new FormData();
  formData.append("file", selectedFile);

  try {
    console.log("[Forensics] Fetching /predict...");
    const response = await fetch(`${API_BASE}/predict`, {
      method: "POST",
      body: formData,
    });
    console.log("[Forensics] /predict status:", response.status);

    stopStepAnimation();

    if (!response.ok) {
      const err = await response.json().catch(() => ({}));
      throw new Error(err.detail || `Server error ${response.status}`);
    }

    const data = await response.json();
    analyzingState.style.display = "none";
    renderResults(data);
    resultsSection.style.display = "block";
    resultsSection.scrollIntoView({ behavior: "smooth", block: "start" });

  } catch (e) {
    console.error("[Forensics] Analysis error:", e);
    stopStepAnimation();
    analyzingState.style.display = "none";
    previewArea.style.display = "flex";
    showError(`Analysis failed: ${e.message}. Make sure the API server is running.`);
  }
}

// ── File Handling ────────────────────────────────────────────────
function handleFile(file) {
  if (!file.type.startsWith("image/")) {
    showError("Please upload a valid image file (JPG, PNG, WebP, BMP).");
    return;
  }
  if (file.size > 15 * 1024 * 1024) {
    showError("File too large. Maximum allowed size is 15 MB.");
    return;
  }
  selectedFile = file;
  const url = URL.createObjectURL(file);
  previewImg.src = url;
  fileNameEl.textContent = file.name;
  fileSizeEl.textContent = `${(file.size / 1024).toFixed(1)} KB  Β·  ${file.type}`;
  hideError();
  previewArea.style.display = "flex";
}

// ── Event Listeners ──────────────────────────────────────────────
if (uploadZone) {
  uploadZone.addEventListener("dragover", e => {
    e.preventDefault();
    uploadZone.classList.add("drag-over");
  });
  uploadZone.addEventListener("dragleave", () => uploadZone.classList.remove("drag-over"));
  uploadZone.addEventListener("drop", e => {
    e.preventDefault();
    uploadZone.classList.remove("drag-over");
    const file = e.dataTransfer.files[0];
    if (file) handleFile(file);
  });
  uploadZone.addEventListener("click", e => {
    if (e.target === selectBtn || selectBtn.contains(e.target)) return;
    fileInput.click();
  });
}

if (selectBtn) selectBtn.addEventListener("click", e => { e.stopPropagation(); fileInput.click(); });
if (fileInput) fileInput.addEventListener("change", () => { if (fileInput.files[0]) handleFile(fileInput.files[0]); });
if (clearBtn) clearBtn.addEventListener("click", window.resetUI);
if (analyzeBtn) analyzeBtn.addEventListener("click", runAnalysis);

// Initialize
checkServerHealth();
setInterval(checkServerHealth, 15000);