Update progress_callback.js
Browse files- progress_callback.js +16 -23
progress_callback.js
CHANGED
|
@@ -8,30 +8,25 @@ const upload = document.getElementById('upload');
|
|
| 8 |
const preview = document.getElementById('preview');
|
| 9 |
const btn = document.getElementById('process-btn');
|
| 10 |
|
| 11 |
-
let
|
| 12 |
|
| 13 |
-
// Function to handle the actual analysis
|
| 14 |
async function runAnalysis() {
|
| 15 |
try {
|
| 16 |
-
output.
|
| 17 |
|
| 18 |
-
// This model
|
| 19 |
-
const
|
| 20 |
-
threshold: 0.5, // Only show things it is 50% sure about
|
| 21 |
-
percentage: true,
|
| 22 |
-
});
|
| 23 |
|
| 24 |
-
if (
|
| 25 |
-
|
| 26 |
-
output.innerHTML = `<strong>AI detected:</strong> ${labels}`;
|
| 27 |
} else {
|
| 28 |
-
output.textContent = "AI
|
| 29 |
}
|
| 30 |
} catch (err) {
|
| 31 |
output.textContent = "Analysis failed: " + err.message;
|
| 32 |
} finally {
|
| 33 |
btn.disabled = false;
|
| 34 |
-
btn.textContent = "Analyze
|
| 35 |
}
|
| 36 |
}
|
| 37 |
|
|
@@ -39,19 +34,18 @@ async function downloadAndInit() {
|
|
| 39 |
try {
|
| 40 |
progressContainer.style.display = 'block';
|
| 41 |
|
| 42 |
-
//
|
| 43 |
const device = navigator.gpu ? 'webgpu' : 'wasm';
|
| 44 |
|
| 45 |
-
|
| 46 |
-
detector = await pipeline('object-detection', 'Xenova/detr-resnet-50', {
|
| 47 |
device: device,
|
| 48 |
progress_callback: (data) => {
|
| 49 |
if (data.status === 'progress') {
|
| 50 |
const pct = Math.round(data.progress);
|
| 51 |
progressBar.style.width = pct + '%';
|
| 52 |
-
progressText.textContent = `Downloading AI
|
| 53 |
} else if (data.status === 'ready') {
|
| 54 |
-
progressText.textContent = "Ready!
|
| 55 |
setTimeout(() => {
|
| 56 |
progressContainer.style.display = 'none';
|
| 57 |
runAnalysis();
|
|
@@ -67,12 +61,12 @@ async function downloadAndInit() {
|
|
| 67 |
|
| 68 |
btn.onclick = async () => {
|
| 69 |
if (!upload.files[0]) {
|
| 70 |
-
alert("Please
|
| 71 |
return;
|
| 72 |
}
|
| 73 |
|
| 74 |
-
if (!
|
| 75 |
-
const userAccepted = confirm("Download
|
| 76 |
if (userAccepted) {
|
| 77 |
btn.disabled = true;
|
| 78 |
btn.textContent = "Downloading...";
|
|
@@ -84,7 +78,6 @@ btn.onclick = async () => {
|
|
| 84 |
}
|
| 85 |
};
|
| 86 |
|
| 87 |
-
// Standard Preview Logic
|
| 88 |
upload.onchange = (e) => {
|
| 89 |
const file = e.target.files[0];
|
| 90 |
if (file) {
|
|
@@ -92,7 +85,7 @@ upload.onchange = (e) => {
|
|
| 92 |
reader.onload = (event) => {
|
| 93 |
preview.src = event.target.result;
|
| 94 |
preview.style.display = 'block';
|
| 95 |
-
output.textContent = "Image
|
| 96 |
};
|
| 97 |
reader.readAsDataURL(file);
|
| 98 |
}
|
|
|
|
| 8 |
const preview = document.getElementById('preview');
|
| 9 |
const btn = document.getElementById('process-btn');
|
| 10 |
|
| 11 |
+
let captioner = null;
|
| 12 |
|
|
|
|
| 13 |
async function runAnalysis() {
|
| 14 |
try {
|
| 15 |
+
output.innerHTML = "✨ <span style='color: #00ffcc;'>Generating detailed description...</span>";
|
| 16 |
|
| 17 |
+
// This model generates a full natural language sentence
|
| 18 |
+
const result = await captioner(preview.src);
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
if (result && result[0] && result[0].generated_text) {
|
| 21 |
+
output.innerHTML = `<strong>AI Description:</strong> ${result[0].generated_text}`;
|
|
|
|
| 22 |
} else {
|
| 23 |
+
output.textContent = "AI processed the image but couldn't form a sentence.";
|
| 24 |
}
|
| 25 |
} catch (err) {
|
| 26 |
output.textContent = "Analysis failed: " + err.message;
|
| 27 |
} finally {
|
| 28 |
btn.disabled = false;
|
| 29 |
+
btn.textContent = "Analyze Another Image";
|
| 30 |
}
|
| 31 |
}
|
| 32 |
|
|
|
|
| 34 |
try {
|
| 35 |
progressContainer.style.display = 'block';
|
| 36 |
|
| 37 |
+
// BLIP is very stable on both WebGPU and WASM (Mobile)
|
| 38 |
const device = navigator.gpu ? 'webgpu' : 'wasm';
|
| 39 |
|
| 40 |
+
captioner = await pipeline('image-to-text', 'Xenova/blip-image-captioning-base', {
|
|
|
|
| 41 |
device: device,
|
| 42 |
progress_callback: (data) => {
|
| 43 |
if (data.status === 'progress') {
|
| 44 |
const pct = Math.round(data.progress);
|
| 45 |
progressBar.style.width = pct + '%';
|
| 46 |
+
progressText.textContent = `Downloading Smart AI (${pct}%)`;
|
| 47 |
} else if (data.status === 'ready') {
|
| 48 |
+
progressText.textContent = "AI Engine Ready! Analyzing now...";
|
| 49 |
setTimeout(() => {
|
| 50 |
progressContainer.style.display = 'none';
|
| 51 |
runAnalysis();
|
|
|
|
| 61 |
|
| 62 |
btn.onclick = async () => {
|
| 63 |
if (!upload.files[0]) {
|
| 64 |
+
alert("Please pick a photo first!");
|
| 65 |
return;
|
| 66 |
}
|
| 67 |
|
| 68 |
+
if (!captioner) {
|
| 69 |
+
const userAccepted = confirm("Download High-Quality AI (90MB)? This will provide detailed descriptions.");
|
| 70 |
if (userAccepted) {
|
| 71 |
btn.disabled = true;
|
| 72 |
btn.textContent = "Downloading...";
|
|
|
|
| 78 |
}
|
| 79 |
};
|
| 80 |
|
|
|
|
| 81 |
upload.onchange = (e) => {
|
| 82 |
const file = e.target.files[0];
|
| 83 |
if (file) {
|
|
|
|
| 85 |
reader.onload = (event) => {
|
| 86 |
preview.src = event.target.result;
|
| 87 |
preview.style.display = 'block';
|
| 88 |
+
output.textContent = "Image loaded. Tap 'Analyze' for a full description.";
|
| 89 |
};
|
| 90 |
reader.readAsDataURL(file);
|
| 91 |
}
|