


"""simple docstring"""
import json
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
from transformers import (
    CONFIG_MAPPING,
    FEATURE_EXTRACTOR_MAPPING,
    AutoConfig,
    AutoFeatureExtractor,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
)
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
from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402


__SCREAMING_SNAKE_CASE							:      int						=      get_tests_dir('fixtures')
__SCREAMING_SNAKE_CASE							:      Optional[Any]						=      get_tests_dir('fixtures/dummy_feature_extractor_config.json')
__SCREAMING_SNAKE_CASE							:      int						=      get_tests_dir('fixtures/dummy-config.json')





class        __A       (unittest.TestCase):





							'''simple docstring'''

							def 						lowerCAmelCase    (			self							: str    )   ->str:



														"""simple docstring"""



														snake_case_          =						0



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



														"""simple docstring"""



														snake_case_          =						AutoFeatureExtractor.from_pretrained("""facebook/wav2vec2-base-960h"""    )
														self.assertIsInstance(UpperCAmelCase_						,    UpperCAmelCase_    )



							def 						lowerCAmelCase    (			self							: Dict    )   ->str:



														"""simple docstring"""



														snake_case_          =						AutoFeatureExtractor.from_pretrained(UpperCAmelCase_    )
														self.assertIsInstance(UpperCAmelCase_						,    UpperCAmelCase_    )



							def 						lowerCAmelCase    (			self							: List[Any]    )   ->Optional[Any]:



														"""simple docstring"""



														with tempfile.TemporaryDirectory() as tmpdirname:
																					snake_case_          =						WavaVecaConfig()

																					# remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally
																					snake_case_          =						AutoFeatureExtractor.from_pretrained(UpperCAmelCase_    ).to_dict()

																					config_dict.pop("""feature_extractor_type"""    )
																					snake_case_          =						WavaVecaFeatureExtractor(**UpperCAmelCase_    )

																					# save in new folder
																					model_config.save_pretrained(UpperCAmelCase_    )
																					config.save_pretrained(UpperCAmelCase_    )

																					snake_case_          =						AutoFeatureExtractor.from_pretrained(UpperCAmelCase_    )

																					# make sure private variable is not incorrectly saved
																					snake_case_          =						json.loads(config.to_json_string()    )
																					self.assertTrue("""_processor_class""" not in dict_as_saved    )

														self.assertIsInstance(UpperCAmelCase_						,    UpperCAmelCase_    )



							def 						lowerCAmelCase    (			self							: List[str]    )   ->List[Any]:



														"""simple docstring"""



														snake_case_          =						AutoFeatureExtractor.from_pretrained(UpperCAmelCase_    )
														self.assertIsInstance(UpperCAmelCase_						,    UpperCAmelCase_    )



							def 						lowerCAmelCase    (			self							: List[str]    )   ->Optional[Any]:



														"""simple docstring"""



														with self.assertRaisesRegex(
														    UpperCAmelCase_						,    """bert-base is not a local folder and is not a valid model identifier"""    ):
																					snake_case_          =						AutoFeatureExtractor.from_pretrained("""bert-base"""    )



							def 						lowerCAmelCase    (			self							: Tuple    )   ->Optional[int]:



														"""simple docstring"""



														with self.assertRaisesRegex(
														    UpperCAmelCase_						,    R"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"""    ):
																					snake_case_          =						AutoFeatureExtractor.from_pretrained(UpperCAmelCase_						,    revision="""aaaaaa"""    )



							def 						lowerCAmelCase    (			self							: str    )   ->Dict:



														"""simple docstring"""



														with self.assertRaisesRegex(
														    UpperCAmelCase_						,    """hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json."""						,    ):
																					snake_case_          =						AutoFeatureExtractor.from_pretrained("""hf-internal-testing/config-no-model"""    )



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



														"""simple docstring"""



														with self.assertRaises(UpperCAmelCase_    ):
																					snake_case_          =						AutoFeatureExtractor.from_pretrained(
																					    """hf-internal-testing/test_dynamic_feature_extractor"""    )
														# If remote code is disabled, we can't load this config.
														with self.assertRaises(UpperCAmelCase_    ):
																					snake_case_          =						AutoFeatureExtractor.from_pretrained(
																					    """hf-internal-testing/test_dynamic_feature_extractor"""						,    trust_remote_code=UpperCAmelCase_    )

														snake_case_          =						AutoFeatureExtractor.from_pretrained(
														    """hf-internal-testing/test_dynamic_feature_extractor"""						,    trust_remote_code=UpperCAmelCase_    )
														self.assertEqual(feature_extractor.__class__.__name__						,    """NewFeatureExtractor"""    )

														# Test feature extractor can be reloaded.
														with tempfile.TemporaryDirectory() as tmp_dir:
																					feature_extractor.save_pretrained(UpperCAmelCase_    )
																					snake_case_          =						AutoFeatureExtractor.from_pretrained(UpperCAmelCase_						,    trust_remote_code=UpperCAmelCase_    )
														self.assertEqual(reloaded_feature_extractor.__class__.__name__						,    """NewFeatureExtractor"""    )



							def 						lowerCAmelCase    (			self							: int    )   ->int:



														"""simple docstring"""



														try:
																					AutoConfig.register("""custom"""						,    UpperCAmelCase_    )
																					AutoFeatureExtractor.register(UpperCAmelCase_						,    UpperCAmelCase_    )
																					# Trying to register something existing in the Transformers library will raise an error
																					with self.assertRaises(UpperCAmelCase_    ):
																												AutoFeatureExtractor.register(UpperCAmelCase_						,    UpperCAmelCase_    )

																					# Now that the config is registered, it can be used as any other config with the auto-API
																					snake_case_          =						CustomFeatureExtractor.from_pretrained(UpperCAmelCase_    )
																					with tempfile.TemporaryDirectory() as tmp_dir:
																												feature_extractor.save_pretrained(UpperCAmelCase_    )
																												snake_case_          =						AutoFeatureExtractor.from_pretrained(UpperCAmelCase_    )
																												self.assertIsInstance(UpperCAmelCase_						,    UpperCAmelCase_    )

														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]



							def 						lowerCAmelCase    (			self							: List[str]    )   ->Any:



														"""simple docstring"""



														class        __A       (snake_case__):





																					'''simple docstring'''

																					__lowercase:						List[str]							 =      True

														try:
																					AutoConfig.register("""custom"""						,    UpperCAmelCase_    )
																					AutoFeatureExtractor.register(UpperCAmelCase_						,    UpperCAmelCase_    )
																					# If remote code is not set, the default is to use local
																					snake_case_          =						AutoFeatureExtractor.from_pretrained(
																					    """hf-internal-testing/test_dynamic_feature_extractor"""    )
																					self.assertEqual(feature_extractor.__class__.__name__						,    """NewFeatureExtractor"""    )
																					self.assertTrue(feature_extractor.is_local    )

																					# If remote code is disabled, we load the local one.
																					snake_case_          =						AutoFeatureExtractor.from_pretrained(
																					    """hf-internal-testing/test_dynamic_feature_extractor"""						,    trust_remote_code=UpperCAmelCase_    )
																					self.assertEqual(feature_extractor.__class__.__name__						,    """NewFeatureExtractor"""    )
																					self.assertTrue(feature_extractor.is_local    )

																					# If remote is enabled, we load from the Hub
																					snake_case_          =						AutoFeatureExtractor.from_pretrained(
																					    """hf-internal-testing/test_dynamic_feature_extractor"""						,    trust_remote_code=UpperCAmelCase_    )
																					self.assertEqual(feature_extractor.__class__.__name__						,    """NewFeatureExtractor"""    )
																					self.assertTrue(not hasattr(UpperCAmelCase_						,    """is_local"""    )    )

														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]







"""simple docstring"""
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        __A       (unittest.TestCase):





							'''simple docstring'''

							def 						lowerCAmelCase    (			self							: Dict    )   ->List[Any]:



														"""simple docstring"""



														snake_case_          =						"""laion/clap-htsat-unfused"""
														snake_case_          =						tempfile.mkdtemp()



							def 						lowerCAmelCase    (			self							: List[str]						,    **UpperCAmelCase_							: Tuple    )   ->str:



														"""simple docstring"""



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



							def 						lowerCAmelCase    (			self							: Optional[Any]						,    **UpperCAmelCase_							: Any    )   ->Optional[Any]:



														"""simple docstring"""



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



							def 						lowerCAmelCase    (			self							: str    )   ->Any:



														"""simple docstring"""



														shutil.rmtree(self.tmpdirname    )



							def 						lowerCAmelCase    (			self							: Tuple    )   ->Optional[int]:



														"""simple docstring"""



														snake_case_          =						self.get_tokenizer()
														snake_case_          =						self.get_feature_extractor()

														snake_case_          =						ClapProcessor(tokenizer=UpperCAmelCase_						,    feature_extractor=UpperCAmelCase_    )

														processor.save_pretrained(self.tmpdirname    )
														snake_case_          =						ClapProcessor.from_pretrained(self.tmpdirname    )

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

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



							def 						lowerCAmelCase    (			self							: int    )   ->List[str]:



														"""simple docstring"""



														snake_case_          =						ClapProcessor(tokenizer=self.get_tokenizer()						,    feature_extractor=self.get_feature_extractor()    )
														processor.save_pretrained(self.tmpdirname    )

														snake_case_          =						self.get_tokenizer(bos_token="""(BOS)"""						,    eos_token="""(EOS)"""    )
														snake_case_          =						self.get_feature_extractor(do_normalize=UpperCAmelCase_						,    padding_value=1.0    )

														snake_case_          =						ClapProcessor.from_pretrained(
														    self.tmpdirname						,    bos_token="""(BOS)"""						,    eos_token="""(EOS)"""						,    do_normalize=UpperCAmelCase_						,    padding_value=1.0    )

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

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



							def 						lowerCAmelCase    (			self							: str    )   ->Dict:



														"""simple docstring"""



														snake_case_          =						self.get_feature_extractor()
														snake_case_          =						self.get_tokenizer()

														snake_case_          =						ClapProcessor(tokenizer=UpperCAmelCase_						,    feature_extractor=UpperCAmelCase_    )

														snake_case_          =						floats_list((3, 1_000)    )

														snake_case_          =						feature_extractor(UpperCAmelCase_						,    return_tensors="""np"""    )
														snake_case_          =						processor(audios=UpperCAmelCase_						,    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 						lowerCAmelCase    (			self							: Union[str, Any]    )   ->Optional[int]:



														"""simple docstring"""



														snake_case_          =						self.get_feature_extractor()
														snake_case_          =						self.get_tokenizer()

														snake_case_          =						ClapProcessor(tokenizer=UpperCAmelCase_						,    feature_extractor=UpperCAmelCase_    )

														snake_case_          =						"""This is a test string"""

														snake_case_          =						processor(text=UpperCAmelCase_    )

														snake_case_          =						tokenizer(UpperCAmelCase_    )

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



							def 						lowerCAmelCase    (			self							: Optional[int]    )   ->Tuple:



														"""simple docstring"""



														snake_case_          =						self.get_feature_extractor()
														snake_case_          =						self.get_tokenizer()

														snake_case_          =						ClapProcessor(tokenizer=UpperCAmelCase_						,    feature_extractor=UpperCAmelCase_    )

														snake_case_          =						[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

														snake_case_          =						processor.batch_decode(UpperCAmelCase_    )
														snake_case_          =						tokenizer.batch_decode(UpperCAmelCase_    )

														self.assertListEqual(UpperCAmelCase_						,    UpperCAmelCase_    )



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



														"""simple docstring"""



														snake_case_          =						self.get_feature_extractor()
														snake_case_          =						self.get_tokenizer()

														snake_case_          =						ClapProcessor(tokenizer=UpperCAmelCase_						,    feature_extractor=UpperCAmelCase_    )

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



