






'''simple docstring'''




import re
import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin

class    _UpperCAmelCase			(     snake_case_						):




   """simple docstring"""

   snake_case    				=						['''image_processor''', '''tokenizer''']
   snake_case    				=						'''AutoImageProcessor'''
   snake_case    				=						'''AutoTokenizer'''

   def __init__(		self :	Union[str, Any]      ,      __UpperCAmelCase :	Union[str, Any]=None      ,      __UpperCAmelCase :	str=None      ,      **__UpperCAmelCase :	Union[str, Any]		):





         '''simple docstring'''





         _A			     =	None
         if "feature_extractor" in kwargs:
               warnings.warn(
                   "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
                   " instead."      ,      __UpperCAmelCase      ,      )
               _A			     =	kwargs.pop("feature_extractor"		)

         _A			     =	image_processor if image_processor is not None else feature_extractor
         if image_processor is None:
               raise ValueError("You need to specify an `image_processor`."		)
         if tokenizer is None:
               raise ValueError("You need to specify a `tokenizer`."		)

         super().__init__(__UpperCAmelCase      ,      __UpperCAmelCase		)
         _A			     =	self.image_processor
         _A			     =	False

   def __call__(		self :	int      ,      *__UpperCAmelCase :	Dict      ,      **__UpperCAmelCase :	Dict		):





         '''simple docstring'''





         if self._in_target_context_manager:
               return self.current_processor(*__UpperCAmelCase      ,      **__UpperCAmelCase		)

         _A			     =	kwargs.pop("images"      ,      __UpperCAmelCase		)
         _A			     =	kwargs.pop("text"      ,      __UpperCAmelCase		)
         if len(__UpperCAmelCase		) > 0:
               _A			     =	args[0]
               _A			     =	args[1:]

         if images is None and text is None:
               raise ValueError("You need to specify either an `images` or `text` input to process."		)

         if images is not None:
               _A			     =	self.image_processor(__UpperCAmelCase      ,      *__UpperCAmelCase      ,      **__UpperCAmelCase		)
         if text is not None:
               _A			     =	self.tokenizer(__UpperCAmelCase      ,      **__UpperCAmelCase		)

         if text is None:
               return inputs
         elif images is None:
               return encodings
         else:
               _A			     =	encodings["input_ids"]
               return inputs

   def 						lowerCAmelCase (		self :	List[str]      ,      *__UpperCAmelCase :	Tuple      ,      **__UpperCAmelCase :	Any		):





         '''simple docstring'''





         return self.tokenizer.batch_decode(*__UpperCAmelCase      ,      **__UpperCAmelCase		)

   def 						lowerCAmelCase (		self :	Tuple      ,      *__UpperCAmelCase :	str      ,      **__UpperCAmelCase :	Dict		):





         '''simple docstring'''





         return self.tokenizer.decode(*__UpperCAmelCase      ,      **__UpperCAmelCase		)

   @contextmanager
   def 						lowerCAmelCase (		self :	Optional[int]		):





         '''simple docstring'''





         warnings.warn(
             "`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your "
             "labels by using the argument `text` of the regular `__call__` method (either in the same call as "
             "your images inputs, or in a separate call."		)
         _A			     =	True
         _A			     =	self.tokenizer
         yield
         _A			     =	self.image_processor
         _A			     =	False

   def 						lowerCAmelCase (		self :	Optional[int]      ,      __UpperCAmelCase :	List[Any]      ,      __UpperCAmelCase :	Optional[int]=False      ,      __UpperCAmelCase :	str=None		):





         '''simple docstring'''





         if added_vocab is None:
               _A			     =	self.tokenizer.get_added_vocab()

         _A			     =	{}

         while tokens:
               _A			     =	re.search(R"<s_(.*?)>"      ,      __UpperCAmelCase      ,      re.IGNORECASE		)
               if start_token is None:
                     break
               _A			     =	start_token.group(1		)
               _A			     =	re.search(Rf'''</s_{key}>'''      ,      __UpperCAmelCase      ,      re.IGNORECASE		)
               _A			     =	start_token.group()
               if end_token is None:
                     _A			     =	tokens.replace(__UpperCAmelCase      ,      ""		)
               else:
                     _A			     =	end_token.group()
                     _A			     =	re.escape(__UpperCAmelCase		)
                     _A			     =	re.escape(__UpperCAmelCase		)
                     _A			     =	re.search(f'''{start_token_escaped}(.*?){end_token_escaped}'''      ,      __UpperCAmelCase      ,      re.IGNORECASE		)
                     if content is not None:
                           _A			     =	content.group(1		).strip()
                           if r"<s_" in content and r"</s_" in content:  # non-leaf node
                                 _A			     =	self.tokenajson(__UpperCAmelCase      ,      is_inner_value=__UpperCAmelCase      ,      added_vocab=__UpperCAmelCase		)
                                 if value:
                                       if len(__UpperCAmelCase		) == 1:
                                             _A			     =	value[0]
                                       _A			     =	value
                           else:  # leaf nodes
                                 _A			     =	[]
                                 for leaf in content.split(R"<sep/>"		):
                                       _A			     =	leaf.strip()
                                       if leaf in added_vocab and leaf[0] == "<" and leaf[-2:] == "/>":
                                             _A			     =	leaf[1:-2]  # for categorical special tokens
                                       output[key].append(__UpperCAmelCase		)
                                 if len(output[key]		) == 1:
                                       _A			     =	output[key][0]

                     _A			     =	tokens[tokens.find(__UpperCAmelCase		) + len(__UpperCAmelCase		) :].strip()
                     if tokens[:6] == r"<sep/>":  # non-leaf nodes
                           return [output] + self.tokenajson(tokens[6:]      ,      is_inner_value=__UpperCAmelCase      ,      added_vocab=__UpperCAmelCase		)

         if len(__UpperCAmelCase		):
               return [output] if is_inner_value else output
         else:
               return [] if is_inner_value else {"text_sequence": tokens}

   @property
   def 						lowerCAmelCase (		self :	Tuple		):





         '''simple docstring'''





         warnings.warn(
             "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."      ,      __UpperCAmelCase      ,      )
         return self.image_processor_class

   @property
   def 						lowerCAmelCase (		self :	List[str]		):





         '''simple docstring'''





         warnings.warn(
             "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."      ,      __UpperCAmelCase      ,      )
         return self.image_processor












'''simple docstring'''







def  _lowerCamelCase				( lowercase						:      bytes						)	->	str:
				return "".join([hex(lowercase						)[2:].zfill(2						).upper() for byte in list(lowercase						)]						)






def  _lowerCamelCase				( lowercase						:      str						)	->	bytes:
				# Check data validity, following RFC3548
				# https://www.ietf.org/rfc/rfc3548.txt
				if (len(lowercase						) % 2) != 0:
								raise ValueError(
								    "Base16 encoded data is invalid:\nData does not have an even number of hex digits."						)
				# Check the character set - the standard base16 alphabet
				# is uppercase according to RFC3548 section 6
				if not set(lowercase						) <= set("0123456789ABCDEF"						):
								raise ValueError(
								    "Base16 encoded data is invalid:\nData is not uppercase hex or it contains invalid characters."						)
				# For every two hexadecimal digits (= a byte), turn it into an integer.
				# Then, string the result together into bytes, and return it.
				return bytes(int(data[i] + data[i + 1]    ,							16						) for i in range(0    ,							len(lowercase						)    ,							2						)						)


if __name__ == "__main__":
					import doctest

					doctest.testmod()
