






import os
import warnings
from pathlib import Path
from typing import Optional, Union

from .dynamic_module_utils import custom_object_save
from .tokenization_utils_base import PreTrainedTokenizerBase
from .utils import PushToHubMixin, copy_func, direct_transformers_import, logging


lowerCamelCase			:							str       =logging.get_logger(__name__)

# Dynamically import the Transformers module to grab the attribute classes of the processor form their names.
lowerCamelCase			:							Dict       =direct_transformers_import(Path(__file__).parent)


lowerCamelCase			:							str       ={
    '''AutoTokenizer''': '''PreTrainedTokenizerBase''',
    '''AutoFeatureExtractor''': '''FeatureExtractionMixin''',
    '''AutoImageProcessor''': '''ImageProcessingMixin''',
}





class     __a						(					A__ ):
 _lowerCAmelCase : Any 				=				['''feature_extractor''', '''tokenizer''']
 # Names need to be attr_class for attr in attributes
 _lowerCAmelCase : Tuple 				=				None
 _lowerCAmelCase : Tuple 				=				None
 _lowerCAmelCase : List[str] 				=				None







 def __init__(	self      :     int							,     *SCREAMING_SNAKE_CASE      :     Tuple							,     **SCREAMING_SNAKE_CASE      :     Union[str, Any]		):


      '''simple docstring'''






      for key in kwargs:
           if key not in self.attributes:
                raise TypeError(F'Unexpected keyword argument {key}.'		)
      for arg, attribute_name in zip(SCREAMING_SNAKE_CASE							,     self.attributes		):
           if attribute_name in kwargs:
                raise TypeError(F'Got multiple values for argument {attribute_name}.'		)
           else:
                UpperCamelCase__    :     Tuple        =						arg

      if len(SCREAMING_SNAKE_CASE		) != len(self.attributes		):
           raise ValueError(
               F'This processor requires {len(self.attributes		)} arguments: {", ".join(self.attributes		)}. Got '
               F'{len(SCREAMING_SNAKE_CASE		)} arguments instead.'		)

      # Check each arg is of the proper class (this will also catch a user initializing in the wrong order)
      for attribute_name, arg in kwargs.items():
           UpperCamelCase__    :     List[Any]        =						getattr(self							,     F'{attribute_name}_class'		)
           # Nothing is ever going to be an instance of "AutoXxx", in that case we check the base class.
           UpperCamelCase__    :     str        =						AUTO_TO_BASE_CLASS_MAPPING.get(SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE		)
           if isinstance(SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE		):
                UpperCamelCase__    :     List[str]        =						tuple(getattr(SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE		) for n in class_name if n is not None		)
           else:
                UpperCamelCase__    :     Optional[Any]        =						getattr(SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE		)

           if not isinstance(SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE		):
                raise ValueError(
                    F'Received a {type(SCREAMING_SNAKE_CASE		).__name__} for argument {attribute_name}, but a {class_name} was expected.'		)

           setattr(self							,     SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE		)







 def __repr__(	self      :     List[str]		):


      '''simple docstring'''






      UpperCamelCase__    :     int        =						[F'- {name}: {repr(getattr(self							,     SCREAMING_SNAKE_CASE		)		)}' for name in self.attributes]
      UpperCamelCase__    :     Union[str, Any]        =						"\n".join(SCREAMING_SNAKE_CASE		)
      return F'{self.__class__.__name__}:\n{attributes_repr}'







 def       __lowercase      (	self      :     List[Any]							,     SCREAMING_SNAKE_CASE      :     List[Any]							,     SCREAMING_SNAKE_CASE      :     bool = False							,     **SCREAMING_SNAKE_CASE      :     Optional[int]		):


      '''simple docstring'''






      os.makedirs(SCREAMING_SNAKE_CASE							,     exist_ok=SCREAMING_SNAKE_CASE		)

      if push_to_hub:
           UpperCamelCase__    :     Dict        =						kwargs.pop("commit_message"							,     SCREAMING_SNAKE_CASE		)
           UpperCamelCase__    :     Any        =						kwargs.pop("repo_id"							,     save_directory.split(os.path.sep		)[-1]		)
           UpperCamelCase__    :     Tuple        =						self._create_repo(SCREAMING_SNAKE_CASE							,     **SCREAMING_SNAKE_CASE		)
           UpperCamelCase__    :     Dict        =						self._get_files_timestamps(SCREAMING_SNAKE_CASE		)
      # If we have a custom config, we copy the file defining it in the folder and set the attributes so it can be
      # loaded from the Hub.
      if self._auto_class is not None:
           UpperCamelCase__    :     str        =						[getattr(self							,     SCREAMING_SNAKE_CASE		) for attribute_name in self.attributes]
           UpperCamelCase__    :     Optional[Any]        =						[(a.init_kwargs if isinstance(SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE		) else a) for a in attrs]
           custom_object_save(self							,     SCREAMING_SNAKE_CASE							,     config=SCREAMING_SNAKE_CASE		)

      for attribute_name in self.attributes:
           UpperCamelCase__    :     Any        =						getattr(self							,     SCREAMING_SNAKE_CASE		)
           # Include the processor class in the attribute config so this processor can then be reloaded with the
           # `AutoProcessor` API.
           if hasattr(SCREAMING_SNAKE_CASE							,     "_set_processor_class"		):
                attribute._set_processor_class(self.__class__.__name__		)
           attribute.save_pretrained(SCREAMING_SNAKE_CASE		)

      if self._auto_class is not None:
           # We added an attribute to the init_kwargs of the tokenizers, which needs to be cleaned up.
           for attribute_name in self.attributes:
                UpperCamelCase__    :     Dict        =						getattr(self							,     SCREAMING_SNAKE_CASE		)
                if isinstance(SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE		):
                     del attribute.init_kwargs["auto_map"]

      if push_to_hub:
           self._upload_modified_files(
               SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE							,     commit_message=SCREAMING_SNAKE_CASE							,     token=kwargs.get("use_auth_token"		)							,     )







 @classmethod
 def       __lowercase      (	cls      :     int							,     SCREAMING_SNAKE_CASE      :     Union[str, os.PathLike]							,     SCREAMING_SNAKE_CASE      :     Optional[Union[str, os.PathLike]] = None							,     SCREAMING_SNAKE_CASE      :     bool = False							,     SCREAMING_SNAKE_CASE      :     bool = False							,     SCREAMING_SNAKE_CASE      :     Optional[Union[str, bool]] = None							,     SCREAMING_SNAKE_CASE      :     str = "main"							,     **SCREAMING_SNAKE_CASE      :     Union[str, Any]							,     ):


      '''simple docstring'''






      UpperCamelCase__    :     Any        =						cache_dir
      UpperCamelCase__    :     List[str]        =						force_download
      UpperCamelCase__    :     Tuple        =						local_files_only
      UpperCamelCase__    :     Tuple        =						revision

      UpperCamelCase__    :     str        =						kwargs.pop("use_auth_token"							,     SCREAMING_SNAKE_CASE		)
      if use_auth_token is not None:
           warnings.warn(
               "The `use_auth_token` argument is deprecated and will be removed in v5 of Transformers."							,     SCREAMING_SNAKE_CASE		)
           if token is not None:
                raise ValueError(
                    "`token` and `use_auth_token` are both specified. Please set only the argument `token`."		)
           UpperCamelCase__    :     Optional[Any]        =						use_auth_token

      if token is not None:
           # change to `token` in a follow-up PR
           UpperCamelCase__    :     List[Any]        =						token

      UpperCamelCase__    :     Dict        =						cls._get_arguments_from_pretrained(SCREAMING_SNAKE_CASE							,     **SCREAMING_SNAKE_CASE		)
      return cls(*SCREAMING_SNAKE_CASE		)







 @classmethod
 def       __lowercase      (	cls      :     Dict							,     SCREAMING_SNAKE_CASE      :     Optional[Any]="AutoProcessor"		):


      '''simple docstring'''






      if not isinstance(SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE		):
           UpperCamelCase__    :     str        =						auto_class.__name__

      import transformers.models.auto as auto_module

      if not hasattr(SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE		):
           raise ValueError(F'{auto_class} is not a valid auto class.'		)

      UpperCamelCase__    :     Dict        =						auto_class







 @classmethod
 def       __lowercase      (	cls      :     Optional[Any]							,     SCREAMING_SNAKE_CASE      :     Union[str, Any]							,     **SCREAMING_SNAKE_CASE      :     int		):


      '''simple docstring'''






      UpperCamelCase__    :     Tuple        =						[]
      for attribute_name in cls.attributes:
           UpperCamelCase__    :     Optional[int]        =						getattr(cls							,     F'{attribute_name}_class'		)
           if isinstance(SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE		):
                UpperCamelCase__    :     List[Any]        =						tuple(getattr(SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE		) if n is not None else None for n in class_name		)
                UpperCamelCase__    :     Optional[int]        =						kwargs.get("use_fast"							,     SCREAMING_SNAKE_CASE		)
                if use_fast and classes[1] is not None:
                     UpperCamelCase__    :     Dict        =						classes[1]
                else:
                     UpperCamelCase__    :     int        =						classes[0]
           else:
                UpperCamelCase__    :     Tuple        =						getattr(SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE		)

           args.append(attribute_class.from_pretrained(SCREAMING_SNAKE_CASE							,     **SCREAMING_SNAKE_CASE		)		)
      return args







 @property
 def       __lowercase      (	self      :     Union[str, Any]		):


      '''simple docstring'''






      UpperCamelCase__    :     List[str]        =						getattr(self							,     self.attributes[0]		)
      return getattr(SCREAMING_SNAKE_CASE							,     "model_input_names"							,     SCREAMING_SNAKE_CASE		)







lowerCamelCase			:							str       =copy_func(ProcessorMixin.push_to_hub)
if ProcessorMixin.push_to_hub.__doc__ is not None:
 lowerCamelCase			:							Tuple       =ProcessorMixin.push_to_hub.__doc__.format(
     object='''processor''', object_class='''AutoProcessor''', object_files='''processor files'''
 )







def 	SCREAMING_SNAKE_CASE							(      __lowerCAmelCase    )				->			str:
     return "".join(chr(ord(__lowerCAmelCase    ) + 32    ) if "A" <= char <= "Z" else char for char in word    )


if __name__ == "__main__":
 from doctest import testmod

 testmod()