



'''simple docstring'''




from queue import Queue
from typing import TYPE_CHECKING, Optional


if TYPE_CHECKING:
						from ..models.auto import AutoTokenizer






class 		_UpperCamelCase						:


			'''simple docstring'''

			def UpperCamelCase__      (							self  :				Union[str, Any]							,  lowerCAmelCase__  :				Any		):





					"""simple docstring"""






					raise NotImplementedError()



			def UpperCamelCase__      (							self  :				int		):





					"""simple docstring"""






					raise NotImplementedError()






class 		_UpperCamelCase						(							lowerCamelCase__					):


			'''simple docstring'''

			def __init__(							self  :				int							,  lowerCAmelCase__  :				"AutoTokenizer"							,  lowerCAmelCase__  :				bool = False							,  **lowerCAmelCase__  :				Tuple		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Dict					 =	tokenizer
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	skip_prompt
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	decode_kwargs

					# variables used in the streaming process
					__SCREAMING_SNAKE_CASE :	List[str]					 =	[]
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	0
					__SCREAMING_SNAKE_CASE :	str					 =	True

			def UpperCamelCase__      (							self  :				str							,  lowerCAmelCase__  :				str		):





					"""simple docstring"""






					if len(value.shape		) > 1 and value.shape[0] > 1:
							raise ValueError("""TextStreamer only supports batch size 1"""		)
					elif len(value.shape		) > 1:
							__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	value[0]

					if self.skip_prompt and self.next_tokens_are_prompt:
							__SCREAMING_SNAKE_CASE :	Dict					 =	False
							return

					# Add the new token to the cache and decodes the entire thing.
					self.token_cache.extend(value.tolist()		)
					__SCREAMING_SNAKE_CASE :	List[str]					 =	self.tokenizer.decode(self.token_cache							,  **self.decode_kwargs		)

					# After the symbol for a new line, we flush the cache.
					if text.endswith("""\n"""		):
							__SCREAMING_SNAKE_CASE :	Any					 =	text[self.print_len :]
							__SCREAMING_SNAKE_CASE :	Tuple					 =	[]
							__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	0
					# If the last token is a CJK character, we print the characters.
					elif len(lowerCAmelCase__		) > 0 and self._is_chinese_char(ord(text[-1]		)		):
							__SCREAMING_SNAKE_CASE :	Tuple					 =	text[self.print_len :]
							self.print_len += len(lowerCAmelCase__		)
					# Otherwise, prints until the last space char (simple heuristic to avoid printing incomplete words,
					# which may change with the subsequent token -- there are probably smarter ways to do this!)
					else:
							__SCREAMING_SNAKE_CASE :	List[str]					 =	text[self.print_len : text.rfind(""" """		) + 1]
							self.print_len += len(lowerCAmelCase__		)

					self.on_finalized_text(lowerCAmelCase__		)

			def UpperCamelCase__      (							self  :				str		):





					"""simple docstring"""






					if len(self.token_cache		) > 0:
							__SCREAMING_SNAKE_CASE :	Tuple					 =	self.tokenizer.decode(self.token_cache							,  **self.decode_kwargs		)
							__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	text[self.print_len :]
							__SCREAMING_SNAKE_CASE :	int					 =	[]
							__SCREAMING_SNAKE_CASE :	List[Any]					 =	0
					else:
							__SCREAMING_SNAKE_CASE :	Tuple					 =	""""""

					__SCREAMING_SNAKE_CASE :	Dict					 =	True
					self.on_finalized_text(lowerCAmelCase__							,  stream_end=lowerCAmelCase__		)

			def UpperCamelCase__      (							self  :				Any							,  lowerCAmelCase__  :				str							,  lowerCAmelCase__  :				bool = False		):





					"""simple docstring"""






					print(lowerCAmelCase__							,  flush=lowerCAmelCase__							,  end="""""" if not stream_end else None		)



			def UpperCamelCase__      (							self  :				Optional[int]							,  lowerCAmelCase__  :				List[Any]		):





					"""simple docstring"""






					if (
					    (cp >= 0X4e00 and cp <= 0X9fff)
					    or (cp >= 0X3400 and cp <= 0X4dbf)  #
					    or (cp >= 0X2_0000 and cp <= 0X2_a6df)  #
					    or (cp >= 0X2_a700 and cp <= 0X2_b73f)  #
					    or (cp >= 0X2_b740 and cp <= 0X2_b81f)  #
					    or (cp >= 0X2_b820 and cp <= 0X2_ceaf)  #
					    or (cp >= 0Xf900 and cp <= 0Xfaff)
					    or (cp >= 0X2_f800 and cp <= 0X2_fa1f)  #
					):  #
							return True

					return False






class 		_UpperCamelCase						(							lowerCamelCase__					):


			'''simple docstring'''

			def __init__(							self  :				Dict							,  lowerCAmelCase__  :				"AutoTokenizer"							,  lowerCAmelCase__  :				bool = False							,  lowerCAmelCase__  :				Optional[float] = None							,  **lowerCAmelCase__  :				Optional[int]		):





					"""simple docstring"""






					super().__init__(lowerCAmelCase__							,  lowerCAmelCase__							,  **lowerCAmelCase__		)
					__SCREAMING_SNAKE_CASE :	str					 =	Queue()
					__SCREAMING_SNAKE_CASE :	List[str]					 =	None
					__SCREAMING_SNAKE_CASE :	List[str]					 =	timeout

			def UpperCamelCase__      (							self  :				int							,  lowerCAmelCase__  :				str							,  lowerCAmelCase__  :				bool = False		):





					"""simple docstring"""






					self.text_queue.put(lowerCAmelCase__							,  timeout=self.timeout		)
					if stream_end:
							self.text_queue.put(self.stop_signal							,  timeout=self.timeout		)

			def __iter__(							self  :				str		):





					"""simple docstring"""






					return self



			def UpperCamelCase__      (							self  :				Optional[Any]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Any					 =	self.text_queue.get(timeout=self.timeout		)
					if value == self.stop_signal:
							raise StopIteration()
					else:
							return value




'''simple docstring'''




from ..utils import DummyObject, requires_backends






class 		_UpperCamelCase						(							metaclass=lowerCamelCase__					):


			'''simple docstring'''

			_A   :	Union[str, Any]    					=	['''torch''', '''transformers''', '''onnx''']

			def __init__(							self  :				Optional[int]							,  *lowerCAmelCase__  :				Optional[Any]							,  **lowerCAmelCase__  :				int		):





					"""simple docstring"""






					requires_backends(self							,  ["""torch""", """transformers""", """onnx"""]		)

			@classmethod
			def UpperCamelCase__      (							cls  :				Tuple							,  *lowerCAmelCase__  :				Union[str, Any]							,  **lowerCAmelCase__  :				Optional[int]		):





					"""simple docstring"""






					requires_backends(cls							,  ["""torch""", """transformers""", """onnx"""]		)



			@classmethod
			def UpperCamelCase__      (							cls  :				str							,  *lowerCAmelCase__  :				int							,  **lowerCAmelCase__  :				Optional[Any]		):





					"""simple docstring"""






					requires_backends(cls							,  ["""torch""", """transformers""", """onnx"""]		)






class 		_UpperCamelCase						(							metaclass=lowerCamelCase__					):


			'''simple docstring'''

			_A   :	Dict    					=	['''torch''', '''transformers''', '''onnx''']

			def __init__(							self  :				str							,  *lowerCAmelCase__  :				str							,  **lowerCAmelCase__  :				List[str]		):





					"""simple docstring"""






					requires_backends(self							,  ["""torch""", """transformers""", """onnx"""]		)

			@classmethod
			def UpperCamelCase__      (							cls  :				Optional[Any]							,  *lowerCAmelCase__  :				Dict							,  **lowerCAmelCase__  :				Dict		):





					"""simple docstring"""






					requires_backends(cls							,  ["""torch""", """transformers""", """onnx"""]		)



			@classmethod
			def UpperCamelCase__      (							cls  :				str							,  *lowerCAmelCase__  :				List[str]							,  **lowerCAmelCase__  :				str		):





					"""simple docstring"""






					requires_backends(cls							,  ["""torch""", """transformers""", """onnx"""]		)






class 		_UpperCamelCase						(							metaclass=lowerCamelCase__					):


			'''simple docstring'''

			_A   :	Tuple    					=	['''torch''', '''transformers''', '''onnx''']

			def __init__(							self  :				str							,  *lowerCAmelCase__  :				List[str]							,  **lowerCAmelCase__  :				Dict		):





					"""simple docstring"""






					requires_backends(self							,  ["""torch""", """transformers""", """onnx"""]		)

			@classmethod
			def UpperCamelCase__      (							cls  :				Any							,  *lowerCAmelCase__  :				Tuple							,  **lowerCAmelCase__  :				Any		):





					"""simple docstring"""






					requires_backends(cls							,  ["""torch""", """transformers""", """onnx"""]		)



			@classmethod
			def UpperCamelCase__      (							cls  :				List[str]							,  *lowerCAmelCase__  :				Union[str, Any]							,  **lowerCAmelCase__  :				Dict		):





					"""simple docstring"""






					requires_backends(cls							,  ["""torch""", """transformers""", """onnx"""]		)






class 		_UpperCamelCase						(							metaclass=lowerCamelCase__					):


			'''simple docstring'''

			_A   :	Optional[int]    					=	['''torch''', '''transformers''', '''onnx''']

			def __init__(							self  :				Optional[int]							,  *lowerCAmelCase__  :				Dict							,  **lowerCAmelCase__  :				List[Any]		):





					"""simple docstring"""






					requires_backends(self							,  ["""torch""", """transformers""", """onnx"""]		)

			@classmethod
			def UpperCamelCase__      (							cls  :				Any							,  *lowerCAmelCase__  :				List[Any]							,  **lowerCAmelCase__  :				List[str]		):





					"""simple docstring"""






					requires_backends(cls							,  ["""torch""", """transformers""", """onnx"""]		)



			@classmethod
			def UpperCamelCase__      (							cls  :				List[str]							,  *lowerCAmelCase__  :				str							,  **lowerCAmelCase__  :				Optional[int]		):





					"""simple docstring"""






					requires_backends(cls							,  ["""torch""", """transformers""", """onnx"""]		)






class 		_UpperCamelCase						(							metaclass=lowerCamelCase__					):


			'''simple docstring'''

			_A   :	Union[str, Any]    					=	['''torch''', '''transformers''', '''onnx''']

			def __init__(							self  :				Optional[int]							,  *lowerCAmelCase__  :				int							,  **lowerCAmelCase__  :				List[str]		):





					"""simple docstring"""






					requires_backends(self							,  ["""torch""", """transformers""", """onnx"""]		)

			@classmethod
			def UpperCamelCase__      (							cls  :				List[str]							,  *lowerCAmelCase__  :				int							,  **lowerCAmelCase__  :				int		):





					"""simple docstring"""






					requires_backends(cls							,  ["""torch""", """transformers""", """onnx"""]		)



			@classmethod
			def UpperCamelCase__      (							cls  :				str							,  *lowerCAmelCase__  :				int							,  **lowerCAmelCase__  :				List[Any]		):





					"""simple docstring"""






					requires_backends(cls							,  ["""torch""", """transformers""", """onnx"""]		)






class 		_UpperCamelCase						(							metaclass=lowerCamelCase__					):


			'''simple docstring'''

			_A   :	Optional[Any]    					=	['''torch''', '''transformers''', '''onnx''']

			def __init__(							self  :				Optional[int]							,  *lowerCAmelCase__  :				Tuple							,  **lowerCAmelCase__  :				List[str]		):





					"""simple docstring"""






					requires_backends(self							,  ["""torch""", """transformers""", """onnx"""]		)

			@classmethod
			def UpperCamelCase__      (							cls  :				Tuple							,  *lowerCAmelCase__  :				Dict							,  **lowerCAmelCase__  :				List[Any]		):





					"""simple docstring"""






					requires_backends(cls							,  ["""torch""", """transformers""", """onnx"""]		)



			@classmethod
			def UpperCamelCase__      (							cls  :				List[str]							,  *lowerCAmelCase__  :				Dict							,  **lowerCAmelCase__  :				Tuple		):





					"""simple docstring"""






					requires_backends(cls							,  ["""torch""", """transformers""", """onnx"""]		)