






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


A__      :     List[str]       =					get_tests_dir('fixtures')
A__      :     int       =					get_tests_dir('fixtures/dummy_feature_extractor_config.json')
A__      :     List[Any]       =					get_tests_dir('fixtures/dummy-config.json')
class  _UpperCAmelCase   (			unittest.TestCase      ):

				"""simple docstring"""




				def 							lowercase__  (	self		:       Tuple    ):

								'''simple docstring'''
								lowercase__  					=     0




				def 							lowercase__  (	self		:       Optional[int]    ):

								'''simple docstring'''
								lowercase__  					=     AutoFeatureExtractor.from_pretrained('''facebook/wav2vec2-base-960h'''    )
								self.assertIsInstance(lowerCamelCase,    lowerCamelCase    )




				def 							lowercase__  (	self		:       List[Any]    ):

								'''simple docstring'''
								lowercase__  					=     AutoFeatureExtractor.from_pretrained(lowerCamelCase    )
								self.assertIsInstance(lowerCamelCase,    lowerCamelCase    )




				def 							lowercase__  (	self		:       int    ):

								'''simple docstring'''
								with tempfile.TemporaryDirectory() as tmpdirname:
												lowercase__  					=     WavaVecaConfig()

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

												config_dict.pop('''feature_extractor_type'''    )
												lowercase__  					=     WavaVecaFeatureExtractor(**lowerCamelCase    )

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

												lowercase__  					=     AutoFeatureExtractor.from_pretrained(lowerCamelCase    )

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

								self.assertIsInstance(lowerCamelCase,    lowerCamelCase    )




				def 							lowercase__  (	self		:       Optional[int]    ):

								'''simple docstring'''
								lowercase__  					=     AutoFeatureExtractor.from_pretrained(lowerCamelCase    )
								self.assertIsInstance(lowerCamelCase,    lowerCamelCase    )




				def 							lowercase__  (	self		:       Optional[Any]    ):

								'''simple docstring'''
								with self.assertRaisesRegex(
								    lowerCamelCase,    '''bert-base is not a local folder and is not a valid model identifier'''    ):
												lowercase__  					=     AutoFeatureExtractor.from_pretrained('''bert-base'''    )




				def 							lowercase__  (	self		:       Union[str, Any]    ):

								'''simple docstring'''
								with self.assertRaisesRegex(
								    lowerCamelCase,    R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''    ):
												lowercase__  					=     AutoFeatureExtractor.from_pretrained(lowerCamelCase,    revision='''aaaaaa'''    )




				def 							lowercase__  (	self		:       int    ):

								'''simple docstring'''
								with self.assertRaisesRegex(
								    lowerCamelCase,    '''hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.''',    ):
												lowercase__  					=     AutoFeatureExtractor.from_pretrained('''hf-internal-testing/config-no-model'''    )




				def 							lowercase__  (	self		:       int    ):

								'''simple docstring'''
								# If remote code is not set, we will time out when asking whether to load the model.
								with self.assertRaises(lowerCamelCase    ):
												lowercase__  					=     AutoFeatureExtractor.from_pretrained(
												    '''hf-internal-testing/test_dynamic_feature_extractor'''    )
								# If remote code is disabled, we can't load this config.
								with self.assertRaises(lowerCamelCase    ):
												lowercase__  					=     AutoFeatureExtractor.from_pretrained(
												    '''hf-internal-testing/test_dynamic_feature_extractor''',    trust_remote_code=lowerCamelCase    )

								lowercase__  					=     AutoFeatureExtractor.from_pretrained(
								    '''hf-internal-testing/test_dynamic_feature_extractor''',    trust_remote_code=lowerCamelCase    )
								self.assertEqual(feature_extractor.__class__.__name__,    '''NewFeatureExtractor'''    )

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




				def 							lowercase__  (	self		:       str    ):

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

												# Now that the config is registered, it can be used as any other config with the auto-API
												lowercase__  					=     CustomFeatureExtractor.from_pretrained(lowerCamelCase    )
												with tempfile.TemporaryDirectory() as tmp_dir:
																feature_extractor.save_pretrained(lowerCamelCase    )
																lowercase__  					=     AutoFeatureExtractor.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]




				def 							lowercase__  (	self		:       Optional[int]    ):

								'''simple docstring'''
								class  _UpperCAmelCase   (			A__      ):

												"""simple docstring"""




												lowercase__											=			True

								try:
												AutoConfig.register('''custom''',    lowerCamelCase    )
												AutoFeatureExtractor.register(lowerCamelCase,    lowerCamelCase    )
												# If remote code is not set, the default is to use local
												lowercase__  					=     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.
												lowercase__  					=     AutoFeatureExtractor.from_pretrained(
												    '''hf-internal-testing/test_dynamic_feature_extractor''',    trust_remote_code=lowerCamelCase    )
												self.assertEqual(feature_extractor.__class__.__name__,    '''NewFeatureExtractor'''    )
												self.assertTrue(feature_extractor.is_local    )

												# If remote is enabled, we load from the Hub
												lowercase__  					=     AutoFeatureExtractor.from_pretrained(
												    '''hf-internal-testing/test_dynamic_feature_extractor''',    trust_remote_code=lowerCamelCase    )
												self.assertEqual(feature_extractor.__class__.__name__,    '''NewFeatureExtractor'''    )
												self.assertTrue(not hasattr(lowerCamelCase,    '''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]












from typing import Dict

from .base import GenericTensor, Pipeline
class  _UpperCAmelCase   (			A__      ):

				"""simple docstring"""




				def 							lowercase__  (	self		:       Any,    lowerCamelCase		:       Tuple=None,    lowerCamelCase		:       Union[str, Any]=None,    lowerCamelCase		:       Union[str, Any]=None,    **lowerCamelCase		:       List[Any]    ):

								'''simple docstring'''
								if tokenize_kwargs is None:
												lowercase__  					=     {}

								if truncation is not None:
												if "truncation" in tokenize_kwargs:
																raise ValueError(
																    '''truncation parameter defined twice (given as keyword argument as well as in tokenize_kwargs)'''    )
												lowercase__  					=     truncation

								lowercase__  					=     tokenize_kwargs

								lowercase__  					=     {}
								if return_tensors is not None:
												lowercase__  					=     return_tensors

								return preprocess_params, {}, postprocess_params




				def 							lowercase__  (	self		:       Tuple,    lowerCamelCase		:       Optional[int],    **lowerCamelCase		:       List[Any]    ):

								'''simple docstring'''
								lowercase__  					=     self.framework
								lowercase__  					=     self.tokenizer(lowerCamelCase,    return_tensors=lowerCamelCase,    **lowerCamelCase    )
								return model_inputs




				def 							lowercase__  (	self		:       Union[str, Any],    lowerCamelCase		:       Dict    ):

								'''simple docstring'''
								lowercase__  					=     self.model(**lowerCamelCase    )
								return model_outputs




				def 							lowercase__  (	self		:       Union[str, Any],    lowerCamelCase		:       Dict,    lowerCamelCase		:       Tuple=False    ):

								'''simple docstring'''
								# [0] is the first available tensor, logits or last_hidden_state.
								if return_tensors:
												return model_outputs[0]
								if self.framework == "pt":
												return model_outputs[0].tolist()
								elif self.framework == "tf":
												return model_outputs[0].numpy().tolist()




				def __call__(	self		:       List[str],    *lowerCamelCase		:       List[Any],    **lowerCamelCase		:       str    ):

								'''simple docstring'''
								return super().__call__(*lowerCamelCase,    **lowerCamelCase    )




