image-classifier / index.html
prernaaaa1234's picture
Update index.html
421e8cb verified
Raw
History Blame Contribute Delete
2.26 kB
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AI Image Classifier</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="card">
<h1>πŸ–ΌοΈ AI Image Classifier</h1>
<p>Upload an image and let AI identify it!</p>
<input type="file" id="imageInput" accept="image/*">
<br><br>
<button id="classifyBtn">πŸ” Classify Image</button>
<p id="status">⏳ Loading AI model...</p>
<div id="result"></div>
</div>
<script type="module">
import { pipeline }
from "https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.8.1";
let classifier;
const status = document.getElementById("status");
const button = document.getElementById("classifyBtn");
const input = document.getElementById("imageInput");
const result = document.getElementById("result");
button.disabled = true;
async function loadModel() {
try {
status.innerText = "⏳ Loading AI model... Please wait.";
classifier = await pipeline(
"image-classification",
"Xenova/vit-base-patch16-224"
);
status.innerText = "βœ… AI model loaded! Upload an image.";
button.disabled = false;
} catch (error) {
status.innerText = "❌ Model failed to load.";
console.error(error);
}
}
button.addEventListener("click", async () => {
if (!input.files.length) {
result.innerText = "⚠️ Please upload an image first.";
return;
}
try {
button.disabled = true;
result.innerText = "πŸ€– AI is analyzing your image...";
const image = input.files[0];
const output = await classifier(image, {
top_k: 3
});
result.innerHTML = `
<h3>🎯 Results</h3>
${output.map(item =>
`<p><b>${item.label}</b> β€” ${(item.score * 100).toFixed(2)}%</p>`
).join("")}
`;
} catch (error) {
result.innerText = "❌ Something went wrong.";
console.error(error);
} finally {
button.disabled = false;
}
});
loadModel();
</script>
</body>
</html>