shiue20 commited on
Commit
eb90761
·
verified ·
1 Parent(s): 01f123f

Update templates/index.html

Browse files
Files changed (1) hide show
  1. templates/index.html +43 -47
templates/index.html CHANGED
@@ -6,7 +6,6 @@
6
  <title>🖼️ Image Enhancement & Damage Repair</title>
7
  <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;600&display=swap" rel="stylesheet" />
8
  <style>
9
- /* (Include your styling from before or similar) */
10
  body {
11
  font-family: 'Poppins', sans-serif;
12
  background: #f5f7fa;
@@ -18,7 +17,6 @@
18
  flex-direction: column;
19
  align-items: center;
20
  }
21
- /* Simplified styles for brevity */
22
  form {
23
  background: white;
24
  padding: 25px 30px;
@@ -214,45 +212,29 @@
214
  submitBtn.disabled = true;
215
 
216
  try {
217
- const response = await fetch('/', {
218
  method: 'POST',
219
  body: formData
220
  });
221
 
222
- if (!response.ok) {
223
- alert('Upload failed. Please try again.');
224
- setControlsEnabled(false);
225
- submitBtn.textContent = 'Upload & Process';
226
- return;
227
- }
228
-
229
- const text = await response.text();
230
-
231
- // Parse returned HTML to extract URLs from img src and anchors
232
- const parser = new DOMParser();
233
- const doc = parser.parseFromString(text, 'text/html');
234
-
235
- const origSrc = doc.getElementById('originalImg')?.src;
236
- const maskSrc = doc.getElementById('maskImg')?.src;
237
- const repairedSrc = doc.getElementById('repairedImg')?.src;
238
-
239
- if (!origSrc || !maskSrc || !repairedSrc) {
240
- alert('Failed to get processed images from server response.');
241
  setControlsEnabled(false);
242
  submitBtn.textContent = 'Upload & Process';
243
  return;
244
  }
245
 
246
- currentFilename = origSrc.split('/').pop();
247
 
248
  // Show images and enable controls
249
- originalImg.src = origSrc;
250
- maskImg.src = maskSrc;
251
- repairedImg.src = repairedSrc;
252
 
253
- downloadOriginal.href = origSrc;
254
- downloadMask.href = maskSrc;
255
- downloadRepaired.href = repairedSrc;
256
 
257
  imagesContainer.style.display = 'grid';
258
  setControlsEnabled(true);
@@ -290,46 +272,60 @@
290
  body: JSON.stringify(data)
291
  });
292
 
293
- if (!response.ok) {
294
- const err = await response.json();
295
- alert('Processing error: ' + (err.error || 'Unknown error'));
 
296
  submitBtn.disabled = false;
297
  submitBtn.textContent = 'Upload & Process';
298
  return;
299
  }
300
 
301
- const json = await response.json();
302
-
303
  // Cache bust URLs to force reload
304
  function cacheBust(url) {
305
  const sep = url.includes('?') ? '&' : '?';
306
- return url + sep + 'cb=' + new Date().getTime();
307
  }
308
 
309
  maskImg.src = cacheBust(json.mask_image);
310
  repairedImg.src = cacheBust(json.repaired_image);
311
- downloadMask.href = maskImg.src;
312
- downloadRepaired.href = repairedImg.src;
 
 
 
 
313
 
314
  } catch (e) {
315
- alert('Network or server error.');
316
- } finally {
317
  submitBtn.disabled = false;
318
  submitBtn.textContent = 'Upload & Process';
319
  }
320
  }
321
 
322
- [thresholdInput, gammaInput, denoiseSelect, stylizeCheckbox].forEach(el => {
323
- el.addEventListener('change', updateImages);
324
- if (el.type === 'range') {
325
- el.addEventListener('input', updateImages);
326
- }
327
- });
 
 
328
 
329
- // Prevent form submit (handled by file input change)
330
- uploadForm.addEventListener('submit', (e) => {
 
 
 
 
 
 
 
331
  e.preventDefault();
 
 
332
  });
 
333
  })();
334
  </script>
335
  </body>
 
6
  <title>🖼️ Image Enhancement & Damage Repair</title>
7
  <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;600&display=swap" rel="stylesheet" />
8
  <style>
 
9
  body {
10
  font-family: 'Poppins', sans-serif;
11
  background: #f5f7fa;
 
17
  flex-direction: column;
18
  align-items: center;
19
  }
 
20
  form {
21
  background: white;
22
  padding: 25px 30px;
 
212
  submitBtn.disabled = true;
213
 
214
  try {
215
+ const response = await fetch('/upload', {
216
  method: 'POST',
217
  body: formData
218
  });
219
 
220
+ const json = await response.json();
221
+ if (json.error) {
222
+ alert('Upload error: ' + json.error);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
223
  setControlsEnabled(false);
224
  submitBtn.textContent = 'Upload & Process';
225
  return;
226
  }
227
 
228
+ currentFilename = json.original_image.split('/').pop();
229
 
230
  // Show images and enable controls
231
+ originalImg.src = json.original_image;
232
+ maskImg.src = json.mask_image;
233
+ repairedImg.src = json.repaired_image;
234
 
235
+ downloadOriginal.href = json.original_image;
236
+ downloadMask.href = json.mask_image;
237
+ downloadRepaired.href = json.repaired_image;
238
 
239
  imagesContainer.style.display = 'grid';
240
  setControlsEnabled(true);
 
272
  body: JSON.stringify(data)
273
  });
274
 
275
+ const json = await response.json();
276
+
277
+ if (json.error) {
278
+ alert('Processing error: ' + json.error);
279
  submitBtn.disabled = false;
280
  submitBtn.textContent = 'Upload & Process';
281
  return;
282
  }
283
 
 
 
284
  // Cache bust URLs to force reload
285
  function cacheBust(url) {
286
  const sep = url.includes('?') ? '&' : '?';
287
+ return url + sep + 't=' + new Date().getTime();
288
  }
289
 
290
  maskImg.src = cacheBust(json.mask_image);
291
  repairedImg.src = cacheBust(json.repaired_image);
292
+
293
+ downloadMask.href = json.mask_image;
294
+ downloadRepaired.href = json.repaired_image;
295
+
296
+ submitBtn.disabled = false;
297
+ submitBtn.textContent = 'Upload & Process';
298
 
299
  } catch (e) {
300
+ alert('Error during processing: ' + e.message);
 
301
  submitBtn.disabled = false;
302
  submitBtn.textContent = 'Upload & Process';
303
  }
304
  }
305
 
306
+ // Hook parameter inputs to updateImages with debounce
307
+ function debounce(func, wait) {
308
+ let timeout;
309
+ return function(...args) {
310
+ clearTimeout(timeout);
311
+ timeout = setTimeout(() => func.apply(this, args), wait);
312
+ };
313
+ }
314
 
315
+ const debouncedUpdate = debounce(updateImages, 700);
316
+
317
+ thresholdInput.addEventListener('input', debouncedUpdate);
318
+ gammaInput.addEventListener('input', debouncedUpdate);
319
+ denoiseSelect.addEventListener('change', debouncedUpdate);
320
+ stylizeCheckbox.addEventListener('change', debouncedUpdate);
321
+
322
+ // Form submit fallback (for accessibility)
323
+ uploadForm.addEventListener('submit', e => {
324
  e.preventDefault();
325
+ if (!fileInput.files.length) return alert('Please select a file first.');
326
+ updateImages();
327
  });
328
+
329
  })();
330
  </script>
331
  </body>