






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


__snake_case							 =  logging.get_logger(__name__)

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


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







class       UpperCAmelCase_						( lowercase			):



      """simple docstring"""
      UpperCamelCase_    : List[Any]									=['feature_extractor', 'tokenizer']
      # Names need to be attr_class for attr in attributes
      UpperCamelCase_    : List[str]									=None
      UpperCamelCase_    : List[Any]									=None
      UpperCamelCase_    : Dict									=None
      def __init__(    self   ,    *SCREAMING_SNAKE_CASE_   ,    **SCREAMING_SNAKE_CASE_			)    ->				List[Any]:
          # 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(SCREAMING_SNAKE_CASE_   ,    self.attributes			):
              if attribute_name in kwargs:
                  raise TypeError(F'''Got multiple values for argument {attribute_name}.'''			)
              else:
                  UpperCamelCase	:Any     							=       arg

          if len(SCREAMING_SNAKE_CASE_			) != len(self.attributes			):
              raise ValueError(
                  F'''This processor requires {len(self.attributes			)} arguments: {", ".join(self.attributes			)}. Got '''
                  F'''{len(SCREAMING_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	:int     							=       AUTO_TO_BASE_CLASS_MAPPING.get(SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_			)
              if isinstance(SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_			):
                  UpperCamelCase	:List[str]     							=       tuple(getattr(SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_			) for n in class_name if n is not None			)
              else:
                  UpperCamelCase	:Optional[int]     							=       getattr(SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_			)

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

              setattr(self   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_			)
      def __repr__(    self			)    ->				List[str]:
          UpperCamelCase	:Optional[Any]     							=       [F'''- {name}: {repr(getattr(self   ,    SCREAMING_SNAKE_CASE_			)			)}''' for name in self.attributes]
          UpperCamelCase	:List[Any]     							=       '''\n'''.join(SCREAMING_SNAKE_CASE_			)
          return F'''{self.__class__.__name__}:\n{attributes_repr}'''
      def    UpperCAmelCase						(    self   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_ = False   ,    **SCREAMING_SNAKE_CASE_			)    ->				str:
          os.makedirs(SCREAMING_SNAKE_CASE_   ,    exist_ok=SCREAMING_SNAKE_CASE_			)

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

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

          if push_to_hub:
              self._upload_modified_files(
                  SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_   ,    commit_message=SCREAMING_SNAKE_CASE_   ,    token=kwargs.get('''use_auth_token'''			)   ,    )
      @classmethod
      def    UpperCAmelCase						(    cls   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_ = None   ,    SCREAMING_SNAKE_CASE_ = False   ,    SCREAMING_SNAKE_CASE_ = False   ,    SCREAMING_SNAKE_CASE_ = None   ,    SCREAMING_SNAKE_CASE_ = "main"   ,    **SCREAMING_SNAKE_CASE_   ,    )    ->				int:
          UpperCamelCase	:Tuple     							=       cache_dir
          UpperCamelCase	:Any     							=       force_download
          UpperCamelCase	:Tuple     							=       local_files_only
          UpperCamelCase	:int     							=       revision

          UpperCamelCase	:Union[str, Any]     							=       kwargs.pop('''use_auth_token'''   ,    SCREAMING_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.'''   ,    SCREAMING_SNAKE_CASE_			)
              if token is not None:
                  raise ValueError(
                      '''`token` and `use_auth_token` are both specified. Please set only the argument `token`.'''			)
              UpperCamelCase	:List[Any]     							=       use_auth_token

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

          UpperCamelCase	:Optional[Any]     							=       cls._get_arguments_from_pretrained(SCREAMING_SNAKE_CASE_   ,    **SCREAMING_SNAKE_CASE_			)
          return cls(*SCREAMING_SNAKE_CASE_			)
      @classmethod
      def    UpperCAmelCase						(    cls   ,    SCREAMING_SNAKE_CASE_="AutoProcessor"			)    ->				Optional[int]:
          if not isinstance(SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_			):
              UpperCamelCase	:str     							=       auto_class.__name__

          import transformers.models.auto as auto_module

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

          UpperCamelCase	:int     							=       auto_class
      @classmethod
      def    UpperCAmelCase						(    cls   ,    SCREAMING_SNAKE_CASE_   ,    **SCREAMING_SNAKE_CASE_			)    ->				Union[str, Any]:
          UpperCamelCase	:str     							=       []
          for attribute_name in cls.attributes:
              UpperCamelCase	:Optional[int]     							=       getattr(cls   ,    F'''{attribute_name}_class'''			)
              if isinstance(SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_			):
                  UpperCamelCase	:str     							=       tuple(getattr(SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_			) if n is not None else None for n in class_name			)
                  UpperCamelCase	:List[Any]     							=       kwargs.get('''use_fast'''   ,    SCREAMING_SNAKE_CASE_			)
                  if use_fast and classes[1] is not None:
                      UpperCamelCase	:Tuple     							=       classes[1]
                  else:
                      UpperCamelCase	:List[Any]     							=       classes[0]
              else:
                  UpperCamelCase	:Tuple     							=       getattr(SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_			)

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




      @property
      def    UpperCAmelCase						(    self			)    ->				Tuple:
          UpperCamelCase	:int     							=       getattr(self   ,    self.attributes[0]			)
          return getattr(SCREAMING_SNAKE_CASE_   ,    '''model_input_names'''   ,    SCREAMING_SNAKE_CASE_			)







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













def   _A       (    SCREAMING_SNAKE_CASE__  :     float					,  SCREAMING_SNAKE_CASE__  :     float					,  SCREAMING_SNAKE_CASE__  :     float	):
    if hue < 0 or hue > 360:
        raise Exception('''hue should be between 0 and 360'''	)

    if saturation < 0 or saturation > 1:
        raise Exception('''saturation should be between 0 and 1'''	)

    if value < 0 or value > 1:
        raise Exception('''value should be between 0 and 1'''	)

    UpperCamelCase	:Tuple     							=       value * saturation
    UpperCamelCase	:Any     							=       hue / 60
    UpperCamelCase	:Any     							=       chroma * (1 - abs(hue_section % 2 - 1	))
    UpperCamelCase	:Optional[int]     							=       value - chroma

    if hue_section >= 0 and hue_section <= 1:
        UpperCamelCase	:Any     							=       round(255 * (chroma + match_value)	)
        UpperCamelCase	:Union[str, Any]     							=       round(255 * (second_largest_component + match_value)	)
        UpperCamelCase	:List[str]     							=       round(255 * (match_value)	)
    elif hue_section > 1 and hue_section <= 2:
        UpperCamelCase	:Dict     							=       round(255 * (second_largest_component + match_value)	)
        UpperCamelCase	:Dict     							=       round(255 * (chroma + match_value)	)
        UpperCamelCase	:str     							=       round(255 * (match_value)	)
    elif hue_section > 2 and hue_section <= 3:
        UpperCamelCase	:Any     							=       round(255 * (match_value)	)
        UpperCamelCase	:Optional[Any]     							=       round(255 * (chroma + match_value)	)
        UpperCamelCase	:Union[str, Any]     							=       round(255 * (second_largest_component + match_value)	)
    elif hue_section > 3 and hue_section <= 4:
        UpperCamelCase	:Tuple     							=       round(255 * (match_value)	)
        UpperCamelCase	:Optional[Any]     							=       round(255 * (second_largest_component + match_value)	)
        UpperCamelCase	:str     							=       round(255 * (chroma + match_value)	)
    elif hue_section > 4 and hue_section <= 5:
        UpperCamelCase	:int     							=       round(255 * (second_largest_component + match_value)	)
        UpperCamelCase	:str     							=       round(255 * (match_value)	)
        UpperCamelCase	:List[str]     							=       round(255 * (chroma + match_value)	)
    else:
        UpperCamelCase	:Union[str, Any]     							=       round(255 * (chroma + match_value)	)
        UpperCamelCase	:List[str]     							=       round(255 * (match_value)	)
        UpperCamelCase	:Optional[int]     							=       round(255 * (second_largest_component + match_value)	)

    return [red, green, blue]


def   _A       (    SCREAMING_SNAKE_CASE__  :     int					,  SCREAMING_SNAKE_CASE__  :     int					,  SCREAMING_SNAKE_CASE__  :     int	):
    if red < 0 or red > 255:
        raise Exception('''red should be between 0 and 255'''	)

    if green < 0 or green > 255:
        raise Exception('''green should be between 0 and 255'''	)

    if blue < 0 or blue > 255:
        raise Exception('''blue should be between 0 and 255'''	)

    UpperCamelCase	:str     							=       red / 255
    UpperCamelCase	:List[str]     							=       green / 255
    UpperCamelCase	:int     							=       blue / 255
    UpperCamelCase	:Optional[int]     							=       max(SCREAMING_SNAKE_CASE__					,  SCREAMING_SNAKE_CASE__					,  SCREAMING_SNAKE_CASE__	)
    UpperCamelCase	:int     							=       value - min(SCREAMING_SNAKE_CASE__					,  SCREAMING_SNAKE_CASE__					,  SCREAMING_SNAKE_CASE__	)
    UpperCamelCase	:Dict     							=       0 if value == 0 else chroma / value

    if chroma == 0:
        UpperCamelCase	:Dict     							=       0.0
    elif value == float_red:
        UpperCamelCase	:Optional[Any]     							=       60 * (0 + (float_green - float_blue) / chroma)
    elif value == float_green:
        UpperCamelCase	:List[str]     							=       60 * (2 + (float_blue - float_red) / chroma)
    else:
        UpperCamelCase	:str     							=       60 * (4 + (float_red - float_green) / chroma)

    UpperCamelCase	:int     							=       (hue + 360) % 360

    return [hue, saturation, value]



def   _A       (    SCREAMING_SNAKE_CASE__  :     list[float]					,  SCREAMING_SNAKE_CASE__  :     list[float]	):
    UpperCamelCase	:str     							=       abs(hsv_a[0] - hsv_a[0]	) < 0.2
    UpperCamelCase	:List[str]     							=       abs(hsv_a[1] - hsv_a[1]	) < 0.0_02
    UpperCamelCase	:Any     							=       abs(hsv_a[2] - hsv_a[2]	) < 0.0_02

    return check_hue and check_saturation and check_value





