





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


A__       :     Optional[Any]          =5_03_58
A__       :     Tuple          =5_03_62




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




      def        lowercase__					(    self    :				Optional[Any]						)       ->   Tuple:
            _lowerCAmelCase							  =							"""openai/whisper-small.en"""
            _lowerCAmelCase							  =							tempfile.mkdtemp()




      def        lowercase__					(    self    :				str    ,    **__snake_case    :				int						)       ->   Optional[int]:
            return WhisperTokenizer.from_pretrained(self.checkpoint    ,    **__SCREAMING_SNAKE_CASE						)




      def        lowercase__					(    self    :				Tuple    ,    **__snake_case    :				Optional[Any]						)       ->   Optional[int]:
            return WhisperFeatureExtractor.from_pretrained(self.checkpoint    ,    **__SCREAMING_SNAKE_CASE						)




      def        lowercase__					(    self    :				Optional[int]						)       ->   Union[str, Any]:
            shutil.rmtree(self.tmpdirname						)




      def        lowercase__					(    self    :				Dict						)       ->   str:
            _lowerCAmelCase							  =							self.get_tokenizer()
            _lowerCAmelCase							  =							self.get_feature_extractor()

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

            processor.save_pretrained(self.tmpdirname						)
            _lowerCAmelCase							  =							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        lowercase__					(    self    :				Union[str, Any]						)       ->   Tuple:
            _lowerCAmelCase							  =							WhisperProcessor(tokenizer=self.get_tokenizer()    ,    feature_extractor=self.get_feature_extractor()						)
            processor.save_pretrained(self.tmpdirname						)

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

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

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

            _lowerCAmelCase							  =							floats_list((3, 10_00)						)

            _lowerCAmelCase							  =							feature_extractor(__SCREAMING_SNAKE_CASE    ,    return_tensors="""np"""						)
            _lowerCAmelCase							  =							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        lowercase__					(    self    :				Optional[int]						)       ->   List[Any]:
            _lowerCAmelCase							  =							self.get_feature_extractor()
            _lowerCAmelCase							  =							self.get_tokenizer()

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

            _lowerCAmelCase							  =							"""This is a test string"""

            _lowerCAmelCase							  =							processor(text=__SCREAMING_SNAKE_CASE						)

            _lowerCAmelCase							  =							tokenizer(__SCREAMING_SNAKE_CASE						)

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




      def        lowercase__					(    self    :				str						)       ->   List[Any]:
            _lowerCAmelCase							  =							self.get_feature_extractor()
            _lowerCAmelCase							  =							self.get_tokenizer()

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

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

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

            self.assertListEqual(__SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE						)




      def        lowercase__					(    self    :				Any						)       ->   Optional[Any]:
            _lowerCAmelCase							  =							self.get_feature_extractor()
            _lowerCAmelCase							  =							self.get_tokenizer()

            _lowerCAmelCase							  =							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        lowercase__					(    self    :				str						)       ->   List[str]:
            _lowerCAmelCase							  =							self.get_feature_extractor()
            _lowerCAmelCase							  =							self.get_tokenizer()

            _lowerCAmelCase							  =							WhisperProcessor(tokenizer=__SCREAMING_SNAKE_CASE    ,    feature_extractor=__SCREAMING_SNAKE_CASE						)
            _lowerCAmelCase							  =							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							  =							[TRANSCRIBE, NOTIMESTAMPS]
            self.assertListEqual([ids[-1] for ids in forced_decoder_ids]    ,    __SCREAMING_SNAKE_CASE						)




      def        lowercase__					(    self    :				Any						)       ->   List[str]:
            _lowerCAmelCase							  =							WhisperProcessor(tokenizer=self.get_tokenizer()    ,    feature_extractor=self.get_feature_extractor()						)
            _lowerCAmelCase							  =							processor.get_prompt_ids("""Mr. Quilter"""						)
            _lowerCAmelCase							  =							processor.tokenizer.decode(__SCREAMING_SNAKE_CASE						)

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




      def        lowercase__					(    self    :				Optional[int]						)       ->   List[Any]:
            _lowerCAmelCase							  =							WhisperProcessor(tokenizer=self.get_tokenizer()    ,    feature_extractor=self.get_feature_extractor()						)
            _lowerCAmelCase							  =							processor.get_prompt_ids(""""""						)
            _lowerCAmelCase							  =							processor.tokenizer.decode(__SCREAMING_SNAKE_CASE						)

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




      def        lowercase__					(    self    :				Tuple						)       ->   Dict:
            _lowerCAmelCase							  =							WhisperProcessor(tokenizer=self.get_tokenizer()    ,    feature_extractor=self.get_feature_extractor()						)

            def _test_prompt_error_raised_helper(__snake_case    :				Optional[Any]    ,    __snake_case    :				Dict						):
                  with pytest.raises(__SCREAMING_SNAKE_CASE						) as excinfo:
                        processor.get_prompt_ids(__SCREAMING_SNAKE_CASE						)
                  _lowerCAmelCase							  =							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'''






class 				UpperCAmelCase  :




      def __init__(    self    :				Optional[Any]						)       ->   List[str]:
            _lowerCAmelCase							  =							[0]
            _lowerCAmelCase							  =							0




      def        lowercase__					(    self    :				str    ,    __snake_case    :				int						)       ->   None:
            _lowerCAmelCase							  =							self.__heap[i]
            while i // 2 > 0:
                  if self.__heap[i] > self.__heap[i // 2]:
                        _lowerCAmelCase							  =							self.__heap[i // 2]
                        _lowerCAmelCase							  =							temporary
                  i //= 2




      def        lowercase__					(    self    :				Optional[Any]    ,    __snake_case    :				int						)       ->   None:
            self.__heap.append(__snake_case						)
            self.__size += 1
            self.__swap_up(self.__size						)




      def        lowercase__					(    self    :				str    ,    __snake_case    :				int						)       ->   None:
            while self.__size >= 2 * i:
                  if 2 * i + 1 > self.__size:
                        _lowerCAmelCase							  =							2 * i
                  else:
                        if self.__heap[2 * i] > self.__heap[2 * i + 1]:
                              _lowerCAmelCase							  =							2 * i
                        else:
                              _lowerCAmelCase							  =							2 * i + 1
                  _lowerCAmelCase							  =							self.__heap[i]
                  if self.__heap[i] < self.__heap[bigger_child]:
                        _lowerCAmelCase							  =							self.__heap[bigger_child]
                        _lowerCAmelCase							  =							temporary
                  _lowerCAmelCase							  =							bigger_child




      def        lowercase__					(    self    :				Tuple						)       ->   int:
            _lowerCAmelCase							  =							self.__heap[1]
            _lowerCAmelCase							  =							self.__heap[self.__size]
            self.__size -= 1
            self.__heap.pop()
            self.__swap_down(1						)
            return max_value




      @property
      def        lowercase__					(    self    :				List[Any]						)       ->   str:
            return self.__heap[1:]




      def __len__(    self    :				Optional[Any]						)       ->   str:
            return self.__size


if __name__ == "__main__":
      import doctest

      doctest.testmod()
      # create an instance of BinaryHeap
      A__       :     Tuple          =BinaryHeap()
      binary_heap.insert(6)
      binary_heap.insert(10)
      binary_heap.insert(15)
      binary_heap.insert(12)
      # pop root(max-values because it is max heap)
      print(binary_heap.pop())  # 15
      print(binary_heap.pop())  # 12
      # get the list and size after operations
      print(binary_heap.get_list)
      print(len(binary_heap))






