




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


__magic_name__     :     Union[str, Any]  							=					logging.get_logger(__name__)

# Dynamically import the Transformers module to grab the attribute classes of the processor form their names.
__magic_name__     :     Optional[Any]  							=					direct_transformers_import(Path(__file__).parent)


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





class 						SCREAMING_SNAKE_CASE__		(_a ):
     lowercase_   :			int   =   ["feature_extractor", "tokenizer"]
     # Names need to be attr_class for attr in attributes
     lowercase_   :			List[Any]   =   None
     lowercase_   :			Tuple   =   None
     lowercase_   :			int   =   None
     def __init__(	self  :		Optional[Any]					,	*__lowerCamelCase  :		Union[str, Any]					,	**__lowerCamelCase  :		Optional[Any]	):



       """simple docstring"""
       # Sanitize args and kwargs
       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:
           lowerCAmelCase__								=			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():
         lowerCAmelCase__								=			getattr(self					,	F"""{attribute_name}_class"""	)
         # Nothing is ever going to be an instance of "AutoXxx", in that case we check the base class.
         lowerCAmelCase__								=			AUTO_TO_BASE_CLASS_MAPPING.get(__lowerCamelCase					,	__lowerCamelCase	)
         if isinstance(__lowerCamelCase					,	__lowerCamelCase	):
           lowerCAmelCase__								=			tuple(getattr(__lowerCamelCase					,	__lowerCamelCase	) for n in class_name if n is not None	)
         else:
           lowerCAmelCase__								=			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  :		List[str]	):



       """simple docstring"""
       lowerCAmelCase__								=			[F"""- {name}: {repr(getattr(self					,	__lowerCamelCase	)	)}""" for name in self.attributes]
       lowerCAmelCase__								=			'''\n'''.join(__lowerCamelCase	)
       return F"""{self.__class__.__name__}:\n{attributes_repr}"""
     def 					A__			(	self  :		int					,	__lowerCamelCase  :		Any					,	__lowerCamelCase  :		bool = False					,	**__lowerCamelCase  :		int	):



       """simple docstring"""
       os.makedirs(__lowerCamelCase					,	exist_ok=__lowerCamelCase	)

       if push_to_hub:
         lowerCAmelCase__								=			kwargs.pop('''commit_message'''					,	__lowerCamelCase	)
         lowerCAmelCase__								=			kwargs.pop('''repo_id'''					,	save_directory.split(os.path.sep	)[-1]	)
         lowerCAmelCase__								=			self._create_repo(__lowerCamelCase					,	**__lowerCamelCase	)
         lowerCAmelCase__								=			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:
         lowerCAmelCase__								=			[getattr(self					,	__lowerCamelCase	) for attribute_name in self.attributes]
         lowerCAmelCase__								=			[(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:
         lowerCAmelCase__								=			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:
           lowerCAmelCase__								=			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 					A__			(	cls  :		Dict					,	__lowerCamelCase  :		Union[str, os.PathLike]					,	__lowerCamelCase  :		Optional[Union[str, os.PathLike]] = None					,	__lowerCamelCase  :		bool = False					,	__lowerCamelCase  :		bool = False					,	__lowerCamelCase  :		Optional[Union[str, bool]] = None					,	__lowerCamelCase  :		str = "main"					,	**__lowerCamelCase  :		Optional[Any]					,	):



       """simple docstring"""
       lowerCAmelCase__								=			cache_dir
       lowerCAmelCase__								=			force_download
       lowerCAmelCase__								=			local_files_only
       lowerCAmelCase__								=			revision

       lowerCAmelCase__								=			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`.'''	)
         lowerCAmelCase__								=			use_auth_token

       if token is not None:
         # change to `token` in a follow-up PR
         lowerCAmelCase__								=			token

       lowerCAmelCase__								=			cls._get_arguments_from_pretrained(__lowerCamelCase					,	**__lowerCamelCase	)
       return cls(*__lowerCamelCase	)
     @classmethod
     def 					A__			(	cls  :		Union[str, Any]					,	__lowerCamelCase  :		Union[str, Any]="AutoProcessor"	):



       """simple docstring"""
       if not isinstance(__lowerCamelCase					,	__lowerCamelCase	):
         lowerCAmelCase__								=			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."""	)

       lowerCAmelCase__								=			auto_class
     @classmethod
     def 					A__			(	cls  :		Optional[Any]					,	__lowerCamelCase  :		int					,	**__lowerCamelCase  :		Optional[int]	):



       """simple docstring"""
       lowerCAmelCase__								=			[]
       for attribute_name in cls.attributes:
         lowerCAmelCase__								=			getattr(cls					,	F"""{attribute_name}_class"""	)
         if isinstance(__lowerCamelCase					,	__lowerCamelCase	):
           lowerCAmelCase__								=			tuple(getattr(__lowerCamelCase					,	__lowerCamelCase	) if n is not None else None for n in class_name	)
           lowerCAmelCase__								=			kwargs.get('''use_fast'''					,	__lowerCamelCase	)
           if use_fast and classes[1] is not None:
             lowerCAmelCase__								=			classes[1]
           else:
             lowerCAmelCase__								=			classes[0]
         else:
           lowerCAmelCase__								=			getattr(__lowerCamelCase					,	__lowerCamelCase	)

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



     @property
     def 					A__			(	self  :		Optional[Any]	):



       """simple docstring"""
       lowerCAmelCase__								=			getattr(self					,	self.attributes[0]	)
       return getattr(__lowerCamelCase					,	'''model_input_names'''					,	__lowerCamelCase	)



__magic_name__     :     Union[str, Any]  							=					copy_func(ProcessorMixin.push_to_hub)
if ProcessorMixin.push_to_hub.__doc__ is not None:
      __magic_name__     :     Dict  							=					ProcessorMixin.push_to_hub.__doc__.format(
          object="""processor""", object_class="""AutoProcessor""", object_files="""processor files"""
      )







from __future__ import annotations

from collections.abc import Iterator
from typing import Any





class 						SCREAMING_SNAKE_CASE__		:
     def __init__(	self  :		int					,	__lowerCamelCase  :		Any	):



       """simple docstring"""
       lowerCAmelCase__								=			data
       lowerCAmelCase__								=			None



     def __str__(	self  :		int	):



       """simple docstring"""
       return F"""{self.data}"""





class 						SCREAMING_SNAKE_CASE__		:
     def __init__(	self  :		Union[str, Any]	):



       """simple docstring"""
       lowerCAmelCase__								=			None
       lowerCAmelCase__								=			None
     def __iter__(	self  :		List[Any]	):



       """simple docstring"""
       lowerCAmelCase__								=			self.front
       while node:
         yield node.data
         lowerCAmelCase__								=			node.next
     def __len__(	self  :		int	):



       """simple docstring"""
       return len(tuple(iter(self	)	)	)
     def __str__(	self  :		Optional[int]	):



       """simple docstring"""
       return " <- ".join(str(__lowerCamelCase	) for item in self	)
     def 					A__			(	self  :		Tuple	):



       """simple docstring"""
       return len(self	) == 0
     def 					A__			(	self  :		str					,	__lowerCamelCase  :		Any	):



       """simple docstring"""
       lowerCAmelCase__								=			Node(__lowerCamelCase	)
       if self.is_empty():
         lowerCAmelCase__								=			lowerCAmelCase__								=			node
       else:
         assert isinstance(self.rear					,	__lowerCamelCase	)
         lowerCAmelCase__								=			node
         lowerCAmelCase__								=			node
     def 					A__			(	self  :		Optional[Any]	):



       """simple docstring"""
       if self.is_empty():
         raise IndexError('''dequeue from empty queue'''	)
       assert isinstance(self.front					,	__lowerCamelCase	)
       lowerCAmelCase__								=			self.front
       lowerCAmelCase__								=			self.front.next
       if self.front is None:
         lowerCAmelCase__								=			None
       return node.data



     def 					A__			(	self  :		Tuple	):



       """simple docstring"""
       lowerCAmelCase__								=			lowerCAmelCase__								=			None


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

      testmod()

