"""simple docstring"""






import json
import os
import sys
import tempfile
import unittest
from pathlib import Path
from shutil import copyfile

from huggingface_hub import HfFolder, Repository, create_repo, delete_repo
from requests.exceptions import HTTPError

import transformers
from transformers import (
    CONFIG_MAPPING,
    FEATURE_EXTRACTOR_MAPPING,
    PROCESSOR_MAPPING,
    TOKENIZER_MAPPING,
    AutoConfig,
    AutoFeatureExtractor,
    AutoProcessor,
    AutoTokenizer,
    BertTokenizer,
    ProcessorMixin,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
    WavaVecaProcessor,
)
from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test
from transformers.tokenization_utils import TOKENIZER_CONFIG_FILE
from transformers.utils import FEATURE_EXTRACTOR_NAME, is_tokenizers_available


sys.path.append(str(Path(__file__).parent.parent.parent.parent / "utils"))

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402
from test_module.custom_processing import CustomProcessor  # noqa E402
from test_module.custom_tokenization import CustomTokenizer  # noqa E402


__A			   = get_tests_dir("fixtures/dummy_feature_extractor_config.json")
__A			   = get_tests_dir("fixtures/vocab.json")
__A			   = get_tests_dir("fixtures")


class     UpperCAmelCase						(unittest.TestCase  ):



    """simple docstring"""

    _UpperCAmelCase	:Tuple						    = ["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]", "bla", "blou"]






    def _snake_case			(   self							):
       lowercase__:   str					=      0






    def _snake_case			(   self							):
       lowercase__:   Dict					=      AutoProcessor.from_pretrained('''facebook/wav2vec2-base-960h'''							)
       self.assertIsInstance(__lowerCamelCase    ,			__lowerCamelCase							)






    def _snake_case			(   self							):
       with tempfile.TemporaryDirectory() as tmpdirname:
          lowercase__:   Union[str, Any]					=      WavaVecaConfig()
          lowercase__:   List[Any]					=      AutoProcessor.from_pretrained('''facebook/wav2vec2-base-960h'''							)

          # save in new folder
          model_config.save_pretrained(__lowerCamelCase							)
          processor.save_pretrained(__lowerCamelCase							)

          lowercase__:   Dict					=      AutoProcessor.from_pretrained(__lowerCamelCase							)

       self.assertIsInstance(__lowerCamelCase    ,			__lowerCamelCase							)






    def _snake_case			(   self							):
       with tempfile.TemporaryDirectory() as tmpdirname:
          # copy relevant files
          copyfile(__lowerCamelCase    ,			os.path.join(__lowerCamelCase    ,			__lowerCamelCase							)							)
          copyfile(__lowerCamelCase    ,			os.path.join(__lowerCamelCase    ,			'''vocab.json'''							)							)

          lowercase__:   str					=      AutoProcessor.from_pretrained(__lowerCamelCase							)

       self.assertIsInstance(__lowerCamelCase    ,			__lowerCamelCase							)






    def _snake_case			(   self							):
       with tempfile.TemporaryDirectory() as tmpdirname:
          lowercase__:   Dict					=      WavaVecaFeatureExtractor()
          lowercase__:   List[Any]					=      AutoTokenizer.from_pretrained('''facebook/wav2vec2-base-960h'''							)

          lowercase__:   Union[str, Any]					=      WavaVecaProcessor(__lowerCamelCase    ,			__lowerCamelCase							)

          # save in new folder
          processor.save_pretrained(__lowerCamelCase							)

          # drop `processor_class` in tokenizer
          with open(os.path.join(__lowerCamelCase    ,			__lowerCamelCase							)    ,			'''r'''							) as f:
             lowercase__:   Dict					=      json.load(__lowerCamelCase							)
             config_dict.pop('''processor_class'''							)

          with open(os.path.join(__lowerCamelCase    ,			__lowerCamelCase							)    ,			'''w'''							) as f:
             f.write(json.dumps(__lowerCamelCase							)							)

          lowercase__:   int					=      AutoProcessor.from_pretrained(__lowerCamelCase							)

       self.assertIsInstance(__lowerCamelCase    ,			__lowerCamelCase							)






    def _snake_case			(   self							):
       with tempfile.TemporaryDirectory() as tmpdirname:
          lowercase__:   Tuple					=      WavaVecaFeatureExtractor()
          lowercase__:   List[str]					=      AutoTokenizer.from_pretrained('''facebook/wav2vec2-base-960h'''							)

          lowercase__:   Union[str, Any]					=      WavaVecaProcessor(__lowerCamelCase    ,			__lowerCamelCase							)

          # save in new folder
          processor.save_pretrained(__lowerCamelCase							)

          # drop `processor_class` in feature extractor
          with open(os.path.join(__lowerCamelCase    ,			__lowerCamelCase							)    ,			'''r'''							) as f:
             lowercase__:   Optional[Any]					=      json.load(__lowerCamelCase							)
             config_dict.pop('''processor_class'''							)

          with open(os.path.join(__lowerCamelCase    ,			__lowerCamelCase							)    ,			'''w'''							) as f:
             f.write(json.dumps(__lowerCamelCase							)							)

          lowercase__:   str					=      AutoProcessor.from_pretrained(__lowerCamelCase							)

       self.assertIsInstance(__lowerCamelCase    ,			__lowerCamelCase							)






    def _snake_case			(   self							):
       with tempfile.TemporaryDirectory() as tmpdirname:
          lowercase__:   Tuple					=      WavaVecaConfig(processor_class='''Wav2Vec2Processor'''							)
          model_config.save_pretrained(__lowerCamelCase							)
          # copy relevant files
          copyfile(__lowerCamelCase    ,			os.path.join(__lowerCamelCase    ,			'''vocab.json'''							)							)
          # create emtpy sample processor
          with open(os.path.join(__lowerCamelCase    ,			__lowerCamelCase							)    ,			'''w'''							) as f:
             f.write('''{}'''							)

          lowercase__:   Optional[int]					=      AutoProcessor.from_pretrained(__lowerCamelCase							)

       self.assertIsInstance(__lowerCamelCase    ,			__lowerCamelCase							)






    def _snake_case			(   self							):
       # If remote code is not set, we will time out when asking whether to load the model.
       with self.assertRaises(__lowerCamelCase							):
          lowercase__:   Any					=      AutoProcessor.from_pretrained('''hf-internal-testing/test_dynamic_processor'''							)
       # If remote code is disabled, we can't load this config.
       with self.assertRaises(__lowerCamelCase							):
          lowercase__:   Optional[Any]					=      AutoProcessor.from_pretrained(
              '''hf-internal-testing/test_dynamic_processor'''    ,			trust_remote_code=__lowerCamelCase							)

       lowercase__:   str					=      AutoProcessor.from_pretrained('''hf-internal-testing/test_dynamic_processor'''    ,			trust_remote_code=__lowerCamelCase							)
       self.assertTrue(processor.special_attribute_present							)
       self.assertEqual(processor.__class__.__name__    ,			'''NewProcessor'''							)

       lowercase__:   Any					=      processor.feature_extractor
       self.assertTrue(feature_extractor.special_attribute_present							)
       self.assertEqual(feature_extractor.__class__.__name__    ,			'''NewFeatureExtractor'''							)

       lowercase__:   Dict					=      processor.tokenizer
       self.assertTrue(tokenizer.special_attribute_present							)
       if is_tokenizers_available():
          self.assertEqual(tokenizer.__class__.__name__    ,			'''NewTokenizerFast'''							)

          # Test we can also load the slow version
          lowercase__:   str					=      AutoProcessor.from_pretrained(
              '''hf-internal-testing/test_dynamic_processor'''    ,			trust_remote_code=__lowerCamelCase    ,			use_fast=__lowerCamelCase							)
          lowercase__:   List[Any]					=      new_processor.tokenizer
          self.assertTrue(new_tokenizer.special_attribute_present							)
          self.assertEqual(new_tokenizer.__class__.__name__    ,			'''NewTokenizer'''							)
       else:
          self.assertEqual(tokenizer.__class__.__name__    ,			'''NewTokenizer'''							)






    def _snake_case			(   self							):
       try:
          AutoConfig.register('''custom'''    ,			__lowerCamelCase							)
          AutoFeatureExtractor.register(__lowerCamelCase    ,			__lowerCamelCase							)
          AutoTokenizer.register(__lowerCamelCase    ,			slow_tokenizer_class=__lowerCamelCase							)
          AutoProcessor.register(__lowerCamelCase    ,			__lowerCamelCase							)
          # Trying to register something existing in the Transformers library will raise an error
          with self.assertRaises(__lowerCamelCase							):
             AutoProcessor.register(__lowerCamelCase    ,			__lowerCamelCase							)

          # Now that the config is registered, it can be used as any other config with the auto-API
          lowercase__:   Any					=      CustomFeatureExtractor.from_pretrained(__lowerCamelCase							)

          with tempfile.TemporaryDirectory() as tmp_dir:
             lowercase__:   Optional[int]					=      os.path.join(__lowerCamelCase    ,			'''vocab.txt'''							)
             with open(__lowerCamelCase    ,			'''w'''    ,			encoding='''utf-8'''							) as vocab_writer:
                vocab_writer.write(''''''.join([x + '''\n''' for x in self.vocab_tokens]							)							)
             lowercase__:   List[str]					=      CustomTokenizer(__lowerCamelCase							)

          lowercase__:   Tuple					=      CustomProcessor(__lowerCamelCase    ,			__lowerCamelCase							)

          with tempfile.TemporaryDirectory() as tmp_dir:
             processor.save_pretrained(__lowerCamelCase							)
             lowercase__:   Tuple					=      AutoProcessor.from_pretrained(__lowerCamelCase							)
             self.assertIsInstance(__lowerCamelCase    ,			__lowerCamelCase							)

       finally:
          if "custom" in CONFIG_MAPPING._extra_content:
             del CONFIG_MAPPING._extra_content["custom"]
          if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
             del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]
          if CustomConfig in TOKENIZER_MAPPING._extra_content:
             del TOKENIZER_MAPPING._extra_content[CustomConfig]
          if CustomConfig in PROCESSOR_MAPPING._extra_content:
             del PROCESSOR_MAPPING._extra_content[CustomConfig]






    def _snake_case			(   self							):


       class     UpperCAmelCase						(_UpperCAmelCase  ):



           """simple docstring"""

           _UpperCAmelCase	:Dict						    = False



       class     UpperCAmelCase						(_UpperCAmelCase  ):



           """simple docstring"""

           _UpperCAmelCase	:Optional[int]						    = False



       class     UpperCAmelCase						(_UpperCAmelCase  ):



           """simple docstring"""

           _UpperCAmelCase	:Optional[Any]						    = "AutoFeatureExtractor"
           _UpperCAmelCase	:List[str]						    = "AutoTokenizer"
           _UpperCAmelCase	:Optional[Any]						    = False

       try:
          AutoConfig.register('''custom'''    ,			__lowerCamelCase							)
          AutoFeatureExtractor.register(__lowerCamelCase    ,			__lowerCamelCase							)
          AutoTokenizer.register(__lowerCamelCase    ,			slow_tokenizer_class=__lowerCamelCase							)
          AutoProcessor.register(__lowerCamelCase    ,			__lowerCamelCase							)
          # If remote code is not set, the default is to use local classes.
          lowercase__:   Dict					=      AutoProcessor.from_pretrained('''hf-internal-testing/test_dynamic_processor'''							)
          self.assertEqual(processor.__class__.__name__    ,			'''NewProcessor'''							)
          self.assertFalse(processor.special_attribute_present							)
          self.assertFalse(processor.feature_extractor.special_attribute_present							)
          self.assertFalse(processor.tokenizer.special_attribute_present							)

          # If remote code is disabled, we load the local ones.
          lowercase__:   Any					=      AutoProcessor.from_pretrained(
              '''hf-internal-testing/test_dynamic_processor'''    ,			trust_remote_code=__lowerCamelCase							)
          self.assertEqual(processor.__class__.__name__    ,			'''NewProcessor'''							)
          self.assertFalse(processor.special_attribute_present							)
          self.assertFalse(processor.feature_extractor.special_attribute_present							)
          self.assertFalse(processor.tokenizer.special_attribute_present							)

          # If remote is enabled, we load from the Hub.
          lowercase__:   Union[str, Any]					=      AutoProcessor.from_pretrained(
              '''hf-internal-testing/test_dynamic_processor'''    ,			trust_remote_code=__lowerCamelCase							)
          self.assertEqual(processor.__class__.__name__    ,			'''NewProcessor'''							)
          self.assertTrue(processor.special_attribute_present							)
          self.assertTrue(processor.feature_extractor.special_attribute_present							)
          self.assertTrue(processor.tokenizer.special_attribute_present							)

       finally:
          if "custom" in CONFIG_MAPPING._extra_content:
             del CONFIG_MAPPING._extra_content["custom"]
          if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
             del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]
          if CustomConfig in TOKENIZER_MAPPING._extra_content:
             del TOKENIZER_MAPPING._extra_content[CustomConfig]
          if CustomConfig in PROCESSOR_MAPPING._extra_content:
             del PROCESSOR_MAPPING._extra_content[CustomConfig]






    def _snake_case			(   self							):
       lowercase__:   Tuple					=      AutoProcessor.from_pretrained('''hf-internal-testing/tiny-random-bert'''							)
       self.assertEqual(processor.__class__.__name__    ,			'''BertTokenizerFast'''							)






    def _snake_case			(   self							):
       lowercase__:   Optional[Any]					=      AutoProcessor.from_pretrained('''hf-internal-testing/tiny-random-convnext'''							)
       self.assertEqual(processor.__class__.__name__    ,			'''ConvNextImageProcessor'''							)



@is_staging_test
class     UpperCAmelCase						(unittest.TestCase  ):



    """simple docstring"""

    _UpperCAmelCase	:List[str]						    = ["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]", "bla", "blou"]






    @classmethod
    def _snake_case			(   cls							):
       lowercase__:   Any					=      TOKEN
       HfFolder.save_token(__lowerCamelCase							)






    @classmethod
    def _snake_case			(   cls							):
       try:
          delete_repo(token=cls._token    ,			repo_id='''test-processor'''							)
       except HTTPError:
          pass

       try:
          delete_repo(token=cls._token    ,			repo_id='''valid_org/test-processor-org'''							)
       except HTTPError:
          pass

       try:
          delete_repo(token=cls._token    ,			repo_id='''test-dynamic-processor'''							)
       except HTTPError:
          pass






    def _snake_case			(   self							):
       lowercase__:   Optional[Any]					=      WavaVecaProcessor.from_pretrained(__lowerCamelCase							)
       with tempfile.TemporaryDirectory() as tmp_dir:
          processor.save_pretrained(
              os.path.join(__lowerCamelCase    ,			'''test-processor'''							)    ,			push_to_hub=__lowerCamelCase    ,			use_auth_token=self._token							)

          lowercase__:   str					=      WavaVecaProcessor.from_pretrained(F"""{USER}/test-processor"""							)
          for k, v in processor.feature_extractor.__dict__.items():
             self.assertEqual(__lowerCamelCase    ,			getattr(new_processor.feature_extractor    ,			__lowerCamelCase							)							)
          self.assertDictEqual(new_processor.tokenizer.get_vocab()    ,			processor.tokenizer.get_vocab()							)






    def _snake_case			(   self							):
       lowercase__:   Optional[Any]					=      WavaVecaProcessor.from_pretrained(__lowerCamelCase							)

       with tempfile.TemporaryDirectory() as tmp_dir:
          processor.save_pretrained(
              os.path.join(__lowerCamelCase    ,			'''test-processor-org'''							)    ,			push_to_hub=__lowerCamelCase    ,			use_auth_token=self._token    ,			organization='''valid_org'''    ,			)

          lowercase__:   List[str]					=      WavaVecaProcessor.from_pretrained('''valid_org/test-processor-org'''							)
          for k, v in processor.feature_extractor.__dict__.items():
             self.assertEqual(__lowerCamelCase    ,			getattr(new_processor.feature_extractor    ,			__lowerCamelCase							)							)
          self.assertDictEqual(new_processor.tokenizer.get_vocab()    ,			processor.tokenizer.get_vocab()							)






    def _snake_case			(   self							):
       CustomFeatureExtractor.register_for_auto_class()
       CustomTokenizer.register_for_auto_class()
       CustomProcessor.register_for_auto_class()

       lowercase__:   Any					=      CustomFeatureExtractor.from_pretrained(__lowerCamelCase							)

       with tempfile.TemporaryDirectory() as tmp_dir:
          lowercase__:   str					=      os.path.join(__lowerCamelCase    ,			'''vocab.txt'''							)
          with open(__lowerCamelCase    ,			'''w'''    ,			encoding='''utf-8'''							) as vocab_writer:
             vocab_writer.write(''''''.join([x + '''\n''' for x in self.vocab_tokens]							)							)
          lowercase__:   List[Any]					=      CustomTokenizer(__lowerCamelCase							)

       lowercase__:   int					=      CustomProcessor(__lowerCamelCase    ,			__lowerCamelCase							)

       with tempfile.TemporaryDirectory() as tmp_dir:
          create_repo(F"""{USER}/test-dynamic-processor"""    ,			token=self._token							)
          lowercase__:   Optional[Any]					=      Repository(__lowerCamelCase    ,			clone_from=F"""{USER}/test-dynamic-processor"""    ,			token=self._token							)
          processor.save_pretrained(__lowerCamelCase							)

          # This has added the proper auto_map field to the feature extractor config
          self.assertDictEqual(
              processor.feature_extractor.auto_map    ,			{
                  '''AutoFeatureExtractor''': '''custom_feature_extraction.CustomFeatureExtractor''',
                  '''AutoProcessor''': '''custom_processing.CustomProcessor''',
              }    ,			)

          # This has added the proper auto_map field to the tokenizer config
          with open(os.path.join(__lowerCamelCase    ,			'''tokenizer_config.json'''							)							) as f:
             lowercase__:   Any					=      json.load(__lowerCamelCase							)
          self.assertDictEqual(
              tokenizer_config['''auto_map''']    ,			{
                  '''AutoTokenizer''': ['''custom_tokenization.CustomTokenizer''', None],
                  '''AutoProcessor''': '''custom_processing.CustomProcessor''',
              }    ,			)

          # The code has been copied from fixtures
          self.assertTrue(os.path.isfile(os.path.join(__lowerCamelCase    ,			'''custom_feature_extraction.py'''							)							)							)
          self.assertTrue(os.path.isfile(os.path.join(__lowerCamelCase    ,			'''custom_tokenization.py'''							)							)							)
          self.assertTrue(os.path.isfile(os.path.join(__lowerCamelCase    ,			'''custom_processing.py'''							)							)							)

          repo.push_to_hub()

       lowercase__:   List[Any]					=      AutoProcessor.from_pretrained(F"""{USER}/test-dynamic-processor"""    ,			trust_remote_code=__lowerCamelCase							)
       # Can't make an isinstance check because the new_processor is from the CustomProcessor class of a dynamic module
       self.assertEqual(new_processor.__class__.__name__    ,			'''CustomProcessor'''							)




def 							_UpperCAmelCase				(UpperCamelCase__     :  Optional[Any]				,							UpperCamelCase__     :  Union[str, Any]						):
       # "extended trapezoidal rule"
       # int(f) = dx/2 * (f1 + 2f2 + ... + fn)
       _A      :       int				    = (boundary[1] - boundary[0]) / steps
       _A      :       Any				    = boundary[0]
       _A      :       List[Any]				    = boundary[1]
       _A      :       str				    = make_points(UpperCamelCase__				,							UpperCamelCase__				,							UpperCamelCase__						)
       _A      :       str				    = 0.0
       y += (h / 2.0) * f(UpperCamelCase__						)
       for i in x_i:
              # print(i)
              y += h * f(UpperCamelCase__						)
       y += (h / 2.0) * f(UpperCamelCase__						)
       return y
def 							_UpperCAmelCase				(UpperCamelCase__     :  List[str]				,							UpperCamelCase__     :  Optional[int]				,							UpperCamelCase__     :  Any						):
       _A      :       Optional[int]				    = a + h
       while x < (b - h):
              yield x
              _A      :       Dict				    = x + h
def 							_UpperCAmelCase				(UpperCamelCase__     :  Optional[int]						):  # enter your function here
       _A      :       Any				    = (x - 0) * (x - 0)
       return y
def 							_UpperCAmelCase				():
       _A      :       Optional[Any]				    = 0.0  # Lower bound of integration
       _A      :       Optional[int]				    = 1.0  # Upper bound of integration
       _A      :       List[Any]				    = 10.0  # define number of steps or resolution
       _A      :       Any				    = [a, b]  # define boundary of integration
       _A      :       Tuple				    = method_a(UpperCamelCase__				,							UpperCamelCase__						)
       print(f"y = {y}"						)


if __name__ == "__main__":
    main()
