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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
// DOM Elements
const uploadArea = document.getElementById("uploadArea");
const imageInput = document.getElementById("imageInput");
const imagePreview = document.getElementById("imagePreview");
const submitButton = document.getElementById("submitButton");
const errorMessage = document.getElementById("errorMessage");
const resultSection = document.getElementById("result");
const resultStatus = document.getElementById("resultStatus");
const resultDescription = document.getElementById("resultDescription");
const probabilityValue = document.getElementById("probabilityValue");
const probabilityFill = document.getElementById("probabilityFill");
const likelyInfo = document.getElementById("likelyInfo");
const resultImage = document.getElementById("resultImage");
const actionButtons = document.getElementById("actionButtons");
const navButtons = document.querySelectorAll(".nav-button");
const pages = document.querySelectorAll(".page");
const resourceCategories = document.querySelectorAll(".resource-category");
const optInCheckbox = document.getElementById("optInCheckbox");
const contactInfoInput = document.getElementById("contactInfo");
const contactOptin = document.querySelector(".contact-optin");

// State
let selectedImage = null;

// Custom dropdown logic
const dropdownToggle = document.getElementById("dropdownToggle");
const dropdownMenu = document.getElementById("dropdownMenu");
const dropdownOptions = dropdownMenu
  ? dropdownMenu.querySelectorAll(".dropdown-option")
  : [];
const selectedModelSpan = document.getElementById("selectedModel");
let selectedModelValue = "CompVis/stable-diffusion-v1-4";
const customDropdown = document.getElementById("modelDropdown");

// Loading animation
const loadingContainer = document.createElement("div");
loadingContainer.id = "loadingAnimation";
loadingContainer.style.display = "none";
loadingContainer.style.justifyContent = "center";
loadingContainer.style.alignItems = "center";
loadingContainer.style.flexDirection = "column";
loadingContainer.style.textAlign = "center";
loadingContainer.style.position = "absolute";
loadingContainer.style.top = "0";
loadingContainer.style.left = "0";
loadingContainer.style.width = "100%";
loadingContainer.style.height = "100%";
loadingContainer.style.zIndex = "2";
const loadingImg = document.createElement("img");
loadingImg.alt = "Loading...";
loadingImg.style.width = "64px";
loadingImg.style.height = "64px";
const loadingMsg = document.createElement("div");
loadingMsg.className = "loading-message";
loadingMsg.textContent = "Processing";
loadingContainer.appendChild(loadingImg);
loadingContainer.appendChild(loadingMsg);
resultSection.appendChild(loadingContainer);
let loadingFrame = 1;
let loadingInterval = null;
const resultContent = resultSection.querySelector(".result-content");

// About overlay logic
const aboutOverlay = document.getElementById("aboutOverlay");
const aboutCloseBtn = document.getElementById("aboutCloseBtn");

function showLoading() {
  resultSection.hidden = false;
  resultSection.style.display = "";
  if (resultContent) resultContent.style.display = "none";
  loadingContainer.style.display = "flex";
  loadingFrame = 1;
  updateLoadingFrame();
  loadingInterval = setInterval(() => {
    loadingFrame = (loadingFrame % 16) + 1;
    updateLoadingFrame();
  }, 60);
}
function hideLoading() {
  loadingContainer.style.display = "none";
  if (resultContent) resultContent.style.display = "";
  if (loadingInterval) clearInterval(loadingInterval);
}
function updateLoadingFrame() {
  loadingImg.src = `static/assets/Infer_LoadingAnimation/Property 1=Variant${loadingFrame}.svg`;
}

function hideResultSection() {
  resultSection.hidden = true;
  resultSection.style.display = "none";
  if (likelyInfo) likelyInfo.hidden = true;
  if (resultImage) resultImage.hidden = true;
  if (resultContent) resultContent.style.display = "";
  loadingContainer.style.display = "none";
}

function showResultSection() {
  resultSection.hidden = false;
  resultSection.style.display = "";
}

// Event Listeners
uploadArea.addEventListener("click", () => imageInput.click());
imageInput.addEventListener("change", handleImageUpload);
submitButton.addEventListener("click", handleSubmit);
navButtons.forEach((button) => {
  button.addEventListener("click", () => {
    const targetPage = button.dataset.page;
    navigateToPage(targetPage);
  });
});

// Hide result section initially
hideResultSection();

// Add drag and drop support
uploadArea.addEventListener("dragover", (e) => {
  e.preventDefault();
  e.stopPropagation();
  uploadArea.classList.add("dragover");
});

uploadArea.addEventListener("dragleave", (e) => {
  e.preventDefault();
  e.stopPropagation();
  uploadArea.classList.remove("dragover");
});

uploadArea.addEventListener("drop", (e) => {
  e.preventDefault();
  e.stopPropagation();
  uploadArea.classList.remove("dragover");
  const file = e.dataTransfer.files[0];
  if (file && file.type.startsWith("image/")) {
    handleImageUpload({ target: { files: [file] } });
  }
});

// Resource category selection
resourceCategories.forEach((category) => {
  category.addEventListener("click", () => {
    resourceCategories.forEach((c) => c.classList.remove("active"));
    category.classList.add("active");
  });
});

// Functions
function handleImageUpload(event) {
  const file = event.target.files[0];
  if (!file) return;

  if (!file.type.startsWith("image/")) {
    showError("Please select an image file");
    return;
  }

  selectedImage = file;
  errorMessage.textContent = "";
  submitButton.disabled = false;

  // Hide result section when a new image is selected
  hideResultSection();

  // Hide contact opt-in on new upload
  if (contactOptin) contactOptin.style.display = "none";

  const reader = new FileReader();
  reader.onload = (e) => {
    imagePreview.src = e.target.result;
    imagePreview.hidden = false;
    document.querySelector(".upload-placeholder").style.display = "none";
  };
  reader.readAsDataURL(file);
}

async function handleSubmit() {
  if (!selectedImage) {
    showError("Please select an image first");
    return;
  }

  submitButton.disabled = true;
  errorMessage.textContent = "";
  hideResultSection();
  showLoading();

  if (contactOptin) contactOptin.style.display = "none";

  try {
    const formData = new FormData();
    formData.append("image", selectedImage);
    formData.append("model", selectedModelValue);
    // Only send opt-in if visible and checked
    if (
      contactOptin &&
      contactOptin.style.display !== "none" &&
      optInCheckbox &&
      optInCheckbox.checked &&
      contactInfoInput &&
      contactInfoInput.value
    ) {
      formData.append("opt_in", "true");
      formData.append("contact_info", contactInfoInput.value);
    } else {
      formData.append("opt_in", "false");
    }

    const response = await fetch(
      "https://s-ahal-infer.hf.space/api/check-membership",
      {
        method: "POST",
        body: formData,
      }
    );

    if (!response.ok) {
      throw new Error(`Server responded with ${response.status}`);
    }

    const data = await response.json();
    displayResult(data);
  } catch (error) {
    showError(`Error: ${error.message}`);
    console.error("Error:", error);
    hideLoading();
  } finally {
    submitButton.disabled = false;
  }
}

function displayResult(data) {
  const probability = data.probability * 100;
  const isLikely = data.probability > 0.5;

  resultStatus.textContent = isLikely ? "Likely" : "Unlikely";
  const modelName = selectedModelSpan.textContent;
  const desc = `This image <strong class='result-description-strong'>${
    isLikely ? "probably is" : "is probably not"
  }</strong> in the training data for the ${modelName} model.`;
  resultDescription.innerHTML = desc;
  probabilityValue.innerHTML = `<span class='result-percentage'>${probability.toFixed(
    1
  )}%</span>`;
  probabilityFill.style.width = "0%";
  setTimeout(() => {
    probabilityFill.style.width = `${probability}%`;
  }, 30);

  if (imagePreview && resultImage) {
    resultImage.src = imagePreview.src;
    resultImage.hidden = false;
  }

  if (isLikely) {
    likelyInfo.hidden = false;
    let usersText = "";
    if (typeof data.likely_count === "number" && data.likely_count > 1) {
      usersText = `There are <strong>${
        data.likely_count - 1
      } other user-tested artworks</strong> that were found to likely be included in this model's training data.`;
    }
    likelyInfo.querySelector(".likely-users").innerHTML = usersText;
    // Show contact opt-in below results
    if (contactOptin) contactOptin.style.display = "";
  } else {
    likelyInfo.hidden = true;
    if (contactOptin) contactOptin.style.display = "none";
  }

  hideLoading();
  showResultSection();
}

function showError(message) {
  errorMessage.textContent = message;
}

function navigateToPage(pageId) {
  // Update active page
  pages.forEach((page) => {
    page.classList.remove("active");
    if (page.id === pageId) {
      page.classList.add("active");
    }
  });

  // Update active nav button
  navButtons.forEach((button) => {
    button.classList.toggle("active", button.dataset.page === pageId);
  });

  // Scroll to top when changing pages
  window.scrollTo(0, 0);
}

// Initialize
navigateToPage("model-select");

function closeDropdown() {
  dropdownMenu.hidden = true;
  dropdownToggle.setAttribute("aria-expanded", "false");
  if (customDropdown) customDropdown.classList.remove("open");
}

function openDropdown() {
  dropdownMenu.hidden = false;
  dropdownToggle.setAttribute("aria-expanded", "true");
  if (customDropdown) customDropdown.classList.add("open");
  // Focus the selected option
  const selected = dropdownMenu.querySelector(".dropdown-option.selected");
  if (selected) selected.focus();
}

dropdownToggle.addEventListener("click", (e) => {
  e.stopPropagation();
  if (dropdownMenu.hidden) {
    openDropdown();
  } else {
    closeDropdown();
  }
});

dropdownOptions.forEach((option) => {
  option.setAttribute("tabindex", "0");
  option.addEventListener("click", (e) => {
    dropdownOptions.forEach((opt) => opt.classList.remove("selected"));
    option.classList.add("selected");
    selectedModelSpan.textContent = option.textContent;
    selectedModelValue = option.getAttribute("data-value");
    closeDropdown();
  });
  option.addEventListener("keydown", (e) => {
    if (e.key === "Enter" || e.key === " ") {
      option.click();
    } else if (e.key === "ArrowDown") {
      e.preventDefault();
      let next = option.nextElementSibling;
      if (!next) next = dropdownMenu.firstElementChild;
      next.focus();
    } else if (e.key === "ArrowUp") {
      e.preventDefault();
      let prev = option.previousElementSibling;
      if (!prev) prev = dropdownMenu.lastElementChild;
      prev.focus();
    } else if (e.key === "Escape") {
      closeDropdown();
      dropdownToggle.focus();
    }
  });
});

document.addEventListener("click", (e) => {
  if (!dropdownMenu.hidden) {
    closeDropdown();
  }
});
dropdownToggle.addEventListener("keydown", (e) => {
  if (e.key === "ArrowDown" || e.key === "Enter" || e.key === " ") {
    openDropdown();
    e.preventDefault();
  }
});

document.addEventListener("keydown", (e) => {
  if (e.key === "Escape") {
    closeDropdown();
  }
});

// Ensure About overlay logic runs after DOM is loaded
window.addEventListener("DOMContentLoaded", function () {
  const aboutOverlay = document.getElementById("aboutOverlay");
  const aboutCloseBtn = document.getElementById("aboutCloseBtn");
  const aboutLink = Array.from(document.querySelectorAll(".nav-link")).find(
    (link) => link.textContent.trim().toLowerCase() === "about"
  );
  if (aboutLink) {
    aboutLink.addEventListener("click", function (e) {
      e.preventDefault();
      if (aboutOverlay) aboutOverlay.style.display = "flex";
    });
  }
  if (aboutCloseBtn) {
    aboutCloseBtn.addEventListener("click", function () {
      if (aboutOverlay) aboutOverlay.style.display = "none";
    });
  }
  if (aboutOverlay) {
    aboutOverlay.addEventListener("click", function (e) {
      if (e.target === aboutOverlay) {
        aboutOverlay.style.display = "none";
      }
    });
  }
});

// Resource card logic for resources page
(function () {
  const resourceData = {
    "do-not-train": {
      title: "Do Not Train",
      html: `<strong>What It Is:</strong> A simple metadata tag artists can add to their images to declare they don't consent to their work being used for training AI.<br><strong>How To Use:</strong> Add a do-not-train tag to your website's HTML or use platforms that support the tag.<br><strong>Note:</strong> It's not enforceable yet, but signals intent for future protections.<br><a href='https://spawning.ai/rightsholders-faq' class='action-button' target='_blank'><span>Learn more</span><span class='arrow'>⟶</span></a>`,
    },
    nightshade: {
      title: "Nightshade",
      html: `<strong>What It Is:</strong> A tool that poisons your artwork's pixels—imperceptibly to humans, but disruptive to training algorithms.<br><strong>Why It Works:</strong> It corrupts how AI models interpret your work, deterring them from using it.<br><a href='https://nightshade.cs.uchicago.edu/' class='action-button' target='_blank'><span>Try it out</span><span class='arrow'>⟶</span></a>`,
    },
    glaze: {
      title: "Glaze",
      html: `<strong>What It Is:</strong> A style cloak that protects your artistic fingerprint.<br><strong>What It Does:</strong> Applies a subtle, AI-visible filter that makes your work harder to mimic.<br><strong>Best For:</strong> Artists with a strong visual signature.<br><a href='https://glaze.cs.uchicago.edu/' class='action-button' target='_blank'><span>Try it out</span><span class='arrow'>⟶</span></a>`,
    },
    copyright: {
      title: "Copyright Resources",
      html: `<strong>Understand Your Rights:</strong><br>In many countries, your work is protected the moment it's created—but enforcing that is another story.<br><br><u>Includes:</u><ul><li>How to formally register your copyright</li><li>Templates for takedown notices</li><li>Intro to fair use & derivative work laws</li></ul>`,
    },
    licensing: {
      title: "Licensing Tools",
      html: `<strong>Put Terms in Writing:</strong><br>Use free or paid licenses to explicitly control how your work can be used.<br><br><u>Options to Explore:</u><ul><li>Creative Commons with "no AI use" clauses</li><li>Personal licensing agreements</li><li>Open-source licenses adapted for artists</li></ul>`,
    },
  };
  const categories = document.querySelectorAll(".resource-category");
  const info = document.getElementById("resource-info");
  if (categories.length && info) {
    function showResource(key) {
      categories.forEach((btn) =>
        btn.classList.toggle("active", btn.dataset.resource === key)
      );
      info.innerHTML = `<h3>${resourceData[key].title}</h3><p>${resourceData[key].html}</p>`;
    }
    categories.forEach((btn) => {
      btn.addEventListener("click", () => showResource(btn.dataset.resource));
    });
    // Show default
    showResource("do-not-train");
  }
})();