






"""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_		:			List[str]				   =					5_0_3_5_8
lowerCAmelCase_		:			Optional[Any]				   =					5_0_3_6_2





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



  def 						UpperCamelCase_ (					self )	->	List[Any]:



         """simple docstring"""





         UpperCAmelCase					=	"""openai/whisper-small.en"""
         UpperCAmelCase					=	tempfile.mkdtemp()



  def 						UpperCamelCase_ (					self       ,						**snake_case__ )	->	List[Any]:



         """simple docstring"""





         return WhisperTokenizer.from_pretrained(self.checkpoint       ,						**snake_case__ )



  def 						UpperCamelCase_ (					self       ,						**snake_case__ )	->	Optional[int]:



         """simple docstring"""





         return WhisperFeatureExtractor.from_pretrained(self.checkpoint       ,						**snake_case__ )



  def 						UpperCamelCase_ (					self )	->	Optional[int]:



         """simple docstring"""





         shutil.rmtree(self.tmpdirname )



  def 						UpperCamelCase_ (					self )	->	str:



         """simple docstring"""





         UpperCAmelCase					=	self.get_tokenizer()
         UpperCAmelCase					=	self.get_feature_extractor()

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

         processor.save_pretrained(self.tmpdirname )
         UpperCAmelCase					=	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 						UpperCamelCase_ (					self )	->	Dict:



         """simple docstring"""





         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=snake_case__       ,						padding_value=1.0 )

         UpperCAmelCase					=	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 						UpperCamelCase_ (					self )	->	Union[str, Any]:



         """simple docstring"""





         UpperCAmelCase					=	self.get_feature_extractor()
         UpperCAmelCase					=	self.get_tokenizer()

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

         UpperCAmelCase					=	floats_list((3, 10_00) )

         UpperCAmelCase					=	feature_extractor(snake_case__       ,						return_tensors="""np""" )
         UpperCAmelCase					=	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 						UpperCamelCase_ (					self )	->	Optional[Any]:



         """simple docstring"""





         UpperCAmelCase					=	self.get_feature_extractor()
         UpperCAmelCase					=	self.get_tokenizer()

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

         UpperCAmelCase					=	"""This is a test string"""

         UpperCAmelCase					=	processor(text=snake_case__ )

         UpperCAmelCase					=	tokenizer(snake_case__ )

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



  def 						UpperCamelCase_ (					self )	->	Dict:



         """simple docstring"""





         UpperCAmelCase					=	self.get_feature_extractor()
         UpperCAmelCase					=	self.get_tokenizer()

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

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

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

         self.assertListEqual(snake_case__       ,						snake_case__ )



  def 						UpperCamelCase_ (					self )	->	int:



         """simple docstring"""





         UpperCAmelCase					=	self.get_feature_extractor()
         UpperCAmelCase					=	self.get_tokenizer()

         UpperCAmelCase					=	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 						UpperCamelCase_ (					self )	->	List[Any]:



         """simple docstring"""





         UpperCAmelCase					=	self.get_feature_extractor()
         UpperCAmelCase					=	self.get_tokenizer()

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



  def 						UpperCamelCase_ (					self )	->	Any:



         """simple docstring"""





         UpperCAmelCase					=	WhisperProcessor(tokenizer=self.get_tokenizer()       ,						feature_extractor=self.get_feature_extractor() )
         UpperCAmelCase					=	processor.get_prompt_ids("""Mr. Quilter""" )
         UpperCAmelCase					=	processor.tokenizer.decode(snake_case__ )

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



  def 						UpperCamelCase_ (					self )	->	Tuple:



         """simple docstring"""





         UpperCAmelCase					=	WhisperProcessor(tokenizer=self.get_tokenizer()       ,						feature_extractor=self.get_feature_extractor() )
         UpperCAmelCase					=	processor.get_prompt_ids("""""" )
         UpperCAmelCase					=	processor.tokenizer.decode(snake_case__ )

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



  def 						UpperCamelCase_ (					self )	->	Optional[int]:



         """simple docstring"""





         UpperCAmelCase					=	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					=	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 collections import OrderedDict
from dataclasses import fields
from typing import Any, Tuple

import numpy as np

from .import_utils import is_torch_available






def 					_lowerCAmelCase				(					lowerCAmelCase						):







       '''simple docstring'''
       if is_torch_available():
              import torch

              if isinstance(lowerCAmelCase		,					torch.Tensor						):
                     return True

       return isinstance(lowerCAmelCase		,					np.ndarray						)





class      UpperCamelCase_       (				a_     ):



  def 						UpperCamelCase_ (					self )	->	Dict:



         """simple docstring"""





         UpperCAmelCase					=	fields(self )

         # Safety and consistency checks
         if not len(snake_case__ ):
                raise ValueError(f'''{self.__class__.__name__} has no fields.''' )

         UpperCAmelCase					=	getattr(self       ,						class_fields[0].name )
         UpperCAmelCase					=	all(getattr(self       ,						field.name ) is None for field in class_fields[1:] )

         if other_fields_are_none and isinstance(snake_case__       ,						snake_case__ ):
                for key, value in first_field.items():
                       UpperCAmelCase					=	value
         else:
                for field in class_fields:
                       UpperCAmelCase					=	getattr(self       ,						field.name )
                       if v is not None:
                              UpperCAmelCase					=	v



  def __delitem__(					self       ,						*snake_case__       ,						**snake_case__ )	->	Optional[int]:



         """simple docstring"""





         raise Exception(f'''You cannot use ``__delitem__`` on a {self.__class__.__name__} instance.''' )



  def 						UpperCamelCase_ (					self       ,						*snake_case__       ,						**snake_case__ )	->	List[str]:



         """simple docstring"""





         raise Exception(f'''You cannot use ``setdefault`` on a {self.__class__.__name__} instance.''' )



  def 						UpperCamelCase_ (					self       ,						*snake_case__       ,						**snake_case__ )	->	Optional[int]:



         """simple docstring"""





         raise Exception(f'''You cannot use ``pop`` on a {self.__class__.__name__} instance.''' )



  def 						UpperCamelCase_ (					self       ,						*snake_case__       ,						**snake_case__ )	->	Optional[int]:



         """simple docstring"""





         raise Exception(f'''You cannot use ``update`` on a {self.__class__.__name__} instance.''' )



  def __getitem__(					self       ,						snake_case__ )	->	List[Any]:



         """simple docstring"""





         if isinstance(snake_case__       ,						snake_case__ ):
                UpperCAmelCase					=	dict(self.items() )
                return inner_dict[k]
         else:
                return self.to_tuple()[k]



  def __setattr__(					self       ,						snake_case__       ,						snake_case__ )	->	Union[str, Any]:



         """simple docstring"""





         if name in self.keys() and value is not None:
                # Don't call self.__setitem__ to avoid recursion errors
                super().__setitem__(snake_case__       ,						snake_case__ )
         super().__setattr__(snake_case__       ,						snake_case__ )



  def __setitem__(					self       ,						snake_case__       ,						snake_case__ )	->	Tuple:



         """simple docstring"""





         super().__setitem__(snake_case__       ,						snake_case__ )
         # Don't call self.__setattr__ to avoid recursion errors
         super().__setattr__(snake_case__       ,						snake_case__ )



  def 						UpperCamelCase_ (					self )	->	Tuple[Any]:



         """simple docstring"""





         return tuple(self[k] for k in self.keys() )
