Spaces:
Running

File size: 6,119 Bytes
10709fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/* Shared helpers for the in-browser demos: page chrome, theme toggle, model loading.
   ONNX Runtime Web is pinned to 1.31.1 (see the <script> tag in each page). Nothing is uploaded:
   models and examples are static files, inference runs in this tab with WebAssembly. */
(function () {
  "use strict";
  var ORT_VERSION = "1.31.1";
  var root = document.documentElement;
  try {
    var t = localStorage.getItem("theme");
    if (t === "light" || t === "dark") root.dataset.theme = t;
  } catch (e) {}

  var SUN = '<svg class="icon-sun" viewBox="0 0 24 24" aria-hidden="true"><circle cx="12" cy="12" r="4.2"/><path d="M12 2.5v2.2M12 19.3v2.2M4.6 4.6l1.6 1.6M17.8 17.8l1.6 1.6M2.5 12h2.2M19.3 12h2.2M4.6 19.4l1.6-1.6M17.8 6.2l1.6-1.6"/></svg>';
  var MOON = '<svg class="icon-moon" viewBox="0 0 24 24" aria-hidden="true"><path d="M20.5 14.2A8.5 8.5 0 0 1 9.8 3.5a8.5 8.5 0 1 0 10.7 10.7z"/></svg>';

  function chrome(opts) {
    var base = opts.base || "https://rigel08.github.io/portfolio-site/";
    var header = document.createElement("header");
    header.className = "topbar scrolled";
    header.innerHTML =
      '<div class="wrap demo-top">' +
      '<a class="brand" href="' + base + '"><span class="brand-mark" aria-hidden="true"></span>Srihith Rao Eravelli</a>' +
      '<nav class="crumbs" aria-label="Demo navigation">' +
      '<a href="' + base + 'demos/">All demos</a>' +
      (opts.repo ? '<a class="hide-sm" href="' + opts.repo + '" target="_blank" rel="noopener">Repo ↗</a>' : "") +
      "</nav>" +
      '<button class="theme-toggle" type="button">' + SUN + MOON + "</button></div>";
    document.body.prepend(header);

    var slot = document.querySelector("[data-banner]");
    if (slot) {
      slot.className = "banner";
      slot.setAttribute("role", "note");
      slot.innerHTML =
        "<span><strong>Research demo, not for clinical use.</strong> Not a medical device; do not use it for any medical decision.</span>" +
        '<span class="private">🔒 ' + (opts.privacy ||
          "Your data stays in your browser: the model runs on this device and nothing you choose or upload is sent anywhere.") + "</span>";
    }

    var foot = document.createElement("footer");
    foot.className = "demo-foot";
    foot.innerHTML = '<div class="wrap">' + (opts.footer || "") +
      ' Inference: <a href="https://onnxruntime.ai/docs/tutorials/web/" target="_blank" rel="noopener">ONNX Runtime Web</a> ' +
      ORT_VERSION + " (WebAssembly), static files on GitHub Pages. " +
      '<a href="https://github.com/rigel08/portfolio-site/blob/main/demos/VERIFY.md" target="_blank" rel="noopener">How the browser outputs were checked against Python</a>.</div>';
    document.body.append(foot);

    var toggle = header.querySelector(".theme-toggle");
    function current() {
      if (root.dataset.theme) return root.dataset.theme;
      return matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
    }
    function sync() {
      var next = current() === "dark" ? "light" : "dark";
      toggle.setAttribute("aria-label", "Switch to " + next + " mode");
      toggle.title = "Switch to " + next + " mode";
    }
    toggle.addEventListener("click", function () {
      root.dataset.theme = current() === "dark" ? "light" : "dark";
      try { localStorage.setItem("theme", root.dataset.theme); } catch (e) {}
      sync();
      document.dispatchEvent(new CustomEvent("themechange"));
    });
    sync();
  }

  /* fetch with a progress callback (fraction 0..1, or null if the size is unknown) */
  async function fetchBytes(url, onProgress) {
    var res = await fetch(url);
    if (!res.ok) throw new Error("Could not download " + url + " (HTTP " + res.status + ")");
    var total = +res.headers.get("content-length") || 0;
    if (!res.body || !total) {
      var buf = await res.arrayBuffer();
      onProgress && onProgress(1, buf.byteLength);
      return new Uint8Array(buf);
    }
    var reader = res.body.getReader(), chunks = [], got = 0;
    for (;;) {
      var r = await reader.read();
      if (r.done) break;
      chunks.push(r.value); got += r.value.length;
      onProgress && onProgress(Math.min(got / total, 1), got);
    }
    var out = new Uint8Array(got), off = 0;
    chunks.forEach(function (c) { out.set(c, off); off += c.length; });
    return out;
  }

  async function loadSession(url, onProgress) {
    if (!window.ort) throw new Error("ONNX Runtime Web did not load (blocked CDN?)");
    ort.env.wasm.numThreads = 1; // GitHub Pages cannot send the headers that multithreading needs
    ort.env.wasm.wasmPaths = "https://cdn.jsdelivr.net/npm/onnxruntime-web@" + ORT_VERSION + "/dist/";
    var bytes = await fetchBytes(url, onProgress);
    return ort.InferenceSession.create(bytes, { executionProviders: ["wasm"], graphOptimizationLevel: "all" });
  }

  function mb(n) { return (n / 1048576).toFixed(1) + " MB"; }
  function sigmoid(z) { return 1 / (1 + Math.exp(-z)); }

  /* Status line + progress bar bound to an element containing .status and .bar-fill */
  function status(el) {
    var txt = el.querySelector(".status"), bar = el.querySelector(".bar-fill");
    return {
      set: function (msg, frac, isErr) {
        txt.textContent = msg; txt.classList.toggle("err", !!isErr);
        if (bar) { bar.style.width = (frac == null ? 0 : Math.round(frac * 100)) + "%"; bar.parentNode.hidden = frac == null; }
      },
    };
  }

  /* Load an <img>/File/URL into an HTMLImageElement */
  function loadImage(src) {
    return new Promise(function (resolve, reject) {
      var img = new Image();
      img.onload = function () { resolve(img); };
      img.onerror = function () { reject(new Error("Could not read that image file.")); };
      img.src = typeof src === "string" ? src : URL.createObjectURL(src);
    });
  }

  function cssVar(name) { return getComputedStyle(root).getPropertyValue(name).trim(); }

  window.Demo = { chrome: chrome, loadSession: loadSession, fetchBytes: fetchBytes, mb: mb, sigmoid: sigmoid,
                  status: status, loadImage: loadImage, cssVar: cssVar, ORT_VERSION: ORT_VERSION };
})();