import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin
from .feature_extraction_wavaveca import WavaVecaFeatureExtractor
from .tokenization_wavaveca import WavaVecaCTCTokenizer


class 						a__							(       __snake_case		):
			A__    :       Any        =      'Wav2Vec2FeatureExtractor'
			A__    :       str        =      'AutoTokenizer'



			def __init__(						self				,						UpperCAmelCase				,						UpperCAmelCase   )			->   Union[str, Any]:
								super().__init__(UpperCAmelCase				,						UpperCAmelCase   )
								__a		 =       self.feature_extractor
								__a		 =       False



			@classmethod
			def __SCREAMING_SNAKE_CASE							(						cls				,						UpperCAmelCase				,						**UpperCAmelCase   )			->   Dict:
								try:
													return super().from_pretrained(UpperCAmelCase				,						**UpperCAmelCase   )
								except OSError:
													warnings.warn(
													    f'''Loading a tokenizer inside {cls.__name__} from a config that does not'''
													    ' include a `tokenizer_class` attribute is deprecated and will be '
													    'removed in v5. Please add `\'tokenizer_class\': \'Wav2Vec2CTCTokenizer\'`'
													    ' attribute to either your `config.json` or `tokenizer_config.json` '
													    'file to suppress this warning: '				,						UpperCAmelCase				,						)

													__a		 =       WavaVecaFeatureExtractor.from_pretrained(UpperCAmelCase				,						**UpperCAmelCase   )
													__a		 =       WavaVecaCTCTokenizer.from_pretrained(UpperCAmelCase				,						**UpperCAmelCase   )

													return cls(feature_extractor=UpperCAmelCase				,						tokenizer=UpperCAmelCase   )



			def __call__(						self				,						*UpperCAmelCase				,						**UpperCAmelCase   )			->   List[Any]:
								# For backward compatibility
								if self._in_target_context_manager:
													return self.current_processor(*UpperCAmelCase				,						**UpperCAmelCase   )

								if "raw_speech" in kwargs:
													warnings.warn('Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.'   )
													__a		 =       kwargs.pop('raw_speech'   )
								else:
													__a		 =       kwargs.pop('audio'				,						UpperCAmelCase   )
								__a		 =       kwargs.pop('sampling_rate'				,						UpperCAmelCase   )
								__a		 =       kwargs.pop('text'				,						UpperCAmelCase   )
								if len(UpperCAmelCase   ) > 0:
													__a		 =       args[0]
													__a		 =       args[1:]

								if audio is None and text is None:
													raise ValueError('You need to specify either an `audio` or `text` input to process.'   )

								if audio is not None:
													__a		 =       self.feature_extractor(UpperCAmelCase				,						*UpperCAmelCase				,						sampling_rate=UpperCAmelCase				,						**UpperCAmelCase   )
								if text is not None:
													__a		 =       self.tokenizer(UpperCAmelCase				,						**UpperCAmelCase   )

								if text is None:
													return inputs
								elif audio is None:
													return encodings
								else:
													__a		 =       encodings['input_ids']
													return inputs



			def __SCREAMING_SNAKE_CASE							(						self				,						*UpperCAmelCase				,						**UpperCAmelCase   )			->   Tuple:
								# For backward compatibility
								if self._in_target_context_manager:
													return self.current_processor.pad(*UpperCAmelCase				,						**UpperCAmelCase   )

								__a		 =       kwargs.pop('input_features'				,						UpperCAmelCase   )
								__a		 =       kwargs.pop('labels'				,						UpperCAmelCase   )
								if len(UpperCAmelCase   ) > 0:
													__a		 =       args[0]
													__a		 =       args[1:]

								if input_features is not None:
													__a		 =       self.feature_extractor.pad(UpperCAmelCase				,						*UpperCAmelCase				,						**UpperCAmelCase   )
								if labels is not None:
													__a		 =       self.tokenizer.pad(UpperCAmelCase				,						**UpperCAmelCase   )

								if labels is None:
													return input_features
								elif input_features is None:
													return labels
								else:
													__a		 =       labels['input_ids']
													return input_features



			def __SCREAMING_SNAKE_CASE							(						self				,						*UpperCAmelCase				,						**UpperCAmelCase   )			->   List[str]:
								return self.tokenizer.batch_decode(*UpperCAmelCase				,						**UpperCAmelCase   )



			def __SCREAMING_SNAKE_CASE							(						self				,						*UpperCAmelCase				,						**UpperCAmelCase   )			->   Union[str, Any]:
								return self.tokenizer.decode(*UpperCAmelCase				,						**UpperCAmelCase   )







			@contextmanager
			def __SCREAMING_SNAKE_CASE							(						self   )			->   List[Any]:
								warnings.warn(
								    '`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your '
								    'labels by using the argument `text` of the regular `__call__` method (either in the same call as '
								    'your audio inputs, or in a separate call.'   )
								__a		 =       True
								__a		 =       self.tokenizer
								yield
								__a		 =       self.feature_extractor
								__a		 =       False





import copy
import json
import os
import tempfile

from transformers import is_torch_available

from .test_configuration_utils import config_common_kwargs


class 						a__							(       __snake_case		):



			def __init__(						self				,						UpperCAmelCase				,						UpperCAmelCase=None				,						UpperCAmelCase=True				,						UpperCAmelCase=None				,						**UpperCAmelCase   )			->   List[Any]:
								__a		 =       parent
								__a		 =       config_class
								__a		 =       has_text_modality
								__a		 =       kwargs
								__a		 =       common_properties



			def __SCREAMING_SNAKE_CASE							(						self   )			->   List[str]:
								__a		 =       self.config_class(**self.inputs_dict   )
								__a		 =       (
								    ['hidden_size', 'num_attention_heads', 'num_hidden_layers']
								    if self.common_properties is None
								    else self.common_properties
								)

								# Add common fields for text models
								if self.has_text_modality:
													common_properties.extend(['vocab_size']   )

								# Test that config has the common properties as getters
								for prop in common_properties:
													self.parent.assertTrue(hasattr(UpperCAmelCase				,						UpperCAmelCase   )				,						msg=f'''`{prop}` does not exist'''   )

								# Test that config has the common properties as setter
								for idx, name in enumerate(UpperCAmelCase   ):
													try:
																		setattr(UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase   )
																		self.parent.assertEqual(
																		    getattr(UpperCAmelCase				,						UpperCAmelCase   )				,						UpperCAmelCase				,						msg=f'''`{name} value {idx} expected, but was {getattr(UpperCAmelCase				,						UpperCAmelCase   )}'''   )
													except NotImplementedError:
																		# Some models might not be able to implement setters for common_properties
																		# In that case, a NotImplementedError is raised
																		pass

        # Test if config class can be called with Config(prop_name=..)
								for idx, name in enumerate(UpperCAmelCase   ):
													try:
																		__a		 =       self.config_class(**{name: idx}   )
																		self.parent.assertEqual(
																		    getattr(UpperCAmelCase				,						UpperCAmelCase   )				,						UpperCAmelCase				,						msg=f'''`{name} value {idx} expected, but was {getattr(UpperCAmelCase				,						UpperCAmelCase   )}'''   )
													except NotImplementedError:
																		# Some models might not be able to implement setters for common_properties
																		# In that case, a NotImplementedError is raised
																		pass



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Tuple:
								__a		 =       self.config_class(**self.inputs_dict   )
								__a		 =       json.loads(config.to_json_string()   )
								for key, value in self.inputs_dict.items():
													self.parent.assertEqual(obj[key]				,						UpperCAmelCase   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Tuple:
								__a		 =       self.config_class(**self.inputs_dict   )

								with tempfile.TemporaryDirectory() as tmpdirname:
													__a		 =       os.path.join(UpperCAmelCase				,						'config.json'   )
													config_first.to_json_file(UpperCAmelCase   )
													__a		 =       self.config_class.from_json_file(UpperCAmelCase   )

								self.parent.assertEqual(config_second.to_dict()				,						config_first.to_dict()   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Tuple:
								__a		 =       self.config_class(**self.inputs_dict   )

								with tempfile.TemporaryDirectory() as tmpdirname:
													config_first.save_pretrained(UpperCAmelCase   )
													__a		 =       self.config_class.from_pretrained(UpperCAmelCase   )

								self.parent.assertEqual(config_second.to_dict()				,						config_first.to_dict()   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   str:
								__a		 =       self.config_class(**self.inputs_dict   )

								__a		 =       'test'
								with tempfile.TemporaryDirectory() as tmpdirname:
													__a		 =       os.path.join(UpperCAmelCase				,						UpperCAmelCase   )
													config_first.save_pretrained(UpperCAmelCase   )
													__a		 =       self.config_class.from_pretrained(UpperCAmelCase				,						subfolder=UpperCAmelCase   )

								self.parent.assertEqual(config_second.to_dict()				,						config_first.to_dict()   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Union[str, Any]:
								__a		 =       self.config_class(**self.inputs_dict				,						num_labels=5   )
								self.parent.assertEqual(len(config.idalabel   )				,						5   )
								self.parent.assertEqual(len(config.labelaid   )				,						5   )

								__a		 =       3
								self.parent.assertEqual(len(config.idalabel   )				,						3   )
								self.parent.assertEqual(len(config.labelaid   )				,						3   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   List[str]:
								if self.config_class.is_composition:
													return
								__a		 =       self.config_class()
								self.parent.assertIsNotNone(UpperCAmelCase   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Union[str, Any]:
								__a		 =       copy.deepcopy(UpperCAmelCase   )
								__a		 =       self.config_class(**UpperCAmelCase   )
								__a		 =       []
								for key, value in config_common_kwargs.items():
													if key == "torch_dtype":
																		if not is_torch_available():
																							continue
																		else:
																							import torch

																							if config.torch_dtype != torch.floataa:
																												wrong_values.append(('torch_dtype', config.torch_dtype, torch.floataa)   )
													elif getattr(UpperCAmelCase				,						UpperCAmelCase   ) != value:
																		wrong_values.append((key, getattr(UpperCAmelCase				,						UpperCAmelCase   ), value)   )

								if len(UpperCAmelCase   ) > 0:
													__a		 =       '\n'.join([f'''- {v[0]}: got {v[1]} instead of {v[2]}''' for v in wrong_values]   )
													raise ValueError(f'''The following keys were not properly set in the config:\n{errors}'''   )







			def __SCREAMING_SNAKE_CASE							(						self   )			->   Optional[Any]:
								self.create_and_test_config_common_properties()
								self.create_and_test_config_to_json_string()
								self.create_and_test_config_to_json_file()
								self.create_and_test_config_from_and_save_pretrained()
								self.create_and_test_config_from_and_save_pretrained_subfolder()
								self.create_and_test_config_with_num_labels()
								self.check_config_can_be_init_without_params()
								self.check_config_arguments_init()




