Update index.js
Browse files
index.js
CHANGED
|
@@ -1,32 +1,94 @@
|
|
| 1 |
import { pipeline } from "@huggingface/transformers";
|
| 2 |
|
| 3 |
-
//
|
| 4 |
-
const
|
| 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 } from "@huggingface/transformers";
|
| 2 |
|
| 3 |
+
// Get DOM elements
|
| 4 |
+
const loadingContainer = document.getElementById('loadingContainer');
|
| 5 |
+
const chatInterface = document.getElementById('chatInterface');
|
| 6 |
+
const progressFill = document.getElementById('progressFill');
|
| 7 |
+
const progressPercent = document.getElementById('progressPercent');
|
| 8 |
+
const questionInput = document.getElementById('questionInput');
|
| 9 |
+
const sendButton = document.getElementById('sendButton');
|
| 10 |
+
const response = document.getElementById('response');
|
| 11 |
+
|
| 12 |
+
async function initializeModel() {
|
| 13 |
+
try {
|
| 14 |
+
// Create a text-generation pipeline with WebGPU
|
| 15 |
+
const generator = await pipeline(
|
| 16 |
+
"text-generation",
|
| 17 |
+
"HuggingFaceTB/SmolLM2-360M",
|
| 18 |
+
{
|
| 19 |
+
device: "webgpu",
|
| 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();
|