

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      :    Union[str, Any]						       =    logging.get_logger(__name__)

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


_lowerCAmelCase      :    List[str]						       =    {
    "AutoTokenizer": "PreTrainedTokenizerBase",
    "AutoFeatureExtractor": "FeatureExtractionMixin",
    "AutoImageProcessor": "ImageProcessingMixin",
}






class 			__magic_name__					(    lowerCAmelCase_   ):
			SCREAMING_SNAKE_CASE							     =		['feature_extractor', 'tokenizer']
			# Names need to be attr_class for attr in attributes
			SCREAMING_SNAKE_CASE							     =		None
			SCREAMING_SNAKE_CASE							     =		None
			SCREAMING_SNAKE_CASE							     =		None





			def __init__(	self      ,						*__snake_case      ,						**__snake_case      )     ->				List[str]:




						'''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(__snake_case      ,						self.attributes      ):
									if attribute_name in kwargs:
												raise TypeError(f'Got multiple values for argument {attribute_name}.'      )
									else:
												__a						   =arg

						if len(__snake_case      ) != len(self.attributes      ):
									raise ValueError(
									    f'This processor requires {len(self.attributes      )} arguments: {", ".join(self.attributes      )}. Got '
									    f'{len(__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():
									__a						   =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						   =AUTO_TO_BASE_CLASS_MAPPING.get(__snake_case      ,						__snake_case      )
									if isinstance(__snake_case      ,						__snake_case      ):
												__a						   =tuple(getattr(__snake_case      ,						__snake_case      ) for n in class_name if n is not None      )
									else:
												__a						   =getattr(__snake_case      ,						__snake_case      )

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

									setattr(self      ,						__snake_case      ,						__snake_case      )





			def __repr__(	self      )     ->				Union[str, Any]:




						'''simple docstring'''
						__a						   =[f'- {name}: {repr(getattr(self      ,						__snake_case      )      )}' for name in self.attributes]
						__a						   ='\n'.join(__snake_case      )
						return f'{self.__class__.__name__}:\n{attributes_repr}'





			def 	__magic_name__					(	self      ,						__snake_case      ,						__snake_case = False      ,						**__snake_case      )     ->				Any:




						'''simple docstring'''
						os.makedirs(__snake_case      ,						exist_ok=__snake_case      )

						if push_to_hub:
									__a						   =kwargs.pop('commit_message'      ,						__snake_case      )
									__a						   =kwargs.pop('repo_id'      ,						save_directory.split(os.path.sep      )[-1]      )
									__a						   =self._create_repo(__snake_case      ,						**__snake_case      )
									__a						   =self._get_files_timestamps(__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:
									__a						   =[getattr(self      ,						__snake_case      ) for attribute_name in self.attributes]
									__a						   =[(a.init_kwargs if isinstance(__snake_case      ,						__snake_case      ) else a) for a in attrs]
									custom_object_save(self      ,						__snake_case      ,						config=__snake_case      )

						for attribute_name in self.attributes:
									__a						   =getattr(self      ,						__snake_case      )
									# Include the processor class in the attribute config so this processor can then be reloaded with the
									# `AutoProcessor` API.
									if hasattr(__snake_case      ,						'_set_processor_class'      ):
												attribute._set_processor_class(self.__class__.__name__      )
									attribute.save_pretrained(__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:
												__a						   =getattr(self      ,						__snake_case      )
												if isinstance(__snake_case      ,						__snake_case      ):
															del attribute.init_kwargs["auto_map"]

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





			@classmethod
			def 	__magic_name__					(	cls      ,						__snake_case      ,						__snake_case = None      ,						__snake_case = False      ,						__snake_case = False      ,						__snake_case = None      ,						__snake_case = "main"      ,						**__snake_case      ,						)     ->				Dict:




						'''simple docstring'''
						__a						   =cache_dir
						__a						   =force_download
						__a						   =local_files_only
						__a						   =revision

						__a						   =kwargs.pop('use_auth_token'      ,						__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.'      ,						__snake_case      )
									if token is not None:
												raise ValueError(
												    '`token` and `use_auth_token` are both specified. Please set only the argument `token`.'      )
									__a						   =use_auth_token

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

						__a						   =cls._get_arguments_from_pretrained(__snake_case      ,						**__snake_case      )
						return cls(*__snake_case      )





			@classmethod
			def 	__magic_name__					(	cls      ,						__snake_case="AutoProcessor"      )     ->				List[Any]:




						'''simple docstring'''
						if not isinstance(__snake_case      ,						__snake_case      ):
									__a						   =auto_class.__name__

						import transformers.models.auto as auto_module

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

						__a						   =auto_class





			@classmethod
			def 	__magic_name__					(	cls      ,						__snake_case      ,						**__snake_case      )     ->				Optional[Any]:




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

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





			@property
			def 	__magic_name__					(	self      )     ->				str:




						'''simple docstring'''
						__a						   =getattr(self      ,						self.attributes[0]      )
						return getattr(__snake_case      ,						'model_input_names'      ,						__snake_case      )


_lowerCAmelCase      :    str						       =    copy_func(ProcessorMixin.push_to_hub)
if ProcessorMixin.push_to_hub.__doc__ is not None:
						_lowerCAmelCase      :    Optional[int]						       =    ProcessorMixin.push_to_hub.__doc__.format(
						    object="processor", object_class="AutoProcessor", object_files="processor files"
						)









from typing import Optional

from ..utils.logging import get_logger
from .audio_classificiation import AudioClassification
from .automatic_speech_recognition import AutomaticSpeechRecognition
from .base import TaskTemplate
from .image_classification import ImageClassification
from .language_modeling import LanguageModeling
from .question_answering import QuestionAnsweringExtractive
from .summarization import Summarization
from .text_classification import TextClassification


_lowerCAmelCase      :    List[Any]						       =    [
    "AutomaticSpeechRecognition",
    "AudioClassification",
    "ImageClassification",
    "LanguageModeling",
    "QuestionAnsweringExtractive",
    "Summarization",
    "TaskTemplate",
    "TextClassification",
]

_lowerCAmelCase      :    List[str]						       =    get_logger(__name__)


_lowerCAmelCase      :    Tuple						       =    {
    AutomaticSpeechRecognition.task: AutomaticSpeechRecognition,
    AudioClassification.task: AudioClassification,
    ImageClassification.task: ImageClassification,
    LanguageModeling.task: LanguageModeling,
    QuestionAnsweringExtractive.task: QuestionAnsweringExtractive,
    Summarization.task: Summarization,
    TextClassification.task: TextClassification,
}






def  UpperCamelCase_(							_snake_case     :						dict			):




			"""simple docstring"""






			__a						   =task_template_dict.get('task'			)
			if task_name is None:
						logger.warning(F'Couldn\'t find template for task \'{task_name}\'. Available templates: {list(_snake_case			)}'			)
						return None
			__a						   =NAMEaTEMPLATE.get(_snake_case			)
			return template.from_dict(_snake_case			)






