
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


snake_case_      :					Optional[int]				      =     5_0_3_5_8
snake_case_      :					Optional[Any]				      =     5_0_3_6_2





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



							def     A__ (     self							)						->     Tuple:






								"""simple docstring"""



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



							def     A__ (     self	,				**__snake_case							)						->     Dict:






								"""simple docstring"""



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



							def     A__ (     self	,				**__snake_case							)						->     Union[str, Any]:






								"""simple docstring"""



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



							def     A__ (     self							)						->     Dict:






								"""simple docstring"""



								shutil.rmtree(self.tmpdirname							)



							def     A__ (     self							)						->     List[Any]:






								"""simple docstring"""



								UpperCAmelCase:					Tuple 						=     self.get_tokenizer()
								UpperCAmelCase:					Optional[Any] 						=     self.get_feature_extractor()

								UpperCAmelCase:					Dict 						=     WhisperProcessor(tokenizer=SCREAMING_SNAKE_CASE_	,				feature_extractor=SCREAMING_SNAKE_CASE_							)

								processor.save_pretrained(self.tmpdirname							)
								UpperCAmelCase:					Dict 						=     WhisperProcessor.from_pretrained(self.tmpdirname							)

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

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



							def     A__ (     self							)						->     Tuple:






								"""simple docstring"""



								UpperCAmelCase:					str 						=     WhisperProcessor(tokenizer=self.get_tokenizer()	,				feature_extractor=self.get_feature_extractor()							)
								processor.save_pretrained(self.tmpdirname							)

								UpperCAmelCase:					str 						=     self.get_tokenizer(bos_token="(BOS)"	,				eos_token="(EOS)"							)
								UpperCAmelCase:					int 						=     self.get_feature_extractor(do_normalize=SCREAMING_SNAKE_CASE_	,				padding_value=1.0							)

								UpperCAmelCase:					Dict 						=     WhisperProcessor.from_pretrained(
								    self.tmpdirname	,				bos_token="(BOS)"	,				eos_token="(EOS)"	,				do_normalize=SCREAMING_SNAKE_CASE_	,				padding_value=1.0							)

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

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



							def     A__ (     self							)						->     Union[str, Any]:






								"""simple docstring"""



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

								UpperCAmelCase:					Dict 						=     WhisperProcessor(tokenizer=SCREAMING_SNAKE_CASE_	,				feature_extractor=SCREAMING_SNAKE_CASE_							)

								UpperCAmelCase:					str 						=     floats_list((3, 1_0_0_0)							)

								UpperCAmelCase:					List[Any] 						=     feature_extractor(SCREAMING_SNAKE_CASE_	,				return_tensors="np"							)
								UpperCAmelCase:					Tuple 						=     processor(SCREAMING_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     A__ (     self							)						->     Any:






								"""simple docstring"""



								UpperCAmelCase:					Optional[Any] 						=     self.get_feature_extractor()
								UpperCAmelCase:					Optional[int] 						=     self.get_tokenizer()

								UpperCAmelCase:					Optional[Any] 						=     WhisperProcessor(tokenizer=SCREAMING_SNAKE_CASE_	,				feature_extractor=SCREAMING_SNAKE_CASE_							)

								UpperCAmelCase:					List[Any] 						=     'This is a test string'

								UpperCAmelCase:					int 						=     processor(text=SCREAMING_SNAKE_CASE_							)

								UpperCAmelCase:					int 						=     tokenizer(SCREAMING_SNAKE_CASE_							)

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



							def     A__ (     self							)						->     int:






								"""simple docstring"""



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

								UpperCAmelCase:					Optional[int] 						=     WhisperProcessor(tokenizer=SCREAMING_SNAKE_CASE_	,				feature_extractor=SCREAMING_SNAKE_CASE_							)

								UpperCAmelCase:					List[Any] 						=     [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

								UpperCAmelCase:					List[Any] 						=     processor.batch_decode(SCREAMING_SNAKE_CASE_							)
								UpperCAmelCase:					Optional[int] 						=     tokenizer.batch_decode(SCREAMING_SNAKE_CASE_							)

								self.assertListEqual(SCREAMING_SNAKE_CASE_	,				SCREAMING_SNAKE_CASE_							)



							def     A__ (     self							)						->     Union[str, Any]:






								"""simple docstring"""



								UpperCAmelCase:					Optional[int] 						=     self.get_feature_extractor()
								UpperCAmelCase:					Tuple 						=     self.get_tokenizer()

								UpperCAmelCase:					Dict 						=     WhisperProcessor(tokenizer=SCREAMING_SNAKE_CASE_	,				feature_extractor=SCREAMING_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     A__ (     self							)						->     Union[str, Any]:






								"""simple docstring"""



								UpperCAmelCase:					Optional[Any] 						=     self.get_feature_extractor()
								UpperCAmelCase:					Tuple 						=     self.get_tokenizer()

								UpperCAmelCase:					List[str] 						=     WhisperProcessor(tokenizer=SCREAMING_SNAKE_CASE_	,				feature_extractor=SCREAMING_SNAKE_CASE_							)
								UpperCAmelCase:					Dict 						=     processor.get_decoder_prompt_ids(task="transcribe"	,				no_timestamps=SCREAMING_SNAKE_CASE_							)

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

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



							def     A__ (     self							)						->     Union[str, Any]:






								"""simple docstring"""



								UpperCAmelCase:					Any 						=     WhisperProcessor(tokenizer=self.get_tokenizer()	,				feature_extractor=self.get_feature_extractor()							)
								UpperCAmelCase:					int 						=     processor.get_prompt_ids("Mr. Quilter"							)
								UpperCAmelCase:					Union[str, Any] 						=     processor.tokenizer.decode(SCREAMING_SNAKE_CASE_							)

								self.assertListEqual(prompt_ids.tolist()	,				[5_0_3_6_0, 1_7_7_0, 1_3, 2_2_6_4, 3_4_6, 3_5_3]							)
								self.assertEqual(SCREAMING_SNAKE_CASE_	,				"<|startofprev|> Mr. Quilter"							)



							def     A__ (     self							)						->     List[Any]:






								"""simple docstring"""



								UpperCAmelCase:					List[Any] 						=     WhisperProcessor(tokenizer=self.get_tokenizer()	,				feature_extractor=self.get_feature_extractor()							)
								UpperCAmelCase:					List[str] 						=     processor.get_prompt_ids(""							)
								UpperCAmelCase:					Optional[int] 						=     processor.tokenizer.decode(SCREAMING_SNAKE_CASE_							)

								self.assertListEqual(prompt_ids.tolist()	,				[5_0_3_6_0, 2_2_0]							)
								self.assertEqual(SCREAMING_SNAKE_CASE_	,				"<|startofprev|> "							)




							def     A__ (     self							)						->     List[Any]:






								"""simple docstring"""



								UpperCAmelCase:					Tuple 						=     WhisperProcessor(tokenizer=self.get_tokenizer()	,				feature_extractor=self.get_feature_extractor()							)

								def _test_prompt_error_raised_helper(__snake_case	,				__snake_case							):
									with pytest.raises(SCREAMING_SNAKE_CASE_							) as excinfo:
										processor.get_prompt_ids(SCREAMING_SNAKE_CASE_							)
									UpperCAmelCase:					Union[str, Any] 						=     F'Encountered text in the prompt corresponding to disallowed special token: {special_token}.'
									self.assertEqual(SCREAMING_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|>"							)





"""simple docstring"""







def 							UpperCamelCase_       (       lowerCAmelCase__ :							int = 400_0000		)		->  int:

  """simple docstring"""




  if n <= 1:
    return 0
  lowerCAmelCase_  :							Union[str, Any]								=					0
  lowerCAmelCase_  :							Tuple								=					2
  lowerCAmelCase_  :							List[Any]								=					0
  while 4 * b + a <= n:
    lowerCAmelCase_		,lowerCAmelCase_  :							str								=					b, 4 * b + a
    count += a
  return count + b


if __name__ == "__main__":
     print(f'{solution() = }')





