

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

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


lowerCAmelCase						:   List[str]										=    {
    '''AutoTokenizer''': '''PreTrainedTokenizerBase''',
    '''AutoFeatureExtractor''': '''FeatureExtractionMixin''',
    '''AutoImageProcessor''': '''ImageProcessingMixin''',
}
class     SCREAMING_SNAKE_CASE__     (						__a		):





       '''simple docstring'''




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

       def __init__(						self       :						Optional[int]    ,		*lowerCAmelCase__       :						Union[str, Any]    ,		**lowerCAmelCase__       :						Dict							)			->    Tuple:
             # 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:
                         snake_case__					=   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():
                   snake_case__					=   getattr(self    ,		f'''{attribute_name}_class'''							)
                   # Nothing is ever going to be an instance of "AutoXxx", in that case we check the base class.
                   snake_case__					=   AUTO_TO_BASE_CLASS_MAPPING.get(lowerCAmelCase__    ,		lowerCAmelCase__							)
                   if isinstance(lowerCAmelCase__    ,		lowerCAmelCase__							):
                         snake_case__					=   tuple(getattr(lowerCAmelCase__    ,		lowerCAmelCase__							) for n in class_name if n is not None							)
                   else:
                         snake_case__					=   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       :						Optional[int]							)			->    List[str]:
             snake_case__					=   [f'''- {name}: {repr(getattr(self    ,		lowerCAmelCase__							)							)}''' for name in self.attributes]
             snake_case__					=   """\n""".join(lowerCAmelCase__							)
             return f'''{self.__class__.__name__}:\n{attributes_repr}'''

       def UpperCAmelCase_						(						self       :						Union[str, Any]    ,		lowerCAmelCase__       :						str    ,		lowerCAmelCase__       :						bool = False    ,		**lowerCAmelCase__       :						str							)			->    Dict:
             os.makedirs(lowerCAmelCase__    ,		exist_ok=lowerCAmelCase__							)

             if push_to_hub:
                   snake_case__					=   kwargs.pop("""commit_message"""    ,		lowerCAmelCase__							)
                   snake_case__					=   kwargs.pop("""repo_id"""    ,		save_directory.split(os.path.sep							)[-1]							)
                   snake_case__					=   self._create_repo(lowerCAmelCase__    ,		**lowerCAmelCase__							)
                   snake_case__					=   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:
                   snake_case__					=   [getattr(self    ,		lowerCAmelCase__							) for attribute_name in self.attributes]
                   snake_case__					=   [(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:
                   snake_case__					=   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:
                         snake_case__					=   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 UpperCAmelCase_						(						cls       :						str    ,		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[int]    ,		)			->    List[str]:
             snake_case__					=   cache_dir
             snake_case__					=   force_download
             snake_case__					=   local_files_only
             snake_case__					=   revision

             snake_case__					=   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`."""							)
                   snake_case__					=   use_auth_token

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

             snake_case__					=   cls._get_arguments_from_pretrained(lowerCAmelCase__    ,		**lowerCAmelCase__							)
             return cls(*lowerCAmelCase__							)

       @classmethod
       def UpperCAmelCase_						(						cls       :						Optional[Any]    ,		lowerCAmelCase__       :						List[str]="AutoProcessor"							)			->    List[Any]:
             if not isinstance(lowerCAmelCase__    ,		lowerCAmelCase__							):
                   snake_case__					=   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.'''							)

             snake_case__					=   auto_class

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

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

       @property
       def UpperCAmelCase_						(						self       :						Dict							)			->    List[Any]:
             snake_case__					=   getattr(self    ,		self.attributes[0]							)
             return getattr(lowerCAmelCase__    ,		"""model_input_names"""    ,		lowerCAmelCase__							)



lowerCAmelCase						:   Dict										=    copy_func(ProcessorMixin.push_to_hub)
if ProcessorMixin.push_to_hub.__doc__ is not None:
      lowerCAmelCase						:   Optional[Any]										=    ProcessorMixin.push_to_hub.__doc__.format(
          object='''processor''', object_class='''AutoProcessor''', object_files='''processor files'''
      )









class     SCREAMING_SNAKE_CASE__     :





       '''simple docstring'''




       def __init__(						self       :						Tuple							)			->    None:
             # numbermap keys are the number and its values are lists of indexes sorted
             # in ascending order
             snake_case__					=   {}
             # indexmap keys are an index and it's values are the number at that index
             snake_case__					=   {}

       def UpperCAmelCase_						(						self       :						Optional[int]    ,		lowerCAmelCase__       :						list | str | range    ,		lowerCAmelCase__       :						int							)			->    list[int]:
             if isinstance(lowerCAmelCase__    ,		(range, str)							):
                   snake_case__					=   list(lowerCAmelCase__							)
             elif not isinstance(lowerCAmelCase__    ,		lowerCAmelCase__							):
                   raise TypeError(
                       """binary_search_delete() only accepts either a list, range or str"""							)

             snake_case__					=   0
             snake_case__					=   len(lowerCAmelCase__							) - 1

             while low <= high:
                   snake_case__					=   (low + high) // 2
                   if array[mid] == item:
                         array.pop(lowerCAmelCase__							)
                         return array
                   elif array[mid] < item:
                         snake_case__					=   mid + 1
                   else:
                         snake_case__					=   mid - 1
             raise ValueError(
                 """Either the item is not in the array or the array was unsorted"""							)

       def UpperCAmelCase_						(						self       :						Optional[Any]    ,		lowerCAmelCase__       :						list | str | range    ,		lowerCAmelCase__       :						int							)			->    list[int]:
             if isinstance(lowerCAmelCase__    ,		(range, str)							):
                   snake_case__					=   list(lowerCAmelCase__							)
             elif not isinstance(lowerCAmelCase__    ,		lowerCAmelCase__							):
                   raise TypeError(
                       """binary_search_insert() only accepts either a list, range or str"""							)

             snake_case__					=   0
             snake_case__					=   len(lowerCAmelCase__							) - 1

             while low <= high:
                   snake_case__					=   (low + high) // 2
                   if array[mid] == index:
                         # If the item already exists in the array,
                         # insert it after the existing item
                         array.insert(mid + 1    ,		lowerCAmelCase__							)
                         return array
                   elif array[mid] < index:
                         snake_case__					=   mid + 1
                   else:
                         snake_case__					=   mid - 1

        # If the item doesn't exist in the array, insert it at the appropriate position
             array.insert(lowerCAmelCase__    ,		lowerCAmelCase__							)
             return array

       def UpperCAmelCase_						(						self       :						str    ,		lowerCAmelCase__       :						int    ,		lowerCAmelCase__       :						int							)			->    None:
             # Remove previous index
             if index in self.indexmap:
                   snake_case__					=   self.indexmap[index]
                   if len(self.numbermap[n]							) == 1:
                         del self.numbermap[n]
                   else:
                         snake_case__					=   self.binary_search_delete(self.numbermap[n]    ,		lowerCAmelCase__							)

        # Set new index
             snake_case__					=   number

             # Number not seen before or empty so insert number value
             if number not in self.numbermap:
                   snake_case__					=   [index]

             # Here we need to perform a binary search insertion in order to insert
             # The item in the correct place
             else:
                   snake_case__					=   self.binary_search_insert(
                       self.numbermap[number]    ,		lowerCAmelCase__							)

       def UpperCAmelCase_						(						self       :						Any    ,		lowerCAmelCase__       :						int							)			->    int:
             return self.numbermap.get(lowerCAmelCase__    ,		[-1]							)[0]


if __name__ == "__main__":
       import doctest

       doctest.testmod()






