





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       :					Tuple     					=     50_358
__A       :					Tuple     					=     50_362

@require_torch
@require_torchaudio
@require_sentencepiece
class 			A_ (unittest.TestCase    ):
	def   _lowercase       (					self	):






				'''simple docstring'''







				UpperCAmelCase     	=       '''openai/whisper-small.en'''
				UpperCAmelCase     	=       tempfile.mkdtemp()
	def   _lowercase       (					self							,      **_A	):






				'''simple docstring'''







				return WhisperTokenizer.from_pretrained(self.checkpoint							,      **_A	)
	def   _lowercase       (					self							,      **_A	):






				'''simple docstring'''







				return WhisperFeatureExtractor.from_pretrained(self.checkpoint							,      **_A	)
	def   _lowercase       (					self	):






				'''simple docstring'''







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






				'''simple docstring'''







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

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

				processor.save_pretrained(self.tmpdirname	)
				UpperCAmelCase     	=       WhisperProcessor.from_pretrained(self.tmpdirname	)

				self.assertEqual(processor.tokenizer.get_vocab()							,      tokenizer.get_vocab()	)
				self.assertIsInstance(processor.tokenizer							,      _A	)

				self.assertEqual(processor.feature_extractor.to_json_string()							,      feature_extractor.to_json_string()	)
				self.assertIsInstance(processor.feature_extractor							,      _A	)
	def   _lowercase       (					self	):






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

				UpperCAmelCase     	=       WhisperProcessor.from_pretrained(
				    self.tmpdirname							,      bos_token='''(BOS)'''							,      eos_token='''(EOS)'''							,      do_normalize=_A							,      padding_value=1.0	)

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

				self.assertEqual(processor.feature_extractor.to_json_string()							,      feature_extractor_add_kwargs.to_json_string()	)
				self.assertIsInstance(processor.feature_extractor							,      _A	)
	def   _lowercase       (					self	):






				'''simple docstring'''







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

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

				UpperCAmelCase     	=       floats_list((3, 1_0_0_0)	)

				UpperCAmelCase     	=       feature_extractor(_A							,      return_tensors='''np'''	)
				UpperCAmelCase     	=       processor(_A							,      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   _lowercase       (					self	):






				'''simple docstring'''







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

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

				UpperCAmelCase     	=       '''This is a test string'''

				UpperCAmelCase     	=       processor(text=_A	)

				UpperCAmelCase     	=       tokenizer(_A	)

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






				'''simple docstring'''







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

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

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

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

				self.assertListEqual(_A							,      _A	)
	def   _lowercase       (					self	):






				'''simple docstring'''







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

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

				self.assertListEqual(
				    processor.model_input_names							,      feature_extractor.model_input_names							,      msg='''`processor` and `feature_extractor` model input names do not match'''							,      )
	def   _lowercase       (					self	):






				'''simple docstring'''







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

				UpperCAmelCase     	=       WhisperProcessor(tokenizer=_A							,      feature_extractor=_A	)
				UpperCAmelCase     	=       processor.get_decoder_prompt_ids(task='''transcribe'''							,      no_timestamps=_A	)

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

				UpperCAmelCase     	=       [TRANSCRIBE, NOTIMESTAMPS]
				self.assertListEqual([ids[-1] for ids in forced_decoder_ids]							,      _A	)
	def   _lowercase       (					self	):






				'''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(_A	)

				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(_A							,      '''<|startofprev|> Mr. Quilter'''	)
	def   _lowercase       (					self	):






				'''simple docstring'''







				UpperCAmelCase     	=       WhisperProcessor(tokenizer=self.get_tokenizer()							,      feature_extractor=self.get_feature_extractor()	)
				UpperCAmelCase     	=       processor.get_prompt_ids(''''''	)
				UpperCAmelCase     	=       processor.tokenizer.decode(_A	)

				self.assertListEqual(prompt_ids.tolist()							,      [5_0_3_6_0, 2_2_0]	)
				self.assertEqual(_A							,      '''<|startofprev|> '''	)







	def   _lowercase       (					self	):






				'''simple docstring'''







				UpperCAmelCase     	=       WhisperProcessor(tokenizer=self.get_tokenizer()							,      feature_extractor=self.get_feature_extractor()	)

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










from ...configuration_utils import PretrainedConfig
from ...utils import logging


__A       :					Any     					=     logging.get_logger(__name__)

__A       :					Tuple     					=     {
    "google/pegasus-x-base": "https://huggingface.co/google/pegasus-x-base/resolve/main/config.json",
    "google/pegasus-x-large": "https://huggingface.co/google/pegasus-x-large/resolve/main/config.json",
    # See all PEGASUS-X models at https://huggingface.co/models?filter=pegasus-x
}

class 			A_ (a_    ):
	UpperCAmelCase__      =    '''pegasus_x'''
	UpperCAmelCase__      =    ['''past_key_values''']
	UpperCAmelCase__      =    {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}
	def __init__(					self							,      _A=9_6_1_0_3							,      _A=1_6_3_8_4							,      _A=1_6							,      _A=4_0_9_6							,      _A=1_6							,      _A=1_6							,      _A=4_0_9_6							,      _A=1_6							,      _A=0.0							,      _A=0.0							,      _A=True							,      _A=True							,      _A="gelu"							,      _A=1_0_2_4							,      _A=0.1							,      _A=0.0							,      _A=0.0							,      _A=0.02							,      _A=0							,      _A=True							,      _A=0							,      _A=1							,      _A=1							,      _A=3_2							,      _A=5_1_2							,      _A=True							,      **_A							,      ):






				'''simple docstring'''







				UpperCAmelCase     	=       vocab_size
				UpperCAmelCase     	=       max_position_embeddings
				UpperCAmelCase     	=       d_model
				UpperCAmelCase     	=       encoder_ffn_dim
				UpperCAmelCase     	=       encoder_layers
				UpperCAmelCase     	=       encoder_attention_heads
				UpperCAmelCase     	=       decoder_ffn_dim
				UpperCAmelCase     	=       decoder_layers
				UpperCAmelCase     	=       decoder_attention_heads
				UpperCAmelCase     	=       dropout
				UpperCAmelCase     	=       attention_dropout
				UpperCAmelCase     	=       activation_dropout
				UpperCAmelCase     	=       activation_function
				UpperCAmelCase     	=       init_std
				UpperCAmelCase     	=       encoder_layerdrop
				UpperCAmelCase     	=       decoder_layerdrop
				UpperCAmelCase     	=       use_cache
				UpperCAmelCase     	=       encoder_layers
				UpperCAmelCase     	=       scale_embedding  # scale factor will be sqrt(d_model) if True

				UpperCAmelCase     	=       num_global_tokens
				UpperCAmelCase     	=       block_size
				UpperCAmelCase     	=       stagger_local_blocks

				super().__init__(
				    pad_token_id=_A							,      eos_token_id=_A							,      is_encoder_decoder=_A							,      decoder_start_token_id=_A							,      forced_eos_token_id=_A							,      **_A							,      )
	@property
	def   _lowercase       (					self	):






				'''simple docstring'''







				return self.encoder_attention_heads







	@property
	def   _lowercase       (					self	):






				'''simple docstring'''







				return self.d_model



