SamJoshua's picture
Chat Template Checker: CTT-Bench v1.0, runs in your browser
4a22613 verified
Raw History Blame Contribute Delete
1.13 kB
let py, ready;
async function init() {
importScripts("https://cdn.jsdelivr.net/pyodide/v0.29.3/full/pyodide.js");
py = await loadPyodide();
await py.loadPackage(["micropip", "jinja2", "markupsafe"]);
const v = py.runPython("import jinja2; jinja2.__version__");
if (v.split(".").map(Number).join(".") < "3.1.6") await py.runPythonAsync("import micropip; await micropip.install('jinja2>=3.1.6')");
for (const f of ["ctt_bench.py", "web_checker.py"]) py.FS.writeFile(f, await (await fetch(f)).text());
py.runPython("import web_checker");
}
ready = init().then(() => postMessage({ type: "ready" }), (e) => postMessage({ type: "initerror", error: String(e) }));
onmessage = async (ev) => {
await ready;
const { id, template, special } = ev.data;
try {
py.globals.set("_t", template); py.globals.set("_s", JSON.stringify(special));
const out = py.runPython("web_checker.run(_t, _s)");
postMessage({ type: "result", id, data: JSON.parse(out) });
} catch (e) { postMessage({ type: "result", id, data: { ok: false, error: "Could not run the template: " + String(e).split("\n").slice(-2)[0] } }); }
};