


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

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


__UpperCamelCase					:	Union[str, Any]          =				{
    'AutoTokenizer': 'PreTrainedTokenizerBase',
    'AutoFeatureExtractor': 'FeatureExtractionMixin',
    'AutoImageProcessor': 'ImageProcessingMixin',
}





class 			lowerCAmelCase__(     snake_case__    ):






    '''simple docstring'''





    A_	:  str								=			['feature_extractor', 'tokenizer']
    # Names need to be attr_class for attr in attributes
    A_	:  int								=			None
    A_	:  Optional[int]								=			None
    A_	:  Any								=			None





    def __init__(						self					: Dict							, *__snake_case					: Optional[Any]							, **__snake_case					: List[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(__snake_case							, self.attributes						):
          if attribute_name in kwargs:
             raise TypeError(f'''Got multiple values for argument {attribute_name}.'''						)
          else:
             UpperCAmelCase_				:   str							= 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():
          UpperCAmelCase_				:   int							= 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_				:   Tuple							= AUTO_TO_BASE_CLASS_MAPPING.get(__snake_case							, __snake_case						)
          if isinstance(__snake_case							, __snake_case						):
             UpperCAmelCase_				:   List[str]							= tuple(getattr(__snake_case							, __snake_case						) for n in class_name if n is not None						)
          else:
             UpperCAmelCase_				:   Any							= 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					: List[str]						):





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





    def        _lowerCamelCase			(						self					: Union[str, Any]							, __snake_case					: int							, __snake_case					: bool = False							, **__snake_case					: List[Any]						):





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

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





       '''simple docstring'''
       UpperCAmelCase_				:   Optional[Any]							= cache_dir
       UpperCAmelCase_				:   Optional[int]							= force_download
       UpperCAmelCase_				:   Any							= local_files_only
       UpperCAmelCase_				:   Any							= revision

       UpperCAmelCase_				:   Union[str, Any]							= 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`.'''						)
          UpperCAmelCase_				:   Optional[int]							= use_auth_token

       if token is not None:
          # change to `token` in a follow-up PR
          UpperCAmelCase_				:   Optional[int]							= token

       UpperCAmelCase_				:   Any							= cls._get_arguments_from_pretrained(__snake_case							, **__snake_case						)
       return cls(*__snake_case						)





    @classmethod
    def        _lowerCamelCase			(						cls					: List[Any]							, __snake_case					: Union[str, Any]="AutoProcessor"						):





       '''simple docstring'''
       if not isinstance(__snake_case							, __snake_case						):
          UpperCAmelCase_				:   Optional[int]							= 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.'''						)

       UpperCAmelCase_				:   str							= auto_class





    @classmethod
    def        _lowerCamelCase			(						cls					: Tuple							, __snake_case					: Tuple							, **__snake_case					: int						):





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

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





    @property
    def        _lowerCamelCase			(						self					: List[Any]						):





       '''simple docstring'''
       UpperCAmelCase_				:   Optional[int]							= getattr(self							, self.attributes[0]						)
       return getattr(__snake_case							, '''model_input_names'''							, __snake_case						)





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



import unittest

import numpy as np

from transformers import is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
       import tensorflow as tf

       from transformers.activations_tf import get_tf_activation





@require_tf
class 			lowerCAmelCase__(     unittest.TestCase    ):






    '''simple docstring'''





    def        _lowerCamelCase			(						self					: Optional[int]						):





       '''simple docstring'''
       UpperCAmelCase_				:   str							= tf.constant([-100, -1.0, -0.1, 0, 0.1, 1.0, 100.0]						)
       UpperCAmelCase_				:   List[Any]							= get_tf_activation('''gelu'''						)
       UpperCAmelCase_				:   Any							= get_tf_activation('''gelu_10'''						)

       UpperCAmelCase_				:   int							= gelu(__snake_case						)
       UpperCAmelCase_				:   Union[str, Any]							= geluaa(__snake_case						)

       UpperCAmelCase_				:   Union[str, Any]							= tf.where(y_gelu_aa < 10.0							, 1.0							, 0.0						)

       self.assertEqual(tf.math.reduce_max(__snake_case						).numpy().item()							, 10.0						)
       self.assertTrue(np.allclose(y_gelu * clipped_mask							, y_gelu_aa * clipped_mask						)						)





    def        _lowerCamelCase			(						self					: Dict						):





       '''simple docstring'''
       get_tf_activation('''gelu'''						)
       get_tf_activation('''gelu_10'''						)
       get_tf_activation('''gelu_fast'''						)
       get_tf_activation('''gelu_new'''						)
       get_tf_activation('''glu'''						)
       get_tf_activation('''mish'''						)
       get_tf_activation('''quick_gelu'''						)
       get_tf_activation('''relu'''						)
       get_tf_activation('''sigmoid'''						)
       get_tf_activation('''silu'''						)
       get_tf_activation('''swish'''						)
       get_tf_activation('''tanh'''						)
       with self.assertRaises(__snake_case						):
          get_tf_activation('''bogus'''						)
       with self.assertRaises(__snake_case						):
          get_tf_activation(__snake_case						)