


'''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


__SCREAMING_SNAKE_CASE				:							Dict		       =		50358
__SCREAMING_SNAKE_CASE				:							Dict		       =		50362






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

						"""simple docstring"""






						def 							_UpperCAmelCase					(     self       :			Optional[int]  ):
													A_ 						=		"""openai/whisper-small.en"""
													A_ 						=		tempfile.mkdtemp()






						def 							_UpperCAmelCase					(     self       :			Any   ,     **lowerCAmelCase       :			Union[str, Any]  ):
													return WhisperTokenizer.from_pretrained(self.checkpoint   ,     **_lowerCAmelCase  )






						def 							_UpperCAmelCase					(     self       :			List[Any]   ,     **lowerCAmelCase       :			str  ):
													return WhisperFeatureExtractor.from_pretrained(self.checkpoint   ,     **_lowerCAmelCase  )






						def 							_UpperCAmelCase					(     self       :			Union[str, Any]  ):
													shutil.rmtree(self.tmpdirname  )






						def 							_UpperCAmelCase					(     self       :			Optional[Any]  ):
													A_ 						=		self.get_tokenizer()
													A_ 						=		self.get_feature_extractor()

													A_ 						=		WhisperProcessor(tokenizer=_lowerCAmelCase   ,     feature_extractor=_lowerCAmelCase  )

													processor.save_pretrained(self.tmpdirname  )
													A_ 						=		WhisperProcessor.from_pretrained(self.tmpdirname  )

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

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






						def 							_UpperCAmelCase					(     self       :			Union[str, Any]  ):
													A_ 						=		WhisperProcessor(tokenizer=self.get_tokenizer()   ,     feature_extractor=self.get_feature_extractor()  )
													processor.save_pretrained(self.tmpdirname  )

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

													A_ 						=		WhisperProcessor.from_pretrained(
													    self.tmpdirname   ,     bos_token="(BOS)"   ,     eos_token="(EOS)"   ,     do_normalize=_lowerCAmelCase   ,     padding_value=1.0  )

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

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






						def 							_UpperCAmelCase					(     self       :			Dict  ):
													A_ 						=		self.get_feature_extractor()
													A_ 						=		self.get_tokenizer()

													A_ 						=		WhisperProcessor(tokenizer=_lowerCAmelCase   ,     feature_extractor=_lowerCAmelCase  )

													A_ 						=		floats_list((3, 10_00)  )

													A_ 						=		feature_extractor(_lowerCAmelCase   ,     return_tensors="np"  )
													A_ 						=		processor(_lowerCAmelCase   ,     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 							_UpperCAmelCase					(     self       :			Dict  ):
													A_ 						=		self.get_feature_extractor()
													A_ 						=		self.get_tokenizer()

													A_ 						=		WhisperProcessor(tokenizer=_lowerCAmelCase   ,     feature_extractor=_lowerCAmelCase  )

													A_ 						=		"""This is a test string"""

													A_ 						=		processor(text=_lowerCAmelCase  )

													A_ 						=		tokenizer(_lowerCAmelCase  )

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






						def 							_UpperCAmelCase					(     self       :			Dict  ):
													A_ 						=		self.get_feature_extractor()
													A_ 						=		self.get_tokenizer()

													A_ 						=		WhisperProcessor(tokenizer=_lowerCAmelCase   ,     feature_extractor=_lowerCAmelCase  )

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

													A_ 						=		processor.batch_decode(_lowerCAmelCase  )
													A_ 						=		tokenizer.batch_decode(_lowerCAmelCase  )

													self.assertListEqual(_lowerCAmelCase   ,     _lowerCAmelCase  )






						def 							_UpperCAmelCase					(     self       :			List[str]  ):
													A_ 						=		self.get_feature_extractor()
													A_ 						=		self.get_tokenizer()

													A_ 						=		WhisperProcessor(tokenizer=_lowerCAmelCase   ,     feature_extractor=_lowerCAmelCase  )

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






						def 							_UpperCAmelCase					(     self       :			Any  ):
													A_ 						=		self.get_feature_extractor()
													A_ 						=		self.get_tokenizer()

													A_ 						=		WhisperProcessor(tokenizer=_lowerCAmelCase   ,     feature_extractor=_lowerCAmelCase  )
													A_ 						=		processor.get_decoder_prompt_ids(task="transcribe"   ,     no_timestamps=_lowerCAmelCase  )

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

													A_ 						=		[TRANSCRIBE, NOTIMESTAMPS]
													self.assertListEqual([ids[-1] for ids in forced_decoder_ids]   ,     _lowerCAmelCase  )






						def 							_UpperCAmelCase					(     self       :			List[str]  ):
													A_ 						=		WhisperProcessor(tokenizer=self.get_tokenizer()   ,     feature_extractor=self.get_feature_extractor()  )
													A_ 						=		processor.get_prompt_ids("Mr. Quilter"  )
													A_ 						=		processor.tokenizer.decode(_lowerCAmelCase  )

													self.assertListEqual(prompt_ids.tolist()   ,     [5_03_60, 17_70, 13, 22_64, 3_46, 3_53]  )
													self.assertEqual(_lowerCAmelCase   ,     "<|startofprev|> Mr. Quilter"  )






						def 							_UpperCAmelCase					(     self       :			int  ):
													A_ 						=		WhisperProcessor(tokenizer=self.get_tokenizer()   ,     feature_extractor=self.get_feature_extractor()  )
													A_ 						=		processor.get_prompt_ids(""  )
													A_ 						=		processor.tokenizer.decode(_lowerCAmelCase  )

													self.assertListEqual(prompt_ids.tolist()   ,     [5_03_60, 2_20]  )
													self.assertEqual(_lowerCAmelCase   ,     "<|startofprev|> "  )






						def 							_UpperCAmelCase					(     self       :			str  ):
													A_ 						=		WhisperProcessor(tokenizer=self.get_tokenizer()   ,     feature_extractor=self.get_feature_extractor()  )

													def _test_prompt_error_raised_helper(lowerCAmelCase       :			Any   ,     lowerCAmelCase       :			int  ):
																				with pytest.raises(_lowerCAmelCase  ) as excinfo:
																											processor.get_prompt_ids(_lowerCAmelCase  )
																				A_ 						=		F"Encountered text in the prompt corresponding to disallowed special token: {special_token}."
																				self.assertEqual(_lowerCAmelCase   ,     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|>"  )









'''simple docstring'''
def 			a_							(  UpperCamelCase_   ):
							if not isinstance(UpperCamelCase_	,     UpperCamelCase_   ):
														A_ 						=		f"Input value of [number={number}] must be an integer"
														raise TypeError(UpperCamelCase_   )
							if number < 0 or number % 2 == 1:
														return False
							A_ 						=		int(number**0.5   )
							return number == number_sqrt * (number_sqrt + 1)


if __name__ == "__main__":
							import doctest

							doctest.testmod()





