


import json
import os
import shutil
import tempfile
import unittest

from transformers import is_speech_available, is_torch_available
from transformers.models.speechta import SpeechTaTokenizer
from transformers.testing_utils import get_tests_dir, require_torch
from transformers.utils import FEATURE_EXTRACTOR_NAME


if is_speech_available() and is_torch_available():
		from transformers import SpeechTaFeatureExtractor, SpeechTaProcessor

		from .test_feature_extraction_speechta import floats_list


lowerCamelCase		=						get_tests_dir('''fixtures/test_sentencepiece_bpe_char.model''')





@require_torch
class    _a				(							unittest.TestCase):




	def 				UpperCAmelCase__(   self		:						List[Any]						)->			List[Any]:
			lowerCAmelCase__  :     Dict       	=   tempfile.mkdtemp()

			lowerCAmelCase__  :     List[str]       	=   SpeechTaTokenizer(_SCREAMING_SNAKE_CASE						)
			tokenizer.save_pretrained(self.tmpdirname						)

			lowerCAmelCase__  :     str       	=   {
			    '''feature_size''': 1,
			    '''padding_value''': 0.0,
			    '''sampling_rate''': 1_6000,
			    '''do_normalize''': False,
			    '''num_mel_bins''': 80,
			    '''hop_length''': 16,
			    '''win_length''': 64,
			    '''win_function''': '''hann_window''',
			    '''fmin''': 80,
			    '''fmax''': 7600,
			    '''mel_floor''': 1E-10,
			    '''reduction_factor''': 2,
			    '''return_attention_mask''': True,
			}

			lowerCAmelCase__  :     Union[str, Any]       	=   os.path.join(self.tmpdirname						,   _SCREAMING_SNAKE_CASE						)
			with open(self.feature_extraction_file						,   '''w'''						,   encoding='''utf-8'''						) as fp:
					fp.write(json.dumps(_SCREAMING_SNAKE_CASE						) + '''\n'''						)




	def 				UpperCAmelCase__(   self		:						int						,   **_SCREAMING_SNAKE_CASE		:						List[Any]						)->			Tuple:
			return SpeechTaTokenizer.from_pretrained(self.tmpdirname						,   **_SCREAMING_SNAKE_CASE						)




	def 				UpperCAmelCase__(   self		:						Any						,   **_SCREAMING_SNAKE_CASE		:						Union[str, Any]						)->			List[Any]:
			return SpeechTaFeatureExtractor.from_pretrained(self.tmpdirname						,   **_SCREAMING_SNAKE_CASE						)




	def 				UpperCAmelCase__(   self		:						List[Any]						)->			Optional[int]:
			shutil.rmtree(self.tmpdirname						)




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

			lowerCAmelCase__  :     Optional[Any]       	=   SpeechTaProcessor(tokenizer=_SCREAMING_SNAKE_CASE						,   feature_extractor=_SCREAMING_SNAKE_CASE						)

			processor.save_pretrained(self.tmpdirname						)
			lowerCAmelCase__  :     Tuple       	=   SpeechTaProcessor.from_pretrained(self.tmpdirname						)

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

			self.assertEqual(processor.feature_extractor.to_json_string()						,   feature_extractor.to_json_string()						)
			self.assertIsInstance(processor.feature_extractor						,   _SCREAMING_SNAKE_CASE						)




	def 				UpperCAmelCase__(   self		:						Tuple						)->			List[str]:
			lowerCAmelCase__  :     Union[str, Any]       	=   SpeechTaProcessor(tokenizer=self.get_tokenizer()						,   feature_extractor=self.get_feature_extractor()						)
			processor.save_pretrained(self.tmpdirname						)

			lowerCAmelCase__  :     Tuple       	=   self.get_tokenizer(bos_token='''(BOS)'''						,   eos_token='''(EOS)'''						)
			lowerCAmelCase__  :     Any       	=   self.get_feature_extractor(do_normalize=_SCREAMING_SNAKE_CASE						,   padding_value=1.0						)

			lowerCAmelCase__  :     Union[str, Any]       	=   SpeechTaProcessor.from_pretrained(
			    self.tmpdirname						,   bos_token='''(BOS)'''						,   eos_token='''(EOS)'''						,   do_normalize=_SCREAMING_SNAKE_CASE						,   padding_value=1.0						)

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

			self.assertEqual(processor.feature_extractor.to_json_string()						,   feature_extractor_add_kwargs.to_json_string()						)
			self.assertIsInstance(processor.feature_extractor						,   _SCREAMING_SNAKE_CASE						)




	def 				UpperCAmelCase__(   self		:						Tuple						)->			List[str]:
			lowerCAmelCase__  :     Optional[Any]       	=   self.get_feature_extractor()
			lowerCAmelCase__  :     str       	=   self.get_tokenizer()

			lowerCAmelCase__  :     Optional[int]       	=   SpeechTaProcessor(tokenizer=_SCREAMING_SNAKE_CASE						,   feature_extractor=_SCREAMING_SNAKE_CASE						)

			lowerCAmelCase__  :     Tuple       	=   floats_list((3, 1000)						)

			lowerCAmelCase__  :     Optional[Any]       	=   feature_extractor(audio=_SCREAMING_SNAKE_CASE						,   return_tensors='''np'''						)
			lowerCAmelCase__  :     Union[str, Any]       	=   processor(audio=_SCREAMING_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		:						Tuple						)->			Any:
			lowerCAmelCase__  :     str       	=   self.get_feature_extractor()
			lowerCAmelCase__  :     List[Any]       	=   self.get_tokenizer()

			lowerCAmelCase__  :     int       	=   SpeechTaProcessor(tokenizer=_SCREAMING_SNAKE_CASE						,   feature_extractor=_SCREAMING_SNAKE_CASE						)

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

			lowerCAmelCase__  :     int       	=   feature_extractor(audio_target=_SCREAMING_SNAKE_CASE						,   return_tensors='''np'''						)
			lowerCAmelCase__  :     List[str]       	=   processor(audio_target=_SCREAMING_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		:						Any						)->			str:
			lowerCAmelCase__  :     int       	=   self.get_feature_extractor()
			lowerCAmelCase__  :     Dict       	=   self.get_tokenizer()

			lowerCAmelCase__  :     List[str]       	=   SpeechTaProcessor(tokenizer=_SCREAMING_SNAKE_CASE						,   feature_extractor=_SCREAMING_SNAKE_CASE						)

			lowerCAmelCase__  :     Optional[Any]       	=   '''This is a test string'''

			lowerCAmelCase__  :     str       	=   processor(text=_SCREAMING_SNAKE_CASE						)
			lowerCAmelCase__  :     str       	=   tokenizer(_SCREAMING_SNAKE_CASE						)

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




	def 				UpperCAmelCase__(   self		:						Any						)->			Tuple:
			lowerCAmelCase__  :     int       	=   self.get_feature_extractor()
			lowerCAmelCase__  :     Union[str, Any]       	=   self.get_tokenizer()

			lowerCAmelCase__  :     List[str]       	=   SpeechTaProcessor(tokenizer=_SCREAMING_SNAKE_CASE						,   feature_extractor=_SCREAMING_SNAKE_CASE						)

			lowerCAmelCase__  :     str       	=   '''This is a test string'''

			lowerCAmelCase__  :     str       	=   processor(text_target=_SCREAMING_SNAKE_CASE						)
			lowerCAmelCase__  :     str       	=   tokenizer(_SCREAMING_SNAKE_CASE						)

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




	def 				UpperCAmelCase__(   self		:						Any						)->			List[str]:
			lowerCAmelCase__  :     List[Any]       	=   self.get_feature_extractor()
			lowerCAmelCase__  :     Union[str, Any]       	=   self.get_tokenizer()

			lowerCAmelCase__  :     Dict       	=   SpeechTaProcessor(tokenizer=_SCREAMING_SNAKE_CASE						,   feature_extractor=_SCREAMING_SNAKE_CASE						)

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

			lowerCAmelCase__  :     Tuple       	=   processor.batch_decode(_SCREAMING_SNAKE_CASE						)
			lowerCAmelCase__  :     Tuple       	=   tokenizer.batch_decode(_SCREAMING_SNAKE_CASE						)

			self.assertListEqual(_SCREAMING_SNAKE_CASE						,   _SCREAMING_SNAKE_CASE						)




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

			lowerCAmelCase__  :     Optional[int]       	=   SpeechTaProcessor(tokenizer=_SCREAMING_SNAKE_CASE						,   feature_extractor=_SCREAMING_SNAKE_CASE						)

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








from __future__ import annotations

from string import ascii_letters






def 	lowerCamelCase_       (			_a      ,  _a      ,  _a = None			):




		"""simple docstring"""






		lowerCAmelCase__  :     List[Any]       	=   alphabet or ascii_letters

		# The final result string
		lowerCAmelCase__  :     Optional[Any]       	=   ''''''

		for character in input_string:
				if character not in alpha:
						# Append without encryption if character is not in the alphabet
						result += character
				else:
						# Get the index of the new key and make sure it isn't too large
						lowerCAmelCase__  :     int       	=   (alpha.index(_a			) + key) % len(_a			)

						# Append the encoded character to the alphabet
						result += alpha[new_key]

		return result






def 	lowerCamelCase_       (			_a      ,  _a      ,  _a = None			):




		"""simple docstring"""






		key *= -1

		return encrypt(_a      ,  _a      ,  _a			)






def 	lowerCamelCase_       (			_a      ,  _a = None			):




		"""simple docstring"""






		lowerCAmelCase__  :     Optional[Any]       	=   alphabet or ascii_letters

		# To store data on all the combinations
		lowerCAmelCase__  :     Optional[int]       	=   {}

		# Cycle through each combination
		for key in range(1      ,  len(_a			) + 1			):
				# Decrypt the message and store the result in the data
				lowerCAmelCase__  :     Optional[int]       	=   decrypt(_a      ,  _a      ,  _a			)

		return brute_force_data


if __name__ == "__main__":
		while True:
				print(f'''\n{"-" * 10}\n Menu\n{"-" * 10}''')
				print(*['''1.Encrypt''', '''2.Decrypt''', '''3.BruteForce''', '''4.Quit'''], sep='''\n''')

				# get user input
				lowerCamelCase		=						input('''\nWhat would you like to do?: ''').strip() or '''4'''

				# run functions based on what the user chose
				if choice not in ("1", "2", "3", "4"):
						print('''Invalid choice, please enter a valid choice''')
				elif choice == "1":
						lowerCamelCase		=						input('''Please enter the string to be encrypted: ''')
						lowerCamelCase		=						int(input('''Please enter off-set: ''').strip())

						print(encrypt(input_string, key))
				elif choice == "2":
						lowerCamelCase		=						input('''Please enter the string to be decrypted: ''')
						lowerCamelCase		=						int(input('''Please enter off-set: ''').strip())

						print(decrypt(input_string, key))
				elif choice == "3":
						lowerCamelCase		=						input('''Please enter the string to be decrypted: ''')
						lowerCamelCase		=						brute_force(input_string)

						for key, value in brute_force_data.items():
								print(f'''Key: {key} | Message: {value}''')

				elif choice == "4":
						print('''Goodbye.''')
						break




