



import string

import numpy

def        __a			(   __lowerCAmelCase      ,	__lowerCAmelCase   )     ->     int:
					return b if a == 0 else greatest_common_divisor(b % a      ,	__lowerCAmelCase   )


class 						lowercase    :
						'''simple docstring'''


						UpperCAmelCase			:		Dict		=		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)
						UpperCAmelCase			:		Any		=		numpy.vectorize(lambda SCREAMING_SNAKE_CASE_: x % 36)

						UpperCAmelCase			:		List[str]		=		numpy.vectorize(SCREAMING_SNAKE_CASE_)







						def __init__(  self    :				Tuple       ,	snake_case    :				numpy.ndarray	):



											'''simple docstring'''


											SCREAMING_SNAKE_CASE							:     str						=				self.modulus(snake_case	)  # mod36 calc's on the encrypt key
											self.check_determinant()  # validate the determinant of the encryption key
											SCREAMING_SNAKE_CASE							:     Any						=				encrypt_key.shape[0]







						def       lowerCamelCase_				(  self    :				Optional[int]       ,	snake_case    :				str	):



											'''simple docstring'''


											return self.key_string.index(snake_case	)







						def       lowerCamelCase_				(  self    :				List[Any]       ,	snake_case    :				int	):



											'''simple docstring'''


											return self.key_string[round(snake_case	)]







						def       lowerCamelCase_				(  self    :				Tuple	):



											'''simple docstring'''


											SCREAMING_SNAKE_CASE							:     str						=				round(numpy.linalg.det(self.encrypt_key	)	)

											if det < 0:
																SCREAMING_SNAKE_CASE							:     List[str]						=				det % len(self.key_string	)

											SCREAMING_SNAKE_CASE							:     Optional[int]						=				len(self.key_string	)
											if greatest_common_divisor(snake_case       ,	len(self.key_string	)	) != 1:
																SCREAMING_SNAKE_CASE							:     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(snake_case	)







						def       lowerCamelCase_				(  self    :				Optional[Any]       ,	snake_case    :				str	):



											'''simple docstring'''


											SCREAMING_SNAKE_CASE							:     Optional[int]						=				[char for char in text.upper() if char in self.key_string]

											SCREAMING_SNAKE_CASE							:     Dict						=				chars[-1]
											while len(snake_case	) % self.break_key != 0:
																chars.append(snake_case	)

											return "".join(snake_case	)







						def       lowerCamelCase_				(  self    :				Dict       ,	snake_case    :				str	):



											'''simple docstring'''


											SCREAMING_SNAKE_CASE							:     Union[str, Any]						=				self.process_text(text.upper()	)
											SCREAMING_SNAKE_CASE							:     Union[str, Any]						=				''

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

											return encrypted







						def       lowerCamelCase_				(  self    :				Dict	):



											'''simple docstring'''


											SCREAMING_SNAKE_CASE							:     List[Any]						=				round(numpy.linalg.det(self.encrypt_key	)	)

											if det < 0:
																SCREAMING_SNAKE_CASE							:     Dict						=				det % len(self.key_string	)
											SCREAMING_SNAKE_CASE							:     Tuple						=				None
											for i in range(len(self.key_string	)	):
																if (det * i) % len(self.key_string	) == 1:
																					SCREAMING_SNAKE_CASE							:     str						=				i
																					break

											SCREAMING_SNAKE_CASE							:     Union[str, Any]						=				(
											    det_inv
											    * numpy.linalg.det(self.encrypt_key	)
											    * numpy.linalg.inv(self.encrypt_key	)
											)

											return self.to_int(self.modulus(snake_case	)	)







						def       lowerCamelCase_				(  self    :				Optional[Any]       ,	snake_case    :				str	):



											'''simple docstring'''


											SCREAMING_SNAKE_CASE							:     List[str]						=				self.make_decrypt_key()
											SCREAMING_SNAKE_CASE							:     List[str]						=				self.process_text(text.upper()	)
											SCREAMING_SNAKE_CASE							:     Any						=				''

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

											return decrypted


def        __a			(   )     ->     None:
					SCREAMING_SNAKE_CASE							:     Optional[int]						=				int(input('Enter the order of the encryption key: '   )   )
					SCREAMING_SNAKE_CASE							:     int						=				[]

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

					SCREAMING_SNAKE_CASE							:     Any						=				HillCipher(numpy.array(__lowerCAmelCase   )   )

					print('Would you like to encrypt or decrypt some text? (1 or 2)'   )
					SCREAMING_SNAKE_CASE							:     Optional[int]						=				input('\n1. Encrypt\n2. Decrypt\n'   )
					if option == "1":
										SCREAMING_SNAKE_CASE							:     List[str]						=				input('What text would you like to encrypt?: '   )
										print('Your encrypted text is:'   )
										print(hc.encrypt(__lowerCAmelCase   )   )
					elif option == "2":
										SCREAMING_SNAKE_CASE							:     List[str]						=				input('What text would you like to decrypt?: '   )
										print('Your decrypted text is:'   )
										print(hc.decrypt(__lowerCAmelCase   )   )


if __name__ == "__main__":
			import doctest

			doctest.testmod()

			main()




import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionPipeline
from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device


_lowerCamelCase    :		Any		      =    False


class 						lowercase    (							unittest.TestCase):
						'''simple docstring'''


						pass


@nightly
@require_torch_gpu
class 						lowercase    (							unittest.TestCase):
						'''simple docstring'''


						def       lowerCamelCase_				(  self    :				List[Any]	):



											'''simple docstring'''


											super().tearDown()
											gc.collect()
											torch.cuda.empty_cache()







						def       lowerCamelCase_				(  self    :				List[str]	):



											'''simple docstring'''


											SCREAMING_SNAKE_CASE							:     Dict						=				VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion'       ,	torch_dtype=torch.floataa	)
											pipe.to(snake_case	)
											pipe.set_progress_bar_config(disable=snake_case	)

											SCREAMING_SNAKE_CASE							:     Dict						=				load_image(
											    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'	)

											SCREAMING_SNAKE_CASE							:     Union[str, Any]						=				torch.manual_seed(0	)
											SCREAMING_SNAKE_CASE							:     Union[str, Any]						=				pipe.dual_guided(
											    prompt='first prompt'       ,	image=snake_case       ,	text_to_image_strength=0.75       ,	generator=snake_case       ,	guidance_scale=7.5       ,	num_inference_steps=2       ,	output_type='numpy'       ,	).images

											with tempfile.TemporaryDirectory() as tmpdirname:
																pipe.save_pretrained(snake_case	)
																SCREAMING_SNAKE_CASE							:     Any						=				VersatileDiffusionPipeline.from_pretrained(snake_case       ,	torch_dtype=torch.floataa	)
											pipe.to(snake_case	)
											pipe.set_progress_bar_config(disable=snake_case	)

											SCREAMING_SNAKE_CASE							:     List[str]						=				generator.manual_seed(0	)
											SCREAMING_SNAKE_CASE							:     Union[str, Any]						=				pipe.dual_guided(
											    prompt='first prompt'       ,	image=snake_case       ,	text_to_image_strength=0.75       ,	generator=snake_case       ,	guidance_scale=7.5       ,	num_inference_steps=2       ,	output_type='numpy'       ,	).images

											assert np.abs(image - new_image	).sum() < 1E-5, "Models don't have the same forward pass"







						def       lowerCamelCase_				(  self    :				Tuple	):



											'''simple docstring'''


											SCREAMING_SNAKE_CASE							:     Dict						=				VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion'       ,	torch_dtype=torch.floataa	)
											pipe.to(snake_case	)
											pipe.set_progress_bar_config(disable=snake_case	)

											SCREAMING_SNAKE_CASE							:     Union[str, Any]						=				'cyberpunk 2077'
											SCREAMING_SNAKE_CASE							:     Optional[int]						=				load_image(
											    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'	)
											SCREAMING_SNAKE_CASE							:     List[Any]						=				torch.manual_seed(0	)
											SCREAMING_SNAKE_CASE							:     Any						=				pipe.dual_guided(
											    prompt=snake_case       ,	image=snake_case       ,	text_to_image_strength=0.75       ,	generator=snake_case       ,	guidance_scale=7.5       ,	num_inference_steps=50       ,	output_type='numpy'       ,	).images

											SCREAMING_SNAKE_CASE							:     List[Any]						=				image[0, 253:256, 253:256, -1]

											assert image.shape == (1, 512, 512, 3)
											SCREAMING_SNAKE_CASE							:     Tuple						=				np.array([0.1448, 0.1619, 0.1741, 0.1086, 0.1147, 0.1128, 0.1199, 0.1165, 0.1001]	)

											assert np.abs(image_slice.flatten() - expected_slice	).max() < 1E-1

											SCREAMING_SNAKE_CASE							:     Any						=				'A painting of a squirrel eating a burger '
											SCREAMING_SNAKE_CASE							:     Any						=				torch.manual_seed(0	)
											SCREAMING_SNAKE_CASE							:     Tuple						=				pipe.text_to_image(
											    prompt=snake_case       ,	generator=snake_case       ,	guidance_scale=7.5       ,	num_inference_steps=50       ,	output_type='numpy'	).images

											SCREAMING_SNAKE_CASE							:     Dict						=				image[0, 253:256, 253:256, -1]

											assert image.shape == (1, 512, 512, 3)
											SCREAMING_SNAKE_CASE							:     List[Any]						=				np.array([0.3367, 0.3169, 0.2656, 0.3870, 0.4790, 0.3796, 0.4009, 0.4878, 0.4778]	)

											assert np.abs(image_slice.flatten() - expected_slice	).max() < 1E-1

											SCREAMING_SNAKE_CASE							:     Optional[Any]						=				pipe.image_variation(snake_case       ,	generator=snake_case       ,	output_type='numpy'	).images

											SCREAMING_SNAKE_CASE							:     Optional[int]						=				image[0, 253:256, 253:256, -1]

											assert image.shape == (1, 512, 512, 3)
											SCREAMING_SNAKE_CASE							:     Optional[int]						=				np.array([0.3076, 0.3123, 0.3284, 0.3782, 0.3770, 0.3894, 0.4297, 0.4331, 0.4456]	)

											assert np.abs(image_slice.flatten() - expected_slice	).max() < 1E-1