



"""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__						:							Optional[int]        =      5_0_3_5_8
lowercase__						:							Optional[Any]        =      5_0_3_6_2







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




      """simple docstring"""




      def 	SCREAMING_SNAKE_CASE__					( self							: Dict			):
        lowerCAmelCase_  :							List[Any]								=					'openai/whisper-small.en'
        lowerCAmelCase_  :							Optional[Any]								=					tempfile.mkdtemp()



      def 	SCREAMING_SNAKE_CASE__					( self							: Dict       ,     **SCREAMING_SNAKE_CASE_							: Union[str, Any]			):
        return WhisperTokenizer.from_pretrained(self.checkpoint       ,     **SCREAMING_SNAKE_CASE_			)



      def 	SCREAMING_SNAKE_CASE__					( self							: Optional[Any]       ,     **SCREAMING_SNAKE_CASE_							: Union[str, Any]			):
        return WhisperFeatureExtractor.from_pretrained(self.checkpoint       ,     **SCREAMING_SNAKE_CASE_			)



      def 	SCREAMING_SNAKE_CASE__					( self							: Any			):
        shutil.rmtree(self.tmpdirname			)



      def 	SCREAMING_SNAKE_CASE__					( self							: Dict			):
        lowerCAmelCase_  :							Tuple								=					self.get_tokenizer()
        lowerCAmelCase_  :							Optional[Any]								=					self.get_feature_extractor()

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

        processor.save_pretrained(self.tmpdirname			)
        lowerCAmelCase_  :							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 	SCREAMING_SNAKE_CASE__					( self							: Tuple			):
        lowerCAmelCase_  :							str								=					WhisperProcessor(tokenizer=self.get_tokenizer()       ,     feature_extractor=self.get_feature_extractor()			)
        processor.save_pretrained(self.tmpdirname			)

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

        lowerCAmelCase_  :							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 	SCREAMING_SNAKE_CASE__					( self							: Optional[int]			):
        lowerCAmelCase_  :							List[str]								=					self.get_feature_extractor()
        lowerCAmelCase_  :							Dict								=					self.get_tokenizer()

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

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

        lowerCAmelCase_  :							List[Any]								=					feature_extractor(SCREAMING_SNAKE_CASE_       ,     return_tensors='np'			)
        lowerCAmelCase_  :							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 	SCREAMING_SNAKE_CASE__					( self							: Any			):
        lowerCAmelCase_  :							Optional[Any]								=					self.get_feature_extractor()
        lowerCAmelCase_  :							Optional[int]								=					self.get_tokenizer()

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

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

        lowerCAmelCase_  :							int								=					processor(text=SCREAMING_SNAKE_CASE_			)

        lowerCAmelCase_  :							int								=					tokenizer(SCREAMING_SNAKE_CASE_			)

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



      def 	SCREAMING_SNAKE_CASE__					( self							: int			):
        lowerCAmelCase_  :							str								=					self.get_feature_extractor()
        lowerCAmelCase_  :							Optional[Any]								=					self.get_tokenizer()

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

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

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

        self.assertListEqual(SCREAMING_SNAKE_CASE_       ,     SCREAMING_SNAKE_CASE_			)



      def 	SCREAMING_SNAKE_CASE__					( self							: List[Any]			):
        lowerCAmelCase_  :							Optional[int]								=					self.get_feature_extractor()
        lowerCAmelCase_  :							Tuple								=					self.get_tokenizer()

        lowerCAmelCase_  :							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 	SCREAMING_SNAKE_CASE__					( self							: str			):
        lowerCAmelCase_  :							Optional[Any]								=					self.get_feature_extractor()
        lowerCAmelCase_  :							Tuple								=					self.get_tokenizer()

        lowerCAmelCase_  :							List[str]								=					WhisperProcessor(tokenizer=SCREAMING_SNAKE_CASE_       ,     feature_extractor=SCREAMING_SNAKE_CASE_			)
        lowerCAmelCase_  :							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)			)

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



      def 	SCREAMING_SNAKE_CASE__					( self							: Tuple			):
        lowerCAmelCase_  :							Any								=					WhisperProcessor(tokenizer=self.get_tokenizer()       ,     feature_extractor=self.get_feature_extractor()			)
        lowerCAmelCase_  :							int								=					processor.get_prompt_ids('Mr. Quilter'			)
        lowerCAmelCase_  :							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 	SCREAMING_SNAKE_CASE__					( self							: List[str]			):
        lowerCAmelCase_  :							List[Any]								=					WhisperProcessor(tokenizer=self.get_tokenizer()       ,     feature_extractor=self.get_feature_extractor()			)
        lowerCAmelCase_  :							List[str]								=					processor.get_prompt_ids(''			)
        lowerCAmelCase_  :							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 	SCREAMING_SNAKE_CASE__					( self							: Dict			):
        lowerCAmelCase_  :							Tuple								=					WhisperProcessor(tokenizer=self.get_tokenizer()       ,     feature_extractor=self.get_feature_extractor()			)

        def _test_prompt_error_raised_helper(SCREAMING_SNAKE_CASE_							: List[Any]       ,     SCREAMING_SNAKE_CASE_							: int			):
          with pytest.raises(SCREAMING_SNAKE_CASE_			) as excinfo:
            processor.get_prompt_ids(SCREAMING_SNAKE_CASE_			)
          lowerCAmelCase_  :							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() = }')





