pet-breed-classifier / index.html
Micky911's picture
Web demo for Micky911/pets-convnext-sagemaker
5587d3f verified
Raw History Blame Contribute Delete
9.27 kB
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Pet Breed Classifier</title>
<style>
:root {
--bg: #f6f6f3; --card: #ffffff; --ink: #16191d; --ink2: #55585e; --muted: #8a8c90; --line: #e4e3dd;
--accent: #1f3a5f; --accent-soft: #e9eff7; --bar: #2a78d6; --bar-bg: #eceff3; --good: #1b7f4b;
}
@media (prefers-color-scheme: dark) {
:root { --bg: #111214; --card: #1b1d20; --ink: #f2f2f0; --ink2: #b8b9b6; --muted: #8a8c90; --line: #2c2e32;
--accent: #9db8de; --accent-soft: #23303f; --bar: #4a90e2; --bar-bg: #2a2d31; --good: #4cc38a; }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--ink);
font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif; line-height: 1.45; }
main { max-width: 960px; margin: 0 auto; padding: 32px 16px 40px; }
header h1 { margin: 0; font-size: clamp(1.6rem, 3vw, 2.1rem); letter-spacing: -0.01em; }
header p { margin: 6px 0 0; color: var(--ink2); max-width: 46em; }
.steps { display: flex; flex-wrap: wrap; gap: 8px 22px; margin: 18px 0 24px; padding: 0; list-style: none;
color: var(--ink2); font-size: 0.9rem; }
.steps li { display: flex; align-items: center; gap: 8px; }
.steps b { display: inline-grid; place-items: center; width: 22px; height: 22px; border-radius: 50%;
background: var(--accent); color: var(--card); font-size: 0.75rem; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 20px;
display: grid; grid-template-columns: 1.05fr 1fr; gap: 24px; box-shadow: 0 1px 2px rgba(0,0,0,0.04); }
@media (max-width: 760px) { .card { grid-template-columns: 1fr; } }
.drop { position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center;
min-height: 300px; border: 2px dashed var(--line); border-radius: 12px; background: var(--bg);
color: var(--ink2); text-align: center; cursor: pointer; overflow: hidden; transition: border-color .15s, background .15s; }
.drop:hover, .drop:focus-visible, .drop.over { border-color: var(--bar); background: var(--accent-soft); outline: none; }
.drop svg { width: 44px; height: 44px; color: var(--bar); margin-bottom: 10px; }
.drop strong { color: var(--ink); }
.drop small { color: var(--muted); margin-top: 4px; }
.drop img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; background: var(--bg); }
.label { margin: 16px 0 8px; font-size: 0.85rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; }
.examples { display: grid; grid-template-columns: repeat(6, 1fr); gap: 8px; }
@media (max-width: 480px) { .examples { grid-template-columns: repeat(3, 1fr); } }
.examples button { all: unset; cursor: pointer; text-align: center; font-size: 0.75rem; color: var(--ink2); }
.examples img { display: block; width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 10px;
border: 1px solid var(--line); margin-bottom: 4px; transition: transform .15s; }
.examples button:hover img, .examples button:focus-visible img { transform: translateY(-2px); border-color: var(--bar); }
.status { font-size: 0.9rem; color: var(--ink2); min-height: 1.4em; }
.progress { height: 6px; border-radius: 3px; background: var(--bar-bg); overflow: hidden; margin: 8px 0 4px; }
.progress div { height: 100%; width: 0; background: var(--bar); transition: width .2s; }
.top { margin: 18px 0 14px; padding: 16px; border-radius: 12px; background: var(--accent-soft); }
.top .name { font-size: 1.5rem; font-weight: 700; }
.top .conf { color: var(--ink2); font-size: 0.95rem; }
.row { display: grid; grid-template-columns: 9.5em 1fr 3.2em; align-items: center; gap: 10px; margin: 10px 0; font-size: 0.95rem; }
.track { height: 10px; border-radius: 5px; background: var(--bar-bg); overflow: hidden; }
.fill { height: 100%; border-radius: 5px; background: var(--bar); width: 0; transition: width .5s ease-out; }
.pct { text-align: right; color: var(--ink2); font-variant-numeric: tabular-nums; }
.hint { color: var(--muted); font-size: 0.85rem; margin-top: 14px; }
.empty { color: var(--muted); margin-top: 18px; }
footer { margin-top: 22px; color: var(--muted); font-size: 0.82rem; }
footer a { color: var(--accent); }
</style>
</head>
<body>
<main>
<header>
<h1>Pet Breed Classifier</h1>
<p>Recognizes 37 cat and dog breeds from a photo. The model runs entirely in your browser,
so your photo never leaves your device.</p>
</header>
<ol class="steps">
<li><b>1</b>Trained on Amazon SageMaker</li>
<li><b>2</b>Published on Hugging Face</li>
<li><b>3</b>Running on your device</li>
</ol>
<section class="card">
<div>
<label class="drop" id="drop" tabindex="0">
<input type="file" id="file" accept="image/*" hidden>
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
<path d="M12 16V4"/><path d="M7 9l5-5 5 5"/><path d="M4 16v3a1 1 0 0 0 1 1h14a1 1 0 0 0 1-1v-3"/>
</svg>
<strong>Drop a photo here or click to choose</strong>
<small>A cat or a dog, JPG or PNG</small>
<img id="preview" alt="Your photo" hidden>
</label>
<div class="label">Or try an example</div>
<div class="examples" id="examples"></div>
</div>
<div>
<div class="status" id="status">Getting the model ready...</div>
<div class="progress" id="progress"><div id="progress-fill"></div></div>
<div id="results"><p class="empty">Results appear here.</p></div>
</div>
</section>
<footer>
Model: <a id="model-link" target="_blank" rel="noopener"></a> ·
data: Oxford-IIIT Pet dataset (CC BY-SA 4.0) · runs with Transformers.js
</footer>
</main>
<script type="module">
// Transformers.js downloads the ONNX copy of the model once and runs it with WebAssembly on this device.
import { pipeline } from "https://cdn.jsdelivr.net/npm/@huggingface/transformers@4.3.2";
const MODEL_ID = "Micky911/pets-convnext-sagemaker"; // filled in by petsml.hf.push_space()
const EXAMPLES = [["bengal", "Bengal"], ["persian", "Persian"], ["siamese", "Siamese"],
["beagle", "Beagle"], ["pug", "Pug"], ["samoyed", "Samoyed"]];
const $ = (id) => document.getElementById(id);
const mb = (bytes) => (bytes / 1e6).toFixed(0);
$("model-link").href = `https://huggingface.co/${MODEL_ID}`;
$("model-link").textContent = MODEL_ID;
const classifier = pipeline("image-classification", MODEL_ID, {
dtype: "fp32",
progress_callback: (p) => {
if (p.status === "progress" && p.file.endsWith(".onnx")) {
$("status").textContent = `Downloading the model (first visit only): ${mb(p.loaded)} / ${mb(p.total)} MB`;
$("progress-fill").style.width = `${p.progress.toFixed(0)}%`;
}
},
});
classifier
.then(() => { $("status").textContent = "Model ready. Choose a photo or an example."; $("progress").hidden = true; })
.catch((err) => { $("status").textContent = "Could not load the model: " + err.message; });
async function classify(url) {
$("preview").src = url;
$("preview").hidden = false;
$("results").innerHTML = "";
$("status").textContent = "Looking at the photo...";
const clf = await classifier;
const start = performance.now();
const top5 = await clf(url, { top_k: 5 });
const ms = Math.round(performance.now() - start);
const [best, ...rest] = top5;
$("status").textContent = "Most likely breed";
$("results").innerHTML = `
<div class="top"><div class="name">${best.label}</div>
<div class="conf">${(best.score * 100).toFixed(0)}% probability</div></div>
${rest.map((p) => `
<div class="row"><span>${p.label}</span>
<div class="track"><div class="fill" data-w="${(p.score * 100).toFixed(1)}"></div></div>
<span class="pct">${(p.score * 100).toFixed(0)}%</span></div>`).join("")}
<div class="hint">Computed on this device in ${ms} ms. The percentages are the model's probabilities, not certainty.</div>`;
requestAnimationFrame(() => document.querySelectorAll(".fill").forEach((el) => { el.style.width = el.dataset.w + "%"; }));
}
const pick = (file) => { if (file && file.type.startsWith("image/")) classify(URL.createObjectURL(file)); };
$("file").addEventListener("change", (e) => pick(e.target.files[0]));
$("drop").addEventListener("keydown", (e) => { if (e.key === "Enter" || e.key === " ") { e.preventDefault(); $("file").click(); } });
$("drop").addEventListener("dragover", (e) => { e.preventDefault(); $("drop").classList.add("over"); });
$("drop").addEventListener("dragleave", () => $("drop").classList.remove("over"));
$("drop").addEventListener("drop", (e) => { e.preventDefault(); $("drop").classList.remove("over"); pick(e.dataTransfer.files[0]); });
for (const [file, name] of EXAMPLES) {
const btn = document.createElement("button");
btn.innerHTML = `<img src="examples/${file}.jpg" alt="${name}">${name}`;
btn.onclick = () => classify(`examples/${file}.jpg`);
$("examples").append(btn);
}
</script>
</body>
</html>