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


UpperCamelCase_  =	5_03_58
UpperCamelCase_  =	5_03_62

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





				def        _UpperCAmelCase						(		self :		Dict							):


								"""simple docstring"""

								A     :  Optional[int]   =				'''openai/whisper-small.en'''
								A     :  List[str]   =				tempfile.mkdtemp()





				def        _UpperCAmelCase						(		self :		int					,      **snake_case_ :		int							):


								"""simple docstring"""

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





				def        _UpperCAmelCase						(		self :		Tuple					,      **snake_case_ :		Tuple							):


								"""simple docstring"""

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





				def        _UpperCAmelCase						(		self :		str							):


								"""simple docstring"""

								shutil.rmtree(self.tmpdirname							)





				def        _UpperCAmelCase						(		self :		List[Any]							):


								"""simple docstring"""

								A     :  List[str]   =				self.get_tokenizer()
								A     :  Optional[int]   =				self.get_feature_extractor()

								A     :  Dict   =				WhisperProcessor(tokenizer=snake_case_					,      feature_extractor=snake_case_							)

								processor.save_pretrained(self.tmpdirname							)
								A     :  List[Any]   =				WhisperProcessor.from_pretrained(self.tmpdirname							)

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

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





				def        _UpperCAmelCase						(		self :		Dict							):


								"""simple docstring"""

								A     :  Any   =				WhisperProcessor(tokenizer=self.get_tokenizer()					,      feature_extractor=self.get_feature_extractor()							)
								processor.save_pretrained(self.tmpdirname							)

								A     :  int   =				self.get_tokenizer(bos_token='''(BOS)'''					,      eos_token='''(EOS)'''							)
								A     :  int   =				self.get_feature_extractor(do_normalize=snake_case_					,      padding_value=1.0							)

								A     :  Dict   =				WhisperProcessor.from_pretrained(
								    self.tmpdirname					,      bos_token='''(BOS)'''					,      eos_token='''(EOS)'''					,      do_normalize=snake_case_					,      padding_value=1.0							)

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

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





				def        _UpperCAmelCase						(		self :		List[Any]							):


								"""simple docstring"""

								A     :  Union[str, Any]   =				self.get_feature_extractor()
								A     :  Tuple   =				self.get_tokenizer()

								A     :  Dict   =				WhisperProcessor(tokenizer=snake_case_					,      feature_extractor=snake_case_							)

								A     :  int   =				floats_list((3, 1000)							)

								A     :  Optional[int]   =				feature_extractor(snake_case_					,      return_tensors='''np'''							)
								A     :  Optional[Any]   =				processor(snake_case_					,      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 :		int							):


								"""simple docstring"""

								A     :  str   =				self.get_feature_extractor()
								A     :  int   =				self.get_tokenizer()

								A     :  List[str]   =				WhisperProcessor(tokenizer=snake_case_					,      feature_extractor=snake_case_							)

								A     :  Tuple   =				'''This is a test string'''

								A     :  Tuple   =				processor(text=snake_case_							)

								A     :  List[str]   =				tokenizer(snake_case_							)

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





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


								"""simple docstring"""

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

								A     :  Tuple   =				WhisperProcessor(tokenizer=snake_case_					,      feature_extractor=snake_case_							)

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

								A     :  str   =				processor.batch_decode(snake_case_							)
								A     :  Optional[int]   =				tokenizer.batch_decode(snake_case_							)

								self.assertListEqual(snake_case_					,      snake_case_							)





				def        _UpperCAmelCase						(		self :		List[str]							):


								"""simple docstring"""

								A     :  Any   =				self.get_feature_extractor()
								A     :  Any   =				self.get_tokenizer()

								A     :  Union[str, Any]   =				WhisperProcessor(tokenizer=snake_case_					,      feature_extractor=snake_case_							)

								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 :		List[Any]							):


								"""simple docstring"""

								A     :  List[str]   =				self.get_feature_extractor()
								A     :  Tuple   =				self.get_tokenizer()

								A     :  Union[str, Any]   =				WhisperProcessor(tokenizer=snake_case_					,      feature_extractor=snake_case_							)
								A     :  Dict   =				processor.get_decoder_prompt_ids(task='''transcribe'''					,      no_timestamps=snake_case_							)

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

								A     :  Any   =				[TRANSCRIBE, NOTIMESTAMPS]
								self.assertListEqual([ids[-1] for ids in forced_decoder_ids]					,      snake_case_							)





				def        _UpperCAmelCase						(		self :		List[Any]							):


								"""simple docstring"""

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

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





				def        _UpperCAmelCase						(		self :		Optional[Any]							):


								"""simple docstring"""

								A     :  List[str]   =				WhisperProcessor(tokenizer=self.get_tokenizer()					,      feature_extractor=self.get_feature_extractor()							)
								A     :  Any   =				processor.get_prompt_ids(''''''							)
								A     :  Optional[int]   =				processor.tokenizer.decode(snake_case_							)

								self.assertListEqual(prompt_ids.tolist()					,      [5_0360, 220]							)
								self.assertEqual(snake_case_					,      '''<|startofprev|> '''							)






				def        _UpperCAmelCase						(		self :		Optional[Any]							):


								"""simple docstring"""

								A     :  int   =				WhisperProcessor(tokenizer=self.get_tokenizer()					,      feature_extractor=self.get_feature_extractor()							)

								def _test_prompt_error_raised_helper(snake_case_ :		Tuple					,      snake_case_ :		int							):
												with pytest.raises(snake_case_							) as excinfo:
																processor.get_prompt_ids(snake_case_							)
												A     :  Dict   =				f"""Encountered text in the prompt corresponding to disallowed special token: {special_token}."""
												self.assertEqual(snake_case_					,      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|>'''							)

UpperCamelCase_  =	10_00





def 							_lowerCamelCase			(					lowerCamelCase_:  float		):



				'''simple docstring'''







				return x * x





def 							_lowerCamelCase			(					lowerCamelCase_:  Tuple				,	lowerCamelCase_:  float				,	lowerCamelCase_:  float				,	lowerCamelCase_:  int = 4		):



				'''simple docstring'''







				assert callable(
				    lowerCamelCase_		), f"""the function(object) passed should be callable your input : {function}"""
				assert isinstance(lowerCamelCase_				,	(float, int)		), f"""a should be float or integer your input : {a}"""
				assert isinstance(function(lowerCamelCase_		)				,	(float, int)		), (
				    "the function should return integer or float return type of your function, "
				    f"""{type(lowerCamelCase_		)}"""
				)
				assert isinstance(lowerCamelCase_				,	(float, int)		), f"""b should be float or integer your input : {b}"""
				assert (
				    isinstance(lowerCamelCase_				,	lowerCamelCase_		) and precision > 0
				), f"""precision should be positive integer your input : {precision}"""

				# just applying the formula of simpson for approximate integration written in
				# mentioned article in first comment of this file and above this function

				A     :  Tuple   =				(b - a) / N_STEPS
				A     :  List[str]   =				function(lowerCamelCase_		) + function(lowerCamelCase_		)

				for i in range(1				,	lowerCamelCase_		):
								A     :  int   =				a + h * i
								result += function(lowerCamelCase_		) * (4 if i % 2 else 2)

				result *= h / 3
				return round(lowerCamelCase_				,	lowerCamelCase_		)


if __name__ == "__main__":
					import doctest

					doctest.testmod()