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


a_				=    logging.get_logger(__name__)

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


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






class 	__snake_case      (	SCREAMING_SNAKE_CASE__  ):





 """simple docstring"""


 _lowerCamelCase				 =  ["""feature_extractor""", """tokenizer"""]
 # Names need to be attr_class for attr in attributes
 _lowerCamelCase				 =  None
 _lowerCamelCase				 =  None
 _lowerCamelCase				 =  None

 def __init__(					self      ,		*__lowerCamelCase      ,		**__lowerCamelCase						):



     '''simple docstring'''




     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:
             __A					:		Union[str, Any]						=  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():
         __A					:		Union[str, Any]						=  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					:		Dict						=  AUTO_TO_BASE_CLASS_MAPPING.get(__lowerCamelCase      ,		__lowerCamelCase						)
         if isinstance(__lowerCamelCase      ,		__lowerCamelCase						):
             __A					:		Tuple						=  tuple(getattr(__lowerCamelCase      ,		__lowerCamelCase						) for n in class_name if n is not None						)
         else:
             __A					:		str						=  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						):



     '''simple docstring'''




     __A					:		str						=  [F"""- {name}: {repr(getattr(self      ,		__lowerCamelCase						)						)}""" for name in self.attributes]
     __A					:		Optional[int]						=  '''\n'''.join(__lowerCamelCase						)
     return F"""{self.__class__.__name__}:\n{attributes_repr}"""

 def       UpperCamelCase__(					self      ,		__lowerCamelCase      ,		__lowerCamelCase = False      ,		**__lowerCamelCase						):



     '''simple docstring'''




     os.makedirs(__lowerCamelCase      ,		exist_ok=__lowerCamelCase						)

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



     '''simple docstring'''




     __A					:		int						=  cache_dir
     __A					:		Optional[Any]						=  force_download
     __A					:		List[str]						=  local_files_only
     __A					:		Optional[int]						=  revision

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

     if token is not None:
         # change to `token` in a follow-up PR
         __A					:		List[str]						=  token

     __A					:		str						=  cls._get_arguments_from_pretrained(__lowerCamelCase      ,		**__lowerCamelCase						)
     return cls(*__lowerCamelCase						)

 @classmethod
 def       UpperCamelCase__(					cls      ,		__lowerCamelCase="AutoProcessor"						):



     '''simple docstring'''




     if not isinstance(__lowerCamelCase      ,		__lowerCamelCase						):
         __A					:		Union[str, Any]						=  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."""						)

     __A					:		int						=  auto_class

 @classmethod
 def       UpperCamelCase__(					cls      ,		__lowerCamelCase      ,		**__lowerCamelCase						):



     '''simple docstring'''




     __A					:		List[Any]						=  []
     for attribute_name in cls.attributes:
         __A					:		Optional[Any]						=  getattr(cls      ,		F"""{attribute_name}_class"""						)
         if isinstance(__lowerCamelCase      ,		__lowerCamelCase						):
             __A					:		List[str]						=  tuple(getattr(__lowerCamelCase      ,		__lowerCamelCase						) if n is not None else None for n in class_name						)
             __A					:		List[Any]						=  kwargs.get('''use_fast'''      ,		__lowerCamelCase						)
             if use_fast and classes[1] is not None:
                 __A					:		Dict						=  classes[1]
             else:
                 __A					:		Optional[int]						=  classes[0]
         else:
             __A					:		str						=  getattr(__lowerCamelCase      ,		__lowerCamelCase						)

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







 @property
 def       UpperCamelCase__(					self						):



     '''simple docstring'''




     __A					:		Union[str, Any]						=  getattr(self      ,		self.attributes[0]						)
     return getattr(__lowerCamelCase      ,		'''model_input_names'''      ,		__lowerCamelCase						)






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











"""simple docstring"""



import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin


a_				=    """
Hugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf originally as a company that developed a chatbot app targeted at teenagers.[2] After open-sourcing the model behind the chatbot, the company pivoted to focus on being a platform for machine learning.

In March 2021, Hugging Face raised $40 million in a Series B funding round.[3]

On April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model.[4] In 2022, the workshop concluded with the announcement of BLOOM, a multilingual large language model with 176 billion parameters.[5]
"""






class 	__snake_case      (	unittest.TestCase							,							SCREAMING_SNAKE_CASE__  ):





 """simple docstring"""


 def       UpperCamelCase__(					self						):



     '''simple docstring'''




     __A					:		Optional[Any]						=  load_tool('''summarization'''						)
     self.tool.setup()
     __A					:		Tuple						=  load_tool('''summarization'''      ,		remote=__lowerCamelCase						)

 def       UpperCamelCase__(					self						):



     '''simple docstring'''




     __A					:		List[str]						=  self.tool(__lowerCamelCase						)
     self.assertEqual(
         __lowerCamelCase      ,		'''Hugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf. In March 2021, Hugging Face raised $40 million in a Series B funding round. On April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model. In 2022, the workshop concluded with the announcement of BLOOM.'''      ,		)

 def       UpperCamelCase__(					self						):



     '''simple docstring'''




     __A					:		int						=  self.remote_tool(__lowerCamelCase						)
     self.assertEqual(
         __lowerCamelCase      ,		'''Hugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf. In March 2021, Hugging Face raised $40 million in a Series B funding round. On April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model. In 2022, the workshop concluded with the announcement of BLOOM.'''      ,		)

 def       UpperCamelCase__(					self						):



     '''simple docstring'''




     __A					:		List[Any]						=  self.tool(text=__lowerCamelCase						)
     self.assertEqual(
         __lowerCamelCase      ,		'''Hugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf. In March 2021, Hugging Face raised $40 million in a Series B funding round. On April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model. In 2022, the workshop concluded with the announcement of BLOOM.'''      ,		)







 def       UpperCamelCase__(					self						):



     '''simple docstring'''




     __A					:		Optional[int]						=  self.remote_tool(text=__lowerCamelCase						)
     self.assertEqual(
         __lowerCamelCase      ,		'''Hugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf. In March 2021, Hugging Face raised $40 million in a Series B funding round. On April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model. In 2022, the workshop concluded with the announcement of BLOOM.'''      ,		)




