




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


__A       :						List[str]												=			50358
__A       :						Optional[Any]												=			50362
@require_torch
@require_torchaudio
@require_sentencepiece
class 							lowerCamelCase						(       unittest.TestCase ):







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







   def 					a_							(							self			,					**SCREAMING_SNAKE_CASE_       ):
       return WhisperTokenizer.from_pretrained(self.checkpoint			,					**snake_case__       )







   def 					a_							(							self			,					**SCREAMING_SNAKE_CASE_       ):
       return WhisperFeatureExtractor.from_pretrained(self.checkpoint			,					**snake_case__       )







   def 					a_							(							self       ):
       shutil.rmtree(self.tmpdirname       )







   def 					a_							(							self       ):
       UpperCamelCase :      Optional[int]      				=							self.get_tokenizer()
       UpperCamelCase :      List[Any]      				=							self.get_feature_extractor()

       UpperCamelCase :      Dict      				=							WhisperProcessor(tokenizer=snake_case__			,					feature_extractor=snake_case__       )

       processor.save_pretrained(self.tmpdirname       )
       UpperCamelCase :      Any      				=							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 					a_							(							self       ):
       UpperCamelCase :      Union[str, Any]      				=							WhisperProcessor(tokenizer=self.get_tokenizer()			,					feature_extractor=self.get_feature_extractor()       )
       processor.save_pretrained(self.tmpdirname       )

       UpperCamelCase :      str      				=							self.get_tokenizer(bos_token="""(BOS)"""			,					eos_token="""(EOS)"""       )
       UpperCamelCase :      Optional[int]      				=							self.get_feature_extractor(do_normalize=snake_case__			,					padding_value=1.0       )

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

       UpperCamelCase :      List[Any]      				=							WhisperProcessor(tokenizer=snake_case__			,					feature_extractor=snake_case__       )

       UpperCamelCase :      Dict      				=							floats_list((3, 1000)       )

       UpperCamelCase :      str      				=							feature_extractor(snake_case__			,					return_tensors="""np"""       )
       UpperCamelCase :      int      				=							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 					a_							(							self       ):
       UpperCamelCase :      Optional[Any]      				=							self.get_feature_extractor()
       UpperCamelCase :      List[Any]      				=							self.get_tokenizer()

       UpperCamelCase :      Union[str, Any]      				=							WhisperProcessor(tokenizer=snake_case__			,					feature_extractor=snake_case__       )

       UpperCamelCase :      Union[str, Any]      				=							"""This is a test string"""

       UpperCamelCase :      Optional[Any]      				=							processor(text=snake_case__       )

       UpperCamelCase :      List[str]      				=							tokenizer(snake_case__       )

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







   def 					a_							(							self       ):
       UpperCamelCase :      Dict      				=							self.get_feature_extractor()
       UpperCamelCase :      Union[str, Any]      				=							self.get_tokenizer()

       UpperCamelCase :      str      				=							WhisperProcessor(tokenizer=snake_case__			,					feature_extractor=snake_case__       )

       UpperCamelCase :      int      				=							[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

       UpperCamelCase :      Union[str, Any]      				=							processor.batch_decode(snake_case__       )
       UpperCamelCase :      Dict      				=							tokenizer.batch_decode(snake_case__       )

       self.assertListEqual(snake_case__			,					snake_case__       )







   def 					a_							(							self       ):
       UpperCamelCase :      Optional[int]      				=							self.get_feature_extractor()
       UpperCamelCase :      Optional[Any]      				=							self.get_tokenizer()

       UpperCamelCase :      Dict      				=							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 					a_							(							self       ):
       UpperCamelCase :      str      				=							self.get_feature_extractor()
       UpperCamelCase :      List[str]      				=							self.get_tokenizer()

       UpperCamelCase :      Union[str, Any]      				=							WhisperProcessor(tokenizer=snake_case__			,					feature_extractor=snake_case__       )
       UpperCamelCase :      Union[str, Any]      				=							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 					a_							(							self       ):
       UpperCamelCase :      List[Any]      				=							WhisperProcessor(tokenizer=self.get_tokenizer()			,					feature_extractor=self.get_feature_extractor()       )
       UpperCamelCase :      Dict      				=							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 					a_							(							self       ):
       UpperCamelCase :      Any      				=							WhisperProcessor(tokenizer=self.get_tokenizer()			,					feature_extractor=self.get_feature_extractor()       )
       UpperCamelCase :      List[str]      				=							processor.get_prompt_ids(""""""       )
       UpperCamelCase :      int      				=							processor.tokenizer.decode(snake_case__       )

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







   def 					a_							(							self       ):
       UpperCamelCase :      str      				=							WhisperProcessor(tokenizer=self.get_tokenizer()			,					feature_extractor=self.get_feature_extractor()       )

       def _test_prompt_error_raised_helper(SCREAMING_SNAKE_CASE_			,					SCREAMING_SNAKE_CASE_       ):
           with pytest.raises(snake_case__       ) as excinfo:
               processor.get_prompt_ids(snake_case__       )
           UpperCamelCase :      List[str]      				=							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"""







import unittest

import numpy as np

from transformers import ElectraConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
 from transformers.models.electra.modeling_flax_electra import (
     FlaxElectraForCausalLM,
     FlaxElectraForMaskedLM,
     FlaxElectraForMultipleChoice,
     FlaxElectraForPreTraining,
     FlaxElectraForQuestionAnswering,
     FlaxElectraForSequenceClassification,
     FlaxElectraForTokenClassification,
     FlaxElectraModel,
 )





class      UpperCamelCase_       (				unittest.TestCase     ):



  def __init__(					self       ,						snake_case__       ,						snake_case__=13       ,						snake_case__=7       ,						snake_case__=True       ,						snake_case__=True       ,						snake_case__=True       ,						snake_case__=True       ,						snake_case__=99       ,						snake_case__=24       ,						snake_case__=32       ,						snake_case__=5       ,						snake_case__=4       ,						snake_case__=37       ,						snake_case__="gelu"       ,						snake_case__=0.1       ,						snake_case__=0.1       ,						snake_case__=5_12       ,						snake_case__=16       ,						snake_case__=2       ,						snake_case__=0.02       ,						snake_case__=4       ,						)	->	int:



         """simple docstring"""





         UpperCAmelCase					=	parent
         UpperCAmelCase					=	batch_size
         UpperCAmelCase					=	seq_length
         UpperCAmelCase					=	is_training
         UpperCAmelCase					=	use_attention_mask
         UpperCAmelCase					=	use_token_type_ids
         UpperCAmelCase					=	use_labels
         UpperCAmelCase					=	vocab_size
         UpperCAmelCase					=	hidden_size
         UpperCAmelCase					=	embedding_size
         UpperCAmelCase					=	num_hidden_layers
         UpperCAmelCase					=	num_attention_heads
         UpperCAmelCase					=	intermediate_size
         UpperCAmelCase					=	hidden_act
         UpperCAmelCase					=	hidden_dropout_prob
         UpperCAmelCase					=	attention_probs_dropout_prob
         UpperCAmelCase					=	max_position_embeddings
         UpperCAmelCase					=	type_vocab_size
         UpperCAmelCase					=	type_sequence_label_size
         UpperCAmelCase					=	initializer_range
         UpperCAmelCase					=	num_choices



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



         """simple docstring"""





         UpperCAmelCase					=	ids_tensor([self.batch_size, self.seq_length]       ,						self.vocab_size )

         UpperCAmelCase					=	None
         if self.use_attention_mask:
                UpperCAmelCase					=	random_attention_mask([self.batch_size, self.seq_length] )

         UpperCAmelCase					=	None
         if self.use_token_type_ids:
                UpperCAmelCase					=	ids_tensor([self.batch_size, self.seq_length]       ,						self.type_vocab_size )

         UpperCAmelCase					=	ElectraConfig(
             vocab_size=self.vocab_size       ,						hidden_size=self.hidden_size       ,						embedding_size=self.embedding_size       ,						num_hidden_layers=self.num_hidden_layers       ,						num_attention_heads=self.num_attention_heads       ,						intermediate_size=self.intermediate_size       ,						hidden_act=self.hidden_act       ,						hidden_dropout_prob=self.hidden_dropout_prob       ,						attention_probs_dropout_prob=self.attention_probs_dropout_prob       ,						max_position_embeddings=self.max_position_embeddings       ,						type_vocab_size=self.type_vocab_size       ,						initializer_range=self.initializer_range       ,						)

         return config, input_ids, token_type_ids, attention_mask



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



         """simple docstring"""





         UpperCAmelCase					=	self.prepare_config_and_inputs()
         UpperCAmelCase			,				UpperCAmelCase			,				UpperCAmelCase			,				UpperCAmelCase					=	config_and_inputs
         UpperCAmelCase					=	{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask}
         return config, inputs_dict





@require_flax
class      UpperCamelCase_       (				a_      ,    unittest.TestCase     ):
  _A				:    str =		True

  _A				:    List[Any] =		(
      (
          FlaxElectraModel,
          FlaxElectraForCausalLM,
          FlaxElectraForMaskedLM,
          FlaxElectraForPreTraining,
          FlaxElectraForTokenClassification,
          FlaxElectraForQuestionAnswering,
          FlaxElectraForMultipleChoice,
          FlaxElectraForSequenceClassification,
      )
      if is_flax_available()
      else ()
  )



  def 						UpperCamelCase_ (					self )	->	str:



         """simple docstring"""





         UpperCAmelCase					=	FlaxElectraModelTester(self )



  @slow
  def 						UpperCamelCase_ (					self )	->	Tuple:



         """simple docstring"""





         for model_class_name in self.all_model_classes:
                if model_class_name == FlaxElectraForMaskedLM:
                       UpperCAmelCase					=	model_class_name.from_pretrained("""google/electra-small-generator""" )
                else:
                       UpperCAmelCase					=	model_class_name.from_pretrained("""google/electra-small-discriminator""" )
                UpperCAmelCase					=	model(np.ones((1, 1) ) )
                self.assertIsNotNone(snake_case__ )
