Spaces:
Running
Running
Upload index.js with huggingface_hub
Browse files
index.js
CHANGED
|
@@ -142,14 +142,40 @@ class Chatbot {
|
|
| 142 |
const messageContent = responseContainer.querySelector('.message-content');
|
| 143 |
|
| 144 |
// Custom streamer to update UI
|
| 145 |
-
const streamer =
|
|
|
|
| 146 |
skip_prompt: true,
|
| 147 |
skip_special_tokens: true,
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
messageContent.textContent += text;
|
| 150 |
this.scrollToBottom();
|
| 151 |
-
}
|
| 152 |
-
|
|
|
|
| 153 |
|
| 154 |
// Generate response
|
| 155 |
const output = await this.generator(this.messages, {
|
|
|
|
| 142 |
const messageContent = responseContainer.querySelector('.message-content');
|
| 143 |
|
| 144 |
// Custom streamer to update UI
|
| 145 |
+
const streamer = {
|
| 146 |
+
tokenizer: this.generator.tokenizer,
|
| 147 |
skip_prompt: true,
|
| 148 |
skip_special_tokens: true,
|
| 149 |
+
text_cache: '',
|
| 150 |
+
put: function(value) {
|
| 151 |
+
const decoded = this.tokenizer.decode(value[0], {
|
| 152 |
+
skip_special_tokens: this.skip_special_tokens
|
| 153 |
+
});
|
| 154 |
+
if (this.skip_prompt && !this.started) {
|
| 155 |
+
this.started = true;
|
| 156 |
+
return;
|
| 157 |
+
}
|
| 158 |
+
this.text_cache += decoded;
|
| 159 |
+
// Only update when we have complete words (space or punctuation)
|
| 160 |
+
if (/[.!?;:\s\n]$/.test(this.text_cache)) {
|
| 161 |
+
messageContent.textContent += this.text_cache;
|
| 162 |
+
this.scrollToBottom();
|
| 163 |
+
this.text_cache = '';
|
| 164 |
+
}
|
| 165 |
+
},
|
| 166 |
+
end: function() {
|
| 167 |
+
if (this.text_cache) {
|
| 168 |
+
messageContent.textContent += this.text_cache;
|
| 169 |
+
this.scrollToBottom();
|
| 170 |
+
this.text_cache = '';
|
| 171 |
+
}
|
| 172 |
+
},
|
| 173 |
+
on_finalized_text: function(text, stream_end) {
|
| 174 |
messageContent.textContent += text;
|
| 175 |
this.scrollToBottom();
|
| 176 |
+
},
|
| 177 |
+
scrollToBottom: () => this.scrollToBottom()
|
| 178 |
+
};
|
| 179 |
|
| 180 |
// Generate response
|
| 181 |
const output = await this.generator(this.messages, {
|