With reference to the TextIterStreamers in transformers, we have rewritten this class
| 62 | |
| 63 | |
| 64 | class TelechatIterTextStreamer: |
| 65 | """ |
| 66 | With reference to the TextIterStreamers in transformers, we have rewritten this class |
| 67 | """ |
| 68 | |
| 69 | def __init__( |
| 70 | self, tokenizer, history: History = None, skip_prompt: bool = False, timeout: Optional[float] = None, |
| 71 | **decode_kwargs |
| 72 | ): |
| 73 | |
| 74 | self.tokenizer = tokenizer |
| 75 | self.history = history |
| 76 | self.skip_prompt = skip_prompt |
| 77 | self.timeout = timeout |
| 78 | self.decode_kwargs = decode_kwargs |
| 79 | |
| 80 | self.text_queue = Queue() |
| 81 | self.token_cache = [] |
| 82 | self.cache_time = 0 |
| 83 | self.text_until = "" |
| 84 | self.stop_signal = None |
| 85 | self.next_tokens_are_prompt = True |
| 86 | |
| 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.""" |