groundline / app.js
Ghongha007's picture
Use Groundline fc5c8c2: the methodology note is marked as a draft
5138fe1 verified
Raw History Blame Contribute Delete
4.16 kB
// Groundline in the browser: Pyodide runs Groundline's own Python code, loaded from GitHub at a pinned commit.
// Sources and draft are written to Pyodide's in-memory file system, checked, then deleted. Nothing is uploaded.
const COMMIT = "fc5c8c20276bc10091db8b2a7a4df22e3c8d1853";
const SOURCE = `https://raw.githubusercontent.com/Ghongha007/groundline/${COMMIT}/`;
const FILES = ["groundline/__init__.py", "groundline/checks.py", "groundline/text.py", "groundline/report.py", "groundline/cli.py"];
const MAX_DRAFT = 30000, MAX_FILE = 50 * 1024 * 1024;
const $ = (id) => document.getElementById(id);
let py = null, busy = false, pdfReady = false, urls = [];
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 Groundline…");
py.FS.mkdirTree("/home/pyodide/groundline");
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.", "ok");
} catch (e) { status("Could not start: " + e.message + ". Reload the page to try again.", "err"); }
}
async function ensurePdf() {
if (pdfReady) return;
status("Loading the PDF reader…");
await py.loadPackage("micropip");
await py.runPythonAsync("import micropip\nawait micropip.install('pypdf==7.1.1')");
pdfReady = true;
}
async function check() {
if (!py) { status("Still loading, one moment…"); return; }
if (busy) return;
const draft = $("draft").value, pasted = $("src").value, files = [...$("files").files];
if (!draft.trim()) { status("Paste the draft you want to check.", "err"); return; }
if (draft.length > MAX_DRAFT) { status(`The draft is longer than ${MAX_DRAFT.toLocaleString()} characters. Split it, or run Groundline on your computer.`, "err"); return; }
if (!pasted.trim() && !files.length) { status("Add at least one source: paste its text or add a file.", "err"); return; }
if (files.some((f) => f.size > MAX_FILE)) { status("A source file is over 50 MB. Use Groundline on your computer for large files.", "err"); return; }
busy = true; const written = [];
try {
if (files.some((f) => /\.pdf$/i.test(f.name))) await ensurePdf();
status("Checking…");
const dir = "/tmp/gl"; py.FS.mkdirTree(dir);
const paths = [], names = [];
if (pasted.trim()) { const p = dir + "/pasted source.txt"; py.FS.writeFile(p, pasted); written.push(p); paths.push(p); names.push("pasted source"); }
for (const [i, f] of files.entries()) {
const safe = f.name.replace(/[\/\\\0]/g, "_"); // keep the real name, so results cite it
py.FS.mkdirTree(`${dir}/${i}`); const p = `${dir}/${i}/${safe}`;
py.FS.writeFile(p, new Uint8Array(await f.arrayBuffer())); written.push(p); paths.push(p); names.push(f.name);
}
const d = dir + "/draft.md"; py.FS.writeFile(d, draft); written.push(d);
const out = JSON.parse(py.globals.get("run")(JSON.stringify(paths), JSON.stringify(names), d));
if (out.error) { status(out.error, "err"); return; }
$("result").innerHTML = out.html;
urls.forEach(URL.revokeObjectURL);
urls = [URL.createObjectURL(new Blob([out.report_html], { type: "text/html" })), URL.createObjectURL(new Blob([out.report_md], { type: "text/markdown" }))];
$("dlHtml").href = urls[0]; $("dlMd").href = urls[1]; $("downloads").hidden = false;
status("Done. Your text was checked in this browser and not uploaded.", "ok");
} catch (e) { status("Something went wrong: " + e.message, "err"); }
finally { written.forEach((p) => { try { py.FS.unlink(p); } catch (_) {} }); busy = false; }
}
$("go").addEventListener("click", check);
$("example").addEventListener("click", async () => {
$("src").value = await (await fetch("examples/audit_report.txt")).text();
$("draft").value = await (await fetch("examples/ai_summary.md")).text();
$("files").value = "";
});
boot();