Update index.html
Browse files3 unmodified lines4567891017 unmodified lines28293031323334353637383940414243444522 unmodified lines68697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052063 unmodified lines<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Image Classification using Hugging Face</title><style>body {17 unmodified lines}input {margin: 15px;padding: 10px;}button {background: #4f46e5;color: white;border: none;padding: 12px 25px;border-radius: 8px;cursor: pointer;margin: 10px;}button:hover {22 unmodified lines#status {color: #444;margin-top: 15px;}</style></head><body><div class="container"><h1>Image Classification</h1><p>Upload an image to identify its contents using AI.</p><input type="password" id="token"placeholder="Enter Hugging Face access token"style="width:85%"><br><input type="file" id="imageInput"accept="image/*"><br><img id="preview" alt="Image Preview"><br><button onclick="classifyImage()">Classify Image</button><p id="status"></p><div id="result"></div></div><script>const imageInput = document.getElementById("imageInput");const preview = document.getElementById("preview");let selectedImage = null;// Preview uploaded imageimageInput.addEventListener("change", function() {selectedImage = this.files[0];if (selectedImage) {preview.src = URL.createObjectURL(selectedImage);preview.style.display = "block";document.getElementById("result").innerHTML = "";document.getElementById("status").innerText = "";}});// Image Classificationasync function classifyImage() {const token = document.getElementById("token").value.trim();const status = document.getElementById("status");const result = document.getElementById("result");if (!token) {status.innerText = "Please enter your Hugging Face token.";return;}if (!selectedImage) {status.innerText = "Please upload an image.";return;}status.innerText = "Analyzing image... Please wait.";result.innerHTML = "";try {const response = await fetch("https://router.huggingface.co/hf-inference/models/google/vit-base-patch16-224",{method: "POST",headers: {"Authorization": "Bearer " + token,"Content-Type": selectedImage.type},body: selectedImage});const data = await response.json();if (!response.ok) {throw new Error(data.error || "Classification request failed. Check your token and model availability.");}if (!Array.isArray(data)) {throw new Error("Unexpected response from the model.");}status.innerText = "Classification completed!";result.innerHTML = "<h3>Classification Results</h3>";data.slice(0, 5).forEach(item => {const percentage = (item.score * 100).toFixed(2);result.innerHTML += `<div class="prediction"><strong>${item.label}</strong><br>Confidence: ${percentage}%</div>`;});} catch (error) {status.innerText = "Error: " + error.message;}}</script></body></html>3 unmodified lines456789101117 unmodified lines2930313233343536373839404142434422 unmodified lines6768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012023 unmodified lines<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Hugging Face Image Classification</title><style>body {17 unmodified lines}input {margin: 20px;}button {background: #4f46e5;color: white;padding: 12px 25px;border: none;border-radius: 8px;cursor: pointer;}button:hover {22 unmodified lines#status {color: #444;margin: 20px;}</style></head><body><div class="container"><h1>Image Classification</h1><p>Upload an image to identify its contents using AI.</p><input type="file" id="imageInput" accept="image/*"><br><img id="preview" alt="Image Preview"><br><button id="classifyBtn">Classify Image</button><p id="status">Loading AI library...</p><div id="result"></div></div><script type="module">import { pipeline } from'https://cdn.jsdelivr.net/npm/@huggingface/transformers';let classifier = null;let selectedImage = null;const imageInput = document.getElementById("imageInput");const preview = document.getElementById("preview");const status = document.getElementById("status");const result = document.getElementById("result");const button = document.getElementById("classifyBtn");// Upload image and previewimageInput.addEventListener("change", function() {selectedImage = this.files[0];if (selectedImage) {preview.src = URL.createObjectURL(selectedImage);preview.style.display = "block";result.innerHTML = "";status.innerText = "Image uploaded successfully!";}});// Load AI modelasync function loadModel() {if (!classifier) {status.innerText = "Loading AI model. Please wait...";classifier = await pipeline("image-classification","Xenova/vit-base-patch16-224");}}// Classify imagebutton.addEventListener("click", async function() {if (!selectedImage) {status.innerText = "Please upload an image first!";return;}button.disabled = true;result.innerHTML = "";try {await loadModel();status.innerText = "Analyzing image...";const imageURL = URL.createObjectURL(selectedImage);const predictions = await classifier(imageURL, {top_k: 5});URL.revokeObjectURL(imageURL);status.innerText = "Image classification completed!";result.innerHTML = "<h3>Classification Results</h3>";predictions.forEach(item => {const percentage = (item.score * 100).toFixed(2);result.innerHTML += `<div class="prediction"><strong>${item.label}</strong><br>Confidence: ${percentage}%</div>`;});} catch (error) {status.innerText = "Error: " + error.message;}button.disabled = false;});// Initial statusstatus.innerText = "Upload an image to get started.";</script></body></html>
index.html+87-91