Spaces:
Sleeping
Sleeping
File size: 11,459 Bytes
8cd1ab1 02bed5c 8cd1ab1 02bed5c 8cd1ab1 02bed5c 95e24be 8cd1ab1 af40191 95e24be 8cd1ab1 f2f112a 8cd1ab1 f2f112a 8cd1ab1 f2f112a 8cd1ab1 95e24be 02bed5c 8cd1ab1 95e24be 02bed5c 8cd1ab1 95e24be f2f112a 8cd1ab1 95e24be f2f112a 8cd1ab1 f2f112a 8cd1ab1 5ee17b1 f2f112a 8cd1ab1 95e24be 8cd1ab1 95e24be 8cd1ab1 95e24be 8cd1ab1 02bed5c 8cd1ab1 95e24be 02bed5c 95e24be 8cd1ab1 95e24be 02bed5c 8cd1ab1 02bed5c 95e24be 8cd1ab1 02bed5c 8cd1ab1 95e24be 02bed5c 8cd1ab1 95e24be 8cd1ab1 95e24be 02bed5c 95e24be 8cd1ab1 95e24be 8cd1ab1 5ee17b1 8cd1ab1 f2f112a 02bed5c 8cd1ab1 02bed5c 8cd1ab1 02bed5c 8cd1ab1 f2f112a 02bed5c 8cd1ab1 95e24be 02bed5c 8cd1ab1 5ee17b1 8cd1ab1 95e24be 8cd1ab1 95e24be 02bed5c 8cd1ab1 95e24be 8cd1ab1 02bed5c 8cd1ab1 95e24be 02bed5c 8cd1ab1 02bed5c 8cd1ab1 02bed5c 8cd1ab1 95e24be 8cd1ab1 |
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 |
// static/main.js
(() => {
// Helpers
const $ = (id) => document.getElementById(id);
const qsa = (sel) => Array.from(document.querySelectorAll(sel));
// Elements
const form = $("runForm");
const runBtn = $("runBtn");
const runBtnText = $("runBtnText");
const spinner = $("spinner");
const resetBtn = $("resetBtn");
const fileInput = $("fileInput");
const dropzone = $("dropzone");
const fileMeta = $("fileMeta");
const previewWrap = $("previewWrap");
const previewImg = $("previewImg");
const statusDot = $("statusDot");
const statusText = $("statusText");
const statusLog = $("statusLog");
const jobPill = $("jobPill");
const imgInput = $("imgInput");
const imgYolo = $("imgYolo");
const imgSelected = $("imgSelected");
const meta = $("meta");
const dlInput = $("dlInput");
const dlYolo = $("dlYolo");
const dlSelected = $("dlSelected");
const copyMetaBtn = $("copyMetaBtn");
// Noise elements
const noiseType = $("noiseType");
const noiseStrengthHidden = $("noiseStrength");
const noisePanelNone = $("noisePanelNone");
const noisePanelGaussian = $("noisePanelGaussian");
const noisePanelLinear = $("noisePanelLinear");
const noisePanelAdv = $("noisePanelAdv");
const noiseGaussian = $("noiseGaussian");
const noiseLinear = $("noiseLinear");
const noiseAdv = $("noiseAdv");
const noiseValGaussian = $("noiseValGaussian");
const noiseValLinear = $("noiseValLinear");
const noiseValAdv = $("noiseValAdv");
// HW noise elements (UI labels only unless backend supports)
const hwNoiseWidth = $("hwNoiseWidth");
const hwNoiseStrength = $("hwNoiseStrength");
const hwNoiseWidthVal = $("hwNoiseWidthVal");
const hwNoiseStrengthVal = $("hwNoiseStrengthVal");
function setStatus(kind, text, logLine) {
// kind: idle | run | ok | bad
if (statusDot) statusDot.className = `dot ${kind}`;
if (statusText) statusText.textContent = text;
if (statusLog && logLine != null) statusLog.textContent = logLine;
}
function setRunning(isRunning) {
if (!runBtn) return;
runBtn.disabled = isRunning;
if (spinner) spinner.classList.toggle("hidden", !isRunning);
if (runBtnText) runBtnText.textContent = isRunning ? "Running…" : "Run";
}
function setPreview(file) {
if (!fileMeta || !previewWrap || !previewImg) return;
if (!file) {
fileMeta.textContent = "No file selected";
previewWrap.classList.add("hidden");
previewImg.removeAttribute("src");
return;
}
fileMeta.textContent = `${file.name} • ${(file.size / 1024).toFixed(1)} KB`;
const url = URL.createObjectURL(file);
previewImg.src = url;
previewWrap.classList.remove("hidden");
}
function setFileToInput(file) {
const dt = new DataTransfer();
dt.items.add(file);
fileInput.files = dt.files;
}
// ---------------- Tabs ----------------
function initTabs() {
const tabBtns = qsa(".tabBtn");
const tabPanels = qsa(".tabPanel");
tabBtns.forEach((btn) => {
btn.addEventListener("click", () => {
tabBtns.forEach((b) => b.classList.remove("active"));
tabPanels.forEach((p) => p.classList.add("hidden"));
btn.classList.add("active");
const id = btn.getAttribute("data-tab");
const panel = $(id);
if (panel) panel.classList.remove("hidden");
});
});
}
// ------------- HDC bits toggle -------------
function initHdcBitsToggle() {
// score function select has NAME but no ID in your HTML
const scoreSel = document.querySelector('select[name="score_function"]');
const bitsField = $("hdcBitsField");
const bitsSel = $("hdcBits");
if (!scoreSel || !bitsField || !bitsSel) {
console.warn("HDC bits toggle: missing DOM elements");
return;
}
const update = () => {
const isHDC = scoreSel.value === "HDC";
bitsField.style.display = isHDC ? "" : "none";
bitsSel.disabled = !isHDC;
};
scoreSel.addEventListener("change", update);
update();
}
// ------------- Noise panel switching -------------
function noiseFamily(val) {
const t = String(val || "none").toLowerCase();
if (t === "none" || t === "default" || t === "off") return "none";
if (t === "gaussian") return "gaussian";
if (t === "linear") return "linear";
if (t.startsWith("adv")) return "adv";
return "none";
}
function updateNoiseHidden() {
if (!noiseType || !noiseStrengthHidden) return;
const fam = noiseFamily(noiseType.value);
let v = 0;
if (fam === "gaussian" && noiseGaussian) v = Number(noiseGaussian.value || 0);
if (fam === "linear" && noiseLinear) v = Number(noiseLinear.value || 0);
if (fam === "adv" && noiseAdv) v = Number(noiseAdv.value || 0);
noiseStrengthHidden.value = String(v);
}
function showNoisePanel() {
if (!noiseType) return;
const fam = noiseFamily(noiseType.value);
[noisePanelNone, noisePanelGaussian, noisePanelLinear, noisePanelAdv].forEach((p) => {
if (p) p.classList.add("hidden");
});
if (fam === "none" && noisePanelNone) noisePanelNone.classList.remove("hidden");
if (fam === "gaussian" && noisePanelGaussian) noisePanelGaussian.classList.remove("hidden");
if (fam === "linear" && noisePanelLinear) noisePanelLinear.classList.remove("hidden");
if (fam === "adv" && noisePanelAdv) noisePanelAdv.classList.remove("hidden");
updateNoiseHidden();
}
function initNoise() {
if (!noiseType) return;
noiseType.addEventListener("change", showNoisePanel);
if (noiseGaussian) {
noiseGaussian.addEventListener("input", () => {
if (noiseValGaussian) noiseValGaussian.textContent = noiseGaussian.value;
if (noiseFamily(noiseType.value) === "gaussian") updateNoiseHidden();
});
}
if (noiseLinear) {
noiseLinear.addEventListener("input", () => {
if (noiseValLinear) noiseValLinear.textContent = noiseLinear.value;
if (noiseFamily(noiseType.value) === "linear") updateNoiseHidden();
});
}
if (noiseAdv) {
noiseAdv.addEventListener("input", () => {
if (noiseValAdv) noiseValAdv.textContent = noiseAdv.value;
if (noiseFamily(noiseType.value) === "adv") updateNoiseHidden();
});
}
showNoisePanel();
}
// ------------- HW noise label updates -------------
function initHwNoiseLabels() {
if (hwNoiseWidth && hwNoiseWidthVal) {
hwNoiseWidthVal.textContent = hwNoiseWidth.value;
hwNoiseWidth.addEventListener("input", () => {
hwNoiseWidthVal.textContent = hwNoiseWidth.value;
});
}
if (hwNoiseStrength && hwNoiseStrengthVal) {
hwNoiseStrengthVal.textContent = hwNoiseStrength.value;
hwNoiseStrength.addEventListener("input", () => {
hwNoiseStrengthVal.textContent = hwNoiseStrength.value;
});
}
}
// ------------- Upload / Dropzone -------------
function initUpload() {
if (!fileInput || !dropzone) return;
dropzone.addEventListener("click", () => fileInput.click());
dropzone.addEventListener("keydown", (e) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
fileInput.click();
}
});
fileInput.addEventListener("change", () => {
const f = fileInput.files && fileInput.files[0];
setPreview(f);
});
dropzone.addEventListener("dragover", (e) => {
e.preventDefault();
dropzone.classList.add("drag");
});
dropzone.addEventListener("dragleave", () => {
dropzone.classList.remove("drag");
});
dropzone.addEventListener("drop", (e) => {
e.preventDefault();
dropzone.classList.remove("drag");
const f = e.dataTransfer?.files?.[0];
if (!f) return;
setFileToInput(f);
setPreview(f);
});
}
// ------------- Reset -------------
function initReset() {
if (!resetBtn || !form) return;
resetBtn.addEventListener("click", () => {
form.reset();
if (fileInput) fileInput.value = "";
setPreview(null);
if (jobPill) jobPill.textContent = "job: —";
if (imgInput) imgInput.removeAttribute("src");
if (imgYolo) imgYolo.removeAttribute("src");
if (imgSelected) imgSelected.removeAttribute("src");
if (meta) meta.textContent = "";
// reset labels
if (noiseValGaussian && noiseGaussian) noiseValGaussian.textContent = noiseGaussian.value;
if (noiseValLinear && noiseLinear) noiseValLinear.textContent = noiseLinear.value;
if (noiseValAdv && noiseAdv) noiseValAdv.textContent = noiseAdv.value;
initHdcBitsToggle();
showNoisePanel();
setStatus("idle", "Idle", "Waiting for input…");
setRunning(false);
});
}
// ------------- Copy meta -------------
function initCopyMeta() {
if (!copyMetaBtn || !meta) return;
copyMetaBtn.addEventListener("click", async () => {
const text = meta.textContent || "";
if (!text) return;
await navigator.clipboard.writeText(text);
setStatus("ok", "Done", "Copied metadata to clipboard.");
});
}
// ------------- Submit -------------
function initSubmit() {
if (!form) return;
form.addEventListener("submit", async (e) => {
e.preventDefault();
const file = fileInput?.files?.[0];
if (!file) {
setStatus("bad", "Error", "No file selected.");
return;
}
updateNoiseHidden();
setRunning(true);
setStatus("run", "Running", "Submitting request…");
try {
const fd = new FormData(form);
const resp = await fetch("/api/run", { method: "POST", body: fd });
const data = await resp.json();
if (!resp.ok || !data.ok) throw new Error(data?.error || `HTTP ${resp.status}`);
if (jobPill) jobPill.textContent = `job: ${data.job_id}`;
const bust = `t=${Date.now()}`;
if (imgInput) imgInput.src = `${data.image_urls.input}?${bust}`;
if (imgYolo) imgYolo.src = `${data.image_urls.yolo}?${bust}`;
if (imgSelected) imgSelected.src = `${data.image_urls.selected}?${bust}`;
if (dlInput) { dlInput.href = data.image_urls.input; dlInput.classList.remove("hidden"); }
if (dlYolo) { dlYolo.href = data.image_urls.yolo; dlYolo.classList.remove("hidden"); }
if (dlSelected) { dlSelected.href = data.image_urls.selected; dlSelected.classList.remove("hidden"); }
if (meta) {
meta.textContent = JSON.stringify(
{
job_id: data.job_id,
task_id: data.task_id,
task_name: data.task_name,
selected_indices: data.selected_indices,
},
null,
2
);
}
setStatus("ok", "Done", "Finished.");
} catch (err) {
setStatus("bad", "Error", String(err));
} finally {
setRunning(false);
}
});
}
// Boot
document.addEventListener("DOMContentLoaded", () => {
if (!form || !fileInput || !dropzone) {
console.error("Missing DOM elements: check ids in HTML.");
if (statusLog) statusLog.textContent = "JS init failed: missing DOM elements (check ids).";
return;
}
initTabs();
initHdcBitsToggle();
initNoise();
initHwNoiseLabels();
initUpload();
initReset();
initCopyMeta();
initSubmit();
setStatus("idle", "Idle", "Waiting for input…");
});
})();
|