

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


__UpperCamelCase						:   Dict							   =	5_0_3_5_8
__UpperCamelCase						:   Dict							   =	5_0_3_6_2







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






					"""simple docstring"""



					def  __lowerCAmelCase				( self    :       Dict	):
						lowerCAmelCase__			:		Union[str, Any]						       =			"""openai/whisper-small.en"""
						lowerCAmelCase__			:		List[str]						       =			tempfile.mkdtemp()







					def  __lowerCAmelCase				( self    :       Optional[Any]     ,**lowercase_    :       Optional[Any]	):
						return WhisperTokenizer.from_pretrained(self.checkpoint     ,**_lowerCAmelCase	)







					def  __lowerCAmelCase				( self    :       str     ,**lowercase_    :       List[str]	):
						return WhisperFeatureExtractor.from_pretrained(self.checkpoint     ,**_lowerCAmelCase	)







					def  __lowerCAmelCase				( self    :       str	):
						shutil.rmtree(self.tmpdirname	)







					def  __lowerCAmelCase				( self    :       Union[str, Any]	):
						lowerCAmelCase__			:		Optional[Any]						       =			self.get_tokenizer()
						lowerCAmelCase__			:		Optional[int]						       =			self.get_feature_extractor()

						lowerCAmelCase__			:		Union[str, Any]						       =			WhisperProcessor(tokenizer=_lowerCAmelCase     ,feature_extractor=_lowerCAmelCase	)

						processor.save_pretrained(self.tmpdirname	)
						lowerCAmelCase__			:		str						       =			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  __lowerCAmelCase				( self    :       Dict	):
						lowerCAmelCase__			:		Optional[int]						       =			WhisperProcessor(tokenizer=self.get_tokenizer()     ,feature_extractor=self.get_feature_extractor()	)
						processor.save_pretrained(self.tmpdirname	)

						lowerCAmelCase__			:		Dict						       =			self.get_tokenizer(bos_token='''(BOS)'''     ,eos_token='''(EOS)'''	)
						lowerCAmelCase__			:		Dict						       =			self.get_feature_extractor(do_normalize=_lowerCAmelCase     ,padding_value=1.0	)

						lowerCAmelCase__			:		Tuple						       =			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  __lowerCAmelCase				( self    :       Tuple	):
						lowerCAmelCase__			:		Union[str, Any]						       =			self.get_feature_extractor()
						lowerCAmelCase__			:		List[Any]						       =			self.get_tokenizer()

						lowerCAmelCase__			:		int						       =			WhisperProcessor(tokenizer=_lowerCAmelCase     ,feature_extractor=_lowerCAmelCase	)

						lowerCAmelCase__			:		Optional[int]						       =			floats_list((3, 1_0_0_0)	)

						lowerCAmelCase__			:		Optional[int]						       =			feature_extractor(_lowerCAmelCase     ,return_tensors='''np'''	)
						lowerCAmelCase__			:		List[Any]						       =			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  __lowerCAmelCase				( self    :       List[str]	):
						lowerCAmelCase__			:		Tuple						       =			self.get_feature_extractor()
						lowerCAmelCase__			:		Optional[Any]						       =			self.get_tokenizer()

						lowerCAmelCase__			:		Any						       =			WhisperProcessor(tokenizer=_lowerCAmelCase     ,feature_extractor=_lowerCAmelCase	)

						lowerCAmelCase__			:		List[Any]						       =			"""This is a test string"""

						lowerCAmelCase__			:		Tuple						       =			processor(text=_lowerCAmelCase	)

						lowerCAmelCase__			:		List[Any]						       =			tokenizer(_lowerCAmelCase	)

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







					def  __lowerCAmelCase				( self    :       Optional[Any]	):
						lowerCAmelCase__			:		Union[str, Any]						       =			self.get_feature_extractor()
						lowerCAmelCase__			:		List[str]						       =			self.get_tokenizer()

						lowerCAmelCase__			:		Dict						       =			WhisperProcessor(tokenizer=_lowerCAmelCase     ,feature_extractor=_lowerCAmelCase	)

						lowerCAmelCase__			:		Tuple						       =			[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

						lowerCAmelCase__			:		Dict						       =			processor.batch_decode(_lowerCAmelCase	)
						lowerCAmelCase__			:		int						       =			tokenizer.batch_decode(_lowerCAmelCase	)

						self.assertListEqual(_lowerCAmelCase     ,_lowerCAmelCase	)







					def  __lowerCAmelCase				( self    :       List[str]	):
						lowerCAmelCase__			:		Dict						       =			self.get_feature_extractor()
						lowerCAmelCase__			:		List[Any]						       =			self.get_tokenizer()

						lowerCAmelCase__			:		Any						       =			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  __lowerCAmelCase				( self    :       Tuple	):
						lowerCAmelCase__			:		int						       =			self.get_feature_extractor()
						lowerCAmelCase__			:		Union[str, Any]						       =			self.get_tokenizer()

						lowerCAmelCase__			:		Optional[int]						       =			WhisperProcessor(tokenizer=_lowerCAmelCase     ,feature_extractor=_lowerCAmelCase	)
						lowerCAmelCase__			:		List[Any]						       =			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)	)

						lowerCAmelCase__			:		Optional[Any]						       =			[TRANSCRIBE, NOTIMESTAMPS]
						self.assertListEqual([ids[-1] for ids in forced_decoder_ids]     ,_lowerCAmelCase	)







					def  __lowerCAmelCase				( self    :       Dict	):
						lowerCAmelCase__			:		str						       =			WhisperProcessor(tokenizer=self.get_tokenizer()     ,feature_extractor=self.get_feature_extractor()	)
						lowerCAmelCase__			:		List[Any]						       =			processor.get_prompt_ids('''Mr. Quilter'''	)
						lowerCAmelCase__			:		Optional[Any]						       =			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  __lowerCAmelCase				( self    :       Optional[int]	):
						lowerCAmelCase__			:		Tuple						       =			WhisperProcessor(tokenizer=self.get_tokenizer()     ,feature_extractor=self.get_feature_extractor()	)
						lowerCAmelCase__			:		Tuple						       =			processor.get_prompt_ids(''''''	)
						lowerCAmelCase__			:		str						       =			processor.tokenizer.decode(_lowerCAmelCase	)

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







					def  __lowerCAmelCase				( self    :       Any	):
						lowerCAmelCase__			:		Optional[int]						       =			WhisperProcessor(tokenizer=self.get_tokenizer()     ,feature_extractor=self.get_feature_extractor()	)

						def _test_prompt_error_raised_helper(lowercase_    :       Tuple     ,lowercase_    :       List[Any]	):
							with pytest.raises(_lowerCAmelCase	) as excinfo:
								processor.get_prompt_ids(_lowerCAmelCase	)
							lowerCAmelCase__			:		Optional[Any]						       =			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"""





import random
import sys

__UpperCamelCase						:   Tuple							   =	'''ABCDEFGHIJKLMNOPQRSTUVWXYZ'''

def 						__SCREAMING_SNAKE_CASE						(   ):
	lowerCAmelCase__			:		List[str]						       =			input('''Enter message: '''				)
	lowerCAmelCase__			:		Optional[int]						       =			'''LFWOAYUISVKMNXPBDCRJTQEGHZ'''
	lowerCAmelCase__			:		List[Any]						       =			input('''Encrypt/Decrypt [e/d]: '''				)

	check_valid_key(A_				)

	if resp.lower().startswith('''e'''				):
		lowerCAmelCase__			:		Tuple						       =			'''encrypt'''
		lowerCAmelCase__			:		Union[str, Any]						       =			encrypt_message(A_		,  A_				)
	elif resp.lower().startswith('''d'''				):
		lowerCAmelCase__			:		Dict						       =			'''decrypt'''
		lowerCAmelCase__			:		str						       =			decrypt_message(A_		,  A_				)

	print(f'\n{mode.title()}ion: \n{translated}'				)

def 						__SCREAMING_SNAKE_CASE						(   A_				):
	lowerCAmelCase__			:		str						       =			list(A_				)
	lowerCAmelCase__			:		int						       =			list(A_				)
	key_list.sort()
	letters_list.sort()

	if key_list != letters_list:
		sys.exit('''Error in the key or symbol set.'''				)

def 						__SCREAMING_SNAKE_CASE						(   A_		,  A_				):
	return translate_message(A_		,  A_		,  '''encrypt'''				)

def 						__SCREAMING_SNAKE_CASE						(   A_		,  A_				):
	return translate_message(A_		,  A_		,  '''decrypt'''				)

def 						__SCREAMING_SNAKE_CASE						(   A_		,  A_		,  A_				):
	lowerCAmelCase__			:		str						       =			''''''
	lowerCAmelCase__			:		List[str]						       =			LETTERS
	lowerCAmelCase__			:		Optional[int]						       =			key

	if mode == "decrypt":
		lowerCAmelCase__       ,lowerCAmelCase__			:		int						       =			chars_b, chars_a

	for symbol in message:
		if symbol.upper() in chars_a:
			lowerCAmelCase__			:		int						       =			chars_a.find(symbol.upper()				)
			if symbol.isupper():
				translated += chars_b[sym_index].upper()
			else:
				translated += chars_b[sym_index].lower()
		else:
			translated += symbol

	return translated

def 						__SCREAMING_SNAKE_CASE						(   ):
	lowerCAmelCase__			:		Optional[int]						       =			list(A_				)
	random.shuffle(A_				)
	return "".join(A_				)


if __name__ == "__main__":
							main()




