


'''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


__UpperCAmelCase    				=							logging.get_logger(__name__)

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


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




class 		a__				(							a__  ):





							'''simple docstring'''






							lowercase__			:   str							=      ["feature_extractor", "tokenizer"]
							# Names need to be attr_class for attr in attributes
							lowercase__			:   List[Any]							=      None
							lowercase__			:   int							=      None
							lowercase__			:   Any							=      None







							def __init__(		self					,    *lowerCamelCase_					,    **lowerCamelCase_   )       ->							str:
														# 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   )       ->							Tuple:
														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   __SCREAMING_SNAKE_CASE							(		self					,    lowerCamelCase_					,    lowerCamelCase_ = False					,    **lowerCamelCase_   )       ->							Dict:
														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   __SCREAMING_SNAKE_CASE							(		cls					,    lowerCamelCase_					,    lowerCamelCase_ = None					,    lowerCamelCase_ = False					,    lowerCamelCase_ = False					,    lowerCamelCase_ = None					,    lowerCamelCase_ = "main"					,    **lowerCamelCase_					,    )       ->							Union[str, Any]:
														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   __SCREAMING_SNAKE_CASE							(		cls					,    lowerCamelCase_="AutoProcessor"   )       ->							Union[str, Any]:
														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   __SCREAMING_SNAKE_CASE							(		cls					,    lowerCamelCase_					,    **lowerCamelCase_   )       ->							List[Any]:
														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   __SCREAMING_SNAKE_CASE							(		self   )       ->							List[Any]:
														lowerCAmelCase__          =	getattr(self					,    self.attributes[0]   )
														return getattr(lowerCamelCase_					,    '''model_input_names'''					,    lowerCamelCase_   )

__UpperCAmelCase    				=							copy_func(ProcessorMixin.push_to_hub)
if ProcessorMixin.push_to_hub.__doc__ is not None:
		__UpperCAmelCase    				=							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_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


__UpperCAmelCase    				=							{
    '''configuration_switch_transformers''': [
        '''SWITCH_TRANSFORMERS_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''SwitchTransformersConfig''',
        '''SwitchTransformersOnnxConfig''',
    ]
}

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__UpperCAmelCase    				=							[
		    '''SWITCH_TRANSFORMERS_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''SwitchTransformersEncoderModel''',
		    '''SwitchTransformersForConditionalGeneration''',
		    '''SwitchTransformersModel''',
		    '''SwitchTransformersPreTrainedModel''',
		    '''SwitchTransformersTop1Router''',
		    '''SwitchTransformersSparseMLP''',
		]


if TYPE_CHECKING:
		from .configuration_switch_transformers import (
		    SWITCH_TRANSFORMERS_PRETRAINED_CONFIG_ARCHIVE_MAP,
		    SwitchTransformersConfig,
		    SwitchTransformersOnnxConfig,
		)

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_switch_transformers import (
				    SWITCH_TRANSFORMERS_PRETRAINED_MODEL_ARCHIVE_LIST,
				    SwitchTransformersEncoderModel,
				    SwitchTransformersForConditionalGeneration,
				    SwitchTransformersModel,
				    SwitchTransformersPreTrainedModel,
				    SwitchTransformersSparseMLP,
				    SwitchTransformersTopaRouter,
				)


else:
		import sys

		__UpperCAmelCase    				=							_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)