
"""simple docstring"""




from queue import Queue
from typing import TYPE_CHECKING, Optional


if TYPE_CHECKING:
  from ..models.auto import AutoTokenizer







class   _lowercase						:


     '''simple docstring'''





     def  lowerCAmelCase__       (      self ,			__UpperCamelCase  )->					Optional[Any]:
          raise NotImplementedError()



     def  lowerCAmelCase__       (      self  )->					List[str]:
          raise NotImplementedError()







class   _lowercase						(       lowerCAmelCase_				):


     '''simple docstring'''





     def __init__(      self ,			__UpperCamelCase ,			__UpperCamelCase = False ,			**__UpperCamelCase  )->					List[str]:
          UpperCAmelCase__							: str 							=						tokenizer
          UpperCAmelCase__							: Optional[int] 							=						skip_prompt
          UpperCAmelCase__							: Optional[Any] 							=						decode_kwargs

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



     def  lowerCAmelCase__       (      self ,			__UpperCamelCase  )->					List[str]:
          if len(value.shape  ) > 1 and value.shape[0] > 1:
               raise ValueError("TextStreamer only supports batch size 1"  )
          elif len(value.shape  ) > 1:
               UpperCAmelCase__							: Optional[int] 							=						value[0]

          if self.skip_prompt and self.next_tokens_are_prompt:
               UpperCAmelCase__							: Tuple 							=						False
               return

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

          # After the symbol for a new line, we flush the cache.
          if text.endswith("\n"  ):
               UpperCAmelCase__							: int 							=						text[self.print_len :]
               UpperCAmelCase__							: Tuple 							=						[]
               UpperCAmelCase__							: Any 							=						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]  )  ):
               UpperCAmelCase__							: Optional[int] 							=						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:
               UpperCAmelCase__							: Dict 							=						text[self.print_len : text.rfind(" "  ) + 1]
               self.print_len += len(__UpperCamelCase  )

          self.on_finalized_text(__UpperCamelCase  )



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

          UpperCAmelCase__							: Optional[Any] 							=						True
          self.on_finalized_text(__UpperCamelCase ,			stream_end=__UpperCamelCase  )



     def  lowerCAmelCase__       (      self ,			__UpperCamelCase ,			__UpperCamelCase = False  )->					Union[str, Any]:
          print(__UpperCamelCase ,			flush=__UpperCamelCase ,			end="" if not stream_end else None  )



     def  lowerCAmelCase__       (      self ,			__UpperCamelCase  )->					int:
          # 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 >= 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   _lowercase						(       lowerCAmelCase_				):


     '''simple docstring'''





     def __init__(      self ,			__UpperCamelCase ,			__UpperCamelCase = False ,			__UpperCamelCase = None ,			**__UpperCamelCase  )->					List[str]:
          super().__init__(__UpperCamelCase ,			__UpperCamelCase ,			**__UpperCamelCase  )
          UpperCAmelCase__							: List[str] 							=						Queue()
          UpperCAmelCase__							: Dict 							=						None
          UpperCAmelCase__							: Tuple 							=						timeout



     def  lowerCAmelCase__       (      self ,			__UpperCamelCase ,			__UpperCamelCase = False  )->					Any:
          self.text_queue.put(__UpperCamelCase ,			timeout=self.timeout  )
          if stream_end:
               self.text_queue.put(self.stop_signal ,			timeout=self.timeout  )



     def __iter__(      self  )->					Dict:
          return self



     def  lowerCAmelCase__       (      self  )->					Optional[int]:
          UpperCAmelCase__							: List[str] 							=						self.text_queue.get(timeout=self.timeout  )
          if value == self.stop_signal:
               raise StopIteration()
          else:
               return value





"""simple docstring"""




def 							a__ (				lowerCAmelCase	:       int		):






     '''simple docstring'''
     if a < 0:
          raise ValueError("Input value must be a positive integer"		)
     elif isinstance(lowerCAmelCase       ,    lowerCAmelCase		):
          raise TypeError("Input value must be a 'int' type"		)
     return bin(lowerCAmelCase		).count("1"		)


if __name__ == "__main__":
    import doctest

    doctest.testmod()



