




"""simple docstring"""






import string

import numpy







def      _snake_case			(				lowercase__							,    lowercase__		):
				return b if a == 0 else greatest_common_divisor(b % a							,    lowercase__		)






class lowerCAmelCase__	:






			'''simple docstring'''



			lowerCamelCase__      =			string.ascii_uppercase + string.digits
			# This cipher takes alphanumerics into account
			# i.e. a total of 36 characters

			# take x and return x % len(key_string)
			lowerCamelCase__      =			numpy.vectorize(lambda lowercase						: x % 36						)

			lowerCamelCase__      =			numpy.vectorize(lowercase						)
			def __init__(  self  ,      lowercase  ):
							_lowerCamelCase     :			Optional[int]    =    self.modulus(lowercase  )  # mod36 calc's on the encrypt key
							self.check_determinant()  # validate the determinant of the encryption key
							_lowerCamelCase     :			Dict    =    encrypt_key.shape[0]
			def 					A_    (  self  ,      lowercase  ):
							return self.key_string.index(lowercase  )
			def 					A_    (  self  ,      lowercase  ):
							return self.key_string[round(lowercase  )]
			def 					A_    (  self  ):
							_lowerCamelCase     :			Optional[Any]    =    round(numpy.linalg.det(self.encrypt_key  )  )

							if det < 0:
											_lowerCamelCase     :			Dict    =    det % len(self.key_string  )

							_lowerCamelCase     :			List[Any]    =    len(self.key_string  )
							if greatest_common_divisor(lowercase  ,      len(self.key_string  )  ) != 1:
											_lowerCamelCase     :			List[Any]    =    (
											    F'''determinant modular {req_l} of encryption key({det}) '''
											    F'''is not co prime w.r.t {req_l}.\nTry another key.'''
											)
											raise ValueError(lowercase  )
			def 					A_    (  self  ,      lowercase  ):
							_lowerCamelCase     :			int    =    [char for char in text.upper() if char in self.key_string]

							_lowerCamelCase     :			Optional[int]    =    chars[-1]
							while len(lowercase  ) % self.break_key != 0:
											chars.append(lowercase  )

							return "".join(lowercase  )
			def 					A_    (  self  ,      lowercase  ):
							_lowerCamelCase     :			List[Any]    =    self.process_text(text.upper()  )
							_lowerCamelCase     :			Dict    =    ''

							for i in range(0  ,      len(lowercase  ) - self.break_key + 1  ,      self.break_key  ):
											_lowerCamelCase     :			Dict    =    text[i : i + self.break_key]
											_lowerCamelCase     :			Any    =    [self.replace_letters(lowercase  ) for char in batch]
											_lowerCamelCase     :			Optional[Any]    =    numpy.array([vec]  ).T
											_lowerCamelCase     :			int    =    self.modulus(self.encrypt_key.dot(lowercase  )  ).T.tolist()[
											    0
											]
											_lowerCamelCase     :			Tuple    =    ''.join(
											    self.replace_digits(lowercase  ) for num in batch_encrypted  )
											encrypted += encrypted_batch

							return encrypted
			def 					A_    (  self  ):
							_lowerCamelCase     :			Tuple    =    round(numpy.linalg.det(self.encrypt_key  )  )

							if det < 0:
											_lowerCamelCase     :			Dict    =    det % len(self.key_string  )
							_lowerCamelCase     :			Tuple    =    None
							for i in range(len(self.key_string  )  ):
											if (det * i) % len(self.key_string  ) == 1:
															_lowerCamelCase     :			Optional[int]    =    i
															break

							_lowerCamelCase     :			Any    =    (
							    det_inv
							    * numpy.linalg.det(self.encrypt_key  )
							    * numpy.linalg.inv(self.encrypt_key  )
							)

							return self.to_int(self.modulus(lowercase  )  )






			def 					A_    (  self  ,      lowercase  ):
							_lowerCamelCase     :			Any    =    self.make_decrypt_key()
							_lowerCamelCase     :			List[Any]    =    self.process_text(text.upper()  )
							_lowerCamelCase     :			Any    =    ''

							for i in range(0  ,      len(lowercase  ) - self.break_key + 1  ,      self.break_key  ):
											_lowerCamelCase     :			Optional[Any]    =    text[i : i + self.break_key]
											_lowerCamelCase     :			Optional[Any]    =    [self.replace_letters(lowercase  ) for char in batch]
											_lowerCamelCase     :			Union[str, Any]    =    numpy.array([vec]  ).T
											_lowerCamelCase     :			List[Any]    =    self.modulus(decrypt_key.dot(lowercase  )  ).T.tolist()[0]
											_lowerCamelCase     :			Tuple    =    ''.join(
											    self.replace_digits(lowercase  ) for num in batch_decrypted  )
											decrypted += decrypted_batch

							return decrypted







def      _snake_case			(				):
				_lowerCamelCase     :			Optional[int]    =    int(input('Enter the order of the encryption key: '		)		)
				_lowerCamelCase     :			str    =    []

				print('Enter each row of the encryption key with space separated integers'		)
				for _ in range(lowercase__		):
								_lowerCamelCase     :			str    =    [int(lowercase__		) for x in input().split()]
								hill_matrix.append(lowercase__		)

				_lowerCamelCase     :			Optional[int]    =    HillCipher(numpy.array(lowercase__		)		)

				print('Would you like to encrypt or decrypt some text? (1 or 2)'		)
				_lowerCamelCase     :			str    =    input('\n1. Encrypt\n2. Decrypt\n'		)
				if option == "1":
								_lowerCamelCase     :			Dict    =    input('What text would you like to encrypt?: '		)
								print('Your encrypted text is:'		)
								print(hc.encrypt(lowercase__		)		)
				elif option == "2":
								_lowerCamelCase     :			Any    =    input('What text would you like to decrypt?: '		)
								print('Your decrypted text is:'		)
								print(hc.decrypt(lowercase__		)		)


if __name__ == "__main__":
			import doctest

			doctest.testmod()

			main()



import copy
import os
from typing import Union

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


UpperCamelCase								=	logging.get_logger(__name__)

UpperCamelCase								=	{
    """google/pix2struct-textcaps-base""": (
        """https://huggingface.co/google/pix2struct-textcaps-base/resolve/main/config.json"""
    ),
}






class   _lowerCamelCase (					UpperCamelCase			):




       """simple docstring"""
       snake_case      =    "pix2struct_text_model"
       snake_case      =    ["past_key_values"]
       snake_case      =    {
           "hidden_size": "hidden_size",
           "num_attention_heads": "num_heads",
           "num_hidden_layers": "num_layers",
       }



       def __init__(      self				,       _SCREAMING_SNAKE_CASE=5_0244				,       _SCREAMING_SNAKE_CASE=768				,       _SCREAMING_SNAKE_CASE=64				,       _SCREAMING_SNAKE_CASE=2048				,       _SCREAMING_SNAKE_CASE=12				,       _SCREAMING_SNAKE_CASE=12				,       _SCREAMING_SNAKE_CASE=32				,       _SCREAMING_SNAKE_CASE=128				,       _SCREAMING_SNAKE_CASE=0.1				,       _SCREAMING_SNAKE_CASE=1e-6				,       _SCREAMING_SNAKE_CASE=1.0				,       _SCREAMING_SNAKE_CASE="gelu_new"				,       _SCREAMING_SNAKE_CASE=0				,       _SCREAMING_SNAKE_CASE=False				,       _SCREAMING_SNAKE_CASE=0				,       _SCREAMING_SNAKE_CASE=1				,       _SCREAMING_SNAKE_CASE=False				,       _SCREAMING_SNAKE_CASE=True				,       **_SCREAMING_SNAKE_CASE				,       )->int:




         '''simple docstring'''





         A_      :				Optional[int]		     =					vocab_size
         A_      :				Any		     =					hidden_size
         A_      :				Optional[Any]		     =					d_kv
         A_      :				int		     =					d_ff
         A_      :				int		     =					num_layers
         A_      :				Dict		     =					num_heads
         A_      :				Any		     =					relative_attention_num_buckets
         A_      :				int		     =					relative_attention_max_distance
         A_      :				Optional[Any]		     =					dropout_rate
         A_      :				Optional[Any]		     =					layer_norm_epsilon
         A_      :				List[Any]		     =					initializer_factor
         A_      :				Optional[int]		     =					use_cache

         A_      :				Optional[Any]		     =					eos_token_id
         A_      :				List[Any]		     =					decoder_start_token_id

         # for backwards compatibility
         A_      :				int		     =					dense_act_fn

         super().__init__(
             pad_token_id=_SCREAMING_SNAKE_CASE				,       eos_token_id=_SCREAMING_SNAKE_CASE				,       decoder_start_token_id=_SCREAMING_SNAKE_CASE				,       tie_word_embeddings=_SCREAMING_SNAKE_CASE				,       is_decoder=_SCREAMING_SNAKE_CASE				,       **_SCREAMING_SNAKE_CASE				,       )







       @classmethod
       def        _snake_case       (      cls				,       _SCREAMING_SNAKE_CASE				,       **_SCREAMING_SNAKE_CASE		)->"PretrainedConfig":




         '''simple docstring'''





         cls._set_token_in_kwargs(_SCREAMING_SNAKE_CASE		)

         A_     ,       A_      :				Union[str, Any]		     =					cls.get_config_dict(_SCREAMING_SNAKE_CASE				,       **_SCREAMING_SNAKE_CASE		)

         # get the text config dict if we are loading from Pix2StructConfig
         if config_dict.get('''model_type'''		) == "pix2struct":
           A_      :				Any		     =					config_dict['''text_config''']

         if "model_type" in config_dict and hasattr(cls				,       '''model_type'''		) and config_dict["model_type"] != cls.model_type:
           logger.warning(
               F'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type '''
               F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''		)

         return cls.from_dict(_SCREAMING_SNAKE_CASE				,       **_SCREAMING_SNAKE_CASE		)






class   _lowerCamelCase (					UpperCamelCase			):




       """simple docstring"""
       snake_case      =    "pix2struct_vision_model"



       def __init__(      self				,       _SCREAMING_SNAKE_CASE=768				,       _SCREAMING_SNAKE_CASE=768				,       _SCREAMING_SNAKE_CASE=2048				,       _SCREAMING_SNAKE_CASE=64				,       _SCREAMING_SNAKE_CASE=12				,       _SCREAMING_SNAKE_CASE=12				,       _SCREAMING_SNAKE_CASE="gelu_new"				,       _SCREAMING_SNAKE_CASE=1e-6				,       _SCREAMING_SNAKE_CASE=0.0				,       _SCREAMING_SNAKE_CASE=0.0				,       _SCREAMING_SNAKE_CASE=1e-10				,       _SCREAMING_SNAKE_CASE=1.0				,       _SCREAMING_SNAKE_CASE=4096				,       _SCREAMING_SNAKE_CASE=32				,       _SCREAMING_SNAKE_CASE=128				,       **_SCREAMING_SNAKE_CASE				,       )->Optional[int]:




         '''simple docstring'''





         super().__init__(**_SCREAMING_SNAKE_CASE		)

         A_      :				Dict		     =					hidden_size
         A_      :				Union[str, Any]		     =					patch_embed_hidden_size
         A_      :				Optional[Any]		     =					d_ff
         A_      :				Optional[Any]		     =					dropout_rate
         A_      :				int		     =					num_hidden_layers
         A_      :				Tuple		     =					num_attention_heads
         A_      :				List[str]		     =					initializer_range
         A_      :				List[str]		     =					initializer_factor
         A_      :				Union[str, Any]		     =					attention_dropout
         A_      :				Union[str, Any]		     =					layer_norm_eps
         A_      :				Dict		     =					dense_act_fn
         A_      :				Union[str, Any]		     =					seq_len
         A_      :				Optional[Any]		     =					relative_attention_num_buckets
         A_      :				Tuple		     =					relative_attention_max_distance
         A_      :				List[Any]		     =					d_kv







       @classmethod
       def        _snake_case       (      cls				,       _SCREAMING_SNAKE_CASE				,       **_SCREAMING_SNAKE_CASE		)->"PretrainedConfig":




         '''simple docstring'''





         cls._set_token_in_kwargs(_SCREAMING_SNAKE_CASE		)

         A_     ,       A_      :				str		     =					cls.get_config_dict(_SCREAMING_SNAKE_CASE				,       **_SCREAMING_SNAKE_CASE		)

         # get the vision config dict if we are loading from Pix2StructConfig
         if config_dict.get('''model_type'''		) == "pix2struct":
           A_      :				Any		     =					config_dict['''vision_config''']

         if "model_type" in config_dict and hasattr(cls				,       '''model_type'''		) and config_dict["model_type"] != cls.model_type:
           logger.warning(
               F'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type '''
               F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''		)

         return cls.from_dict(_SCREAMING_SNAKE_CASE				,       **_SCREAMING_SNAKE_CASE		)






class   _lowerCamelCase (					UpperCamelCase			):




       """simple docstring"""
       snake_case      =    "pix2struct"
       snake_case      =    True



       def __init__(      self				,       _SCREAMING_SNAKE_CASE=None				,       _SCREAMING_SNAKE_CASE=None				,       _SCREAMING_SNAKE_CASE=1.0				,       _SCREAMING_SNAKE_CASE=0.0_2				,       _SCREAMING_SNAKE_CASE=False				,       _SCREAMING_SNAKE_CASE=False				,       _SCREAMING_SNAKE_CASE=True				,       **_SCREAMING_SNAKE_CASE				,       )->Any:




         '''simple docstring'''





         super().__init__(tie_word_embeddings=_SCREAMING_SNAKE_CASE				,       is_encoder_decoder=_SCREAMING_SNAKE_CASE				,       **_SCREAMING_SNAKE_CASE		)

         if text_config is None:
           A_      :				Dict		     =					{}
           logger.info('''text_config is None. Initializing the Pix2StructTextConfig with default values.'''		)

         if vision_config is None:
           A_      :				List[Any]		     =					{}
           logger.info('''vision_config is None. Initializing the Pix2StructVisionConfig with default values.'''		)

         A_      :				int		     =					PixaStructTextConfig(**_SCREAMING_SNAKE_CASE		)
         A_      :				Dict		     =					PixaStructVisionConfig(**_SCREAMING_SNAKE_CASE		)

         A_      :				Optional[Any]		     =					self.text_config.decoder_start_token_id
         A_      :				Tuple		     =					self.text_config.pad_token_id
         A_      :				Union[str, Any]		     =					self.text_config.eos_token_id

         A_      :				str		     =					initializer_factor
         A_      :				Tuple		     =					initializer_range

         A_      :				List[str]		     =					self.initializer_range
         A_      :				int		     =					self.initializer_range

         A_      :				List[Any]		     =					is_vqa



       @classmethod
       def        _snake_case       (      cls				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       **_SCREAMING_SNAKE_CASE		)->Union[str, Any]:




         '''simple docstring'''





         return cls(text_config=text_config.to_dict()				,       vision_config=vision_config.to_dict()				,       **_SCREAMING_SNAKE_CASE		)







       def        _snake_case       (      self		)->Dict:




         '''simple docstring'''





         A_      :				List[Any]		     =					copy.deepcopy(self.__dict__		)
         A_      :				Dict		     =					self.text_config.to_dict()
         A_      :				int		     =					self.vision_config.to_dict()
         A_      :				List[str]		     =					self.__class__.model_type
         return output




