






import shutil
import tempfile
import unittest

from transformers import ClapFeatureExtractor, ClapProcessor, RobertaTokenizer, RobertaTokenizerFast
from transformers.testing_utils import require_sentencepiece, require_torchaudio

from .test_feature_extraction_clap import floats_list




@require_torchaudio
@require_sentencepiece
class 			snake_case__   (							unittest.TestCase):

       '''simple docstring'''



       def 					__lowercase						(							self    ) ->      int:






            '''simple docstring'''





            __snake_case			:List[Any]          =						"""laion/clap-htsat-unfused"""
            __snake_case			:Optional[Any]          =						tempfile.mkdtemp()



       def 					__lowercase						(							self			,						**a__    ) ->      List[str]:






            '''simple docstring'''





            return RobertaTokenizer.from_pretrained(self.checkpoint			,						**a__    )



       def 					__lowercase						(							self			,						**a__    ) ->      Optional[int]:






            '''simple docstring'''





            return ClapFeatureExtractor.from_pretrained(self.checkpoint			,						**a__    )



       def 					__lowercase						(							self    ) ->      Tuple:






            '''simple docstring'''





            shutil.rmtree(self.tmpdirname    )



       def 					__lowercase						(							self    ) ->      int:






            '''simple docstring'''





            __snake_case			:Union[str, Any]          =						self.get_tokenizer()
            __snake_case			:Optional[Any]          =						self.get_feature_extractor()

            __snake_case			:Union[str, Any]          =						ClapProcessor(tokenizer=a__			,						feature_extractor=a__    )

            processor.save_pretrained(self.tmpdirname    )
            __snake_case			:Optional[int]          =						ClapProcessor.from_pretrained(self.tmpdirname    )

            self.assertEqual(processor.tokenizer.get_vocab()			,						tokenizer.get_vocab()    )
            self.assertIsInstance(processor.tokenizer			,						a__    )

            self.assertEqual(processor.feature_extractor.to_json_string()			,						feature_extractor.to_json_string()    )
            self.assertIsInstance(processor.feature_extractor			,						a__    )



       def 					__lowercase						(							self    ) ->      str:






            '''simple docstring'''





            __snake_case			:Optional[int]          =						ClapProcessor(tokenizer=self.get_tokenizer()			,						feature_extractor=self.get_feature_extractor()    )
            processor.save_pretrained(self.tmpdirname    )

            __snake_case			:Optional[Any]          =						self.get_tokenizer(bos_token="""(BOS)"""			,						eos_token="""(EOS)"""    )
            __snake_case			:Dict          =						self.get_feature_extractor(do_normalize=a__			,						padding_value=1.0    )

            __snake_case			:Optional[Any]          =						ClapProcessor.from_pretrained(
                self.tmpdirname			,						bos_token="""(BOS)"""			,						eos_token="""(EOS)"""			,						do_normalize=a__			,						padding_value=1.0    )

            self.assertEqual(processor.tokenizer.get_vocab()			,						tokenizer_add_kwargs.get_vocab()    )
            self.assertIsInstance(processor.tokenizer			,						a__    )

            self.assertEqual(processor.feature_extractor.to_json_string()			,						feature_extractor_add_kwargs.to_json_string()    )
            self.assertIsInstance(processor.feature_extractor			,						a__    )



       def 					__lowercase						(							self    ) ->      Union[str, Any]:






            '''simple docstring'''





            __snake_case			:Any          =						self.get_feature_extractor()
            __snake_case			:List[Any]          =						self.get_tokenizer()

            __snake_case			:Any          =						ClapProcessor(tokenizer=a__			,						feature_extractor=a__    )

            __snake_case			:int          =						floats_list((3, 10_00)    )

            __snake_case			:Optional[int]          =						feature_extractor(a__			,						return_tensors="""np"""    )
            __snake_case			:List[str]          =						processor(audios=a__			,						return_tensors="""np"""    )

            for key in input_feat_extract.keys():
                 self.assertAlmostEqual(input_feat_extract[key].sum()			,						input_processor[key].sum()			,						delta=1e-2    )



       def 					__lowercase						(							self    ) ->      List[str]:






            '''simple docstring'''





            __snake_case			:Optional[Any]          =						self.get_feature_extractor()
            __snake_case			:Optional[int]          =						self.get_tokenizer()

            __snake_case			:List[Any]          =						ClapProcessor(tokenizer=a__			,						feature_extractor=a__    )

            __snake_case			:Dict          =						"""This is a test string"""

            __snake_case			:List[Any]          =						processor(text=a__    )

            __snake_case			:Tuple          =						tokenizer(a__    )

            for key in encoded_tok.keys():
                 self.assertListEqual(encoded_tok[key]			,						encoded_processor[key]    )



       def 					__lowercase						(							self    ) ->      Tuple:






            '''simple docstring'''





            __snake_case			:Tuple          =						self.get_feature_extractor()
            __snake_case			:Dict          =						self.get_tokenizer()

            __snake_case			:Union[str, Any]          =						ClapProcessor(tokenizer=a__			,						feature_extractor=a__    )

            __snake_case			:Union[str, Any]          =						[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

            __snake_case			:List[str]          =						processor.batch_decode(a__    )
            __snake_case			:List[str]          =						tokenizer.batch_decode(a__    )

            self.assertListEqual(a__			,						a__    )



       def 					__lowercase						(							self    ) ->      Optional[int]:






            '''simple docstring'''





            __snake_case			:Tuple          =						self.get_feature_extractor()
            __snake_case			:Union[str, Any]          =						self.get_tokenizer()

            __snake_case			:Optional[Any]          =						ClapProcessor(tokenizer=a__			,						feature_extractor=a__    )

            self.assertListEqual(
                processor.model_input_names[2:]			,						feature_extractor.model_input_names			,						msg="""`processor` and `feature_extractor` model input names do not match"""			,						)










import importlib
import json
import os
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
import transformers.models.auto
from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig
from transformers.models.bert.configuration_bert import BertConfig
from transformers.models.roberta.configuration_roberta import RobertaConfig
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


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

from test_module.custom_configuration import CustomConfig  # noqa E402


lowerCamelCase__							=     get_tests_dir("""fixtures/dummy-config.json""")




class 			snake_case__   (							unittest.TestCase):

       '''simple docstring'''



       def 					__lowercase						(							self    ) ->      Union[str, Any]:






            '''simple docstring'''





            __snake_case			:Optional[int]          =						0



       def 					__lowercase						(							self    ) ->      int:






            '''simple docstring'''





            self.assertIsNotNone(transformers.models.auto.__spec__    )
            self.assertIsNotNone(importlib.util.find_spec("""transformers.models.auto"""    )    )



       def 					__lowercase						(							self    ) ->      List[str]:






            '''simple docstring'''





            __snake_case			:Optional[Any]          =						AutoConfig.from_pretrained("""bert-base-uncased"""    )
            self.assertIsInstance(a__			,						a__    )



       def 					__lowercase						(							self    ) ->      Optional[int]:






            '''simple docstring'''





            __snake_case			:List[str]          =						AutoConfig.from_pretrained(a__    )
            self.assertIsInstance(a__			,						a__    )



       def 					__lowercase						(							self    ) ->      List[Any]:






            '''simple docstring'''





            __snake_case			:Tuple          =						AutoConfig.from_pretrained(a__    )
            self.assertIsInstance(a__			,						a__    )



       def 					__lowercase						(							self    ) ->      str:






            '''simple docstring'''





            __snake_case			:Optional[int]          =						AutoConfig.for_model("""roberta"""    )
            self.assertIsInstance(a__			,						a__    )



       def 					__lowercase						(							self    ) ->      List[Any]:






            '''simple docstring'''





            with tempfile.TemporaryDirectory() as tmp_dir:
                 # This model name contains bert and roberta, but roberta ends up being picked.
                 __snake_case			:List[Any]          =						os.path.join(a__			,						"""fake-roberta"""    )
                 os.makedirs(a__			,						exist_ok=a__    )
                 with open(os.path.join(a__			,						"""config.json"""    )			,						"""w"""    ) as f:
                      f.write(json.dumps({}    )    )
                 __snake_case			:Optional[Any]          =						AutoConfig.from_pretrained(a__    )
                 self.assertEqual(type(a__    )			,						a__    )



       def 					__lowercase						(							self    ) ->      Dict:






            '''simple docstring'''





            try:
                 AutoConfig.register("""custom"""			,						a__    )
                 # Wrong model type will raise an error
                 with self.assertRaises(a__    ):
                      AutoConfig.register("""model"""			,						a__    )
                 # Trying to register something existing in the Transformers library will raise an error
                 with self.assertRaises(a__    ):
                      AutoConfig.register("""bert"""			,						a__    )

                 # Now that the config is registered, it can be used as any other config with the auto-API
                 __snake_case			:Optional[int]          =						CustomConfig()
                 with tempfile.TemporaryDirectory() as tmp_dir:
                      config.save_pretrained(a__    )
                      __snake_case			:Tuple          =						AutoConfig.from_pretrained(a__    )
                      self.assertIsInstance(a__			,						a__    )

            finally:
                 if "custom" in CONFIG_MAPPING._extra_content:
                      del CONFIG_MAPPING._extra_content["custom"]



       def 					__lowercase						(							self    ) ->      int:






            '''simple docstring'''





            with self.assertRaisesRegex(
                a__			,						"""bert-base is not a local folder and is not a valid model identifier"""    ):
                 __snake_case			:str          =						AutoConfig.from_pretrained("""bert-base"""    )



       def 					__lowercase						(							self    ) ->      str:






            '''simple docstring'''





            with self.assertRaisesRegex(
                a__			,						R"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"""    ):
                 __snake_case			:Dict          =						AutoConfig.from_pretrained(a__			,						revision="""aaaaaa"""    )



       def 					__lowercase						(							self    ) ->      Optional[Any]:






            '''simple docstring'''





            with self.assertRaisesRegex(
                a__			,						"""hf-internal-testing/no-config-test-repo does not appear to have a file named config.json."""			,						):
                 __snake_case			:Optional[int]          =						AutoConfig.from_pretrained("""hf-internal-testing/no-config-test-repo"""    )



       def 					__lowercase						(							self    ) ->      int:






            '''simple docstring'''





            with self.assertRaises(a__    ):
                 __snake_case			:Dict          =						AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""    )
            # If remote code is disabled, we can't load this config.
            with self.assertRaises(a__    ):
                 __snake_case			:int          =						AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""			,						trust_remote_code=a__    )

            __snake_case			:Tuple          =						AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""			,						trust_remote_code=a__    )
            self.assertEqual(config.__class__.__name__			,						"""NewModelConfig"""    )

            # Test config can be reloaded.
            with tempfile.TemporaryDirectory() as tmp_dir:
                 config.save_pretrained(a__    )
                 __snake_case			:Union[str, Any]          =						AutoConfig.from_pretrained(a__			,						trust_remote_code=a__    )
            self.assertEqual(reloaded_config.__class__.__name__			,						"""NewModelConfig"""    )



       def 					__lowercase						(							self    ) ->      Dict:






            '''simple docstring'''





            class 			snake_case__   (							lowercase_):

                   '''simple docstring'''
                   lowerCamelCase							:				Optional[Any] 				= "new-model"

            try:
                 AutoConfig.register("""new-model"""			,						a__    )
                 # If remote code is not set, the default is to use local
                 __snake_case			:List[Any]          =						AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""    )
                 self.assertEqual(config.__class__.__name__			,						"""NewModelConfigLocal"""    )

                 # If remote code is disabled, we load the local one.
                 __snake_case			:Tuple          =						AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""			,						trust_remote_code=a__    )
                 self.assertEqual(config.__class__.__name__			,						"""NewModelConfigLocal"""    )

                 # If remote is enabled, we load from the Hub
                 __snake_case			:List[Any]          =						AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""			,						trust_remote_code=a__    )
                 self.assertEqual(config.__class__.__name__			,						"""NewModelConfig"""    )

            finally:
                 if "new-model" in CONFIG_MAPPING._extra_content:
                      del CONFIG_MAPPING._extra_content["new-model"]


