// aitrace in the browser: Pyodide runs aitrace's own Python code, loaded from GitHub at a pinned commit. // The chosen file is read into Pyodide's in-memory file system, checked, then deleted. Nothing is uploaded. const COMMIT = "dfd60666c2fc20ddec109151936bda6e6741e6fd"; const SOURCE = `https://raw.githubusercontent.com/Ghongha007/aitrace/${COMMIT}/`; const FILES = ["aitrace/__init__.py", "aitrace/core.py"]; const MAX = 100 * 1024 * 1024; const $ = (id) => document.getElementById(id); let py = null, busy = false; function status(text, kind = "") { const s = $("status"); s.textContent = text; s.className = "status " + kind; } async function boot() { try { py = await loadPyodide({ indexURL: "https://cdn.jsdelivr.net/pyodide/v0.29.5/full/" }); status("Loading aitrace…"); py.FS.mkdirTree("/home/pyodide/aitrace"); for (const f of FILES) { const r = await fetch(SOURCE + f); if (!r.ok) throw new Error(`could not load ${f} (${r.status})`); py.FS.writeFile("/home/pyodide/" + f, await r.text()); } py.runPython(await (await fetch("web.py")).text()); status("Ready. Choose a file.", "ok"); } catch (e) { status("Could not start: " + e.message + ". Reload the page to try again.", "err"); } } async function run(bytes, name) { if (!py) { status("Still loading, one moment…"); return; } if (busy) return; busy = true; const path = "/tmp/input" + (name.match(/\.[A-Za-z0-9]{1,5}$/) || [""])[0]; try { status(`Reading ${name}…`); py.FS.writeFile(path, bytes); const out = JSON.parse(py.globals.get("check")(path, name)); $("result").innerHTML = out.html; $("meta").textContent = out.meta; $("metaBox").hidden = false; status("Done. The file was read in this browser and not uploaded.", "ok"); } catch (e) { status("Could not read this file: " + e.message, "err"); } finally { try { py.FS.unlink(path); } catch (_) {} busy = false; } } async function fromFile(file) { if (!file) return; if (file.size > MAX) { status("This file is over 100 MB. Use aitrace on your computer for large files.", "err"); return; } run(new Uint8Array(await file.arrayBuffer()), file.name); } $("file").addEventListener("change", (e) => fromFile(e.target.files[0])); const drop = $("drop"); ["dragenter", "dragover"].forEach((t) => drop.addEventListener(t, (e) => { e.preventDefault(); drop.classList.add("over"); })); ["dragleave", "drop"].forEach((t) => drop.addEventListener(t, (e) => { e.preventDefault(); drop.classList.remove("over"); })); drop.addEventListener("drop", (e) => fromFile(e.dataTransfer.files[0])); document.querySelectorAll("[data-sample]").forEach((b) => b.addEventListener("click", async () => { const r = await fetch(b.dataset.sample); run(new Uint8Array(await r.arrayBuffer()), b.dataset.sample.split("/").pop()); })); boot();