



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	:				Dict  					=			50_358
__snake_case	:				Dict  					=			50_362

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





     """simple docstring"""






     def 						A__  (					self :							int ):
          A__				=   """openai/whisper-small.en"""
          A__				=   tempfile.mkdtemp()





     def 						A__  (					self :							Optional[Any]	,      **_lowerCamelCase :							List[Any] ):
          return WhisperTokenizer.from_pretrained(self.checkpoint	,      **_lowerCAmelCase )





     def 						A__  (					self :							Union[str, Any]	,      **_lowerCamelCase :							Union[str, Any] ):
          return WhisperFeatureExtractor.from_pretrained(self.checkpoint	,      **_lowerCAmelCase )





     def 						A__  (					self :							Optional[int] ):
          shutil.rmtree(self.tmpdirname )





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

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

          A__				=   floats_list((3, 1_0_0_0) )

          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 						A__  (					self :							Optional[int] ):
          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 						A__  (					self :							str ):
          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 						A__  (					self :							int ):
          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 						A__  (					self :							Union[str, Any] ):
          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 						A__  (					self :							Dict ):
          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_0_3_6_0, 1_7_7_0, 1_3, 2_2_6_4, 3_4_6, 3_5_3] )
          self.assertEqual(_lowerCAmelCase	,      '''<|startofprev|> Mr. Quilter''' )





     def 						A__  (					self :							Dict ):
          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_0_3_6_0, 2_2_0] )
          self.assertEqual(_lowerCAmelCase	,      '''<|startofprev|> ''' )





     def 						A__  (					self :							Tuple ):
          A__				=   WhisperProcessor(tokenizer=self.get_tokenizer()	,      feature_extractor=self.get_feature_extractor() )

          def _test_prompt_error_raised_helper(_lowerCamelCase :							Any	,      _lowerCamelCase :							Union[str, Any] ):
               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"""







from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_torch_available,
)


__snake_case	:				List[Any]  					=			{
    'configuration_unispeech_sat': ['UNISPEECH_SAT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'UniSpeechSatConfig'],
}

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 __snake_case	:				str  					=			[
     'UNISPEECH_SAT_PRETRAINED_MODEL_ARCHIVE_LIST',
     'UniSpeechSatForAudioFrameClassification',
     'UniSpeechSatForCTC',
     'UniSpeechSatForPreTraining',
     'UniSpeechSatForSequenceClassification',
     'UniSpeechSatForXVector',
     'UniSpeechSatModel',
     'UniSpeechSatPreTrainedModel',
 ]

if TYPE_CHECKING:
 from .configuration_unispeech_sat import UNISPEECH_SAT_PRETRAINED_CONFIG_ARCHIVE_MAP, UniSpeechSatConfig

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_unispeech_sat import (
      UNISPEECH_SAT_PRETRAINED_MODEL_ARCHIVE_LIST,
      UniSpeechSatForAudioFrameClassification,
      UniSpeechSatForCTC,
      UniSpeechSatForPreTraining,
      UniSpeechSatForSequenceClassification,
      UniSpeechSatForXVector,
      UniSpeechSatModel,
      UniSpeechSatPreTrainedModel,
  )

else:
 import sys

 __snake_case	:				Union[str, Any]  					=			_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)



