tessera-small-onnx / web /example.html
cfahlgren1's picture
cfahlgren1 HF Staff
int8 onnx build of tessera-small with browser and python usage
7a4aa11
Raw History Blame Contribute Delete
2.84 kB
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Tessera in the browser</title>
<style>
body { margin: 0; padding: 32px 16px; font: 16px/1.5 system-ui, sans-serif; background: #f5f5f7; color: #1d1d1f; }
main { max-width: 640px; margin: 0 auto; display: flex; flex-direction: column; gap: 16px; }
form { display: flex; gap: 8px; flex-wrap: wrap; }
input { flex: 1; min-width: 0; font-size: 16px; padding: 10px 14px; border: 1px solid #d2d2d7; border-radius: 10px; }
button { font-size: 16px; padding: 10px 16px; border: 0; border-radius: 10px; background: #1d1d1f; color: #fff; cursor: pointer; }
button:disabled { opacity: 0.4; cursor: default; }
#sprites { display: flex; flex-wrap: wrap; gap: 12px; }
#sprites canvas { width: 128px; height: 128px; background: #fff; border-radius: 12px; image-rendering: pixelated; }
#status { color: #6e6e73; font-size: 14px; }
</style>
</head>
<body>
<main>
<h1>Tessera in the browser</h1>
<form id="form">
<input id="prompt" value="rubber duck" aria-label="Describe a sprite" />
<button id="go" disabled>Paint</button>
</form>
<div id="sprites"></div>
<div id="status">Loading…</div>
</main>
<script type="module">
import { Tessera } from "./tessera.js";
const $ = (id) => document.getElementById(id);
// ?model=<url> loads the model files from somewhere else, e.g. a local copy.
// ?device=wasm forces the CPU.
const params = new URLSearchParams(location.search);
const modelUrl = params.get("model") ?? undefined;
const device = params.get("device") ?? "auto";
const tessera = await Tessera.load({
modelUrl,
device,
onProgress: ({ stage, loaded, total }) => {
$("status").textContent =
stage === "download" ? `Downloading the model · ${Math.round((loaded / total) * 100)}%`
: stage === "load" ? "Loading the model…"
: "Testing your GPU…";
},
});
$("status").textContent = `Ready, on ${tessera.device === "webgpu" ? "your GPU" : "your CPU"}.`;
$("go").disabled = false;
$("form").onsubmit = async (e) => {
e.preventDefault();
$("go").disabled = true;
const t0 = performance.now();
const sprites = await tessera.generate($("prompt").value, {
// Watch the pixels fill in, step by step.
onStep: (_step, partial) => $("sprites").replaceChildren(...partial.map((s) => s.toCanvas(8))),
});
$("sprites").replaceChildren(...sprites.map((s) => s.toCanvas(8)));
$("status").textContent = `${sprites.length} sprites in ${((performance.now() - t0) / 1000).toFixed(1)} s on ${tessera.device}`;
$("go").disabled = false;
};
</script>
</body>
</html>