





from __future__ import annotations

from PIL import Image

# Define the first generation of cells
# fmt: off
_SCREAMING_SNAKE_CASE   						=		[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]


def 				SCREAMING_SNAKE_CASE__    (     __a ):
	return [int(__a ) for c in f"""{ruleset:08}"""[:8]]


def 				SCREAMING_SNAKE_CASE__    (     __a ,			__a ,			__a ):
	snake_case_		:      Any           =						len(cells[0] )  # 31
	snake_case_		:      Any           =						[]
	for i in range(__a ):
		# Get the neighbors of each cell
		# Handle neighbours outside bounds by using 0 as their value
		snake_case_		:      int           =						0 if i == 0 else cells[time][i - 1]
		snake_case_		:      str           =						0 if i == population - 1 else cells[time][i + 1]
		# Define a new cell and add it to the new generation
		snake_case_		:      Optional[int]           =						7 - int(f"""{left_neighbor}{cells[time][i]}{right_neighbor}""" ,			2 )
		next_generation.append(rule[situation] )
	return next_generation


def 				SCREAMING_SNAKE_CASE__    (     __a ):
	snake_case_		:      Tuple           =						Image.new('RGB' ,			(len(cells[0] ), len(__a )) )
	snake_case_		:      List[str]           =						img.load()
	# Generates image
	for w in range(img.width ):
		for h in range(img.height ):
			snake_case_		:      Optional[int]           =						2_55 - int(2_55 * cells[h][w] )
			snake_case_		:      int           =						(color, color, color)
	return img


if __name__ == "__main__":
						_SCREAMING_SNAKE_CASE   						=		bin(int(input("""Rule:\n""").strip()))[2:]
						_SCREAMING_SNAKE_CASE   						=		format_ruleset(int(rule_num))
						for time in range(16):
												CELLS.append(new_generation(CELLS, rule, time))
						_SCREAMING_SNAKE_CASE   						=		generate_image(CELLS)
						# Uncomment to save the image
						# img.save(f"rule_{rule_num}.png")
						img.show()


import gc
import tempfile
import unittest

import numpy as np
import torch

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


lowerCAmelCase__	:  Union[str, Any]						=  False





@nightly
@require_torch_gpu
class 					__snake_case    (     unittest.TestCase      ):




					def      __a     (  self						)	->    str:



									'''simple docstring'''







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




					def      __a     (  self						)	->    Optional[int]:



									'''simple docstring'''







									snake_case__   :      str   				= VersatileDiffusionDualGuidedPipeline.from_pretrained('shi-labs/versatile-diffusion'						)
									# remove text_unet
									pipe.remove_unused_weights()
									pipe.to(__UpperCamelCase						)
									pipe.set_progress_bar_config(disable=__UpperCamelCase						)

									snake_case__   :      Union[str, Any]   				= load_image(
									    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'						)

									snake_case__   :      Dict   				= torch.manual_seed(0						)
									snake_case__   :      Tuple   				= pipe(
									    prompt='first prompt'       ,			image=__UpperCamelCase       ,			text_to_image_strength=0.7_5       ,			generator=__UpperCamelCase       ,			guidance_scale=7.5       ,			num_inference_steps=2       ,			output_type='numpy'       ,			).images

									with tempfile.TemporaryDirectory() as tmpdirname:
													pipe.save_pretrained(__UpperCamelCase						)
													snake_case__   :      List[Any]   				= VersatileDiffusionDualGuidedPipeline.from_pretrained(__UpperCamelCase						)

									pipe.to(__UpperCamelCase						)
									pipe.set_progress_bar_config(disable=__UpperCamelCase						)

									snake_case__   :      str   				= generator.manual_seed(0						)
									snake_case__   :      List[str]   				= pipe(
									    prompt='first prompt'       ,			image=__UpperCamelCase       ,			text_to_image_strength=0.7_5       ,			generator=__UpperCamelCase       ,			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      __a     (  self						)	->    Optional[Any]:



									'''simple docstring'''







									snake_case__   :      Union[str, Any]   				= VersatileDiffusionDualGuidedPipeline.from_pretrained('shi-labs/versatile-diffusion'						)
									pipe.remove_unused_weights()
									pipe.to(__UpperCamelCase						)
									pipe.set_progress_bar_config(disable=__UpperCamelCase						)

									snake_case__   :      str   				= 'cyberpunk 2077'
									snake_case__   :      Dict   				= load_image(
									    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'						)
									snake_case__   :      Union[str, Any]   				= torch.manual_seed(0						)
									snake_case__   :      Union[str, Any]   				= pipe(
									    prompt=__UpperCamelCase       ,			image=__UpperCamelCase       ,			text_to_image_strength=0.7_5       ,			generator=__UpperCamelCase       ,			guidance_scale=7.5       ,			num_inference_steps=50       ,			output_type='numpy'       ,			).images

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

									assert image.shape == (1, 512, 512, 3)
									snake_case__   :      List[str]   				= np.array([0.0_7_8_7, 0.0_8_4_9, 0.0_8_2_6, 0.0_8_1_2, 0.0_8_0_7, 0.0_7_9_5, 0.0_8_1_8, 0.0_7_9_8, 0.0_7_7_9]						)

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


