Spaces:
Running
Running
Update index.html
Browse files- index.html +10 -9
index.html
CHANGED
@@ -125,7 +125,7 @@
|
|
125 |
</style>
|
126 |
</head>
|
127 |
<body>
|
128 |
-
<h1>Gemma Chatbot</h1>
|
129 |
|
130 |
<div id="chat-container">
|
131 |
<div id="chat-messages">
|
@@ -141,6 +141,7 @@
|
|
141 |
<script type="module">
|
142 |
import { pipeline, env } from "https://cdn.jsdelivr.net/npm/@huggingface/[email protected]/dist/transformers.js";
|
143 |
|
|
|
144 |
env.allowRemoteModels = true;
|
145 |
env.cacheDir = './cache';
|
146 |
|
@@ -160,14 +161,13 @@
|
|
160 |
try {
|
161 |
generator = await pipeline(
|
162 |
"text-generation",
|
163 |
-
"
|
164 |
{
|
165 |
-
dtype: "q4",
|
166 |
progress_callback: (progress) => {
|
167 |
if (progress.status === 'progress') {
|
168 |
loadingIndicator.textContent = `Loading... ${progress.file} - ${Math.round(progress.loaded / 1000000)}MB/${Math.round(progress.total / 1000000)}MB`;
|
169 |
}
|
170 |
-
// IMPORTANT: Set loading indicator to complete when done
|
171 |
if (progress.status === 'loaded') {
|
172 |
loadingIndicator.textContent = 'Model Loaded!';
|
173 |
}
|
@@ -177,18 +177,18 @@
|
|
177 |
|
178 |
} catch (error) {
|
179 |
console.error("Error loading model:", error);
|
180 |
-
loadingIndicator.textContent = "Error loading model. Check console and
|
181 |
loadingIndicator.style.color = "red";
|
182 |
-
sendButton.disabled = true;
|
183 |
return;
|
184 |
}
|
185 |
-
//
|
186 |
-
// loadingIndicator.style.display = 'none';
|
187 |
addMessage("bot", "Hello! I'm ready to chat. Ask me anything!");
|
188 |
}
|
189 |
|
190 |
initializePipeline();
|
191 |
|
|
|
192 |
function addMessage(role, content) {
|
193 |
const messageDiv = document.createElement('div');
|
194 |
messageDiv.classList.add('message', `${role}-message`);
|
@@ -214,7 +214,7 @@
|
|
214 |
try {
|
215 |
const output = await generator(chatHistory, {
|
216 |
max_new_tokens: 512,
|
217 |
-
do_sample: false
|
218 |
});
|
219 |
|
220 |
const botResponse = output[0].generated_text.at(-1).content;
|
@@ -229,6 +229,7 @@
|
|
229 |
}
|
230 |
}
|
231 |
|
|
|
232 |
sendButton.addEventListener('click', sendMessage);
|
233 |
userInput.addEventListener('keypress', (event) => {
|
234 |
if (event.key === 'Enter') {
|
|
|
125 |
</style>
|
126 |
</head>
|
127 |
<body>
|
128 |
+
<h1>Gemma 3 Chatbot</h1>
|
129 |
|
130 |
<div id="chat-container">
|
131 |
<div id="chat-messages">
|
|
|
141 |
<script type="module">
|
142 |
import { pipeline, env } from "https://cdn.jsdelivr.net/npm/@huggingface/[email protected]/dist/transformers.js";
|
143 |
|
144 |
+
// Allow remote models and set a cache directory.
|
145 |
env.allowRemoteModels = true;
|
146 |
env.cacheDir = './cache';
|
147 |
|
|
|
161 |
try {
|
162 |
generator = await pipeline(
|
163 |
"text-generation",
|
164 |
+
"onnx-community/gemma-3-1b-it-ONNX", // Use correct model name
|
165 |
{
|
166 |
+
dtype: "q4", // Use quantized weights (smaller, faster)
|
167 |
progress_callback: (progress) => {
|
168 |
if (progress.status === 'progress') {
|
169 |
loadingIndicator.textContent = `Loading... ${progress.file} - ${Math.round(progress.loaded / 1000000)}MB/${Math.round(progress.total / 1000000)}MB`;
|
170 |
}
|
|
|
171 |
if (progress.status === 'loaded') {
|
172 |
loadingIndicator.textContent = 'Model Loaded!';
|
173 |
}
|
|
|
177 |
|
178 |
} catch (error) {
|
179 |
console.error("Error loading model:", error);
|
180 |
+
loadingIndicator.textContent = "Error loading model. Check console, internet, and browser.";
|
181 |
loadingIndicator.style.color = "red";
|
182 |
+
sendButton.disabled = true;
|
183 |
return;
|
184 |
}
|
185 |
+
// Don't hide until 'loaded' in progress_callback
|
|
|
186 |
addMessage("bot", "Hello! I'm ready to chat. Ask me anything!");
|
187 |
}
|
188 |
|
189 |
initializePipeline();
|
190 |
|
191 |
+
|
192 |
function addMessage(role, content) {
|
193 |
const messageDiv = document.createElement('div');
|
194 |
messageDiv.classList.add('message', `${role}-message`);
|
|
|
214 |
try {
|
215 |
const output = await generator(chatHistory, {
|
216 |
max_new_tokens: 512,
|
217 |
+
do_sample: false // Try setting to `true` for varied responses.
|
218 |
});
|
219 |
|
220 |
const botResponse = output[0].generated_text.at(-1).content;
|
|
|
229 |
}
|
230 |
}
|
231 |
|
232 |
+
|
233 |
sendButton.addEventListener('click', sendMessage);
|
234 |
userInput.addEventListener('keypress', (event) => {
|
235 |
if (event.key === 'Enter') {
|