Update index.html
Browse files- index.html +87 -91
index.html
CHANGED
|
@@ -4,7 +4,8 @@
|
|
| 4 |
<head>
|
| 5 |
<meta charset="UTF-8">
|
| 6 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
<style>
|
| 10 |
body {
|
|
@@ -28,18 +29,16 @@
|
|
| 28 |
}
|
| 29 |
|
| 30 |
input {
|
| 31 |
-
margin:
|
| 32 |
-
padding: 10px;
|
| 33 |
}
|
| 34 |
|
| 35 |
button {
|
| 36 |
background: #4f46e5;
|
| 37 |
color: white;
|
| 38 |
-
border: none;
|
| 39 |
padding: 12px 25px;
|
|
|
|
| 40 |
border-radius: 8px;
|
| 41 |
cursor: pointer;
|
| 42 |
-
margin: 10px;
|
| 43 |
}
|
| 44 |
|
| 45 |
button:hover {
|
|
@@ -68,139 +67,136 @@
|
|
| 68 |
|
| 69 |
#status {
|
| 70 |
color: #444;
|
| 71 |
-
margin
|
| 72 |
}
|
| 73 |
</style>
|
| 74 |
</head>
|
| 75 |
|
| 76 |
<body>
|
| 77 |
|
| 78 |
-
|
| 79 |
|
| 80 |
-
|
| 81 |
|
| 82 |
-
|
| 83 |
|
| 84 |
-
|
| 85 |
-
placeholder="Enter Hugging Face access token"
|
| 86 |
-
style="width:85%">
|
| 87 |
|
| 88 |
-
|
| 89 |
|
| 90 |
-
|
| 91 |
-
accept="image/*">
|
| 92 |
|
| 93 |
-
|
| 94 |
|
| 95 |
-
|
| 96 |
|
| 97 |
-
|
| 98 |
|
| 99 |
-
|
| 100 |
-
Classify Image
|
| 101 |
-
</button>
|
| 102 |
|
| 103 |
-
|
| 104 |
|
| 105 |
-
|
| 106 |
|
| 107 |
-
|
|
|
|
| 108 |
|
| 109 |
-
|
|
|
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
-
|
|
|
|
| 115 |
|
| 116 |
-
|
| 117 |
-
imageInput.addEventListener("change", function() {
|
| 118 |
|
| 119 |
-
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
preview.style.display = "block";
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
-
|
| 131 |
-
async function classifyImage() {
|
| 132 |
|
| 133 |
-
|
| 134 |
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
return;
|
| 141 |
-
}
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
| 150 |
|
| 151 |
-
|
| 152 |
|
| 153 |
-
|
| 154 |
-
"https://router.huggingface.co/hf-inference/models/google/vit-base-patch16-224",
|
| 155 |
-
{
|
| 156 |
-
method: "POST",
|
| 157 |
|
| 158 |
-
|
| 159 |
-
"Authorization": "Bearer " + token,
|
| 160 |
-
"Content-Type": selectedImage.type
|
| 161 |
-
},
|
| 162 |
|
| 163 |
-
|
| 164 |
-
}
|
| 165 |
-
);
|
| 166 |
|
| 167 |
-
|
|
|
|
|
|
|
| 168 |
|
| 169 |
-
|
| 170 |
-
throw new Error(
|
| 171 |
-
data.error || "Classification request failed. Check your token and model availability."
|
| 172 |
-
);
|
| 173 |
-
}
|
| 174 |
|
| 175 |
-
|
| 176 |
-
throw new Error("Unexpected response from the model.");
|
| 177 |
-
}
|
| 178 |
|
| 179 |
-
|
| 180 |
|
| 181 |
-
|
| 182 |
|
| 183 |
-
|
| 184 |
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
-
|
| 188 |
-
<div class="prediction">
|
| 189 |
-
<strong>${item.label}</strong>
|
| 190 |
-
<br>
|
| 191 |
-
Confidence: ${percentage}%
|
| 192 |
-
</div>
|
| 193 |
-
`;
|
| 194 |
-
});
|
| 195 |
|
| 196 |
-
|
| 197 |
|
| 198 |
-
|
| 199 |
|
| 200 |
-
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
-
|
| 204 |
|
| 205 |
</body>
|
| 206 |
</html>
|
|
|
|
| 4 |
<head>
|
| 5 |
<meta charset="UTF-8">
|
| 6 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 7 |
+
|
| 8 |
+
<title>Hugging Face Image Classification</title>
|
| 9 |
|
| 10 |
<style>
|
| 11 |
body {
|
|
|
|
| 29 |
}
|
| 30 |
|
| 31 |
input {
|
| 32 |
+
margin: 20px;
|
|
|
|
| 33 |
}
|
| 34 |
|
| 35 |
button {
|
| 36 |
background: #4f46e5;
|
| 37 |
color: white;
|
|
|
|
| 38 |
padding: 12px 25px;
|
| 39 |
+
border: none;
|
| 40 |
border-radius: 8px;
|
| 41 |
cursor: pointer;
|
|
|
|
| 42 |
}
|
| 43 |
|
| 44 |
button:hover {
|
|
|
|
| 67 |
|
| 68 |
#status {
|
| 69 |
color: #444;
|
| 70 |
+
margin: 20px;
|
| 71 |
}
|
| 72 |
</style>
|
| 73 |
</head>
|
| 74 |
|
| 75 |
<body>
|
| 76 |
|
| 77 |
+
<div class="container">
|
| 78 |
|
| 79 |
+
<h1>Image Classification</h1>
|
| 80 |
|
| 81 |
+
<p>Upload an image to identify its contents using AI.</p>
|
| 82 |
|
| 83 |
+
<input type="file" id="imageInput" accept="image/*">
|
|
|
|
|
|
|
| 84 |
|
| 85 |
+
<br>
|
| 86 |
|
| 87 |
+
<img id="preview" alt="Image Preview">
|
|
|
|
| 88 |
|
| 89 |
+
<br>
|
| 90 |
|
| 91 |
+
<button id="classifyBtn">Classify Image</button>
|
| 92 |
|
| 93 |
+
<p id="status">Loading AI library...</p>
|
| 94 |
|
| 95 |
+
<div id="result"></div>
|
|
|
|
|
|
|
| 96 |
|
| 97 |
+
</div>
|
| 98 |
|
| 99 |
+
<script type="module">
|
| 100 |
|
| 101 |
+
import { pipeline } from
|
| 102 |
+
'https://cdn.jsdelivr.net/npm/@huggingface/transformers';
|
| 103 |
|
| 104 |
+
let classifier = null;
|
| 105 |
+
let selectedImage = null;
|
| 106 |
|
| 107 |
+
const imageInput = document.getElementById("imageInput");
|
| 108 |
+
const preview = document.getElementById("preview");
|
| 109 |
+
const status = document.getElementById("status");
|
| 110 |
+
const result = document.getElementById("result");
|
| 111 |
+
const button = document.getElementById("classifyBtn");
|
| 112 |
|
| 113 |
+
// Upload image and preview
|
| 114 |
+
imageInput.addEventListener("change", function() {
|
| 115 |
|
| 116 |
+
selectedImage = this.files[0];
|
|
|
|
| 117 |
|
| 118 |
+
if (selectedImage) {
|
| 119 |
|
| 120 |
+
preview.src = URL.createObjectURL(selectedImage);
|
| 121 |
+
preview.style.display = "block";
|
|
|
|
| 122 |
|
| 123 |
+
result.innerHTML = "";
|
| 124 |
+
status.innerText = "Image uploaded successfully!";
|
| 125 |
+
|
| 126 |
+
}
|
| 127 |
+
});
|
| 128 |
+
|
| 129 |
+
// Load AI model
|
| 130 |
+
async function loadModel() {
|
| 131 |
|
| 132 |
+
if (!classifier) {
|
|
|
|
| 133 |
|
| 134 |
+
status.innerText = "Loading AI model. Please wait...";
|
| 135 |
|
| 136 |
+
classifier = await pipeline(
|
| 137 |
+
"image-classification",
|
| 138 |
+
"Xenova/vit-base-patch16-224"
|
| 139 |
+
);
|
| 140 |
+
}
|
| 141 |
+
}
|
| 142 |
|
| 143 |
+
// Classify image
|
| 144 |
+
button.addEventListener("click", async function() {
|
|
|
|
|
|
|
| 145 |
|
| 146 |
+
if (!selectedImage) {
|
| 147 |
+
status.innerText = "Please upload an image first!";
|
| 148 |
+
return;
|
| 149 |
+
}
|
| 150 |
|
| 151 |
+
button.disabled = true;
|
| 152 |
+
result.innerHTML = "";
|
| 153 |
|
| 154 |
+
try {
|
| 155 |
|
| 156 |
+
await loadModel();
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
+
status.innerText = "Analyzing image...";
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
+
const imageURL = URL.createObjectURL(selectedImage);
|
|
|
|
|
|
|
| 161 |
|
| 162 |
+
const predictions = await classifier(imageURL, {
|
| 163 |
+
top_k: 5
|
| 164 |
+
});
|
| 165 |
|
| 166 |
+
URL.revokeObjectURL(imageURL);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
+
status.innerText = "Image classification completed!";
|
|
|
|
|
|
|
| 169 |
|
| 170 |
+
result.innerHTML = "<h3>Classification Results</h3>";
|
| 171 |
|
| 172 |
+
predictions.forEach(item => {
|
| 173 |
|
| 174 |
+
const percentage = (item.score * 100).toFixed(2);
|
| 175 |
|
| 176 |
+
result.innerHTML += `
|
| 177 |
+
<div class="prediction">
|
| 178 |
+
<strong>${item.label}</strong>
|
| 179 |
+
<br>
|
| 180 |
+
Confidence: ${percentage}%
|
| 181 |
+
</div>
|
| 182 |
+
`;
|
| 183 |
|
| 184 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
+
} catch (error) {
|
| 187 |
|
| 188 |
+
status.innerText = "Error: " + error.message;
|
| 189 |
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
button.disabled = false;
|
| 193 |
+
|
| 194 |
+
});
|
| 195 |
+
|
| 196 |
+
// Initial status
|
| 197 |
+
status.innerText = "Upload an image to get started.";
|
| 198 |
|
| 199 |
+
</script>
|
| 200 |
|
| 201 |
</body>
|
| 202 |
</html>
|