Spaces:
Running
Running
Update index.html
Browse files- index.html +28 -15
index.html
CHANGED
@@ -5,7 +5,7 @@
|
|
5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
<title>Gemma Chatbot</title>
|
7 |
<style>
|
8 |
-
/* (Same CSS as
|
9 |
body {
|
10 |
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
11 |
display: flex;
|
@@ -139,31 +139,45 @@
|
|
139 |
</div>
|
140 |
|
141 |
<script type="module">
|
142 |
-
import { pipeline } from "https://cdn.jsdelivr.net/npm/@huggingface/[email protected]/dist/transformers.js";
|
|
|
|
|
|
|
|
|
|
|
143 |
|
144 |
const chatMessagesDiv = document.getElementById('chat-messages');
|
145 |
const userInput = document.getElementById('user-input');
|
146 |
const sendButton = document.getElementById('send-button');
|
147 |
const loadingIndicator = document.getElementById('loading-indicator');
|
148 |
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
|
153 |
let generator;
|
154 |
|
155 |
async function initializePipeline() {
|
156 |
loadingIndicator.style.display = 'block';
|
157 |
try {
|
158 |
-
// Use the correct model name for text generation with Gemma.
|
159 |
generator = await pipeline(
|
160 |
"text-generation",
|
161 |
-
"Xenova/gemma-1b-it-quantized",
|
162 |
-
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
);
|
|
|
164 |
} catch (error) {
|
165 |
console.error("Error loading model:", error);
|
166 |
-
loadingIndicator.textContent = "Error loading model. Check console and ensure
|
167 |
loadingIndicator.style.color = "red";
|
168 |
sendButton.disabled = true;
|
169 |
return;
|
@@ -181,9 +195,9 @@
|
|
181 |
chatMessagesDiv.appendChild(messageDiv);
|
182 |
chatMessagesDiv.scrollTop = chatMessagesDiv.scrollHeight;
|
183 |
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
}
|
188 |
|
189 |
async function sendMessage() {
|
@@ -196,12 +210,11 @@
|
|
196 |
sendButton.disabled = true;
|
197 |
|
198 |
try {
|
199 |
-
|
200 |
max_new_tokens: 512,
|
201 |
-
do_sample: false
|
202 |
});
|
203 |
|
204 |
-
// Correctly access the last message in the generated text.
|
205 |
const botResponse = output[0].generated_text.at(-1).content;
|
206 |
addMessage('assistant', botResponse);
|
207 |
|
|
|
5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
<title>Gemma Chatbot</title>
|
7 |
<style>
|
8 |
+
/* (Same CSS as before - good styling) */
|
9 |
body {
|
10 |
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
11 |
display: flex;
|
|
|
139 |
</div>
|
140 |
|
141 |
<script type="module">
|
142 |
+
import { pipeline, env } from "https://cdn.jsdelivr.net/npm/@huggingface/[email protected]/dist/transformers.js";
|
143 |
+
|
144 |
+
// VERY IMPORTANT: Explicitly set the cache directory and allow remote models.
|
145 |
+
env.allowRemoteModels = true; // MUST be true to use models from the Hub
|
146 |
+
env.cacheDir = './cache'; // Or another directory if you prefer
|
147 |
+
|
148 |
|
149 |
const chatMessagesDiv = document.getElementById('chat-messages');
|
150 |
const userInput = document.getElementById('user-input');
|
151 |
const sendButton = document.getElementById('send-button');
|
152 |
const loadingIndicator = document.getElementById('loading-indicator');
|
153 |
|
154 |
+
let chatHistory = [
|
155 |
+
{ role: "system", content: "You are a helpful assistant." },
|
156 |
+
];
|
157 |
|
158 |
let generator;
|
159 |
|
160 |
async function initializePipeline() {
|
161 |
loadingIndicator.style.display = 'block';
|
162 |
try {
|
|
|
163 |
generator = await pipeline(
|
164 |
"text-generation",
|
165 |
+
"Xenova/gemma-1b-it-quantized", // Correct model name
|
166 |
+
{
|
167 |
+
dtype: "q4",
|
168 |
+
// Add progress_callback to show loading progress. This is important for UX.
|
169 |
+
progress_callback: (progress) => {
|
170 |
+
if (progress.status === 'progress') {
|
171 |
+
let progressText = `Loading... ${progress.file} - ${Math.round(progress.loaded/1000000)}MB/${Math.round(progress.total/1000000)}MB`;
|
172 |
+
loadingIndicator.textContent = progressText
|
173 |
+
}
|
174 |
+
}
|
175 |
+
}
|
176 |
);
|
177 |
+
|
178 |
} catch (error) {
|
179 |
console.error("Error loading model:", error);
|
180 |
+
loadingIndicator.textContent = "Error loading model. Check console and ensure a modern browser.";
|
181 |
loadingIndicator.style.color = "red";
|
182 |
sendButton.disabled = true;
|
183 |
return;
|
|
|
195 |
chatMessagesDiv.appendChild(messageDiv);
|
196 |
chatMessagesDiv.scrollTop = chatMessagesDiv.scrollHeight;
|
197 |
|
198 |
+
if (role === 'user' || role === 'assistant') {
|
199 |
+
chatHistory.push({ role: role, content: content });
|
200 |
+
}
|
201 |
}
|
202 |
|
203 |
async function sendMessage() {
|
|
|
210 |
sendButton.disabled = true;
|
211 |
|
212 |
try {
|
213 |
+
const output = await generator(chatHistory, {
|
214 |
max_new_tokens: 512,
|
215 |
+
do_sample: false // Try setting to `true` for varied responses
|
216 |
});
|
217 |
|
|
|
218 |
const botResponse = output[0].generated_text.at(-1).content;
|
219 |
addMessage('assistant', botResponse);
|
220 |
|