



"""simple docstring"""






import gc
import unittest

import numpy as np
import torch

from diffusers import StableDiffusionKDiffusionPipeline
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()

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


					'''simple docstring'''





					def 	snake_case ( self							:						Optional[Any]  )->      Optional[int]:
								# clean up the VRAM after each test
								super().tearDown()
								gc.collect()
								torch.cuda.empty_cache()



					def 	snake_case ( self							:						Tuple  )->      Optional[int]:
								lowerCamelCase__	:	Optional[Any]				=StableDiffusionKDiffusionPipeline.from_pretrained('''CompVis/stable-diffusion-v1-4'''  )
								lowerCamelCase__	:	List[str]				=sd_pipe.to(lowerCamelCase  )
								sd_pipe.set_progress_bar_config(disable=lowerCamelCase  )

								sd_pipe.set_scheduler('''sample_euler'''  )

								lowerCamelCase__	:	List[str]				='''A painting of a squirrel eating a burger'''
								lowerCamelCase__	:	Optional[int]				=torch.manual_seed(0  )
								lowerCamelCase__	:	Union[str, Any]				=sd_pipe([prompt],						generator=lowerCamelCase,						guidance_scale=9.0,						num_inference_steps=20,						output_type='''np'''  )

								lowerCamelCase__	:	int				=output.images

								lowerCamelCase__	:	List[Any]				=image[0, -3:, -3:, -1]

								assert image.shape == (1, 512, 512, 3)
								lowerCamelCase__	:	str				=np.array([0.0_447, 0.0_492, 0.0_468, 0.0_408, 0.0_383, 0.0_408, 0.0_354, 0.0_380, 0.0_339]  )

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



					def 	snake_case ( self							:						List[str]  )->      Tuple:
								lowerCamelCase__	:	int				=StableDiffusionKDiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base'''  )
								lowerCamelCase__	:	str				=sd_pipe.to(lowerCamelCase  )
								sd_pipe.set_progress_bar_config(disable=lowerCamelCase  )

								sd_pipe.set_scheduler('''sample_euler'''  )

								lowerCamelCase__	:	str				='''A painting of a squirrel eating a burger'''
								lowerCamelCase__	:	List[str]				=torch.manual_seed(0  )
								lowerCamelCase__	:	Union[str, Any]				=sd_pipe([prompt],						generator=lowerCamelCase,						guidance_scale=9.0,						num_inference_steps=20,						output_type='''np'''  )

								lowerCamelCase__	:	Union[str, Any]				=output.images

								lowerCamelCase__	:	str				=image[0, -3:, -3:, -1]

								assert image.shape == (1, 512, 512, 3)
								lowerCamelCase__	:	List[Any]				=np.array([0.1_237, 0.1_320, 0.1_438, 0.1_359, 0.1_390, 0.1_132, 0.1_277, 0.1_175, 0.1_112]  )

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




					def 	snake_case ( self							:						Any  )->      int:
								lowerCamelCase__	:	Optional[int]				=StableDiffusionKDiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base'''  )
								lowerCamelCase__	:	Tuple				=sd_pipe.to(lowerCamelCase  )
								sd_pipe.set_progress_bar_config(disable=lowerCamelCase  )

								sd_pipe.set_scheduler('''sample_dpmpp_2m'''  )

								lowerCamelCase__	:	List[Any]				='''A painting of a squirrel eating a burger'''
								lowerCamelCase__	:	Any				=torch.manual_seed(0  )
								lowerCamelCase__	:	Any				=sd_pipe(
								    [prompt],						generator=lowerCamelCase,						guidance_scale=7.5,						num_inference_steps=15,						output_type='''np''',						use_karras_sigmas=lowerCamelCase,						)

								lowerCamelCase__	:	List[str]				=output.images

								lowerCamelCase__	:	Optional[int]				=image[0, -3:, -3:, -1]

								assert image.shape == (1, 512, 512, 3)
								lowerCamelCase__	:	Tuple				=np.array(
								    [0.11_381_689, 0.12_112_921, 0.1_389_457, 0.12_549_606, 0.1_244_964, 0.10_831_517, 0.11_562_866, 0.10_867_816, 0.10_499_048]  )

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









"""simple docstring"""






import colorsys

from PIL import Image  # type: ignore






def   snake_case__     (	__lowerCamelCase       :					float							,	__lowerCamelCase       :					float							,	__lowerCamelCase       :					int			):
			"""simple docstring"""
			lowerCamelCase__	:	Optional[Any]				=x
			lowerCamelCase__	:	Any				=y
			for step in range(__lowerCamelCase			):  # noqa: B007
						lowerCamelCase__	:	List[Any]				=a * a - b * b + x
						lowerCamelCase__	:	Optional[int]				=2 * a * b + y
						lowerCamelCase__	:	Union[str, Any]				=a_new

						# divergence happens for all complex number with an absolute value
						# greater than 4
						if a * a + b * b > 4:
									break
			return step / (max_step - 1)






def   snake_case__     (	__lowerCamelCase       :					float			):
			"""simple docstring"""
			if distance == 1:
						return (0, 0, 0)
			else:
						return (255, 255, 255)






def   snake_case__     (	__lowerCamelCase       :					float			):
			"""simple docstring"""
			if distance == 1:
						return (0, 0, 0)
			else:
						return tuple(round(i * 255			) for i in colorsys.hsv_to_rgb(__lowerCamelCase							,	1							,	1			)			)






def   snake_case__     (	__lowerCamelCase       :					int = 800							,	__lowerCamelCase       :					int = 600							,	__lowerCamelCase       :					float = -0.6							,	__lowerCamelCase       :					float = 0							,	__lowerCamelCase       :					float = 3.2							,	__lowerCamelCase       :					int = 50							,	__lowerCamelCase       :					bool = True							,	):
			"""simple docstring"""
			lowerCamelCase__	:	Optional[Any]				=Image.new('''RGB'''							,	(image_width, image_height)			)
			lowerCamelCase__	:	Optional[int]				=img.load()

			# loop through the image-coordinates
			for image_x in range(__lowerCamelCase			):
						for image_y in range(__lowerCamelCase			):
									# determine the figure-coordinates based on the image-coordinates
									lowerCamelCase__	:	Optional[Any]				=figure_width / image_width * image_height
									lowerCamelCase__	:	Dict				=figure_center_x + (image_x / image_width - 0.5) * figure_width
									lowerCamelCase__	:	Optional[int]				=figure_center_y + (image_y / image_height - 0.5) * figure_height

									lowerCamelCase__	:	Any				=get_distance(__lowerCamelCase							,	__lowerCamelCase							,	__lowerCamelCase			)

									# color the corresponding pixel based on the selected coloring-function
									if use_distance_color_coding:
												lowerCamelCase__	:	int				=get_color_coded_rgb(__lowerCamelCase			)
									else:
												lowerCamelCase__	:	Optional[int]				=get_black_and_white_rgb(__lowerCamelCase			)

			return img


if __name__ == "__main__":
		import doctest

		doctest.testmod()

		# colored version, full figure
		_lowercase				:				Optional[Any]   		=		get_image()

		# uncomment for colored version, different section, zoomed in
		# img = get_image(figure_center_x = -0.6, figure_center_y = -0.4,
		# figure_width = 0.8)

		# uncomment for black and white version, full figure
		# img = get_image(use_distance_color_coding = False)

		# uncomment to save the image
		# img.save("mandelbrot.png")

		img.show()




