image-classification / index.html
Dhanavarshitha's picture
Update index.html
0c10715 verified
Raw History Blame Contribute Delete
7.84 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Classification - Model Comparison</title>
<style>
body {
font-family: Arial, sans-serif;
background: #eef2ff;
text-align: center;
padding: 25px;
}
.container {
background: white;
max-width: 1000px;
margin: auto;
padding: 30px;
border-radius: 15px;
box-shadow: 0 4px 15px #999;
}
h1 {
color: #4f46e5;
}
input {
margin: 20px;
}
button {
background: #4f46e5;
color: white;
padding: 12px 25px;
border: none;
border-radius: 8px;
cursor: pointer;
}
button:hover {
background: #3730a3;
}
button:disabled {
opacity: 0.5;
cursor: not-allowed;
}
#preview {
max-width: 250px;
max-height: 250px;
margin: 20px auto;
border-radius: 10px;
display: none;
}
.models {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 20px;
margin-top: 30px;
}
.model-card {
background: #f8faff;
border: 1px solid #dbeafe;
padding: 20px;
border-radius: 12px;
text-align: left;
}
.model-card h2 {
color: #4f46e5;
text-align: center;
}
.prediction {
background: white;
padding: 12px;
margin: 10px 0;
border-radius: 8px;
border: 1px solid #ddd;
}
.prediction strong {
color: #333;
}
#status {
margin: 20px;
color: #444;
}
#comparison {
background: #eef2ff;
padding: 20px;
border-radius: 10px;
margin-top: 25px;
}
@media(max-width: 650px) {
.models {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<div class="container">
<h1>Image Classification</h1>
<h3>Compare Two AI Models</h3>
<p>Upload one image and compare the predictions
of two Hugging Face models.</p>
<input type="file" id="imageInput" accept="image/*">
<br>
<img id="preview" alt="Uploaded Image">
<br>
<button id="classifyBtn">
Compare Models
</button>
<p id="status">Please upload an image.</p>
<div class="models">
<!-- MODEL 1 -->
<div class="model-card">
<h2>Model 1 - ViT</h2>
<p>Vision Transformer</p>
<p><small>google/vit-base-patch16-224</small></p>
<div id="result1">
Predictions will appear here.
</div>
</div>
<!-- MODEL 2 -->
<div class="model-card">
<h2>Model 2 - ConvNeXt</h2>
<p>Convolutional Neural Network</p>
<p><small>facebook/convnext-tiny-224</small></p>
<div id="result2">
Predictions will appear here.
</div>
</div>
</div>
<div id="comparison"></div>
</div>
<script type="module">
import { pipeline } from
'https://cdn.jsdelivr.net/npm/@huggingface/transformers';
let model1 = null;
let model2 = null;
let selectedImage = null;
const imageInput = document.getElementById("imageInput");
const preview = document.getElementById("preview");
const status = document.getElementById("status");
const button = document.getElementById("classifyBtn");
const result1 = document.getElementById("result1");
const result2 = document.getElementById("result2");
const comparison = document.getElementById("comparison");
// Upload and preview image
imageInput.addEventListener("change", function() {
selectedImage = this.files[0];
if (selectedImage) {
preview.src = URL.createObjectURL(selectedImage);
preview.style.display = "block";
result1.textContent = "Ready to classify.";
result2.textContent = "Ready to classify.";
comparison.innerHTML = "";
status.innerText = "Image uploaded successfully!";
}
});
// Load first model
async function loadModel1() {
if (!model1) {
status.innerText = "Loading Model 1 - ViT...";
model1 = await pipeline(
"image-classification",
"Xenova/vit-base-patch16-224",
{ dtype: "q8" }
);
}
return model1;
}
// Load second model
async function loadModel2() {
if (!model2) {
status.innerText = "Loading Model 2 - ConvNeXt...";
model2 = await pipeline(
"image-classification",
"Xenova/convnext-tiny-224",
{ dtype: "q8" }
);
}
return model2;
}
// Display predictions
function displayResults(container, predictions) {
container.innerHTML = "";
predictions.forEach((item, index) => {
const card = document.createElement("div");
card.className = "prediction";
const label = document.createElement("strong");
label.textContent = (index + 1) + ". " + item.label;
const score = document.createElement("p");
score.textContent =
"Confidence: " + (item.score * 100).toFixed(2) + "%";
card.appendChild(label);
card.appendChild(score);
container.appendChild(card);
});
}
// Compare both models
button.addEventListener("click", async function() {
if (!selectedImage) {
status.innerText = "Please upload an image first!";
return;
}
button.disabled = true;
result1.textContent = "Processing...";
result2.textContent = "Processing...";
comparison.innerHTML = "";
let imageURL = URL.createObjectURL(selectedImage);
try {
// Load both models
await loadModel1();
await loadModel2();
status.innerText = "Both models loaded. Classifying image...";
// Give the same image to both models
const [predictions1, predictions2] = await Promise.all([
model1(imageURL, { top_k: 5 }),
model2(imageURL, { top_k: 5 })
]);
// Display results
displayResults(result1, predictions1);
displayResults(result2, predictions2);
// Compare top predictions
const label1 = predictions1[0].label;
const label2 = predictions2[0].label;
comparison.innerHTML = "";
const heading = document.createElement("h2");
heading.textContent = "Model Comparison";
const first = document.createElement("p");
first.textContent = "ViT Prediction: " + label1;
const second = document.createElement("p");
second.textContent = "ConvNeXt Prediction: " + label2;
const conclusion = document.createElement("h3");
if (label1 === label2) {
conclusion.textContent =
"Both models predicted the same category!";
} else {
conclusion.textContent =
"The two models predicted different categories.";
}
comparison.append(heading, first, second, conclusion);
status.innerText = "Comparison completed successfully!";
}
catch(error) {
status.innerText = "Error: " + error.message;
console.error(error);
}
finally {
URL.revokeObjectURL(imageURL);
button.disabled = false;
}
});
</script>
</body>
</html>