



"""simple docstring"""






from importlib import import_module

from .logging import get_logger


lowerCAmelCase__              =							get_logger(__name__)






class 			__snake_case			:




  def __init__(					self	:		str		,   __lowerCAmelCase	:		Optional[int]		,   __lowerCAmelCase	:		Optional[Any]=None						):






         """simple docstring"""






         _lowerCamelCase		:							List[str]            =					attrs or []
         if module is not None:
                for key in module.__dict__:
                       if key in attrs or not key.startswith('''__'''						):
                              setattr(self		,   __lowerCAmelCase		,   getattr(__lowerCAmelCase		,   __lowerCAmelCase						)						)
         _lowerCamelCase		:							List[str]            =					module._original_module if isinstance(__lowerCAmelCase		,   _PatchedModuleObj						) else module






class 			__snake_case			:
  snake_case__							:  Optional[Any]         =		[]




  def __init__(					self	:		str		,   __lowerCAmelCase	:		Optional[Any]		,   __lowerCAmelCase	:		str		,   __lowerCAmelCase	:		List[Any]		,   __lowerCAmelCase	:		Optional[int]=None						):






         """simple docstring"""






         _lowerCamelCase		:							Union[str, Any]            =					obj
         _lowerCamelCase		:							Tuple            =					target
         _lowerCamelCase		:							Optional[Any]            =					new
         _lowerCamelCase		:							Optional[Any]            =					target.split('''.'''						)[0]
         _lowerCamelCase		:							List[str]            =					{}
         _lowerCamelCase		:							Optional[Any]            =					attrs or []




  def __enter__(					self	:		Optional[Any]						):






         """simple docstring"""






         *_lowerCamelCase     ,       _lowerCamelCase		:							str            =					self.target.split('''.'''						)

         # Patch modules:
         # it's used to patch attributes of submodules like "os.path.join";
         # in this case we need to patch "os" and "os.path"

         for i in range(len(__lowerCAmelCase						)						):
                try:
                       _lowerCamelCase		:							Tuple            =					import_module('''.'''.join(submodules[: i + 1]						)						)
                except ModuleNotFoundError:
                       continue
                # We iterate over all the globals in self.obj in case we find "os" or "os.path"
                for attr in self.obj.__dir__():
                       _lowerCamelCase		:							Optional[int]            =					getattr(self.obj		,   __lowerCAmelCase						)
                       # We don't check for the name of the global, but rather if its value *is* "os" or "os.path".
                       # This allows to patch renamed modules like "from os import path as ospath".
                       if obj_attr is submodule or (
                           (isinstance(__lowerCAmelCase		,   _PatchedModuleObj						) and obj_attr._original_module is submodule)
                       ):
                              _lowerCamelCase		:							Optional[Any]            =					obj_attr
                              # patch at top level
                              setattr(self.obj		,   __lowerCAmelCase		,   _PatchedModuleObj(__lowerCAmelCase		,   attrs=self.attrs						)						)
                              _lowerCamelCase		:							Tuple            =					getattr(self.obj		,   __lowerCAmelCase						)
                              # construct lower levels patches
                              for key in submodules[i + 1 :]:
                                     setattr(__lowerCAmelCase		,   __lowerCAmelCase		,   _PatchedModuleObj(getattr(__lowerCAmelCase		,   __lowerCAmelCase		,   __lowerCAmelCase						)		,   attrs=self.attrs						)						)
                                     _lowerCamelCase		:							List[Any]            =					getattr(__lowerCAmelCase		,   __lowerCAmelCase						)
                              # finally set the target attribute
                              setattr(__lowerCAmelCase		,   __lowerCAmelCase		,   self.new						)

        # Patch attribute itself:
        # it's used for builtins like "open",
        # and also to patch "os.path.join" we may also need to patch "join"
        # itself if it was imported as "from os.path import join".

         if submodules:  # if it's an attribute of a submodule like "os.path.join"
                try:
                       _lowerCamelCase		:							List[str]            =					getattr(import_module('''.'''.join(__lowerCAmelCase						)						)		,   __lowerCAmelCase						)
                except (AttributeError, ModuleNotFoundError):
                       return
                # We iterate over all the globals in self.obj in case we find "os.path.join"
                for attr in self.obj.__dir__():
                       # We don't check for the name of the global, but rather if its value *is* "os.path.join".
                       # This allows to patch renamed attributes like "from os.path import join as pjoin".
                       if getattr(self.obj		,   __lowerCAmelCase						) is attr_value:
                              _lowerCamelCase		:							Dict            =					getattr(self.obj		,   __lowerCAmelCase						)
                              setattr(self.obj		,   __lowerCAmelCase		,   self.new						)
         elif target_attr in globals()["__builtins__"]:  # if it'a s builtin like "open"
                _lowerCamelCase		:							str            =					globals()['''__builtins__'''][target_attr]
                setattr(self.obj		,   __lowerCAmelCase		,   self.new						)
         else:
                raise RuntimeError(f'''Tried to patch attribute {target_attr} instead of a submodule.'''						)




  def __exit__(					self	:		Optional[int]		,   *__lowerCAmelCase	:		Dict						):






         """simple docstring"""






         for attr in list(self.original						):
                setattr(self.obj		,   __lowerCAmelCase		,   self.original.pop(__lowerCAmelCase						)						)




  def 		SCREAMING_SNAKE_CASE							(					self	:		List[str]						):






         """simple docstring"""






         self.__enter__()
         self._active_patches.append(self						)




  def 		SCREAMING_SNAKE_CASE							(					self	:		int						):






         """simple docstring"""






         try:
                self._active_patches.remove(self						)
         except ValueError:
                # If the patch hasn't been started this will fail
                return None

         return self.__exit__()








'''simple docstring'''



import warnings

from transformers import AutoTokenizer
from transformers.utils import is_torch_available
from transformers.utils.generic import ExplicitEnum

from ...processing_utils import ProcessorMixin


if is_torch_available():
      import torch






class 							_a				(			SCREAMING_SNAKE_CASE ):





       '''simple docstring'''






       A						:				Dict        =						'''char'''
       A						:				Any        =						'''bpe'''
       A						:				Dict        =						'''wp'''

UpperCamelCase_    	=				(DecodeType.CHARACTER, DecodeType.BPE, DecodeType.WORDPIECE)






class 							_a				(			SCREAMING_SNAKE_CASE ):





       '''simple docstring'''






       A						:				List[Any]        =						['''image_processor''', '''char_tokenizer''']
       A						:				int        =						'''ViTImageProcessor'''
       A						:				List[str]        =						'''MgpstrTokenizer'''







       def __init__(      self,							A=None,							A=None,							**A  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						None
             if "feature_extractor" in kwargs:
                   warnings.warn(
                       'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
                       ' instead.',							A,							)
                   SCREAMING_SNAKE_CASE   :      str    					=						kwargs.pop('feature_extractor'  )

             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						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`.'  )

             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						tokenizer
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						AutoTokenizer.from_pretrained('gpt2'  )
             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						AutoTokenizer.from_pretrained('bert-base-uncased'  )

             super().__init__(A,							A  )







       def __call__(      self,							A=None,							A=None,							A=None,							**A  ):







             '''simple docstring'''







             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:
                   SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						self.image_processor(A,							return_tensors=A,							**A  )
             if text is not None:
                   SCREAMING_SNAKE_CASE   :      int    					=						self.char_tokenizer(A,							return_tensors=A,							**A  )

             if text is None:
                   return inputs
             elif images is None:
                   return encodings
             else:
                   SCREAMING_SNAKE_CASE   :      Any    					=						encodings['input_ids']
                   return inputs







       def        UpperCamelCase_							(      self,							A  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      int    					=						sequences
             SCREAMING_SNAKE_CASE   :      List[str]    					=						char_preds.size(0  )

             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      Dict    					=						self._decode_helper(A,							'char'  )
             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      int    					=						self._decode_helper(A,							'bpe'  )
             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						self._decode_helper(A,							'wp'  )

             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						[]
             SCREAMING_SNAKE_CASE   :      Tuple    					=						[]
             for i in range(A  ):
                   SCREAMING_SNAKE_CASE   :      str    					=						[char_scores[i], bpe_scores[i], wp_scores[i]]
                   SCREAMING_SNAKE_CASE   :      Dict    					=						[char_strs[i], bpe_strs[i], wp_strs[i]]
                   SCREAMING_SNAKE_CASE   :      List[str]    					=						scores.index(max(A  )  )
                   final_strs.append(strs[max_score_index]  )
                   final_scores.append(scores[max_score_index]  )

             SCREAMING_SNAKE_CASE   :      List[Any]    					=						{}
             SCREAMING_SNAKE_CASE   :      int    					=						final_strs
             SCREAMING_SNAKE_CASE   :      Any    					=						final_scores
             SCREAMING_SNAKE_CASE   :      Dict    					=						char_strs
             SCREAMING_SNAKE_CASE   :      Any    					=						bpe_strs
             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						wp_strs
             return out







       def        UpperCamelCase_							(      self,							A,							A  ):







             '''simple docstring'''







             if format == DecodeType.CHARACTER:
                   SCREAMING_SNAKE_CASE   :      List[Any]    					=						self.char_decode
                   SCREAMING_SNAKE_CASE   :      Optional[int]    					=						1
                   SCREAMING_SNAKE_CASE   :      str    					=						'[s]'
             elif format == DecodeType.BPE:
                   SCREAMING_SNAKE_CASE   :      str    					=						self.bpe_decode
                   SCREAMING_SNAKE_CASE   :      str    					=						2
                   SCREAMING_SNAKE_CASE   :      List[str]    					=						'#'
             elif format == DecodeType.WORDPIECE:
                   SCREAMING_SNAKE_CASE   :      Any    					=						self.wp_decode
                   SCREAMING_SNAKE_CASE   :      Tuple    					=						102
                   SCREAMING_SNAKE_CASE   :      List[Any]    					=						'[SEP]'
             else:
                   raise ValueError(F"Format {format} is not supported."  )

             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						[], []
             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						pred_logits.size(0  )
             SCREAMING_SNAKE_CASE   :      Any    					=						pred_logits.size(1  )
             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      List[Any]    					=						pred_logits.topk(1,							dim=-1,							largest=A,							sorted=A  )
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						preds_index.view(-1,							A  )[:, 1:]
             SCREAMING_SNAKE_CASE   :      List[Any]    					=						decoder(A  )
             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      Optional[int]    					=						torch.nn.functional.softmax(A,							dim=2  ).max(dim=2  )
             SCREAMING_SNAKE_CASE   :      Dict    					=						preds_max_prob[:, 1:]

             for index in range(A  ):
                   SCREAMING_SNAKE_CASE   :      Optional[int]    					=						preds_str[index].find(A  )
                   SCREAMING_SNAKE_CASE   :      List[Any]    					=						preds_str[index][:pred_eos]
                   SCREAMING_SNAKE_CASE   :      Dict    					=						preds_index[index].cpu().tolist()
                   SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						pred_index.index(A  ) if eos_token in pred_index else -1
                   SCREAMING_SNAKE_CASE   :      Optional[int]    					=						preds_max_prob[index][: pred_eos_index + 1]
                   SCREAMING_SNAKE_CASE   :      Optional[int]    					=						pred_max_prob.cumprod(dim=0  )[-1] if pred_max_prob.nelement() != 0 else 0.0
                   dec_strs.append(A  )
                   conf_scores.append(A  )

             return dec_strs, conf_scores







       def        UpperCamelCase_							(      self,							A  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						[seq.replace(' ',							''  ) for seq in self.char_tokenizer.batch_decode(A  )]
             return decode_strs







       def        UpperCamelCase_							(      self,							A  ):







             '''simple docstring'''







             return self.bpe_tokenizer.batch_decode(A  )







       def        UpperCamelCase_							(      self,							A  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      List[str]    					=						[seq.replace(' ',							''  ) for seq in self.wp_tokenizer.batch_decode(A  )]
             return decode_strs


