code: 
  
  
  
  
  
  
  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   							=							5_03_58
  SCREAMING_SNAKE_CASE__		:       Dict   							=							5_03_62
  
  
  
  
  
  
  
  @require_torch
  @require_torchaudio
  @require_sentencepiece
  class 							UpperCAmelCase_     (		unittest.TestCase		):
  
  
  
  
       def      __UpperCAmelCase     (							self		):
            UpperCAmelCase__			:     Union[str, Any]										= """openai/whisper-small.en"""
            UpperCAmelCase__			:     List[str]										= tempfile.mkdtemp()
  
  
  
  
       def      __UpperCAmelCase     (							self  ,	**_lowerCAmelCase		):
            return WhisperTokenizer.from_pretrained(self.checkpoint  ,	**_lowerCAmelCase		)
  
  
  
  
       def      __UpperCAmelCase     (							self  ,	**_lowerCAmelCase		):
            return WhisperFeatureExtractor.from_pretrained(self.checkpoint  ,	**_lowerCAmelCase		)
  
  
  
  
       def      __UpperCAmelCase     (							self		):
            shutil.rmtree(self.tmpdirname		)
  
  
  
  
       def      __UpperCAmelCase     (							self		):
            UpperCAmelCase__			:     Optional[Any]										= self.get_tokenizer()
            UpperCAmelCase__			:     Optional[int]										= self.get_feature_extractor()
  
            UpperCAmelCase__			:     Union[str, Any]										= WhisperProcessor(tokenizer=_lowerCAmelCase  ,	feature_extractor=_lowerCAmelCase		)
  
            processor.save_pretrained(self.tmpdirname		)
            UpperCAmelCase__			:     str										= 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		):
            UpperCAmelCase__			:     Optional[int]										= WhisperProcessor(tokenizer=self.get_tokenizer()  ,	feature_extractor=self.get_feature_extractor()		)
            processor.save_pretrained(self.tmpdirname		)
  
            UpperCAmelCase__			:     Dict										= self.get_tokenizer(bos_token="""(BOS)"""  ,	eos_token="""(EOS)"""		)
            UpperCAmelCase__			:     Dict										= self.get_feature_extractor(do_normalize=_lowerCAmelCase  ,	padding_value=1.0		)
  
            UpperCAmelCase__			:     Tuple										= 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		):
            UpperCAmelCase__			:     Union[str, Any]										= self.get_feature_extractor()
            UpperCAmelCase__			:     List[Any]										= self.get_tokenizer()
  
            UpperCAmelCase__			:     int										= WhisperProcessor(tokenizer=_lowerCAmelCase  ,	feature_extractor=_lowerCAmelCase		)
  
            UpperCAmelCase__			:     Optional[int]										= floats_list((3, 1000)		)
  
            UpperCAmelCase__			:     Optional[int]										= feature_extractor(_lowerCAmelCase  ,	return_tensors="""np"""		)
            UpperCAmelCase__			:     List[Any]										= 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		):
            UpperCAmelCase__			:     Tuple										= self.get_feature_extractor()
            UpperCAmelCase__			:     Optional[Any]										= self.get_tokenizer()
  
            UpperCAmelCase__			:     Any										= WhisperProcessor(tokenizer=_lowerCAmelCase  ,	feature_extractor=_lowerCAmelCase		)
  
            UpperCAmelCase__			:     List[Any]										= """This is a test string"""
  
            UpperCAmelCase__			:     Tuple										= processor(text=_lowerCAmelCase		)
  
            UpperCAmelCase__			:     List[Any]										= tokenizer(_lowerCAmelCase		)
  
            for key in encoded_tok.keys():
                 self.assertListEqual(encoded_tok[key]  ,	encoded_processor[key]		)
  
  
  
  
       def      __UpperCAmelCase     (							self		):
            UpperCAmelCase__			:     Union[str, Any]										= self.get_feature_extractor()
            UpperCAmelCase__			:     List[str]										= self.get_tokenizer()
  
            UpperCAmelCase__			:     Dict										= WhisperProcessor(tokenizer=_lowerCAmelCase  ,	feature_extractor=_lowerCAmelCase		)
  
            UpperCAmelCase__			:     Tuple										= [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]
  
            UpperCAmelCase__			:     Dict										= processor.batch_decode(_lowerCAmelCase		)
            UpperCAmelCase__			:     int										= tokenizer.batch_decode(_lowerCAmelCase		)
  
            self.assertListEqual(_lowerCAmelCase  ,	_lowerCAmelCase		)
  
  
  
  
       def      __UpperCAmelCase     (							self		):
            UpperCAmelCase__			:     Dict										= self.get_feature_extractor()
            UpperCAmelCase__			:     List[Any]										= self.get_tokenizer()
  
            UpperCAmelCase__			:     Any										= 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		):
            UpperCAmelCase__			:     int										= self.get_feature_extractor()
            UpperCAmelCase__			:     Union[str, Any]										= self.get_tokenizer()
  
            UpperCAmelCase__			:     Optional[int]										= WhisperProcessor(tokenizer=_lowerCAmelCase  ,	feature_extractor=_lowerCAmelCase		)
            UpperCAmelCase__			:     List[Any]										= 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)		)
  
            UpperCAmelCase__			:     Optional[Any]										= [TRANSCRIBE, NOTIMESTAMPS]
            self.assertListEqual([ids[-1] for ids in forced_decoder_ids]  ,	_lowerCAmelCase		)
  
  
  
  
       def      __UpperCAmelCase     (							self		):
            UpperCAmelCase__			:     str										= WhisperProcessor(tokenizer=self.get_tokenizer()  ,	feature_extractor=self.get_feature_extractor()		)
            UpperCAmelCase__			:     List[Any]										= processor.get_prompt_ids("""Mr. Quilter"""		)
            UpperCAmelCase__			:     Optional[Any]										= processor.tokenizer.decode(_lowerCAmelCase		)
  
            self.assertListEqual(prompt_ids.tolist()  ,	[50360, 1770, 13, 2264, 346, 353]		)
            self.assertEqual(_lowerCAmelCase  ,	"""<|startofprev|> Mr. Quilter"""		)
  
  
  
  
       def      __UpperCAmelCase     (							self		):
            UpperCAmelCase__			:     Tuple										= WhisperProcessor(tokenizer=self.get_tokenizer()  ,	feature_extractor=self.get_feature_extractor()		)
            UpperCAmelCase__			:     Tuple										= processor.get_prompt_ids(""""""		)
            UpperCAmelCase__			:     str										= processor.tokenizer.decode(_lowerCAmelCase		)
  
            self.assertListEqual(prompt_ids.tolist()  ,	[50360, 220]		)
            self.assertEqual(_lowerCAmelCase  ,	"""<|startofprev|> """		)
  
  
  
  
       def      __UpperCAmelCase     (							self		):
            UpperCAmelCase__			:     Optional[int]										= WhisperProcessor(tokenizer=self.get_tokenizer()  ,	feature_extractor=self.get_feature_extractor()		)
  
            def _test_prompt_error_raised_helper(_lowerCAmelCase  ,	_lowerCAmelCase		):
                 with pytest.raises(_lowerCAmelCase		) as excinfo:
                      processor.get_prompt_ids(_lowerCAmelCase		)
                 UpperCAmelCase__			:     Optional[Any]										= 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|>"""		)
  
  
  
  
  
  
  
style_context: 
  
  
  
  
  
  
  import math
  import os
  
  import cva
  import numpy as np
  
  SCREAMING_SNAKE_CASE__		:       Optional[Any]   							=							255.0
  
  
  
  
  
  def    _lowerCamelCase	(	__lowerCamelCase   ,       __lowerCamelCase					)   -> float:
  
       '''simple docstring'''
       UpperCAmelCase__			:     List[Any]										= np.mean((original - contrast) ** 2					)
       if mse == 0:
            return 100
  
       return 20 * math.logaa(PIXEL_MAX / math.sqrt(__lowerCamelCase					)					)
  
  
  
  
  
  def    _lowerCamelCase	(	)   -> None:
  
       '''simple docstring'''
       UpperCAmelCase__			:     Union[str, Any]										= os.path.dirname(os.path.realpath(__lowerCamelCase					)					)
       # Loading images (original image and compressed image)
       UpperCAmelCase__			:     List[Any]										= cva.imread(os.path.join(__lowerCamelCase   ,       """image_data/original_image.png"""					)					)
       UpperCAmelCase__			:     Dict										= cva.imread(os.path.join(__lowerCamelCase   ,       """image_data/compressed_image.png"""					)   ,       1					)
  
       UpperCAmelCase__			:     Tuple										= cva.imread(os.path.join(__lowerCamelCase   ,       """image_data/PSNR-example-base.png"""					)					)
       UpperCAmelCase__			:     int										= cva.imread(
           os.path.join(__lowerCamelCase   ,       """image_data/PSNR-example-comp-10.jpg"""					)   ,       1					)
  
       # Value expected: 29.73dB
       print("""-- First Test --"""					)
       print(F"PSNR value is {peak_signal_to_noise_ratio(__lowerCamelCase   ,       __lowerCamelCase					)} dB"					)
  
       # # Value expected: 31.53dB (Wikipedia Example)
       print("""\n-- Second Test --"""					)
       print(F"PSNR value is {peak_signal_to_noise_ratio(__lowerCamelCase   ,       __lowerCamelCase					)} dB"					)
  
  
  if __name__ == "__main__":
        main()
  
  
  
  
  
  
  