Nausad commited on
Commit
05f8cdf
Β·
verified Β·
1 Parent(s): 64bbe42

Update templates/index.html

Browse files
Files changed (1) hide show
  1. templates/index.html +58 -18
templates/index.html CHANGED
@@ -603,7 +603,7 @@
603
  <input type="file" id="file-input" accept="image/*" />
604
  <div class="upload-icon-wrap"><i class="fa-solid fa-image"></i></div>
605
  <h3>Drag & Drop an Image</h3>
606
- <p>Supports JPEG, PNG, WEBP, BMP β€” auto‑resized to 32Γ—32</p>
607
  <button class="btn btn-primary" onclick="document.getElementById('file-input').click()">
608
  <i class="fa-solid fa-folder-open"></i> Browse File
609
  </button>
@@ -716,20 +716,56 @@
716
  function handleFile(file) {
717
  if (!file.type.startsWith("image/")) return;
718
 
 
 
 
719
  const reader = new FileReader();
720
  reader.onload = evt => {
721
  // Show original preview
722
  origWrap.innerHTML = `<img src="${evt.target.result}" alt="preview"/>`;
723
 
724
- // Draw pixelated 32x32
 
725
  const img = new Image();
726
- img.onload = () => drawPixel(img);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
727
  img.src = evt.target.result;
728
  };
729
  reader.readAsDataURL(file);
730
-
731
- // Send to Flask
732
- uploadToFlask(file);
733
  }
734
 
735
  // ── Draw 32Γ—32 pixel grid ────────────────────────────────────────────
@@ -759,27 +795,31 @@
759
  pixelWrap.appendChild(cv);
760
  }
761
 
762
- // ── Upload to Flask ──────────────────────────────────────────────────
763
- function uploadToFlask(file) {
764
- // Show spinner
765
  resultsBody.innerHTML = `
766
- <div class="spinner-wrap">
767
- <div class="spinner"></div>
768
- Running transformer inference…
769
- </div>`;
 
 
 
 
 
770
 
771
  const fd = new FormData();
772
- fd.append("file", file);
773
 
774
  fetch("/predict", { method: "POST", body: fd })
775
  .then(r => r.json())
776
  .then(renderResults)
777
  .catch(err => {
778
  resultsBody.innerHTML = `
779
- <div class="empty-state">
780
- <i class="fa-solid fa-circle-exclamation" style="color:#ff4d6d"></i>
781
- Error: ${err.message}
782
- </div>`;
783
  });
784
  }
785
 
 
603
  <input type="file" id="file-input" accept="image/*" />
604
  <div class="upload-icon-wrap"><i class="fa-solid fa-image"></i></div>
605
  <h3>Drag & Drop an Image</h3>
606
+ <p>Supports JPEG, PNG, WEBP, AVIF, BMP β€” auto‑resized to 32Γ—32</p>
607
  <button class="btn btn-primary" onclick="document.getElementById('file-input').click()">
608
  <i class="fa-solid fa-folder-open"></i> Browse File
609
  </button>
 
716
  function handleFile(file) {
717
  if (!file.type.startsWith("image/")) return;
718
 
719
+ // Show spinner immediately so the user sees feedback
720
+ showSpinner("Converting image…");
721
+
722
  const reader = new FileReader();
723
  reader.onload = evt => {
724
  // Show original preview
725
  origWrap.innerHTML = `<img src="${evt.target.result}" alt="preview"/>`;
726
 
727
+ // Load into an HTMLImageElement so the browser decodes it
728
+ // (works for AVIF, HEIC on Safari, WebP, PNG, JPEG, etc.)
729
  const img = new Image();
730
+
731
+ img.onload = () => {
732
+ // Draw the 32Γ—32 pixel preview
733
+ drawPixel(img);
734
+
735
+ // ── Convert to JPEG via Canvas, then upload ─────────────────────
736
+ // This lets PIL always receive a JPEG regardless of the original
737
+ // format (AVIF, HEIC, exotic formats PIL doesn't support).
738
+ const cvs = document.createElement("canvas");
739
+ cvs.width = img.naturalWidth || img.width;
740
+ cvs.height = img.naturalHeight || img.height;
741
+ cvs.getContext("2d").drawImage(img, 0, 0);
742
+
743
+ cvs.toBlob(blob => {
744
+ if (!blob) {
745
+ resultsBody.innerHTML = `
746
+ <div class="empty-state">
747
+ <i class="fa-solid fa-circle-exclamation" style="color:#ff4d6d"></i>
748
+ Failed to convert this image. Please try a different file.
749
+ </div>`;
750
+ return;
751
+ }
752
+ // Use a .jpg filename so the server sees the correct extension
753
+ const safeName = (file.name || "image").replace(/\.[^.]+$/, "") + ".jpg";
754
+ uploadToFlask(blob, safeName);
755
+ }, "image/jpeg", 0.92);
756
+ };
757
+
758
+ img.onerror = () => {
759
+ resultsBody.innerHTML = `
760
+ <div class="empty-state">
761
+ <i class="fa-solid fa-circle-exclamation" style="color:#ff4d6d"></i>
762
+ Your browser cannot decode this image format. Please try JPEG, PNG, or WebP.
763
+ </div>`;
764
+ };
765
+
766
  img.src = evt.target.result;
767
  };
768
  reader.readAsDataURL(file);
 
 
 
769
  }
770
 
771
  // ── Draw 32Γ—32 pixel grid ────────────────────────────────────────────
 
795
  pixelWrap.appendChild(cv);
796
  }
797
 
798
+ // ── Show spinner helper ──────────────────────────────────────────────
799
+ function showSpinner(msg) {
 
800
  resultsBody.innerHTML = `
801
+ <div class="spinner-wrap">
802
+ <div class="spinner"></div>
803
+ ${msg}
804
+ </div>`;
805
+ }
806
+
807
+ // ── Upload JPEG blob to Flask ────────────────────────────────────────
808
+ function uploadToFlask(blob, filename) {
809
+ showSpinner("Running transformer inference…");
810
 
811
  const fd = new FormData();
812
+ fd.append("file", blob, filename);
813
 
814
  fetch("/predict", { method: "POST", body: fd })
815
  .then(r => r.json())
816
  .then(renderResults)
817
  .catch(err => {
818
  resultsBody.innerHTML = `
819
+ <div class="empty-state">
820
+ <i class="fa-solid fa-circle-exclamation" style="color:#ff4d6d"></i>
821
+ Error: ${err.message}
822
+ </div>`;
823
  });
824
  }
825