Spaces:
Running
Running
| <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> |