

'''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


lowerCAmelCase__      				=			50358
lowerCAmelCase__      				=			50362



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


							def     _snake_case				(      self)				->					Optional[Any]:
								UpperCAmelCase_		:   Any													=       'openai/whisper-small.en'
								UpperCAmelCase_		:   List[str]													=       tempfile.mkdtemp()


							def     _snake_case				(      self     ,      **_snake_case)				->					int:
								return WhisperTokenizer.from_pretrained(self.checkpoint     ,      **_snake_case)


							def     _snake_case				(      self     ,      **_snake_case)				->					List[str]:
								return WhisperFeatureExtractor.from_pretrained(self.checkpoint     ,      **_snake_case)


							def     _snake_case				(      self)				->					List[Any]:
								shutil.rmtree(self.tmpdirname)


							def     _snake_case				(      self)				->					str:
								UpperCAmelCase_		:   List[str]													=       self.get_tokenizer()
								UpperCAmelCase_		:   Union[str, Any]													=       self.get_feature_extractor()

								UpperCAmelCase_		:   List[Any]													=       WhisperProcessor(tokenizer=_snake_case     ,      feature_extractor=_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     ,      _snake_case)

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


							def     _snake_case				(      self)				->					Dict:
								UpperCAmelCase_		:   Union[str, Any]													=       WhisperProcessor(tokenizer=self.get_tokenizer()     ,      feature_extractor=self.get_feature_extractor())
								processor.save_pretrained(self.tmpdirname)

								UpperCAmelCase_		:   Union[str, Any]													=       self.get_tokenizer(bos_token='(BOS)'     ,      eos_token='(EOS)')
								UpperCAmelCase_		:   Optional[Any]													=       self.get_feature_extractor(do_normalize=_snake_case     ,      padding_value=1.0)

								UpperCAmelCase_		:   List[str]													=       WhisperProcessor.from_pretrained(
								    self.tmpdirname     ,      bos_token='(BOS)'     ,      eos_token='(EOS)'     ,      do_normalize=_snake_case     ,      padding_value=1.0)

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

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


							def     _snake_case				(      self)				->					Tuple:
								UpperCAmelCase_		:   Any													=       self.get_feature_extractor()
								UpperCAmelCase_		:   Any													=       self.get_tokenizer()

								UpperCAmelCase_		:   Any													=       WhisperProcessor(tokenizer=_snake_case     ,      feature_extractor=_snake_case)

								UpperCAmelCase_		:   str													=       floats_list((3, 1000))

								UpperCAmelCase_		:   Optional[int]													=       feature_extractor(_snake_case     ,      return_tensors='np')
								UpperCAmelCase_		:   str													=       processor(_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     _snake_case				(      self)				->					Any:
								UpperCAmelCase_		:   Tuple													=       self.get_feature_extractor()
								UpperCAmelCase_		:   int													=       self.get_tokenizer()

								UpperCAmelCase_		:   List[Any]													=       WhisperProcessor(tokenizer=_snake_case     ,      feature_extractor=_snake_case)

								UpperCAmelCase_		:   str													=       'This is a test string'

								UpperCAmelCase_		:   List[str]													=       processor(text=_snake_case)

								UpperCAmelCase_		:   Tuple													=       tokenizer(_snake_case)

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


							def     _snake_case				(      self)				->					Dict:
								UpperCAmelCase_		:   Optional[int]													=       self.get_feature_extractor()
								UpperCAmelCase_		:   Any													=       self.get_tokenizer()

								UpperCAmelCase_		:   Tuple													=       WhisperProcessor(tokenizer=_snake_case     ,      feature_extractor=_snake_case)

								UpperCAmelCase_		:   Tuple													=       [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

								UpperCAmelCase_		:   Dict													=       processor.batch_decode(_snake_case)
								UpperCAmelCase_		:   List[str]													=       tokenizer.batch_decode(_snake_case)

								self.assertListEqual(_snake_case     ,      _snake_case)


							def     _snake_case				(      self)				->					List[str]:
								UpperCAmelCase_		:   List[Any]													=       self.get_feature_extractor()
								UpperCAmelCase_		:   Dict													=       self.get_tokenizer()

								UpperCAmelCase_		:   Optional[Any]													=       WhisperProcessor(tokenizer=_snake_case     ,      feature_extractor=_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     _snake_case				(      self)				->					Union[str, Any]:
								UpperCAmelCase_		:   Tuple													=       self.get_feature_extractor()
								UpperCAmelCase_		:   List[Any]													=       self.get_tokenizer()

								UpperCAmelCase_		:   Union[str, Any]													=       WhisperProcessor(tokenizer=_snake_case     ,      feature_extractor=_snake_case)
								UpperCAmelCase_		:   Optional[int]													=       processor.get_decoder_prompt_ids(task='transcribe'     ,      no_timestamps=_snake_case)

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

								UpperCAmelCase_		:   str													=       [TRANSCRIBE, NOTIMESTAMPS]
								self.assertListEqual([ids[-1] for ids in forced_decoder_ids]     ,      _snake_case)


							def     _snake_case				(      self)				->					str:
								UpperCAmelCase_		:   Tuple													=       WhisperProcessor(tokenizer=self.get_tokenizer()     ,      feature_extractor=self.get_feature_extractor())
								UpperCAmelCase_		:   Tuple													=       processor.get_prompt_ids('Mr. Quilter')
								UpperCAmelCase_		:   Optional[Any]													=       processor.tokenizer.decode(_snake_case)

								self.assertListEqual(prompt_ids.tolist()     ,      [5_0360, 1770, 13, 2264, 346, 353])
								self.assertEqual(_snake_case     ,      '<|startofprev|> Mr. Quilter')


							def     _snake_case				(      self)				->					Optional[int]:
								UpperCAmelCase_		:   Union[str, Any]													=       WhisperProcessor(tokenizer=self.get_tokenizer()     ,      feature_extractor=self.get_feature_extractor())
								UpperCAmelCase_		:   Union[str, Any]													=       processor.get_prompt_ids('')
								UpperCAmelCase_		:   List[str]													=       processor.tokenizer.decode(_snake_case)

								self.assertListEqual(prompt_ids.tolist()     ,      [5_0360, 220])
								self.assertEqual(_snake_case     ,      '<|startofprev|> ')





							def     _snake_case				(      self)				->					Optional[Any]:
								UpperCAmelCase_		:   Any													=       WhisperProcessor(tokenizer=self.get_tokenizer()     ,      feature_extractor=self.get_feature_extractor())

								def _test_prompt_error_raised_helper(_snake_case     ,      _snake_case):
									with pytest.raises(_snake_case) as excinfo:
										processor.get_prompt_ids(_snake_case)
									UpperCAmelCase_		:   int													=       F"""Encountered text in the prompt corresponding to disallowed special token: {special_token}."""
									self.assertEqual(_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'''


from __future__ import annotations

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

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



class        lowercase    (	Generic[T]			):


							def __init__(      self     ,      _snake_case)				->					None:
								UpperCAmelCase_		:   T													=       value
								UpperCAmelCase_		:   SkewNode[T] | None													=       None
								UpperCAmelCase_		:   SkewNode[T] | None													=       None


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





							@staticmethod
							def     _snake_case				(      _snake_case     ,      _snake_case)				->					SkewNode[T] | None:
								if not roota:
									return roota

								if not roota:
									return roota

								if roota.value > roota.value:
									UpperCAmelCase_						,     UpperCAmelCase_		:   Union[str, Any]													=       roota, roota

								UpperCAmelCase_		:   Dict													=       roota
								UpperCAmelCase_		:   Dict													=       roota.right
								UpperCAmelCase_		:   Optional[int]													=       roota.left
								UpperCAmelCase_		:   Dict													=       SkewNode.merge(_snake_case     ,      _snake_case)

								return result



class        lowercase    (	Generic[T]			):


							def __init__(      self     ,      _snake_case = ())				->					None:
								UpperCAmelCase_		:   SkewNode[T] | None													=       None
								if data:
									for item in data:
										self.insert(_snake_case)


							def __bool__(      self)				->					bool:
								return self._root is not None


							def __iter__(      self)				->					Iterator[T]:
								UpperCAmelCase_		:   list[Any]													=       []
								while self:
									result.append(self.pop())

								# Pushing items back to the heap not to clear it.
								for item in result:
									self.insert(_snake_case)

								return iter(_snake_case)


							def     _snake_case				(      self     ,      _snake_case)				->					None:
								UpperCAmelCase_		:   Any													=       SkewNode.merge(self._root     ,      SkewNode(_snake_case))


							def     _snake_case				(      self)				->					T | None:
								UpperCAmelCase_		:   Union[str, Any]													=       self.top()
								UpperCAmelCase_		:   Tuple													=       (
								    SkewNode.merge(self._root.left     ,      self._root.right) if self._root else None
								)

								return result


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





							def     _snake_case				(      self)				->					None:
								UpperCAmelCase_		:   Union[str, Any]													=       None


if __name__ == "__main__":
						import doctest

						doctest.testmod()






