Spaces:
Running
Running
Update index.js
Browse files
index.js
CHANGED
|
@@ -129,7 +129,6 @@ class StreamingChatbot {
|
|
| 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,26 +152,47 @@ class StreamingChatbot {
|
|
| 153 |
streamingIndicator.style.marginLeft = '4px';
|
| 154 |
messageContent.appendChild(streamingIndicator);
|
| 155 |
|
| 156 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 166 |
-
|
|
|
|
|
|
|
| 167 |
this.scrollToBottom();
|
| 168 |
}
|
| 169 |
});
|
| 170 |
|
| 171 |
-
//
|
| 172 |
-
const output = await this.generator(
|
| 173 |
max_new_tokens: 500,
|
| 174 |
-
|
|
|
|
|
|
|
| 175 |
streamer: this.currentStreamer,
|
|
|
|
| 176 |
});
|
| 177 |
|
| 178 |
// Clean up streaming indicator if still present
|
|
@@ -180,16 +200,31 @@ class StreamingChatbot {
|
|
| 180 |
streamingIndicator.remove();
|
| 181 |
}
|
| 182 |
|
| 183 |
-
//
|
| 184 |
-
|
| 185 |
-
if (
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
messageContent.textContent = generatedText;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
}
|
| 188 |
|
| 189 |
-
//
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
this.messages.push({ role: "assistant", content: finalResponse });
|
| 193 |
}
|
| 194 |
|
| 195 |
} catch (error) {
|
|
|
|
| 129 |
|
| 130 |
// Add user message to chat
|
| 131 |
this.addMessage(message, 'user');
|
|
|
|
| 132 |
|
| 133 |
// Clear input
|
| 134 |
this.userInput.value = '';
|
|
|
|
| 152 |
streamingIndicator.style.marginLeft = '4px';
|
| 153 |
messageContent.appendChild(streamingIndicator);
|
| 154 |
|
| 155 |
+
// Build the prompt with conversation history
|
| 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 (!indicatorRemoved && streamingIndicator.parentNode) {
|
| 177 |
streamingIndicator.remove();
|
| 178 |
+
indicatorRemoved = true;
|
| 179 |
}
|
| 180 |
+
|
| 181 |
+
// Accumulate and display text
|
| 182 |
+
accumulatedText += text;
|
| 183 |
+
messageContent.textContent = accumulatedText;
|
| 184 |
this.scrollToBottom();
|
| 185 |
}
|
| 186 |
});
|
| 187 |
|
| 188 |
+
// Generate with streamer - use prompt instead of messages for better streaming
|
| 189 |
+
const output = await this.generator(prompt, {
|
| 190 |
max_new_tokens: 500,
|
| 191 |
+
temperature: 0.7,
|
| 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 |
streamingIndicator.remove();
|
| 201 |
}
|
| 202 |
|
| 203 |
+
// Extract the generated text
|
| 204 |
+
let generatedText = "";
|
| 205 |
+
if (typeof output === 'string') {
|
| 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 |
+
// Keep conversation history manageable
|
| 226 |
+
if (this.messages.length > 10) {
|
| 227 |
+
this.messages = this.messages.slice(-10);
|
|
|
|
| 228 |
}
|
| 229 |
|
| 230 |
} catch (error) {
|