





"""simple docstring"""



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


a_				=    logging.get_logger(__name__)

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


a_				=    {
    """AutoTokenizer""": """PreTrainedTokenizerBase""",
    """AutoFeatureExtractor""": """FeatureExtractionMixin""",
    """AutoImageProcessor""": """ImageProcessingMixin""",
}






class 	__snake_case      (	SCREAMING_SNAKE_CASE__  ):





 """simple docstring"""


 _lowerCamelCase				 =  ["""feature_extractor""", """tokenizer"""]
 # Names need to be attr_class for attr in attributes
 _lowerCamelCase				 =  None
 _lowerCamelCase				 =  None
 _lowerCamelCase				 =  None

 def __init__(					self      ,		*__lowerCamelCase      ,		**__lowerCamelCase						):



     '''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(__lowerCamelCase      ,		self.attributes						):
         if attribute_name in kwargs:
             raise TypeError(F"""Got multiple values for argument {attribute_name}."""						)
         else:
             __A					:		Union[str, Any]						=  arg

     if len(__lowerCamelCase						) != len(self.attributes						):
         raise ValueError(
             F"""This processor requires {len(self.attributes						)} arguments: {', '.join(self.attributes						)}. Got """
             F"""{len(__lowerCamelCase						)} 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():
         __A					:		Union[str, 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.
         __A					:		Dict						=  AUTO_TO_BASE_CLASS_MAPPING.get(__lowerCamelCase      ,		__lowerCamelCase						)
         if isinstance(__lowerCamelCase      ,		__lowerCamelCase						):
             __A					:		Tuple						=  tuple(getattr(__lowerCamelCase      ,		__lowerCamelCase						) for n in class_name if n is not None						)
         else:
             __A					:		str						=  getattr(__lowerCamelCase      ,		__lowerCamelCase						)

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

         setattr(self      ,		__lowerCamelCase      ,		__lowerCamelCase						)

 def __repr__(					self						):



     '''simple docstring'''




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

 def       UpperCamelCase__(					self      ,		__lowerCamelCase      ,		__lowerCamelCase = False      ,		**__lowerCamelCase						):



     '''simple docstring'''




     os.makedirs(__lowerCamelCase      ,		exist_ok=__lowerCamelCase						)

     if push_to_hub:
         __A					:		Optional[Any]						=  kwargs.pop('''commit_message'''      ,		__lowerCamelCase						)
         __A					:		Union[str, Any]						=  kwargs.pop('''repo_id'''      ,		save_directory.split(os.path.sep						)[-1]						)
         __A					:		List[str]						=  self._create_repo(__lowerCamelCase      ,		**__lowerCamelCase						)
         __A					:		str						=  self._get_files_timestamps(__lowerCamelCase						)
     # 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:
         __A					:		Optional[int]						=  [getattr(self      ,		__lowerCamelCase						) for attribute_name in self.attributes]
         __A					:		List[Any]						=  [(a.init_kwargs if isinstance(__lowerCamelCase      ,		__lowerCamelCase						) else a) for a in attrs]
         custom_object_save(self      ,		__lowerCamelCase      ,		config=__lowerCamelCase						)

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

     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:
             __A					:		List[Any]						=  getattr(self      ,		__lowerCamelCase						)
             if isinstance(__lowerCamelCase      ,		__lowerCamelCase						):
                 del attribute.init_kwargs["auto_map"]

     if push_to_hub:
         self._upload_modified_files(
             __lowerCamelCase      ,		__lowerCamelCase      ,		__lowerCamelCase      ,		commit_message=__lowerCamelCase      ,		token=kwargs.get('''use_auth_token'''						)      ,		)

 @classmethod
 def       UpperCamelCase__(					cls      ,		__lowerCamelCase      ,		__lowerCamelCase = None      ,		__lowerCamelCase = False      ,		__lowerCamelCase = False      ,		__lowerCamelCase = None      ,		__lowerCamelCase = "main"      ,		**__lowerCamelCase      ,		):



     '''simple docstring'''




     __A					:		int						=  cache_dir
     __A					:		Optional[Any]						=  force_download
     __A					:		List[str]						=  local_files_only
     __A					:		Optional[int]						=  revision

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

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

     __A					:		str						=  cls._get_arguments_from_pretrained(__lowerCamelCase      ,		**__lowerCamelCase						)
     return cls(*__lowerCamelCase						)

 @classmethod
 def       UpperCamelCase__(					cls      ,		__lowerCamelCase="AutoProcessor"						):



     '''simple docstring'''




     if not isinstance(__lowerCamelCase      ,		__lowerCamelCase						):
         __A					:		Union[str, Any]						=  auto_class.__name__

     import transformers.models.auto as auto_module

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

     __A					:		int						=  auto_class

 @classmethod
 def       UpperCamelCase__(					cls      ,		__lowerCamelCase      ,		**__lowerCamelCase						):



     '''simple docstring'''




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

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







 @property
 def       UpperCamelCase__(					self						):



     '''simple docstring'''




     __A					:		Union[str, Any]						=  getattr(self      ,		self.attributes[0]						)
     return getattr(__lowerCamelCase      ,		'''model_input_names'''      ,		__lowerCamelCase						)






a_				=    copy_func(ProcessorMixin.push_to_hub)
if ProcessorMixin.push_to_hub.__doc__ is not None:
       a_				=    ProcessorMixin.push_to_hub.__doc__.format(
           object="""processor""", object_class="""AutoProcessor""", object_files="""processor files"""
       )











"""simple docstring"""



from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


a_				=    {
    """configuration_layoutlm""": ["""LAYOUTLM_PRETRAINED_CONFIG_ARCHIVE_MAP""", """LayoutLMConfig""", """LayoutLMOnnxConfig"""],
    """tokenization_layoutlm""": ["""LayoutLMTokenizer"""],
}

try:
       if not is_tokenizers_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       a_				=    ["""LayoutLMTokenizerFast"""]

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       a_				=    [
           """LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST""",
           """LayoutLMForMaskedLM""",
           """LayoutLMForSequenceClassification""",
           """LayoutLMForTokenClassification""",
           """LayoutLMForQuestionAnswering""",
           """LayoutLMModel""",
           """LayoutLMPreTrainedModel""",
       ]

try:
       if not is_tf_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       a_				=    [
           """TF_LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST""",
           """TFLayoutLMForMaskedLM""",
           """TFLayoutLMForSequenceClassification""",
           """TFLayoutLMForTokenClassification""",
           """TFLayoutLMForQuestionAnswering""",
           """TFLayoutLMMainLayer""",
           """TFLayoutLMModel""",
           """TFLayoutLMPreTrainedModel""",
       ]


if TYPE_CHECKING:
       from .configuration_layoutlm import LAYOUTLM_PRETRAINED_CONFIG_ARCHIVE_MAP, LayoutLMConfig, LayoutLMOnnxConfig
       from .tokenization_layoutlm import LayoutLMTokenizer

       try:
              if not is_tokenizers_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .tokenization_layoutlm_fast import LayoutLMTokenizerFast

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_layoutlm import (
                  LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST,
                  LayoutLMForMaskedLM,
                  LayoutLMForQuestionAnswering,
                  LayoutLMForSequenceClassification,
                  LayoutLMForTokenClassification,
                  LayoutLMModel,
                  LayoutLMPreTrainedModel,
              )
       try:
              if not is_tf_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_tf_layoutlm import (
                  TF_LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST,
                  TFLayoutLMForMaskedLM,
                  TFLayoutLMForQuestionAnswering,
                  TFLayoutLMForSequenceClassification,
                  TFLayoutLMForTokenClassification,
                  TFLayoutLMMainLayer,
                  TFLayoutLMModel,
                  TFLayoutLMPreTrainedModel,
              )

else:
       import sys

       a_				=    _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)




