Update index.js
Browse files
index.js
CHANGED
|
@@ -1,94 +1,19 @@
|
|
| 1 |
-
import { pipeline } from
|
| 2 |
-
|
| 3 |
-
//
|
| 4 |
-
const
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
progress_callback: (progress) => {
|
| 21 |
-
// Calculate percentage
|
| 22 |
-
const percentage = Math.round(progress.progress * 100);
|
| 23 |
-
|
| 24 |
-
// Update progress bar and text
|
| 25 |
-
progressFill.style.width = `${percentage}%`;
|
| 26 |
-
progressPercent.textContent = `${percentage}%`;
|
| 27 |
-
|
| 28 |
-
// When loading is complete
|
| 29 |
-
if (progress.progress === 1) {
|
| 30 |
-
setTimeout(() => {
|
| 31 |
-
loadingContainer.style.display = 'none';
|
| 32 |
-
chatInterface.style.display = 'block';
|
| 33 |
-
}, 500);
|
| 34 |
-
}
|
| 35 |
-
}
|
| 36 |
-
}
|
| 37 |
-
);
|
| 38 |
-
|
| 39 |
-
// Add event listener for send button
|
| 40 |
-
sendButton.addEventListener('click', async () => {
|
| 41 |
-
const question = questionInput.value.trim();
|
| 42 |
-
if (!question) return;
|
| 43 |
-
|
| 44 |
-
try {
|
| 45 |
-
// Disable input while generating
|
| 46 |
-
sendButton.disabled = true;
|
| 47 |
-
questionInput.disabled = true;
|
| 48 |
-
response.textContent = 'Generating response...';
|
| 49 |
-
|
| 50 |
-
const results = await generator(question, {
|
| 51 |
-
max_length: 50,
|
| 52 |
-
num_return_sequences: 1,
|
| 53 |
-
temperature: 0.7,
|
| 54 |
-
top_p: 0.9
|
| 55 |
-
});
|
| 56 |
-
|
| 57 |
-
// Display the generated text
|
| 58 |
-
response.textContent = results[0].generated_text;
|
| 59 |
-
|
| 60 |
-
} catch (error) {
|
| 61 |
-
response.textContent = 'Error generating response: ' + error.message;
|
| 62 |
-
} finally {
|
| 63 |
-
// Re-enable input
|
| 64 |
-
sendButton.disabled = false;
|
| 65 |
-
questionInput.disabled = false;
|
| 66 |
-
}
|
| 67 |
-
});
|
| 68 |
-
|
| 69 |
-
} catch (error) {
|
| 70 |
-
progressPercent.textContent = 'Error loading model: ' + error.message;
|
| 71 |
-
}
|
| 72 |
-
}
|
| 73 |
-
|
| 74 |
-
// Check for WebGPU support
|
| 75 |
-
async function checkWebGPUSupport() {
|
| 76 |
-
if (!navigator.gpu) {
|
| 77 |
-
loadingContainer.innerHTML = `
|
| 78 |
-
<div class="error-message">
|
| 79 |
-
WebGPU is not supported in your browser.
|
| 80 |
-
Please use a browser that supports WebGPU, such as Chrome Canary with WebGPU flags enabled.
|
| 81 |
-
</div>`;
|
| 82 |
-
return false;
|
| 83 |
-
}
|
| 84 |
-
return true;
|
| 85 |
-
}
|
| 86 |
-
|
| 87 |
-
// Initialize when page loads
|
| 88 |
-
async function init() {
|
| 89 |
-
if (await checkWebGPUSupport()) {
|
| 90 |
-
initializeModel();
|
| 91 |
-
}
|
| 92 |
-
}
|
| 93 |
-
|
| 94 |
-
init();
|
|
|
|
| 1 |
+
import { pipeline } from '@huggingface/transformers';
|
| 2 |
+
|
| 3 |
+
// Create a text generation pipeline
|
| 4 |
+
const generator = await pipeline(
|
| 5 |
+
'text-generation',
|
| 6 |
+
'onnx-community/Qwen2.5-0.5B-Instruct',
|
| 7 |
+
{ dtype: 'q4', device: 'webgpu' },
|
| 8 |
+
);
|
| 9 |
+
|
| 10 |
+
// Define the list of messages
|
| 11 |
+
const messages = [
|
| 12 |
+
{ role: 'system', content: 'You are a helpful assistant.' },
|
| 13 |
+
{ role: 'user', content: 'Tell me a funny joke.' },
|
| 14 |
+
];
|
| 15 |
+
|
| 16 |
+
// Generate a response
|
| 17 |
+
const output = await generator(messages, { max_new_tokens: 128 });
|
| 18 |
+
console.log(output[0].generated_text.at(-1).content);
|
| 19 |
+
// "Why did the tomato turn red?\n\nBecause it saw the salad dressing!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|