


from queue import Queue
from typing import TYPE_CHECKING, Optional


if TYPE_CHECKING:
   from ..models.auto import AutoTokenizer







class lowerCAmelCase_						:

 '''simple docstring'''

 def 						UpperCamelCase__					(     self		,				_UpperCAmelCase ):
       raise NotImplementedError()





 def 						UpperCamelCase__					(     self ):
       raise NotImplementedError()







class lowerCAmelCase_						(			lowerCamelCase__	):

 '''simple docstring'''

 def __init__(     self		,				_UpperCAmelCase		,				_UpperCAmelCase = False		,				**_UpperCAmelCase ):
       snake_case_	=				tokenizer
       snake_case_	=				skip_prompt
       snake_case_	=				decode_kwargs

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



 def 						UpperCamelCase__					(     self		,				_UpperCAmelCase ):
       if len(value.shape ) > 1 and value.shape[0] > 1:
             raise ValueError('''TextStreamer only supports batch size 1''' )
       elif len(value.shape ) > 1:
             snake_case_	=				value[0]

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

       # Add the new token to the cache and decodes the entire thing.
       self.token_cache.extend(value.tolist() )
       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''' ):
             snake_case_	=				text[self.print_len :]
             snake_case_	=				[]
             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] ) ):
             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:
             snake_case_	=				text[self.print_len : text.rfind(''' ''' ) + 1]
             self.print_len += len(_UpperCAmelCase )

       self.on_finalized_text(_UpperCAmelCase )



 def 						UpperCamelCase__					(     self ):
       # Flush the cache, if it exists
       if len(self.token_cache ) > 0:
             snake_case_	=				self.tokenizer.decode(self.token_cache		,				**self.decode_kwargs )
             snake_case_	=				text[self.print_len :]
             snake_case_	=				[]
             snake_case_	=				0
       else:
             snake_case_	=				''''''

       snake_case_	=				True
       self.on_finalized_text(_UpperCAmelCase		,				stream_end=_UpperCAmelCase )



 def 						UpperCamelCase__					(     self		,				_UpperCAmelCase		,				_UpperCAmelCase = False ):
       print(_UpperCAmelCase		,				flush=_UpperCAmelCase		,				end='''''' if not stream_end else None )





 def 						UpperCamelCase__					(     self		,				_UpperCAmelCase ):
       # 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 >= 0x20000 and cp <= 0x2A6DF)  #
           or (cp >= 0x2A700 and cp <= 0x2B73F)  #
           or (cp >= 0x2B740 and cp <= 0x2B81F)  #
           or (cp >= 0x2B820 and cp <= 0x2CEAF)  #
           or (cp >= 0xF900 and cp <= 0xFAFF)
           or (cp >= 0x2F800 and cp <= 0x2FA1F)  #
       ):  #
             return True

       return False







class lowerCAmelCase_						(			lowerCamelCase__	):

 '''simple docstring'''

 def __init__(     self		,				_UpperCAmelCase		,				_UpperCAmelCase = False		,				_UpperCAmelCase = None		,				**_UpperCAmelCase ):
       super().__init__(_UpperCAmelCase		,				_UpperCAmelCase		,				**_UpperCAmelCase )
       snake_case_	=				Queue()
       snake_case_	=				None
       snake_case_	=				timeout



 def 						UpperCamelCase__					(     self		,				_UpperCAmelCase		,				_UpperCAmelCase = False ):
       self.text_queue.put(_UpperCAmelCase		,				timeout=self.timeout )
       if stream_end:
             self.text_queue.put(self.stop_signal		,				timeout=self.timeout )



 def __iter__(     self ):
       return self





 def 						UpperCamelCase__					(     self ):
       snake_case_	=				self.text_queue.get(timeout=self.timeout )
       if value == self.stop_signal:
             raise StopIteration()
       else:
             return value



import unittest

from transformers.utils.backbone_utils import (
    BackboneMixin,
    get_aligned_output_features_output_indices,
    verify_out_features_out_indices,
)







class lowerCAmelCase_						(			unittest.TestCase	):

 '''simple docstring'''

 def 						UpperCamelCase__					(     self ):
       snake_case_	=				['''a''', '''b''', '''c''']

       # Defaults to last layer if both are None
       snake_case_	,   snake_case_	=				get_aligned_output_features_output_indices(_UpperCAmelCase		,				_UpperCAmelCase		,				_UpperCAmelCase )
       self.assertEqual(_UpperCAmelCase		,				['''c'''] )
       self.assertEqual(_UpperCAmelCase		,				[2] )

       # Out indices set to match out features
       snake_case_	,   snake_case_	=				get_aligned_output_features_output_indices(['''a''', '''c''']		,				_UpperCAmelCase		,				_UpperCAmelCase )
       self.assertEqual(_UpperCAmelCase		,				['''a''', '''c'''] )
       self.assertEqual(_UpperCAmelCase		,				[0, 2] )

       # Out features set to match out indices
       snake_case_	,   snake_case_	=				get_aligned_output_features_output_indices(_UpperCAmelCase		,				[0, 2]		,				_UpperCAmelCase )
       self.assertEqual(_UpperCAmelCase		,				['''a''', '''c'''] )
       self.assertEqual(_UpperCAmelCase		,				[0, 2] )

       # Out features selected from negative indices
       snake_case_	,   snake_case_	=				get_aligned_output_features_output_indices(_UpperCAmelCase		,				[-3, -1]		,				_UpperCAmelCase )
       self.assertEqual(_UpperCAmelCase		,				['''a''', '''c'''] )
       self.assertEqual(_UpperCAmelCase		,				[-3, -1] )



 def 						UpperCamelCase__					(     self ):
       # Stage names must be set
       with self.assertRaises(_UpperCAmelCase ):
             verify_out_features_out_indices(['''a''', '''b''']		,				(0, 1)		,				_UpperCAmelCase )

       # Out features must be a list
       with self.assertRaises(_UpperCAmelCase ):
             verify_out_features_out_indices(('''a''', '''b''')		,				(0, 1)		,				['''a''', '''b'''] )

       # Out features must be a subset of stage names
       with self.assertRaises(_UpperCAmelCase ):
             verify_out_features_out_indices(['''a''', '''b''']		,				(0, 1)		,				['''a'''] )

       # Out indices must be a list or tuple
       with self.assertRaises(_UpperCAmelCase ):
             verify_out_features_out_indices(_UpperCAmelCase		,				0		,				['''a''', '''b'''] )

       # Out indices must be a subset of stage names
       with self.assertRaises(_UpperCAmelCase ):
             verify_out_features_out_indices(_UpperCAmelCase		,				(0, 1)		,				['''a'''] )

       # Out features and out indices must be the same length
       with self.assertRaises(_UpperCAmelCase ):
             verify_out_features_out_indices(['''a''', '''b''']		,				(0,)		,				['''a''', '''b''', '''c'''] )

       # Out features should match out indices
       with self.assertRaises(_UpperCAmelCase ):
             verify_out_features_out_indices(['''a''', '''b''']		,				(0, 2)		,				['''a''', '''b''', '''c'''] )

       # Out features and out indices should be in order
       with self.assertRaises(_UpperCAmelCase ):
             verify_out_features_out_indices(['''b''', '''a''']		,				(0, 1)		,				['''a''', '''b'''] )

       # Check passes with valid inputs
       verify_out_features_out_indices(['''a''', '''b''', '''d''']		,				(0, 1, -1)		,				['''a''', '''b''', '''c''', '''d'''] )





 def 						UpperCamelCase__					(     self ):
       snake_case_	=				BackboneMixin()

       snake_case_	=				['''a''', '''b''', '''c''']
       snake_case_	=				['''a''', '''c''']
       snake_case_	=				[0, 2]

       # Check that the output features and indices are set correctly
       self.assertEqual(backbone.out_features		,				['''a''', '''c'''] )
       self.assertEqual(backbone.out_indices		,				[0, 2] )

       # Check out features and indices are updated correctly
       snake_case_	=				['''a''', '''b''']
       self.assertEqual(backbone.out_features		,				['''a''', '''b'''] )
       self.assertEqual(backbone.out_indices		,				[0, 1] )

       snake_case_	=				[-3, -1]
       self.assertEqual(backbone.out_features		,				['''a''', '''c'''] )
       self.assertEqual(backbone.out_indices		,				[-3, -1] )