Update index.js
Browse files
index.js
CHANGED
|
@@ -1,76 +1,30 @@
|
|
| 1 |
-
import { pipeline } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.
|
| 2 |
|
| 3 |
-
// Reference the elements that we will need
|
| 4 |
const status = document.getElementById('status');
|
| 5 |
-
const
|
| 6 |
-
const
|
| 7 |
-
const
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
//
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
e.preventDefault();
|
| 18 |
-
detect(EXAMPLE_URL);
|
| 19 |
-
});
|
| 20 |
-
|
| 21 |
-
fileUpload.addEventListener('change', function (e) {
|
| 22 |
-
const file = e.target.files[0];
|
| 23 |
-
if (!file) {
|
| 24 |
-
return;
|
| 25 |
-
}
|
| 26 |
-
|
| 27 |
-
const reader = new FileReader();
|
| 28 |
-
|
| 29 |
-
// Set up a callback when the file is loaded
|
| 30 |
-
reader.onload = e2 => detect(e2.target.result);
|
| 31 |
-
|
| 32 |
-
reader.readAsDataURL(file);
|
| 33 |
-
});
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
// Detect objects in the image
|
| 37 |
-
async function detect(img) {
|
| 38 |
-
imageContainer.innerHTML = '';
|
| 39 |
-
imageContainer.style.backgroundImage = `url(${img})`;
|
| 40 |
-
|
| 41 |
-
status.textContent = 'Analysing...';
|
| 42 |
-
const output = await detector(img, {
|
| 43 |
-
threshold: 0.5,
|
| 44 |
-
percentage: true,
|
| 45 |
-
});
|
| 46 |
-
status.textContent = '';
|
| 47 |
-
output.forEach(renderBox);
|
| 48 |
}
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
const
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
borderColor: color,
|
| 62 |
-
left: 100 * xmin + '%',
|
| 63 |
-
top: 100 * ymin + '%',
|
| 64 |
-
width: 100 * (xmax - xmin) + '%',
|
| 65 |
-
height: 100 * (ymax - ymin) + '%',
|
| 66 |
-
})
|
| 67 |
-
|
| 68 |
-
// Draw label
|
| 69 |
-
const labelElement = document.createElement('span');
|
| 70 |
-
labelElement.textContent = label;
|
| 71 |
-
labelElement.className = 'bounding-box-label';
|
| 72 |
-
labelElement.style.backgroundColor = color;
|
| 73 |
|
| 74 |
-
|
| 75 |
-
imageContainer.appendChild(boxElement);
|
| 76 |
-
}
|
|
|
|
| 1 |
+
import { pipeline } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.0.0';
|
| 2 |
|
|
|
|
| 3 |
const status = document.getElementById('status');
|
| 4 |
+
const output = document.getElementById('output');
|
| 5 |
+
const input = document.getElementById('input');
|
| 6 |
+
const submitBtn = document.getElementById('submit');
|
| 7 |
+
|
| 8 |
+
let generator;
|
| 9 |
+
|
| 10 |
+
// Initialize model directly in browser
|
| 11 |
+
async function init() {
|
| 12 |
+
status.textContent = 'Loading model...';
|
| 13 |
+
generator = await pipeline('text-generation', 'onnx-community/Qwen2.5-0.5B-Instruct');
|
| 14 |
+
status.textContent = 'Model Ready!';
|
| 15 |
+
submitBtn.disabled = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
}
|
| 17 |
|
| 18 |
+
submitBtn.addEventListener('click', async () => {
|
| 19 |
+
const text = input.value;
|
| 20 |
+
if (!text) return;
|
| 21 |
+
|
| 22 |
+
status.textContent = 'Analyzing...';
|
| 23 |
+
const messages = [{ role: 'user', content: text }];
|
| 24 |
+
const result = await generator(messages, { max_new_tokens: 200 });
|
| 25 |
+
|
| 26 |
+
output.textContent = result[0].generated_text.at(-1).content;
|
| 27 |
+
status.textContent = 'Done!';
|
| 28 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
init();
|
|
|
|
|
|