





from string import ascii_uppercase

UpperCamelCase__      ={char: i for i, char in enumerate(ascii_uppercase)}
UpperCamelCase__      =dict(enumerate(ascii_uppercase))






def     lowerCamelCase__			(__lowerCamelCase,       __lowerCamelCase	):
     _SCREAMING_SNAKE_CASE       :	int						    =							len(__lowerCamelCase	)
     _SCREAMING_SNAKE_CASE       :	int						    =							0
     while True:
          if x == i:
               _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							0
          if len(__lowerCamelCase	) == len(__lowerCamelCase	):
               break
          key += key[i]
          i += 1
     return key






def     lowerCamelCase__			(__lowerCamelCase,       __lowerCamelCase	):
     _SCREAMING_SNAKE_CASE       :	int						    =							""
     _SCREAMING_SNAKE_CASE       :	str						    =							0
     for letter in message:
          if letter == " ":
               cipher_text += " "
          else:
               _SCREAMING_SNAKE_CASE       :	int						    =							(dicta[letter] - dicta[key_new[i]]) % 26
               i += 1
               cipher_text += dicta[x]
     return cipher_text






def     lowerCamelCase__			(__lowerCamelCase,       __lowerCamelCase	):
     _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							""
     _SCREAMING_SNAKE_CASE       :	Union[str, Any]						    =							0
     for letter in cipher_text:
          if letter == " ":
               or_txt += " "
          else:
               _SCREAMING_SNAKE_CASE       :	str						    =							(dicta[letter] + dicta[key_new[i]] + 26) % 26
               i += 1
               or_txt += dicta[x]
     return or_txt






def     lowerCamelCase__			():
     _SCREAMING_SNAKE_CASE       :	str						    =							"THE GERMAN ATTACK"
     _SCREAMING_SNAKE_CASE       :	Optional[Any]						    =							"SECRET"
     _SCREAMING_SNAKE_CASE       :	List[Any]						    =							generate_key(__lowerCamelCase,       __lowerCamelCase	)
     _SCREAMING_SNAKE_CASE       :	List[str]						    =							cipher_text(__lowerCamelCase,       __lowerCamelCase	)
     print(f"""Encrypted Text = {s}"""	)
     print(f"""Original Text = {original_text(__lowerCamelCase,       __lowerCamelCase	)}"""	)


if __name__ == "__main__":
       import doctest

       doctest.testmod()
       main()






import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    EulerAncestralDiscreteScheduler,
    LMSDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionPanoramaPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()





@skip_mps
class 			lowerCAmelCase__( __lowercase  ,     __lowercase  ,     unittest.TestCase    ):






       '''simple docstring'''


       __snake_case 			=    StableDiffusionPanoramaPipeline
       __snake_case 			=    TEXT_TO_IMAGE_PARAMS
       __snake_case 			=    TEXT_TO_IMAGE_BATCH_PARAMS
       __snake_case 			=    TEXT_TO_IMAGE_IMAGE_PARAMS
       __snake_case 			=    TEXT_TO_IMAGE_IMAGE_PARAMS




       def     UpperCamelCase_			(      self							)			->     Any:
            torch.manual_seed(0							)
            _SCREAMING_SNAKE_CASE       :	Tuple						    =							UNetaDConditionModel(
                block_out_channels=(3_2, 6_4)    ,						layers_per_block=1    ,						sample_size=3_2    ,						in_channels=4    ,						out_channels=4    ,						down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")    ,						up_block_types=("CrossAttnUpBlock2D", "UpBlock2D")    ,						cross_attention_dim=3_2    ,						)
            _SCREAMING_SNAKE_CASE       :	Optional[Any]						    =							DDIMScheduler()
            torch.manual_seed(0							)
            _SCREAMING_SNAKE_CASE       :	Optional[Any]						    =							AutoencoderKL(
                block_out_channels=[3_2, 6_4]    ,						in_channels=3    ,						out_channels=3    ,						down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"]    ,						up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"]    ,						latent_channels=4    ,						)
            torch.manual_seed(0							)
            _SCREAMING_SNAKE_CASE       :	int						    =							CLIPTextConfig(
                bos_token_id=0    ,						eos_token_id=2    ,						hidden_size=3_2    ,						intermediate_size=3_7    ,						layer_norm_eps=1E-05    ,						num_attention_heads=4    ,						num_hidden_layers=5    ,						pad_token_id=1    ,						vocab_size=1_0_0_0    ,						)
            _SCREAMING_SNAKE_CASE       :	Union[str, Any]						    =							CLIPTextModel(__lowerCamelCase							)
            _SCREAMING_SNAKE_CASE       :	int						    =							CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"							)

            _SCREAMING_SNAKE_CASE       :	str						    =							{
                "unet": unet,
                "scheduler": scheduler,
                "vae": vae,
                "text_encoder": text_encoder,
                "tokenizer": tokenizer,
                "safety_checker": None,
                "feature_extractor": None,
            }
            return components




       def     UpperCamelCase_			(      self    ,						__lowerCamelCase    ,						__lowerCamelCase=0							)			->     Union[str, Any]:
            _SCREAMING_SNAKE_CASE       :	List[Any]						    =							torch.manual_seed(__lowerCamelCase							)
            _SCREAMING_SNAKE_CASE       :	List[str]						    =							{
                "prompt": "a photo of the dolomites",
                "generator": generator,
                # Setting height and width to None to prevent OOMs on CPU.
                "height": None,
                "width": None,
                "num_inference_steps": 1,
                "guidance_scale": 6.0,
                "output_type": "numpy",
            }
            return inputs




       def     UpperCamelCase_			(      self							)			->     Dict:
            _SCREAMING_SNAKE_CASE       :	Any						    =							"cpu"  # ensure determinism for the device-dependent torch.Generator
            _SCREAMING_SNAKE_CASE       :	Any						    =							self.get_dummy_components()
            _SCREAMING_SNAKE_CASE       :	int						    =							StableDiffusionPanoramaPipeline(**__lowerCamelCase							)
            _SCREAMING_SNAKE_CASE       :	Union[str, Any]						    =							sd_pipe.to(__lowerCamelCase							)
            sd_pipe.set_progress_bar_config(disable=__lowerCamelCase							)

            _SCREAMING_SNAKE_CASE       :	int						    =							self.get_dummy_inputs(__lowerCamelCase							)
            _SCREAMING_SNAKE_CASE       :	Any						    =							sd_pipe(**__lowerCamelCase							).images
            _SCREAMING_SNAKE_CASE       :	List[Any]						    =							image[0, -3:, -3:, -1]
            assert image.shape == (1, 6_4, 6_4, 3)

            _SCREAMING_SNAKE_CASE       :	Union[str, Any]						    =							np.array([0.6186, 0.5374, 0.4915, 0.4135, 0.4114, 0.4563, 0.5128, 0.4977, 0.4757]							)

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




       def     UpperCamelCase_			(      self							)			->     Union[str, Any]:
            super().test_inference_batch_consistent(batch_sizes=[1, 2]							)




       def     UpperCamelCase_			(      self							)			->     List[str]:
            super().test_inference_batch_single_identical(batch_size=2    ,						expected_max_diff=3.25E-3							)




       def     UpperCamelCase_			(      self							)			->     Dict:
            _SCREAMING_SNAKE_CASE       :	List[str]						    =							"cpu"  # ensure determinism for the device-dependent torch.Generator
            _SCREAMING_SNAKE_CASE       :	List[Any]						    =							self.get_dummy_components()
            _SCREAMING_SNAKE_CASE       :	Any						    =							StableDiffusionPanoramaPipeline(**__lowerCamelCase							)
            _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							sd_pipe.to(__lowerCamelCase							)
            sd_pipe.set_progress_bar_config(disable=__lowerCamelCase							)

            _SCREAMING_SNAKE_CASE       :	int						    =							self.get_dummy_inputs(__lowerCamelCase							)
            _SCREAMING_SNAKE_CASE       :	str						    =							"french fries"
            _SCREAMING_SNAKE_CASE       :	str						    =							sd_pipe(**__lowerCamelCase    ,						negative_prompt=__lowerCamelCase							)
            _SCREAMING_SNAKE_CASE       :	int						    =							output.images
            _SCREAMING_SNAKE_CASE       :	str						    =							image[0, -3:, -3:, -1]

            assert image.shape == (1, 6_4, 6_4, 3)

            _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							np.array([0.6187, 0.5375, 0.4915, 0.4136, 0.4114, 0.4563, 0.5128, 0.4976, 0.4757]							)

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




       def     UpperCamelCase_			(      self							)			->     str:
            _SCREAMING_SNAKE_CASE       :	Any						    =							"cpu"  # ensure determinism for the device-dependent torch.Generator
            _SCREAMING_SNAKE_CASE       :	List[Any]						    =							self.get_dummy_components()
            _SCREAMING_SNAKE_CASE       :	Dict						    =							StableDiffusionPanoramaPipeline(**__lowerCamelCase							)
            _SCREAMING_SNAKE_CASE       :	Tuple						    =							sd_pipe.to(__lowerCamelCase							)
            sd_pipe.set_progress_bar_config(disable=__lowerCamelCase							)

            _SCREAMING_SNAKE_CASE       :	List[str]						    =							self.get_dummy_inputs(__lowerCamelCase							)
            _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							sd_pipe(**__lowerCamelCase    ,						view_batch_size=2							)
            _SCREAMING_SNAKE_CASE       :	List[str]						    =							output.images
            _SCREAMING_SNAKE_CASE       :	str						    =							image[0, -3:, -3:, -1]

            assert image.shape == (1, 6_4, 6_4, 3)

            _SCREAMING_SNAKE_CASE       :	List[Any]						    =							np.array([0.6187, 0.5375, 0.4915, 0.4136, 0.4114, 0.4563, 0.5128, 0.4976, 0.4757]							)

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




       def     UpperCamelCase_			(      self							)			->     int:
            _SCREAMING_SNAKE_CASE       :	List[Any]						    =							"cpu"  # ensure determinism for the device-dependent torch.Generator
            _SCREAMING_SNAKE_CASE       :	int						    =							self.get_dummy_components()
            _SCREAMING_SNAKE_CASE       :	Tuple						    =							EulerAncestralDiscreteScheduler(
                beta_start=0.0_0085    ,						beta_end=0.012    ,						beta_schedule="scaled_linear"							)
            _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							StableDiffusionPanoramaPipeline(**__lowerCamelCase							)
            _SCREAMING_SNAKE_CASE       :	List[Any]						    =							sd_pipe.to(__lowerCamelCase							)
            sd_pipe.set_progress_bar_config(disable=__lowerCamelCase							)

            _SCREAMING_SNAKE_CASE       :	Dict						    =							self.get_dummy_inputs(__lowerCamelCase							)
            _SCREAMING_SNAKE_CASE       :	int						    =							sd_pipe(**__lowerCamelCase							).images
            _SCREAMING_SNAKE_CASE       :	Optional[Any]						    =							image[0, -3:, -3:, -1]

            assert image.shape == (1, 6_4, 6_4, 3)

            _SCREAMING_SNAKE_CASE       :	Any						    =							np.array([0.4024, 0.6510, 0.4901, 0.5378, 0.5813, 0.5622, 0.4795, 0.4467, 0.4952]							)

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







       def     UpperCamelCase_			(      self							)			->     int:
            _SCREAMING_SNAKE_CASE       :	Dict						    =							"cpu"  # ensure determinism for the device-dependent torch.Generator
            _SCREAMING_SNAKE_CASE       :	int						    =							self.get_dummy_components()
            _SCREAMING_SNAKE_CASE       :	List[str]						    =							PNDMScheduler(
                beta_start=0.0_0085    ,						beta_end=0.012    ,						beta_schedule="scaled_linear"    ,						skip_prk_steps=__lowerCamelCase							)
            _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							StableDiffusionPanoramaPipeline(**__lowerCamelCase							)
            _SCREAMING_SNAKE_CASE       :	int						    =							sd_pipe.to(__lowerCamelCase							)
            sd_pipe.set_progress_bar_config(disable=__lowerCamelCase							)

            _SCREAMING_SNAKE_CASE       :	int						    =							self.get_dummy_inputs(__lowerCamelCase							)
            _SCREAMING_SNAKE_CASE       :	Dict						    =							sd_pipe(**__lowerCamelCase							).images
            _SCREAMING_SNAKE_CASE       :	int						    =							image[0, -3:, -3:, -1]

            assert image.shape == (1, 6_4, 6_4, 3)

            _SCREAMING_SNAKE_CASE       :	Union[str, Any]						    =							np.array([0.6391, 0.6291, 0.4861, 0.5134, 0.5552, 0.4578, 0.5032, 0.5023, 0.4539]							)

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





@slow
@require_torch_gpu
class 			lowerCAmelCase__( unittest.TestCase    ):






       '''simple docstring'''


       def     UpperCamelCase_			(      self							)			->     List[Any]:
            super().tearDown()
            gc.collect()
            torch.cuda.empty_cache()




       def     UpperCamelCase_			(      self    ,						__lowerCamelCase=0							)			->     int:
            _SCREAMING_SNAKE_CASE       :	List[str]						    =							torch.manual_seed(__lowerCamelCase							)
            _SCREAMING_SNAKE_CASE       :	int						    =							{
                "prompt": "a photo of the dolomites",
                "generator": generator,
                "num_inference_steps": 3,
                "guidance_scale": 7.5,
                "output_type": "numpy",
            }
            return inputs




       def     UpperCamelCase_			(      self							)			->     Union[str, Any]:
            _SCREAMING_SNAKE_CASE       :	int						    =							"stabilityai/stable-diffusion-2-base"
            _SCREAMING_SNAKE_CASE       :	Any						    =							DDIMScheduler.from_pretrained(__lowerCamelCase    ,						subfolder="scheduler"							)
            _SCREAMING_SNAKE_CASE       :	Any						    =							StableDiffusionPanoramaPipeline.from_pretrained(__lowerCamelCase    ,						scheduler=__lowerCamelCase    ,						safety_checker=__lowerCamelCase							)
            pipe.to(__lowerCamelCase							)
            pipe.set_progress_bar_config(disable=__lowerCamelCase							)
            pipe.enable_attention_slicing()

            _SCREAMING_SNAKE_CASE       :	int						    =							self.get_inputs()
            _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							pipe(**__lowerCamelCase							).images
            _SCREAMING_SNAKE_CASE       :	List[Any]						    =							image[0, -3:, -3:, -1].flatten()

            assert image.shape == (1, 5_1_2, 2_0_4_8, 3)

            _SCREAMING_SNAKE_CASE       :	List[str]						    =							np.array(
                [
                    0.3696_8392,
                    0.2702_5372,
                    0.3244_6766,
                    0.2837_9387,
                    0.3636_3274,
                    0.3073_3347,
                    0.2710_0027,
                    0.2705_4125,
                    0.2553_6096,
                ]							)

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




       def     UpperCamelCase_			(      self							)			->     Optional[int]:
            _SCREAMING_SNAKE_CASE       :	Dict						    =							StableDiffusionPanoramaPipeline.from_pretrained(
                "stabilityai/stable-diffusion-2-base"    ,						safety_checker=__lowerCamelCase							)
            _SCREAMING_SNAKE_CASE       :	Union[str, Any]						    =							LMSDiscreteScheduler.from_config(pipe.scheduler.config							)
            pipe.to(__lowerCamelCase							)
            pipe.set_progress_bar_config(disable=__lowerCamelCase							)
            pipe.enable_attention_slicing()

            _SCREAMING_SNAKE_CASE       :	Tuple						    =							self.get_inputs()
            _SCREAMING_SNAKE_CASE       :	Tuple						    =							pipe(**__lowerCamelCase							).images
            _SCREAMING_SNAKE_CASE       :	int						    =							image[0, -3:, -3:, -1].flatten()

            assert image.shape == (1, 5_1_2, 2_0_4_8, 3)

            _SCREAMING_SNAKE_CASE       :	Optional[Any]						    =							np.array(
                [
                    [
                        0.0,
                        0.0,
                        0.0,
                        0.0,
                        0.0,
                        0.0,
                        0.0,
                        0.0,
                        0.0,
                    ]
                ]							)

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




       def     UpperCamelCase_			(      self							)			->     Dict:
            _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							0

            def callback_fn(__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase							) -> None:
                 _SCREAMING_SNAKE_CASE       :	Dict						    =							True
                 nonlocal number_of_steps
                 number_of_steps += 1
                 if step == 1:
                      _SCREAMING_SNAKE_CASE       :	str						    =							latents.detach().cpu().numpy()
                      assert latents.shape == (1, 4, 6_4, 2_5_6)
                      _SCREAMING_SNAKE_CASE       :	List[Any]						    =							latents[0, -3:, -3:, -1]

                      _SCREAMING_SNAKE_CASE       :	Dict						    =							np.array(
                          [
                              0.1868_1869,
                              0.3390_7816,
                              0.536_1276,
                              0.1443_2865,
                              -0.0285_6611,
                              -0.7394_1123,
                              0.2339_7987,
                              0.4732_2682,
                              -0.3782_3164,
                          ]							)
                      assert np.abs(latents_slice.flatten() - expected_slice							).max() < 5E-2
                 elif step == 2:
                      _SCREAMING_SNAKE_CASE       :	Union[str, Any]						    =							latents.detach().cpu().numpy()
                      assert latents.shape == (1, 4, 6_4, 2_5_6)
                      _SCREAMING_SNAKE_CASE       :	str						    =							latents[0, -3:, -3:, -1]

                      _SCREAMING_SNAKE_CASE       :	Dict						    =							np.array(
                          [
                              0.1853_9645,
                              0.3398_7248,
                              0.537_8559,
                              0.1443_7142,
                              -0.0245_5261,
                              -0.733_8317,
                              0.2399_0755,
                              0.4735_6272,
                              -0.378_6505,
                          ]							)

                      assert np.abs(latents_slice.flatten() - expected_slice							).max() < 5E-2

            _SCREAMING_SNAKE_CASE       :	List[Any]						    =							False

            _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							"stabilityai/stable-diffusion-2-base"
            _SCREAMING_SNAKE_CASE       :	Optional[Any]						    =							DDIMScheduler.from_pretrained(__lowerCamelCase    ,						subfolder="scheduler"							)
            _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							StableDiffusionPanoramaPipeline.from_pretrained(__lowerCamelCase    ,						scheduler=__lowerCamelCase    ,						safety_checker=__lowerCamelCase							)
            _SCREAMING_SNAKE_CASE       :	Union[str, Any]						    =							pipe.to(__lowerCamelCase							)
            pipe.set_progress_bar_config(disable=__lowerCamelCase							)
            pipe.enable_attention_slicing()

            _SCREAMING_SNAKE_CASE       :	List[str]						    =							self.get_inputs()
            pipe(**__lowerCamelCase    ,						callback=__lowerCamelCase    ,						callback_steps=1							)
            assert callback_fn.has_been_called
            assert number_of_steps == 3







       def     UpperCamelCase_			(      self							)			->     Optional[int]:
            torch.cuda.empty_cache()
            torch.cuda.reset_max_memory_allocated()
            torch.cuda.reset_peak_memory_stats()

            _SCREAMING_SNAKE_CASE       :	List[Any]						    =							"stabilityai/stable-diffusion-2-base"
            _SCREAMING_SNAKE_CASE       :	Dict						    =							DDIMScheduler.from_pretrained(__lowerCamelCase    ,						subfolder="scheduler"							)
            _SCREAMING_SNAKE_CASE       :	Optional[Any]						    =							StableDiffusionPanoramaPipeline.from_pretrained(__lowerCamelCase    ,						scheduler=__lowerCamelCase    ,						safety_checker=__lowerCamelCase							)
            _SCREAMING_SNAKE_CASE       :	List[str]						    =							pipe.to(__lowerCamelCase							)
            pipe.set_progress_bar_config(disable=__lowerCamelCase							)
            pipe.enable_attention_slicing(1							)
            pipe.enable_sequential_cpu_offload()

            _SCREAMING_SNAKE_CASE       :	Dict						    =							self.get_inputs()
            _SCREAMING_SNAKE_CASE       :	str						    =							pipe(**__lowerCamelCase							)

            _SCREAMING_SNAKE_CASE       :	Union[str, Any]						    =							torch.cuda.max_memory_allocated()
            # make sure that less than 5.2 GB is allocated
            assert mem_bytes < 5.5 * 1_0**9