



'''simple docstring'''



import shutil
import tempfile
import unittest

import pytest

from transformers import WhisperTokenizer, is_speech_available
from transformers.testing_utils import require_sentencepiece, require_torch, require_torchaudio

from .test_feature_extraction_whisper import floats_list


if is_speech_available():
				from transformers import WhisperFeatureExtractor, WhisperProcessor


_lowercase       :						List[Any]						=5_0358
_lowercase       :						str						=5_0362






@require_torch
@require_torchaudio
@require_sentencepiece
class 			_SCREAMING_SNAKE_CASE				(unittest.TestCase      ):



			def 				lowerCAmelCase (     self		:						Tuple			)      ->      str:
							"""simple docstring"""





							snake_case__      :   List[Any]      =    '''openai/whisper-small.en'''
							snake_case__      :   str      =    tempfile.mkdtemp()



			def 				lowerCAmelCase (     self		:						Union[str, Any]    ,			**__UpperCamelCase		:						Union[str, Any]			)      ->      Any:
							"""simple docstring"""





							return WhisperTokenizer.from_pretrained(self.checkpoint    ,			**__UpperCamelCase			)



			def 				lowerCAmelCase (     self		:						List[str]    ,			**__UpperCamelCase		:						Union[str, Any]			)      ->      Optional[int]:
							"""simple docstring"""





							return WhisperFeatureExtractor.from_pretrained(self.checkpoint    ,			**__UpperCamelCase			)



			def 				lowerCAmelCase (     self		:						str			)      ->      str:
							"""simple docstring"""





							shutil.rmtree(self.tmpdirname			)



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





							snake_case__      :   Union[str, Any]      =    self.get_tokenizer()
							snake_case__      :   int      =    self.get_feature_extractor()

							snake_case__      :   List[Any]      =    WhisperProcessor(tokenizer=__UpperCamelCase    ,			feature_extractor=__UpperCamelCase			)

							processor.save_pretrained(self.tmpdirname			)
							snake_case__      :   Optional[Any]      =    WhisperProcessor.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		:						Tuple			)      ->      Optional[Any]:
							"""simple docstring"""





							snake_case__      :   Any      =    WhisperProcessor(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__      :   Any      =    self.get_feature_extractor(do_normalize=__UpperCamelCase    ,			padding_value=1.0			)

							snake_case__      :   Dict      =    WhisperProcessor.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			)      ->      Optional[Any]:
							"""simple docstring"""





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

							snake_case__      :   Optional[Any]      =    WhisperProcessor(tokenizer=__UpperCamelCase    ,			feature_extractor=__UpperCamelCase			)

							snake_case__      :   List[str]      =    floats_list((3, 1000)			)

							snake_case__      :   Any      =    feature_extractor(__UpperCamelCase    ,			return_tensors='''np'''			)
							snake_case__      :   int      =    processor(__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[Any]:
							"""simple docstring"""





							snake_case__      :   List[str]      =    self.get_feature_extractor()
							snake_case__      :   Dict      =    self.get_tokenizer()

							snake_case__      :   List[str]      =    WhisperProcessor(tokenizer=__UpperCamelCase    ,			feature_extractor=__UpperCamelCase			)

							snake_case__      :   Optional[int]      =    '''This is a test string'''

							snake_case__      :   Tuple      =    processor(text=__UpperCamelCase			)

							snake_case__      :   Optional[int]      =    tokenizer(__UpperCamelCase			)

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



			def 				lowerCAmelCase (     self		:						Optional[int]			)      ->      List[str]:
							"""simple docstring"""





							snake_case__      :   List[Any]      =    self.get_feature_extractor()
							snake_case__      :   Union[str, Any]      =    self.get_tokenizer()

							snake_case__      :   Tuple      =    WhisperProcessor(tokenizer=__UpperCamelCase    ,			feature_extractor=__UpperCamelCase			)

							snake_case__      :   int      =    [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

							snake_case__      :   str      =    processor.batch_decode(__UpperCamelCase			)
							snake_case__      :   Tuple      =    tokenizer.batch_decode(__UpperCamelCase			)

							self.assertListEqual(__UpperCamelCase    ,			__UpperCamelCase			)



			def 				lowerCAmelCase (     self		:						Optional[int]			)      ->      str:
							"""simple docstring"""





							snake_case__      :   int      =    self.get_feature_extractor()
							snake_case__      :   Dict      =    self.get_tokenizer()

							snake_case__      :   Optional[int]      =    WhisperProcessor(tokenizer=__UpperCamelCase    ,			feature_extractor=__UpperCamelCase			)

							self.assertListEqual(
							    processor.model_input_names    ,			feature_extractor.model_input_names    ,			msg='''`processor` and `feature_extractor` model input names do not match'''    ,			)



			def 				lowerCAmelCase (     self		:						Union[str, Any]			)      ->      str:
							"""simple docstring"""





							snake_case__      :   List[str]      =    self.get_feature_extractor()
							snake_case__      :   Optional[Any]      =    self.get_tokenizer()

							snake_case__      :   Any      =    WhisperProcessor(tokenizer=__UpperCamelCase    ,			feature_extractor=__UpperCamelCase			)
							snake_case__      :   List[Any]      =    processor.get_decoder_prompt_ids(task='''transcribe'''    ,			no_timestamps=__UpperCamelCase			)

							self.assertIsInstance(__UpperCamelCase    ,			__UpperCamelCase			)
							for ids in forced_decoder_ids:
											self.assertIsInstance(__UpperCamelCase    ,			(list, tuple)			)

							snake_case__      :   List[str]      =    [TRANSCRIBE, NOTIMESTAMPS]
							self.assertListEqual([ids[-1] for ids in forced_decoder_ids]    ,			__UpperCamelCase			)



			def 				lowerCAmelCase (     self		:						Union[str, Any]			)      ->      Optional[int]:
							"""simple docstring"""





							snake_case__      :   Optional[Any]      =    WhisperProcessor(tokenizer=self.get_tokenizer()    ,			feature_extractor=self.get_feature_extractor()			)
							snake_case__      :   Union[str, Any]      =    processor.get_prompt_ids('''Mr. Quilter'''			)
							snake_case__      :   Optional[int]      =    processor.tokenizer.decode(__UpperCamelCase			)

							self.assertListEqual(prompt_ids.tolist()    ,			[50360, 1770, 13, 2264, 346, 353]			)
							self.assertEqual(__UpperCamelCase    ,			'''<|startofprev|> Mr. Quilter'''			)



			def 				lowerCAmelCase (     self		:						Optional[int]			)      ->      Optional[Any]:
							"""simple docstring"""





							snake_case__      :   List[str]      =    WhisperProcessor(tokenizer=self.get_tokenizer()    ,			feature_extractor=self.get_feature_extractor()			)
							snake_case__      :   List[Any]      =    processor.get_prompt_ids(''''''			)
							snake_case__      :   List[Any]      =    processor.tokenizer.decode(__UpperCamelCase			)

							self.assertListEqual(prompt_ids.tolist()    ,			[50360, 220]			)
							self.assertEqual(__UpperCamelCase    ,			'''<|startofprev|> '''			)







			def 				lowerCAmelCase (     self		:						Dict			)      ->      int:
							"""simple docstring"""





							snake_case__      :   Union[str, Any]      =    WhisperProcessor(tokenizer=self.get_tokenizer()    ,			feature_extractor=self.get_feature_extractor()			)

							def _test_prompt_error_raised_helper(__UpperCamelCase		:						Dict    ,			__UpperCamelCase		:						Tuple			):
											with pytest.raises(__UpperCamelCase			) as excinfo:
															processor.get_prompt_ids(__UpperCamelCase			)
											snake_case__      :   int      =    F'''Encountered text in the prompt corresponding to disallowed special token: {special_token}.'''
											self.assertEqual(__UpperCamelCase    ,			str(excinfo.value			)			)

							_test_prompt_error_raised_helper('''<|startofprev|> test'''    ,			'''<|startofprev|>'''			)
							_test_prompt_error_raised_helper('''test <|notimestamps|>'''    ,			'''<|notimestamps|>'''			)
							_test_prompt_error_raised_helper('''test <|zh|> test <|transcribe|>'''    ,			'''<|zh|>'''			)









from typing import TYPE_CHECKING

# rely on isort to merge the imports
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_torch_available


__UpperCAmelCase    	=  {
    '''configuration_ernie_m''': ['''ERNIE_M_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ErnieMConfig'''],
}

try:
		if not is_sentencepiece_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__UpperCAmelCase    	=  ['''ErnieMTokenizer''']

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__UpperCAmelCase    	=  [
		    '''ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''ErnieMForMultipleChoice''',
		    '''ErnieMForQuestionAnswering''',
		    '''ErnieMForSequenceClassification''',
		    '''ErnieMForTokenClassification''',
		    '''ErnieMModel''',
		    '''ErnieMPreTrainedModel''',
		    '''ErnieMForInformationExtraction''',
		]


if TYPE_CHECKING:
		from .configuration_ernie_m import ERNIE_M_PRETRAINED_CONFIG_ARCHIVE_MAP, ErnieMConfig

		try:
				if not is_sentencepiece_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .tokenization_ernie_m import ErnieMTokenizer

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_ernie_m import (
				    ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST,
				    ErnieMForInformationExtraction,
				    ErnieMForMultipleChoice,
				    ErnieMForQuestionAnswering,
				    ErnieMForSequenceClassification,
				    ErnieMForTokenClassification,
				    ErnieMModel,
				    ErnieMPreTrainedModel,
				)


else:
		import sys

		__UpperCAmelCase    	=  _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)





