


"""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      :    Optional[int]      ,      UpperCAmelCase__      :    List[Any]       )	->     List[str]:
						raise NotImplementedError()




	def   UpperCAmelCase_				(			self      :    Tuple       )	->     List[Any]:
						raise NotImplementedError()




class 			UpperCamelCase_     (			UpperCamelCase):

	"""simple docstring"""





	def __init__(			self      :    int      ,      UpperCAmelCase__      :    "AutoTokenizer"      ,      UpperCAmelCase__      :    bool = False      ,      **UpperCAmelCase__      :    int       )	->     Any:
						__SCREAMING_SNAKE_CASE						       =    tokenizer
						__SCREAMING_SNAKE_CASE						       =    skip_prompt
						__SCREAMING_SNAKE_CASE						       =    decode_kwargs

						# variables used in the streaming process
						__SCREAMING_SNAKE_CASE						       =    []
						__SCREAMING_SNAKE_CASE						       =    0
						__SCREAMING_SNAKE_CASE						       =    True




	def   UpperCAmelCase_				(			self      :    List[str]      ,      UpperCAmelCase__      :    Dict       )	->     Optional[Any]:
						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						       =    value[0]

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

						# Add the new token to the cache and decodes the entire thing.
						self.token_cache.extend(value.tolist()       )
						__SCREAMING_SNAKE_CASE						       =    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						       =    text[self.print_len :]
											__SCREAMING_SNAKE_CASE						       =    []
											__SCREAMING_SNAKE_CASE						       =    0
						# If the last token is a CJK character, we print the characters.
						elif len(UpperCAmelCase__       ) > 0 and self._is_chinese_char(ord(text[-1]       )       ):
											__SCREAMING_SNAKE_CASE						       =    text[self.print_len :]
											self.print_len += len(UpperCAmelCase__       )
						# 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						       =    text[self.print_len : text.rfind(" "       ) + 1]
											self.print_len += len(UpperCAmelCase__       )

						self.on_finalized_text(UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    List[Any]       )	->     Tuple:
						# Flush the cache, if it exists
						if len(self.token_cache       ) > 0:
											__SCREAMING_SNAKE_CASE						       =    self.tokenizer.decode(self.token_cache      ,      **self.decode_kwargs       )
											__SCREAMING_SNAKE_CASE						       =    text[self.print_len :]
											__SCREAMING_SNAKE_CASE						       =    []
											__SCREAMING_SNAKE_CASE						       =    0
						else:
											__SCREAMING_SNAKE_CASE						       =    ""

						__SCREAMING_SNAKE_CASE						       =    True
						self.on_finalized_text(UpperCAmelCase__      ,      stream_end=UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    Any      ,      UpperCAmelCase__      :    str      ,      UpperCAmelCase__      :    bool = False       )	->     List[str]:
						print(UpperCAmelCase__      ,      flush=UpperCAmelCase__      ,      end="" if not stream_end else None       )




	def   UpperCAmelCase_				(			self      :    List[Any]      ,      UpperCAmelCase__      :    Optional[int]       )	->     List[Any]:
						# This defines a "chinese character" as anything in the CJK Unicode block:
						#   https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_(Unicode_block)
						#
						# Note that the CJK Unicode block is NOT all Japanese and Korean characters,
						# despite its name. The modern Korean Hangul alphabet is a different block,
						# as is Japanese Hiragana and Katakana. Those alphabets are used to write
						# space-separated words, so they are not treated specially and handled
						# like the all of the other languages.
						if (
						    (cp >= 0x4_e_0_0 and cp <= 0x9_f_f_f)
						    or (cp >= 0x3_4_0_0 and cp <= 0x4_d_b_f)  #
						    or (cp >= 0x2_0_0_0_0 and cp <= 0x2_a_6_d_f)  #
						    or (cp >= 0x2_a_7_0_0 and cp <= 0x2_b_7_3_f)  #
						    or (cp >= 0x2_b_7_4_0 and cp <= 0x2_b_8_1_f)  #
						    or (cp >= 0x2_b_8_2_0 and cp <= 0x2_c_e_a_f)  #
						    or (cp >= 0xf_9_0_0 and cp <= 0xf_a_f_f)
						    or (cp >= 0x2_f_8_0_0 and cp <= 0x2_f_a_1_f)  #
						):  #
											return True

						return False




class 			UpperCamelCase_     (			UpperCamelCase):

	"""simple docstring"""





	def __init__(			self      :    Any      ,      UpperCAmelCase__      :    "AutoTokenizer"      ,      UpperCAmelCase__      :    bool = False      ,      UpperCAmelCase__      :    Optional[float] = None      ,      **UpperCAmelCase__      :    int       )	->     Optional[Any]:
						super().__init__(UpperCAmelCase__      ,      UpperCAmelCase__      ,      **UpperCAmelCase__       )
						__SCREAMING_SNAKE_CASE						       =    Queue()
						__SCREAMING_SNAKE_CASE						       =    None
						__SCREAMING_SNAKE_CASE						       =    timeout




	def   UpperCAmelCase_				(			self      :    Optional[Any]      ,      UpperCAmelCase__      :    str      ,      UpperCAmelCase__      :    bool = False       )	->     Tuple:
						self.text_queue.put(UpperCAmelCase__      ,      timeout=self.timeout       )
						if stream_end:
											self.text_queue.put(self.stop_signal      ,      timeout=self.timeout       )




	def __iter__(			self      :    Optional[Any]       )	->     Tuple:
						return self




	def   UpperCAmelCase_				(			self      :    str       )	->     Any:
						__SCREAMING_SNAKE_CASE						       =    self.text_queue.get(timeout=self.timeout       )
						if value == self.stop_signal:
											raise StopIteration()
						else:
											return value




"""simple docstring"""







from ...utils import (
    OptionalDependencyNotAvailable,
    is_torch_available,
    is_transformers_available,
    is_transformers_version,
)


try:
		if not (is_transformers_available() and is_torch_available()):
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		from ...utils.dummy_torch_and_transformers_objects import KandinskyPipeline, KandinskyPriorPipeline
else:
		from .pipeline_kandinsky import KandinskyPipeline
		from .pipeline_kandinsky_imgaimg import KandinskyImgaImgPipeline
		from .pipeline_kandinsky_inpaint import KandinskyInpaintPipeline
		from .pipeline_kandinsky_prior import KandinskyPriorPipeline, KandinskyPriorPipelineOutput
		from .text_encoder import MultilingualCLIP
