kimhyunwoo commited on
Commit
2cec2e5
·
verified ·
1 Parent(s): e7706f8

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +16 -14
index.html CHANGED
@@ -141,10 +141,8 @@
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');
@@ -162,27 +160,30 @@
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;
184
  }
185
- loadingIndicator.style.display = 'none';
 
186
  addMessage("bot", "Hello! I'm ready to chat. Ask me anything!");
187
  }
188
 
@@ -193,7 +194,7 @@
193
  messageDiv.classList.add('message', `${role}-message`);
194
  messageDiv.textContent = content;
195
  chatMessagesDiv.appendChild(messageDiv);
196
- chatMessagesDiv.scrollTop = chatMessagesDiv.scrollHeight;
197
 
198
  if (role === 'user' || role === 'assistant') {
199
  chatHistory.push({ role: role, content: content });
@@ -207,12 +208,13 @@
207
  addMessage('user', message);
208
  userInput.value = '';
209
  loadingIndicator.style.display = 'block';
 
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;
 
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
 
147
  const chatMessagesDiv = document.getElementById('chat-messages');
148
  const userInput = document.getElementById('user-input');
 
160
  try {
161
  generator = await pipeline(
162
  "text-generation",
163
+ "Xenova/gemma-1b-it-quantized",
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
  }
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 and internet connection.";
181
  loadingIndicator.style.color = "red";
182
+ sendButton.disabled = true; // Disable sending until loaded.
183
  return;
184
  }
185
+ // DO NOT hide loading indicator immediately. Let progress_callback handle it.
186
+ // loadingIndicator.style.display = 'none';
187
  addMessage("bot", "Hello! I'm ready to chat. Ask me anything!");
188
  }
189
 
 
194
  messageDiv.classList.add('message', `${role}-message`);
195
  messageDiv.textContent = content;
196
  chatMessagesDiv.appendChild(messageDiv);
197
+ chatMessagesDiv.scrollTop = chatMessagesDiv.scrollHeight; // Scroll to bottom
198
 
199
  if (role === 'user' || role === 'assistant') {
200
  chatHistory.push({ role: role, content: content });
 
208
  addMessage('user', message);
209
  userInput.value = '';
210
  loadingIndicator.style.display = 'block';
211
+ loadingIndicator.textContent = 'Generating response...'; // Indicate response generation
212
  sendButton.disabled = true;
213
 
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;