Update index.html
Browse files- index.html +81 -32
index.html
CHANGED
|
@@ -1,32 +1,81 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.17.1';
|
| 2 |
+
|
| 3 |
+
// 1. Select UI Elements
|
| 4 |
+
const progressContainer = document.getElementById('progress-container');
|
| 5 |
+
const progressBar = document.getElementById('progress-bar');
|
| 6 |
+
const progressText = document.getElementById('progress-text');
|
| 7 |
+
const output = document.getElementById('output');
|
| 8 |
+
const upload = document.getElementById('upload');
|
| 9 |
+
const preview = document.getElementById('preview');
|
| 10 |
+
const btn = document.getElementById('process-btn');
|
| 11 |
+
|
| 12 |
+
let classifier = null;
|
| 13 |
+
|
| 14 |
+
// 2. Initialize Model Function
|
| 15 |
+
async function initModel() {
|
| 16 |
+
try {
|
| 17 |
+
progressContainer.style.display = 'block';
|
| 18 |
+
output.textContent = "Loading AI engine...";
|
| 19 |
+
|
| 20 |
+
classifier = await pipeline('image-classification', 'Xenova/vit-gpt2-image-captioning', {
|
| 21 |
+
device: 'webgpu', // Forces local GPU usage
|
| 22 |
+
progress_callback: (data) => {
|
| 23 |
+
if (data.status === 'progress') {
|
| 24 |
+
const pct = Math.round(data.progress);
|
| 25 |
+
progressBar.style.width = pct + '%';
|
| 26 |
+
progressBar.setAttribute('aria-valuenow', pct);
|
| 27 |
+
progressText.textContent = `Downloading ${data.file}: ${pct}%`;
|
| 28 |
+
} else if (data.status === 'ready') {
|
| 29 |
+
progressText.textContent = "Model Ready!";
|
| 30 |
+
setTimeout(() => progressContainer.style.display = 'none', 1500);
|
| 31 |
+
}
|
| 32 |
+
}
|
| 33 |
+
});
|
| 34 |
+
return true;
|
| 35 |
+
} catch (err) {
|
| 36 |
+
console.error("WebGPU Initialization Error:", err);
|
| 37 |
+
output.textContent = "WebGPU Error: " + err.message;
|
| 38 |
+
progressText.textContent = "Try using Chrome or Edge with Hardware Acceleration ON.";
|
| 39 |
+
return false;
|
| 40 |
+
}
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
// 3. Handle Analysis
|
| 44 |
+
btn.onclick = async () => {
|
| 45 |
+
if (!upload.files[0]) {
|
| 46 |
+
output.textContent = "⚠️ Please select an image first.";
|
| 47 |
+
return;
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
// Load model if it's the first time clicking
|
| 51 |
+
if (!classifier) {
|
| 52 |
+
const ready = await initModel();
|
| 53 |
+
if (!ready) return;
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
try {
|
| 57 |
+
btn.disabled = true;
|
| 58 |
+
btn.textContent = "Processing...";
|
| 59 |
+
output.textContent = "Thinking...";
|
| 60 |
+
|
| 61 |
+
const result = await classifier(preview.src);
|
| 62 |
+
|
| 63 |
+
output.innerHTML = `<strong>Result:</strong> ${result[0].label}`;
|
| 64 |
+
} catch (err) {
|
| 65 |
+
output.textContent = "Analysis failed: " + err.message;
|
| 66 |
+
} finally {
|
| 67 |
+
btn.disabled = false;
|
| 68 |
+
btn.textContent = "Analyze Image";
|
| 69 |
+
}
|
| 70 |
+
};
|
| 71 |
+
|
| 72 |
+
// 4. Handle Image Preview
|
| 73 |
+
upload.onchange = (e) => {
|
| 74 |
+
const file = e.target.files[0];
|
| 75 |
+
if (file) {
|
| 76 |
+
const url = URL.createObjectURL(file);
|
| 77 |
+
preview.src = url;
|
| 78 |
+
preview.style.display = 'block';
|
| 79 |
+
output.textContent = "Image ready. Click Analyze.";
|
| 80 |
+
}
|
| 81 |
+
};
|