



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_         = 5_0358
snake_case_         = 5_0362







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



						"""simple docstring"""






						def 			UpperCAmelCase__  (      self				:Any      )       ->    Optional[Any]:
												UpperCAmelCase    =				'openai/whisper-small.en'
												UpperCAmelCase    =				tempfile.mkdtemp()






						def 			UpperCAmelCase__  (      self				:Optional[int]     ,						**lowercase_				:List[Any]      )       ->    Dict:
												return WhisperTokenizer.from_pretrained(self.checkpoint     ,						**lowercase_      )






						def 			UpperCAmelCase__  (      self				:Tuple     ,						**lowercase_				:str      )       ->    int:
												return WhisperFeatureExtractor.from_pretrained(self.checkpoint     ,						**lowercase_      )






						def 			UpperCAmelCase__  (      self				:str      )       ->    Any:
												shutil.rmtree(self.tmpdirname      )






						def 			UpperCAmelCase__  (      self				:Optional[int]      )       ->    int:
												UpperCAmelCase    =				self.get_tokenizer()
												UpperCAmelCase    =				self.get_feature_extractor()

												UpperCAmelCase    =				WhisperProcessor(tokenizer=lowercase_     ,						feature_extractor=lowercase_      )

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

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

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






						def 			UpperCAmelCase__  (      self				:Optional[int]      )       ->    List[str]:
												UpperCAmelCase    =				WhisperProcessor(tokenizer=self.get_tokenizer()     ,						feature_extractor=self.get_feature_extractor()      )
												processor.save_pretrained(self.tmpdirname      )

												UpperCAmelCase    =				self.get_tokenizer(bos_token='(BOS)'     ,						eos_token='(EOS)'      )
												UpperCAmelCase    =				self.get_feature_extractor(do_normalize=lowercase_     ,						padding_value=1.0      )

												UpperCAmelCase    =				WhisperProcessor.from_pretrained(
												    self.tmpdirname     ,						bos_token='(BOS)'     ,						eos_token='(EOS)'     ,						do_normalize=lowercase_     ,						padding_value=1.0      )

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

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






						def 			UpperCAmelCase__  (      self				:Any      )       ->    List[Any]:
												UpperCAmelCase    =				self.get_feature_extractor()
												UpperCAmelCase    =				self.get_tokenizer()

												UpperCAmelCase    =				WhisperProcessor(tokenizer=lowercase_     ,						feature_extractor=lowercase_      )

												UpperCAmelCase    =				floats_list((3, 10_00)      )

												UpperCAmelCase    =				feature_extractor(lowercase_     ,						return_tensors='np'      )
												UpperCAmelCase    =				processor(lowercase_     ,						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				:Optional[int]      )       ->    Optional[Any]:
												UpperCAmelCase    =				self.get_feature_extractor()
												UpperCAmelCase    =				self.get_tokenizer()

												UpperCAmelCase    =				WhisperProcessor(tokenizer=lowercase_     ,						feature_extractor=lowercase_      )

												UpperCAmelCase    =				'This is a test string'

												UpperCAmelCase    =				processor(text=lowercase_      )

												UpperCAmelCase    =				tokenizer(lowercase_      )

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






						def 			UpperCAmelCase__  (      self				:Optional[Any]      )       ->    Optional[int]:
												UpperCAmelCase    =				self.get_feature_extractor()
												UpperCAmelCase    =				self.get_tokenizer()

												UpperCAmelCase    =				WhisperProcessor(tokenizer=lowercase_     ,						feature_extractor=lowercase_      )

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

												UpperCAmelCase    =				processor.batch_decode(lowercase_      )
												UpperCAmelCase    =				tokenizer.batch_decode(lowercase_      )

												self.assertListEqual(lowercase_     ,						lowercase_      )






						def 			UpperCAmelCase__  (      self				:Optional[int]      )       ->    Any:
												UpperCAmelCase    =				self.get_feature_extractor()
												UpperCAmelCase    =				self.get_tokenizer()

												UpperCAmelCase    =				WhisperProcessor(tokenizer=lowercase_     ,						feature_extractor=lowercase_      )

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

												UpperCAmelCase    =				WhisperProcessor(tokenizer=lowercase_     ,						feature_extractor=lowercase_      )
												UpperCAmelCase    =				processor.get_decoder_prompt_ids(task='transcribe'     ,						no_timestamps=lowercase_      )

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

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






						def 			UpperCAmelCase__  (      self				:Tuple      )       ->    Optional[int]:
												UpperCAmelCase    =				WhisperProcessor(tokenizer=self.get_tokenizer()     ,						feature_extractor=self.get_feature_extractor()      )
												UpperCAmelCase    =				processor.get_prompt_ids('Mr. Quilter'      )
												UpperCAmelCase    =				processor.tokenizer.decode(lowercase_      )

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






						def 			UpperCAmelCase__  (      self				:int      )       ->    Optional[int]:
												UpperCAmelCase    =				WhisperProcessor(tokenizer=self.get_tokenizer()     ,						feature_extractor=self.get_feature_extractor()      )
												UpperCAmelCase    =				processor.get_prompt_ids(''      )
												UpperCAmelCase    =				processor.tokenizer.decode(lowercase_      )

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






						def 			UpperCAmelCase__  (      self				:Dict      )       ->    Any:
												UpperCAmelCase    =				WhisperProcessor(tokenizer=self.get_tokenizer()     ,						feature_extractor=self.get_feature_extractor()      )

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


from __future__ import annotations

import random
from collections.abc import Iterable
from typing import Any, Generic, TypeVar

snake_case_         = TypeVar("""T""", bound=bool)







class    A_    (  Generic[T]       ):



						"""simple docstring"""






						def __init__(      self				:str     ,						lowercase_				:T      )       ->    None:
												UpperCAmelCase    =				value
												UpperCAmelCase    =				None
												UpperCAmelCase    =				None






						@property
						def 			UpperCAmelCase__  (      self				:Any      )       ->    T:
												return self._value






						@staticmethod
						def 			UpperCAmelCase__  (      lowercase_				:RandomizedHeapNode[T] | None     ,						lowercase_				:RandomizedHeapNode[T] | None      )       ->    RandomizedHeapNode[T] | None:
												if not roota:
																		return roota

												if not roota:
																		return roota

												if roota.value > roota.value:
																		UpperCAmelCase   ,					UpperCAmelCase    =				roota, roota

												if random.choice([True, False]      ):
																		UpperCAmelCase   ,					UpperCAmelCase    =				roota.right, roota.left

												UpperCAmelCase    =				RandomizedHeapNode.merge(roota.left     ,						lowercase_      )

												return roota







class    A_    (  Generic[T]       ):



						"""simple docstring"""






						def __init__(      self				:Optional[Any]     ,						lowercase_				:Iterable[T] | None = ()      )       ->    None:
												UpperCAmelCase    =				None

												if data:
																		for item in data:
																								self.insert(lowercase_      )






						def 			UpperCAmelCase__  (      self				:Dict     ,						lowercase_				:T      )       ->    None:
												UpperCAmelCase    =				RandomizedHeapNode.merge(self._root     ,						RandomizedHeapNode(lowercase_      )      )






						def 			UpperCAmelCase__  (      self				:Dict      )       ->    T | None:
												UpperCAmelCase    =				self.top()

												if self._root is None:
																		return None

												UpperCAmelCase    =				RandomizedHeapNode.merge(self._root.left     ,						self._root.right      )

												return result






						def 			UpperCAmelCase__  (      self				:Tuple      )       ->    T:
												if not self._root:
																		raise IndexError('Can\'t get top element for the empty heap.'      )
												return self._root.value






						def 			UpperCAmelCase__  (      self				:Dict      )       ->    None:
												UpperCAmelCase    =				None






						def 			UpperCAmelCase__  (      self				:Union[str, Any]      )       ->    list[Any]:
												UpperCAmelCase    =				[]
												while self:
																		result.append(self.pop()      )

												return result






						def __bool__(      self				:Optional[Any]      )       ->    bool:
												return self._root is not None


if __name__ == "__main__":
				import doctest

				doctest.testmod()





