






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


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


   '''simple docstring'''

   def     _SCREAMING_SNAKE_CASE    (     self			:					List[Any]  ):

     '''simple docstring'''
     __A						      =			'''openai/whisper-small.en'''
     __A						      =			tempfile.mkdtemp()


   def     _SCREAMING_SNAKE_CASE    (     self			:					Tuple,	**_lowerCamelCase			:					str  ):

     '''simple docstring'''
     return WhisperTokenizer.from_pretrained(self.checkpoint,	**_lowerCamelCase  )


   def     _SCREAMING_SNAKE_CASE    (     self			:					Dict,	**_lowerCamelCase			:					List[Any]  ):

     '''simple docstring'''
     return WhisperFeatureExtractor.from_pretrained(self.checkpoint,	**_lowerCamelCase  )


   def     _SCREAMING_SNAKE_CASE    (     self			:					List[str]  ):

     '''simple docstring'''
     shutil.rmtree(self.tmpdirname  )


   def     _SCREAMING_SNAKE_CASE    (     self			:					int  ):

     '''simple docstring'''
     __A						      =			self.get_tokenizer()
     __A						      =			self.get_feature_extractor()

     __A						      =			WhisperProcessor(tokenizer=_lowerCamelCase,	feature_extractor=_lowerCamelCase  )

     processor.save_pretrained(self.tmpdirname  )
     __A						      =			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     _SCREAMING_SNAKE_CASE    (     self			:					Any  ):

     '''simple docstring'''
     __A						      =			WhisperProcessor(tokenizer=self.get_tokenizer(),	feature_extractor=self.get_feature_extractor()  )
     processor.save_pretrained(self.tmpdirname  )

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

     __A						      =			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     _SCREAMING_SNAKE_CASE    (     self			:					Optional[int]  ):

     '''simple docstring'''
     __A						      =			self.get_feature_extractor()
     __A						      =			self.get_tokenizer()

     __A						      =			WhisperProcessor(tokenizer=_lowerCamelCase,	feature_extractor=_lowerCamelCase  )

     __A						      =			floats_list((3, 10_00)  )

     __A						      =			feature_extractor(_lowerCamelCase,	return_tensors='''np'''  )
     __A						      =			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     _SCREAMING_SNAKE_CASE    (     self			:					Union[str, Any]  ):

     '''simple docstring'''
     __A						      =			self.get_feature_extractor()
     __A						      =			self.get_tokenizer()

     __A						      =			WhisperProcessor(tokenizer=_lowerCamelCase,	feature_extractor=_lowerCamelCase  )

     __A						      =			'''This is a test string'''

     __A						      =			processor(text=_lowerCamelCase  )

     __A						      =			tokenizer(_lowerCamelCase  )

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


   def     _SCREAMING_SNAKE_CASE    (     self			:					Union[str, Any]  ):

     '''simple docstring'''
     __A						      =			self.get_feature_extractor()
     __A						      =			self.get_tokenizer()

     __A						      =			WhisperProcessor(tokenizer=_lowerCamelCase,	feature_extractor=_lowerCamelCase  )

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

     __A						      =			processor.batch_decode(_lowerCamelCase  )
     __A						      =			tokenizer.batch_decode(_lowerCamelCase  )

     self.assertListEqual(_lowerCamelCase,	_lowerCamelCase  )


   def     _SCREAMING_SNAKE_CASE    (     self			:					str  ):

     '''simple docstring'''
     __A						      =			self.get_feature_extractor()
     __A						      =			self.get_tokenizer()

     __A						      =			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     _SCREAMING_SNAKE_CASE    (     self			:					List[str]  ):

     '''simple docstring'''
     __A						      =			self.get_feature_extractor()
     __A						      =			self.get_tokenizer()

     __A						      =			WhisperProcessor(tokenizer=_lowerCamelCase,	feature_extractor=_lowerCamelCase  )
     __A						      =			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)  )

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


   def     _SCREAMING_SNAKE_CASE    (     self			:					int  ):

     '''simple docstring'''
     __A						      =			WhisperProcessor(tokenizer=self.get_tokenizer(),	feature_extractor=self.get_feature_extractor()  )
     __A						      =			processor.get_prompt_ids('''Mr. Quilter'''  )
     __A						      =			processor.tokenizer.decode(_lowerCamelCase  )

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


   def     _SCREAMING_SNAKE_CASE    (     self			:					str  ):

     '''simple docstring'''
     __A						      =			WhisperProcessor(tokenizer=self.get_tokenizer(),	feature_extractor=self.get_feature_extractor()  )
     __A						      =			processor.get_prompt_ids(''''''  )
     __A						      =			processor.tokenizer.decode(_lowerCamelCase  )

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






   def     _SCREAMING_SNAKE_CASE    (     self			:					Union[str, Any]  ):

     '''simple docstring'''
     __A						      =			WhisperProcessor(tokenizer=self.get_tokenizer(),	feature_extractor=self.get_feature_extractor()  )

     def _test_prompt_error_raised_helper(_lowerCamelCase			:					Any,	_lowerCamelCase			:					int  ):
       with pytest.raises(_lowerCamelCase  ) as excinfo:
         processor.get_prompt_ids(_lowerCamelCase  )
       __A						      =			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|>'''  )












"""simple docstring"""


def  lowerCAmelCase    (   __UpperCamelCase			):




  """simple docstring"""







  if not isinstance(__UpperCamelCase		,	__UpperCamelCase			):
    raise ValueError('''Input series is not valid, valid series - [2, 4, 8]'''			)
  if len(__UpperCamelCase			) == 0:
    raise ValueError('''Input list must be a non empty list'''			)
  if len(__UpperCamelCase			) == 1:
    return True
  try:
    __A						      =			series[1] / series[0]
    for index in range(len(__UpperCamelCase			) - 1			):
      if series[index + 1] / series[index] != common_ratio:
        return False
  except ZeroDivisionError:
    return False
  return True



def  lowerCAmelCase    (   __UpperCamelCase			):




  """simple docstring"""







  if not isinstance(__UpperCamelCase		,	__UpperCamelCase			):
    raise ValueError('''Input series is not valid, valid series - [2, 4, 8]'''			)
  if len(__UpperCamelCase			) == 0:
    raise ValueError('''Input list must be a non empty list'''			)
  __A						      =			1
  for value in series:
    answer *= value
  return pow(__UpperCamelCase		,	1 / len(__UpperCamelCase			)			)


if __name__ == "__main__":
       import doctest

       doctest.testmod()




