Spaces:
Running
Running
Update index.js
Browse files
index.js
CHANGED
|
@@ -129,6 +129,7 @@ class StreamingChatbot {
|
|
| 129 |
|
| 130 |
// Add user message to chat
|
| 131 |
this.addMessage(message, 'user');
|
|
|
|
| 132 |
|
| 133 |
// Clear input
|
| 134 |
this.userInput.value = '';
|
|
@@ -152,47 +153,26 @@ class StreamingChatbot {
|
|
| 152 |
streamingIndicator.style.marginLeft = '4px';
|
| 153 |
messageContent.appendChild(streamingIndicator);
|
| 154 |
|
| 155 |
-
//
|
| 156 |
-
let prompt = "";
|
| 157 |
-
for (const msg of this.messages) {
|
| 158 |
-
if (msg.role === "user") {
|
| 159 |
-
prompt += `User: ${msg.content}\n`;
|
| 160 |
-
} else if (msg.role === "assistant") {
|
| 161 |
-
prompt += `Assistant: ${msg.content}\n`;
|
| 162 |
-
}
|
| 163 |
-
}
|
| 164 |
-
prompt += `User: ${message}\nAssistant:`;
|
| 165 |
-
|
| 166 |
-
// Store accumulated text
|
| 167 |
-
let accumulatedText = "";
|
| 168 |
-
let indicatorRemoved = false;
|
| 169 |
-
|
| 170 |
-
// Create TextStreamer with proper configuration
|
| 171 |
this.currentStreamer = new TextStreamer(this.generator.tokenizer, {
|
| 172 |
skip_prompt: true,
|
| 173 |
skip_special_tokens: true,
|
| 174 |
callback_function: (text) => {
|
| 175 |
// Remove streaming indicator on first text
|
| 176 |
-
if (
|
| 177 |
streamingIndicator.remove();
|
| 178 |
-
indicatorRemoved = true;
|
| 179 |
}
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
accumulatedText += text;
|
| 183 |
-
messageContent.textContent = accumulatedText;
|
| 184 |
this.scrollToBottom();
|
| 185 |
}
|
| 186 |
});
|
| 187 |
|
| 188 |
-
//
|
| 189 |
-
const output = await this.generator(
|
| 190 |
max_new_tokens: 500,
|
| 191 |
-
|
| 192 |
-
do_sample: true,
|
| 193 |
-
top_p: 0.95,
|
| 194 |
streamer: this.currentStreamer,
|
| 195 |
-
return_full_text: false
|
| 196 |
});
|
| 197 |
|
| 198 |
// Clean up streaming indicator if still present
|
|
@@ -200,31 +180,16 @@ class StreamingChatbot {
|
|
| 200 |
streamingIndicator.remove();
|
| 201 |
}
|
| 202 |
|
| 203 |
-
//
|
| 204 |
-
|
| 205 |
-
if (
|
| 206 |
-
generatedText = output;
|
| 207 |
-
} else if (Array.isArray(output) && output[0]) {
|
| 208 |
-
generatedText = output[0].generated_text || output[0];
|
| 209 |
-
} else if (output && output.generated_text) {
|
| 210 |
-
generatedText = output.generated_text;
|
| 211 |
-
}
|
| 212 |
-
|
| 213 |
-
// If no text was streamed, use the generated text
|
| 214 |
-
if (!accumulatedText && generatedText) {
|
| 215 |
messageContent.textContent = generatedText;
|
| 216 |
-
accumulatedText = generatedText;
|
| 217 |
-
}
|
| 218 |
-
|
| 219 |
-
// Update message history
|
| 220 |
-
this.messages.push({ role: "user", content: message });
|
| 221 |
-
if (accumulatedText) {
|
| 222 |
-
this.messages.push({ role: "assistant", content: accumulatedText });
|
| 223 |
}
|
| 224 |
|
| 225 |
-
//
|
| 226 |
-
|
| 227 |
-
|
|
|
|
| 228 |
}
|
| 229 |
|
| 230 |
} catch (error) {
|
|
|
|
| 129 |
|
| 130 |
// Add user message to chat
|
| 131 |
this.addMessage(message, 'user');
|
| 132 |
+
this.messages.push({ role: "user", content: message });
|
| 133 |
|
| 134 |
// Clear input
|
| 135 |
this.userInput.value = '';
|
|
|
|
| 153 |
streamingIndicator.style.marginLeft = '4px';
|
| 154 |
messageContent.appendChild(streamingIndicator);
|
| 155 |
|
| 156 |
+
// Create enhanced TextStreamer with proper callback handling
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
this.currentStreamer = new TextStreamer(this.generator.tokenizer, {
|
| 158 |
skip_prompt: true,
|
| 159 |
skip_special_tokens: true,
|
| 160 |
callback_function: (text) => {
|
| 161 |
// Remove streaming indicator on first text
|
| 162 |
+
if (streamingIndicator.parentNode) {
|
| 163 |
streamingIndicator.remove();
|
|
|
|
| 164 |
}
|
| 165 |
+
// Append new text and scroll
|
| 166 |
+
messageContent.textContent += text;
|
|
|
|
|
|
|
| 167 |
this.scrollToBottom();
|
| 168 |
}
|
| 169 |
});
|
| 170 |
|
| 171 |
+
// Use the original working format - messages array with streamer
|
| 172 |
+
const output = await this.generator(this.messages, {
|
| 173 |
max_new_tokens: 500,
|
| 174 |
+
do_sample: false,
|
|
|
|
|
|
|
| 175 |
streamer: this.currentStreamer,
|
|
|
|
| 176 |
});
|
| 177 |
|
| 178 |
// Clean up streaming indicator if still present
|
|
|
|
| 180 |
streamingIndicator.remove();
|
| 181 |
}
|
| 182 |
|
| 183 |
+
// Get the generated text and add to conversation history
|
| 184 |
+
const generatedText = output[0].generated_text || messageContent.textContent;
|
| 185 |
+
if (generatedText && !messageContent.textContent) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
messageContent.textContent = generatedText;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
}
|
| 188 |
|
| 189 |
+
// Add assistant's response to message history
|
| 190 |
+
const finalResponse = messageContent.textContent.trim();
|
| 191 |
+
if (finalResponse) {
|
| 192 |
+
this.messages.push({ role: "assistant", content: finalResponse });
|
| 193 |
}
|
| 194 |
|
| 195 |
} catch (error) {
|