Update progress_callback.js
Browse files- progress_callback.js +49 -28
progress_callback.js
CHANGED
|
@@ -1,49 +1,70 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
btn.textContent = "Send";
|
| 3 |
input.disabled = false;
|
| 4 |
btn.disabled = false;
|
| 5 |
|
| 6 |
btn.onclick = async () => {
|
| 7 |
const userText = input.value.trim();
|
| 8 |
-
if (!userText
|
| 9 |
|
| 10 |
-
|
| 11 |
input.value = '';
|
| 12 |
-
|
| 13 |
-
status.textContent = "Thinking...";
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
max_new_tokens: 50,
|
| 20 |
-
temperature: 0.7,
|
| 21 |
-
do_sample: true
|
| 22 |
-
});
|
| 23 |
-
|
| 24 |
-
addMessage('ai', output[0].generated_text);
|
| 25 |
-
} catch (err) {
|
| 26 |
-
console.error("Generation Error:", err);
|
| 27 |
-
status.textContent = "Error during generation.";
|
| 28 |
-
} finally {
|
| 29 |
-
status.textContent = "Ready!";
|
| 30 |
-
btn.disabled = false;
|
| 31 |
-
}
|
| 32 |
};
|
| 33 |
} catch (e) {
|
| 34 |
-
|
| 35 |
-
|
| 36 |
}
|
| 37 |
}
|
| 38 |
|
| 39 |
-
// Helper function to append messages to your chat container
|
| 40 |
function addMessage(sender, text) {
|
| 41 |
-
const container = document.getElementById('chat-container'); // Ensure this ID exists in your HTML
|
| 42 |
const msgDiv = document.createElement('div');
|
| 43 |
-
msgDiv.className = `
|
| 44 |
msgDiv.textContent = text;
|
| 45 |
-
|
| 46 |
-
|
| 47 |
}
|
| 48 |
|
|
|
|
| 49 |
init();
|
|
|
|
| 1 |
+
import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.0.0';
|
| 2 |
+
|
| 3 |
+
// CONFIG MUST COME FIRST
|
| 4 |
+
env.allowRemoteModels = true;
|
| 5 |
+
env.allowLocalModels = false;
|
| 6 |
+
env.backends.onnx.wasm.numThreads = 1; // Stick to 1 for stability on mobile
|
| 7 |
+
env.backends.onnx.wasm.proxy = false;
|
| 8 |
+
|
| 9 |
+
const status = document.getElementById('status');
|
| 10 |
+
const btn = document.getElementById('main-btn');
|
| 11 |
+
const input = document.getElementById('chat-input');
|
| 12 |
+
const chatBox = document.getElementById('chat-box');
|
| 13 |
+
let generator = null;
|
| 14 |
+
|
| 15 |
+
async function init() {
|
| 16 |
+
try {
|
| 17 |
+
btn.disabled = true;
|
| 18 |
+
status.textContent = "Downloading Model (Stay on page)...";
|
| 19 |
+
|
| 20 |
+
generator = await pipeline(
|
| 21 |
+
'text-generation',
|
| 22 |
+
'Xenova/phi-1_5-tiny-onnx',
|
| 23 |
+
{
|
| 24 |
+
device: 'wasm',
|
| 25 |
+
dtype: 'q4', // Critical: Using 4-bit quantization to save RAM
|
| 26 |
+
progress_callback: (d) => {
|
| 27 |
+
if (d.status === 'progress') {
|
| 28 |
+
status.textContent = `Loading: ${Math.round(d.progress)}%`;
|
| 29 |
+
}
|
| 30 |
+
}
|
| 31 |
+
}
|
| 32 |
+
);
|
| 33 |
+
|
| 34 |
+
status.textContent = "Ready!";
|
| 35 |
btn.textContent = "Send";
|
| 36 |
input.disabled = false;
|
| 37 |
btn.disabled = false;
|
| 38 |
|
| 39 |
btn.onclick = async () => {
|
| 40 |
const userText = input.value.trim();
|
| 41 |
+
if (!userText) return;
|
| 42 |
|
| 43 |
+
addMessage('user', userText);
|
| 44 |
input.value = '';
|
| 45 |
+
status.textContent = "AI is thinking...";
|
|
|
|
| 46 |
|
| 47 |
+
const output = await generator(userText, {
|
| 48 |
+
max_new_tokens: 30, // Keep this low for mobile
|
| 49 |
+
temperature: 0.7
|
| 50 |
+
});
|
| 51 |
|
| 52 |
+
addMessage('ai', output[0].generated_text.replace(userText, '').trim());
|
| 53 |
+
status.textContent = "Ready!";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
};
|
| 55 |
} catch (e) {
|
| 56 |
+
status.textContent = "Error: Mobile RAM limit hit.";
|
| 57 |
+
console.error(e);
|
| 58 |
}
|
| 59 |
}
|
| 60 |
|
|
|
|
| 61 |
function addMessage(sender, text) {
|
|
|
|
| 62 |
const msgDiv = document.createElement('div');
|
| 63 |
+
msgDiv.className = `msg ${sender}-message`;
|
| 64 |
msgDiv.textContent = text;
|
| 65 |
+
chatBox.appendChild(msgDiv);
|
| 66 |
+
chatBox.scrollTop = chatBox.scrollHeight;
|
| 67 |
}
|
| 68 |
|
| 69 |
+
// Start the process
|
| 70 |
init();
|