

from typing import Any, Callable, Dict, List, Optional, Union

import torch
from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DiffusionPipeline,
    LMSDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionPipeline,
    UNetaDConditionModel,
)
from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker


lowerCAmelCase__	   = """CompVis/stable-diffusion-v1-1"""
lowerCAmelCase__	   = """CompVis/stable-diffusion-v1-2"""
lowerCAmelCase__	   = """CompVis/stable-diffusion-v1-3"""
lowerCAmelCase__	   = """CompVis/stable-diffusion-v1-4"""

class 					a__						(					__UpperCAmelCase			):
			"""simple docstring"""







			def __init__(    self      ,      lowercase      ,      lowercase      ,      lowercase      ,      lowercase      ,      lowercase      ,      lowercase      ,      lowercase      ,      lowercase = True      ,      )  ->						Optional[int]:
							'''simple docstring'''






							super()._init_()

							A__        =			StableDiffusionPipeline.from_pretrained(lowercase       )
							A__        =			StableDiffusionPipeline.from_pretrained(lowercase       )
							A__        =			StableDiffusionPipeline.from_pretrained(lowercase       )
							A__        =			StableDiffusionPipeline(
							    vae=lowercase      ,      text_encoder=lowercase      ,      tokenizer=lowercase      ,      unet=lowercase      ,      scheduler=lowercase      ,      safety_checker=lowercase      ,      feature_extractor=lowercase      ,      requires_safety_checker=lowercase      ,      )

							self.register_modules(pipelinea=self.pipea      ,      pipelinea=self.pipea      ,      pipelinea=self.pipea      ,      pipelinea=self.pipea       )


			@property
			def      UpperCamelCase		(    self       )  ->						Optional[Any]:
							'''simple docstring'''






							return {k: getattr(self      ,      lowercase       ) for k in self.config.keys() if not k.startswith("_"       )}


			def      UpperCamelCase		(    self      ,      lowercase = "auto"       )  ->						int:
							'''simple docstring'''






							if slice_size == "auto":
											# half the attention head size is usually a good trade-off between
											# speed and memory
											A__        =			self.unet.config.attention_head_dim // 2
							self.unet.set_attention_slice(lowercase       )


			def      UpperCamelCase		(    self       )  ->						Dict:
							'''simple docstring'''






							self.enable_attention_slicing(lowercase       )


			@torch.no_grad()
			def      UpperCamelCase		(    self      ,      lowercase      ,      lowercase = 512      ,      lowercase = 512      ,      lowercase = 50      ,      lowercase = 7.5      ,      lowercase = None      ,      lowercase = 1      ,      lowercase = 0.0      ,      lowercase = None      ,      lowercase = None      ,      lowercase = "pil"      ,      lowercase = True      ,      lowercase = None      ,      lowercase = 1      ,      **lowercase      ,      )  ->						List[str]:
							'''simple docstring'''






							return self.pipea(
							    prompt=lowercase      ,      height=lowercase      ,      width=lowercase      ,      num_inference_steps=lowercase      ,      guidance_scale=lowercase      ,      negative_prompt=lowercase      ,      num_images_per_prompt=lowercase      ,      eta=lowercase      ,      generator=lowercase      ,      latents=lowercase      ,      output_type=lowercase      ,      return_dict=lowercase      ,      callback=lowercase      ,      callback_steps=lowercase      ,      **lowercase      ,      )


			@torch.no_grad()
			def      UpperCamelCase		(    self      ,      lowercase      ,      lowercase = 512      ,      lowercase = 512      ,      lowercase = 50      ,      lowercase = 7.5      ,      lowercase = None      ,      lowercase = 1      ,      lowercase = 0.0      ,      lowercase = None      ,      lowercase = None      ,      lowercase = "pil"      ,      lowercase = True      ,      lowercase = None      ,      lowercase = 1      ,      **lowercase      ,      )  ->						str:
							'''simple docstring'''






							return self.pipea(
							    prompt=lowercase      ,      height=lowercase      ,      width=lowercase      ,      num_inference_steps=lowercase      ,      guidance_scale=lowercase      ,      negative_prompt=lowercase      ,      num_images_per_prompt=lowercase      ,      eta=lowercase      ,      generator=lowercase      ,      latents=lowercase      ,      output_type=lowercase      ,      return_dict=lowercase      ,      callback=lowercase      ,      callback_steps=lowercase      ,      **lowercase      ,      )


			@torch.no_grad()
			def      UpperCamelCase		(    self      ,      lowercase      ,      lowercase = 512      ,      lowercase = 512      ,      lowercase = 50      ,      lowercase = 7.5      ,      lowercase = None      ,      lowercase = 1      ,      lowercase = 0.0      ,      lowercase = None      ,      lowercase = None      ,      lowercase = "pil"      ,      lowercase = True      ,      lowercase = None      ,      lowercase = 1      ,      **lowercase      ,      )  ->						Tuple:
							'''simple docstring'''






							return self.pipea(
							    prompt=lowercase      ,      height=lowercase      ,      width=lowercase      ,      num_inference_steps=lowercase      ,      guidance_scale=lowercase      ,      negative_prompt=lowercase      ,      num_images_per_prompt=lowercase      ,      eta=lowercase      ,      generator=lowercase      ,      latents=lowercase      ,      output_type=lowercase      ,      return_dict=lowercase      ,      callback=lowercase      ,      callback_steps=lowercase      ,      **lowercase      ,      )


			@torch.no_grad()
			def      UpperCamelCase		(    self      ,      lowercase      ,      lowercase = 512      ,      lowercase = 512      ,      lowercase = 50      ,      lowercase = 7.5      ,      lowercase = None      ,      lowercase = 1      ,      lowercase = 0.0      ,      lowercase = None      ,      lowercase = None      ,      lowercase = "pil"      ,      lowercase = True      ,      lowercase = None      ,      lowercase = 1      ,      **lowercase      ,      )  ->						str:
							'''simple docstring'''






							return self.pipea(
							    prompt=lowercase      ,      height=lowercase      ,      width=lowercase      ,      num_inference_steps=lowercase      ,      guidance_scale=lowercase      ,      negative_prompt=lowercase      ,      num_images_per_prompt=lowercase      ,      eta=lowercase      ,      generator=lowercase      ,      latents=lowercase      ,      output_type=lowercase      ,      return_dict=lowercase      ,      callback=lowercase      ,      callback_steps=lowercase      ,      **lowercase      ,      )


			@torch.no_grad()
			def      UpperCamelCase		(    self      ,      lowercase      ,      lowercase = 512      ,      lowercase = 512      ,      lowercase = 50      ,      lowercase = 7.5      ,      lowercase = None      ,      lowercase = 1      ,      lowercase = 0.0      ,      lowercase = None      ,      lowercase = None      ,      lowercase = "pil"      ,      lowercase = True      ,      lowercase = None      ,      lowercase = 1      ,      **lowercase      ,      )  ->						Dict:
							'''simple docstring'''






							A__        =			"cuda" if torch.cuda.is_available() else "cpu"
							self.to(lowercase       )

							# Checks if the height and width are divisible by 8 or not
							if height % 8 != 0 or width % 8 != 0:
											raise ValueError(F'`height` and `width` must be divisible by 8 but are {height} and {width}.'       )

							# Get first result from Stable Diffusion Checkpoint v1.1
							A__        =			self.textaimg_sda_a(
							    prompt=lowercase      ,      height=lowercase      ,      width=lowercase      ,      num_inference_steps=lowercase      ,      guidance_scale=lowercase      ,      negative_prompt=lowercase      ,      num_images_per_prompt=lowercase      ,      eta=lowercase      ,      generator=lowercase      ,      latents=lowercase      ,      output_type=lowercase      ,      return_dict=lowercase      ,      callback=lowercase      ,      callback_steps=lowercase      ,      **lowercase      ,      )

							# Get first result from Stable Diffusion Checkpoint v1.2
							A__        =			self.textaimg_sda_a(
							    prompt=lowercase      ,      height=lowercase      ,      width=lowercase      ,      num_inference_steps=lowercase      ,      guidance_scale=lowercase      ,      negative_prompt=lowercase      ,      num_images_per_prompt=lowercase      ,      eta=lowercase      ,      generator=lowercase      ,      latents=lowercase      ,      output_type=lowercase      ,      return_dict=lowercase      ,      callback=lowercase      ,      callback_steps=lowercase      ,      **lowercase      ,      )

							# Get first result from Stable Diffusion Checkpoint v1.3
							A__        =			self.textaimg_sda_a(
							    prompt=lowercase      ,      height=lowercase      ,      width=lowercase      ,      num_inference_steps=lowercase      ,      guidance_scale=lowercase      ,      negative_prompt=lowercase      ,      num_images_per_prompt=lowercase      ,      eta=lowercase      ,      generator=lowercase      ,      latents=lowercase      ,      output_type=lowercase      ,      return_dict=lowercase      ,      callback=lowercase      ,      callback_steps=lowercase      ,      **lowercase      ,      )

							# Get first result from Stable Diffusion Checkpoint v1.4
							A__        =			self.textaimg_sda_a(
							    prompt=lowercase      ,      height=lowercase      ,      width=lowercase      ,      num_inference_steps=lowercase      ,      guidance_scale=lowercase      ,      negative_prompt=lowercase      ,      num_images_per_prompt=lowercase      ,      eta=lowercase      ,      generator=lowercase      ,      latents=lowercase      ,      output_type=lowercase      ,      return_dict=lowercase      ,      callback=lowercase      ,      callback_steps=lowercase      ,      **lowercase      ,      )

							# Get all result images into a single list and pass it via StableDiffusionPipelineOutput for final result
							return StableDiffusionPipelineOutput([resa[0], resa[0], resa[0], resa[0]]       )









from __future__ import annotations



def 					lowerCAmelCase__							(	SCREAMING_SNAKE_CASE_:							list[int]	,       SCREAMING_SNAKE_CASE_:							list[int]	,       SCREAMING_SNAKE_CASE_:							list[int]	,       SCREAMING_SNAKE_CASE_:							list[list[str]]	,       SCREAMING_SNAKE_CASE_:							int	,       )			->      None:




				'''simple docstring'''





				A__        =			len(SCREAMING_SNAKE_CASE_      )

				# If row is equal to the size of the board it means there are a queen in each row in
				# the current board (possible_board)
				if row == n:
								# We convert the variable possible_board that looks like this: [1, 3, 0, 2] to
								# this: ['. Q . . ', '. . . Q ', 'Q . . . ', '. . Q . ']
								boards.append([". " * i + "Q " + ". " * (n - 1 - i) for i in possible_board]      )
								return

				# We iterate each column in the row to find all possible results in each row
				for col in range(SCREAMING_SNAKE_CASE_      ):
								# We apply that we learned previously. First we check that in the current board
								# (possible_board) there are not other same value because if there is it means
								# that there are a collision in vertical. Then we apply the two formulas we
								# learned before:
								#
								# 45º: y - x = b or 45: row - col = b
								# 135º: y + x = b or row + col = b.
								#
								# And we verify if the results of this two formulas not exist in their variables
								# respectively.  (diagonal_right_collisions, diagonal_left_collisions)
								#
								# If any or these are True it means there is a collision so we continue to the
								# next value in the for loop.
								if (
								    col in possible_board
								    or row - col in diagonal_right_collisions
								    or row + col in diagonal_left_collisions
								):
												continue

								# If it is False we call dfs function again and we update the inputs
								depth_first_search(
								    [*possible_board, col]	,       [*diagonal_right_collisions, row - col]	,       [*diagonal_left_collisions, row + col]	,       SCREAMING_SNAKE_CASE_	,       SCREAMING_SNAKE_CASE_	,       )



def 					lowerCAmelCase__							(	SCREAMING_SNAKE_CASE_:							int      )			->      None:




				'''simple docstring'''





				A__        =			[]
				depth_first_search([]	,       []	,       []	,       SCREAMING_SNAKE_CASE_	,       SCREAMING_SNAKE_CASE_      )

				# Print all the boards
				for board in boards:
								for column in board:
												print(SCREAMING_SNAKE_CASE_      )
								print(""      )

				print(len(SCREAMING_SNAKE_CASE_      )	,       "solutions were found."      )


if __name__ == "__main__":
	import doctest

	doctest.testmod()
	n_queens_solution(4)

