put printable text into queue
(self, value)
| 87 | self.history.append({"role": "bot", "content": self.text_until}) |
| 88 | |
| 89 | def put(self, value): |
| 90 | """ |
| 91 | put printable text into queue |
| 92 | """ |
| 93 | if len(value.shape) > 1 and value.shape[0] > 1: |
| 94 | raise ValueError("TextStreamer only supports batch size 1") |
| 95 | elif len(value.shape) > 1: |
| 96 | value = value[0] |
| 97 | |
| 98 | if self.skip_prompt and self.next_tokens_are_prompt: |
| 99 | self.next_tokens_are_prompt = False |
| 100 | return |
| 101 | |
| 102 | if value[-1] == self.tokenizer.eos_token_id: |
| 103 | return |
| 104 | |
| 105 | # there may be some smart way to decode. |
| 106 | self.token_cache.extend(value.tolist()) |
| 107 | text = self.tokenizer.decode(self.token_cache, **self.decode_kwargs) |
| 108 | self.cache_time += 1 |
| 109 | |
| 110 | if self._is_printable(text) or self.cache_time >= 6: |
| 111 | self.text_until += text |
| 112 | self.token_cache = [] |
| 113 | self.cache_time = 0 |
| 114 | |
| 115 | else: |
| 116 | return |
| 117 | |
| 118 | self.on_finalized_text(text) |
| 119 | |
| 120 | def end(self): |
| 121 | """Flushes any remaining cache and prints a newline to stdout.""" |
no test coverage detected