Update index.js
Browse files
index.js
CHANGED
|
@@ -1,79 +1,32 @@
|
|
| 1 |
-
import { pipeline
|
| 2 |
-
|
| 3 |
-
//
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
//
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
const
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
detect(EXAMPLE_URL);
|
| 22 |
});
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
reader.readAsDataURL(file);
|
| 36 |
-
});
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
// Detect objects in the image
|
| 40 |
-
async function detect(img) {
|
| 41 |
-
imageContainer.innerHTML = '';
|
| 42 |
-
imageContainer.style.backgroundImage = `url(${img})`;
|
| 43 |
-
|
| 44 |
-
status.textContent = 'Analysing...';
|
| 45 |
-
const output = await detector(img, {
|
| 46 |
-
threshold: 0.5,
|
| 47 |
-
percentage: true,
|
| 48 |
-
});
|
| 49 |
-
status.textContent = '';
|
| 50 |
-
output.forEach(renderBox);
|
| 51 |
-
}
|
| 52 |
-
|
| 53 |
-
// Render a bounding box and label on the image
|
| 54 |
-
function renderBox({ box, label }) {
|
| 55 |
-
const { xmax, xmin, ymax, ymin } = box;
|
| 56 |
-
|
| 57 |
-
// Generate a random color for the box
|
| 58 |
-
const color = '#' + Math.floor(Math.random() * 0xFFFFFF).toString(16).padStart(6, 0);
|
| 59 |
-
|
| 60 |
-
// Draw the box
|
| 61 |
-
const boxElement = document.createElement('div');
|
| 62 |
-
boxElement.className = 'bounding-box';
|
| 63 |
-
Object.assign(boxElement.style, {
|
| 64 |
-
borderColor: color,
|
| 65 |
-
left: 100 * xmin + '%',
|
| 66 |
-
top: 100 * ymin + '%',
|
| 67 |
-
width: 100 * (xmax - xmin) + '%',
|
| 68 |
-
height: 100 * (ymax - ymin) + '%',
|
| 69 |
-
})
|
| 70 |
-
|
| 71 |
-
// Draw label
|
| 72 |
-
const labelElement = document.createElement('span');
|
| 73 |
-
labelElement.textContent = label;
|
| 74 |
-
labelElement.className = 'bounding-box-label';
|
| 75 |
-
labelElement.style.backgroundColor = color;
|
| 76 |
-
|
| 77 |
-
boxElement.appendChild(labelElement);
|
| 78 |
-
imageContainer.appendChild(boxElement);
|
| 79 |
-
}
|
|
|
|
| 1 |
+
import { pipeline } from "@huggingface/transformers";
|
| 2 |
+
|
| 3 |
+
// Create a text-generation pipeline
|
| 4 |
+
const generator = await pipeline(
|
| 5 |
+
"text-generation",
|
| 6 |
+
"HuggingFaceTB/SmolLM2-360M", // You can replace this with other models like "EleutherAI/gpt-neo-125M" or "facebook/opt-125m"
|
| 7 |
+
{ device: "webgpu" }
|
| 8 |
+
);
|
| 9 |
+
|
| 10 |
+
// Generate text
|
| 11 |
+
const prompts = [
|
| 12 |
+
"Once upon a time",
|
| 13 |
+
"The artificial intelligence"
|
| 14 |
+
];
|
| 15 |
+
|
| 16 |
+
const results = await generator(prompts, {
|
| 17 |
+
max_length: 50,
|
| 18 |
+
num_return_sequences: 1,
|
| 19 |
+
temperature: 0.7,
|
| 20 |
+
top_p: 0.9
|
|
|
|
| 21 |
});
|
| 22 |
|
| 23 |
+
console.log(results);
|
| 24 |
+
// Will output something like:
|
| 25 |
+
// [
|
| 26 |
+
// [{
|
| 27 |
+
// "generated_text": "Once upon a time there was a young princess who lived in a castle..."
|
| 28 |
+
// }],
|
| 29 |
+
// [{
|
| 30 |
+
// "generated_text": "The artificial intelligence revolution has transformed the way we..."
|
| 31 |
+
// }]
|
| 32 |
+
// ]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|