


'''simple docstring'''


import math
from typing import Callable, List, Optional, Union

import numpy as np
import PIL
import torch
from PIL import Image
from transformers import CLIPTextModel, CLIPTokenizer

from diffusers.models import AutoencoderKL, UNetaDConditionModel
from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_upscale import StableDiffusionUpscalePipeline
from diffusers.schedulers import DDIMScheduler, DDPMScheduler, LMSDiscreteScheduler, PNDMScheduler







def 			snake_case_   (  SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__=[]     ):







				"""simple docstring"""





				_SCREAMING_SNAKE_CASE :       Any     		=      size[0] - overlap_pixels * 2
				_SCREAMING_SNAKE_CASE :       int     		=      size[1] - overlap_pixels * 2
				for letter in ["l", "r"]:
								if letter in remove_borders:
												size_x += overlap_pixels
				for letter in ["t", "b"]:
								if letter in remove_borders:
												size_y += overlap_pixels
				_SCREAMING_SNAKE_CASE :       Any     		=      np.ones((size_y, size_x)							,      dtype=np.uinta     ) * 255
				_SCREAMING_SNAKE_CASE :       Any     		=      np.pad(SCREAMING_SNAKE_CASE__							,      mode="""linear_ramp"""							,      pad_width=SCREAMING_SNAKE_CASE__							,      end_values=0     )

				if "l" in remove_borders:
								_SCREAMING_SNAKE_CASE :       Any     		=      mask[:, overlap_pixels : mask.shape[1]]
				if "r" in remove_borders:
								_SCREAMING_SNAKE_CASE :       Dict     		=      mask[:, 0 : mask.shape[1] - overlap_pixels]
				if "t" in remove_borders:
								_SCREAMING_SNAKE_CASE :       Optional[int]     		=      mask[overlap_pixels : mask.shape[0], :]
				if "b" in remove_borders:
								_SCREAMING_SNAKE_CASE :       List[Any]     		=      mask[0 : mask.shape[0] - overlap_pixels, :]
				return mask







def 			snake_case_   (  SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__     ):







				"""simple docstring"""





				return max(SCREAMING_SNAKE_CASE__							,      min(SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__     )     )







def 			snake_case_   (  SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__     ):







				"""simple docstring"""





				return (
				    clamp(rect[0]							,      min[0]							,      max[0]     ),
				    clamp(rect[1]							,      min[1]							,      max[1]     ),
				    clamp(rect[2]							,      min[0]							,      max[0]     ),
				    clamp(rect[3]							,      min[1]							,      max[1]     ),
				)







def 			snake_case_   (  SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__     ):







				"""simple docstring"""





				_SCREAMING_SNAKE_CASE :       Optional[int]     		=      list(SCREAMING_SNAKE_CASE__     )
				rect[0] -= overlap
				rect[1] -= overlap
				rect[2] += overlap
				rect[3] += overlap
				_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      clamp_rect(SCREAMING_SNAKE_CASE__							,      [0, 0]							,      [image_size[0], image_size[1]]     )
				return rect







def 			snake_case_   (  SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__     ):







				"""simple docstring"""





				_SCREAMING_SNAKE_CASE :       List[Any]     		=      Image.new("""RGB"""							,      (tile.size[0] + original_slice, tile.size[1])     )
				result.paste(
				    original_image.resize((tile.size[0], tile.size[1])							,      Image.BICUBIC     ).crop(
				        (slice_x, 0, slice_x + original_slice, tile.size[1])     )							,      (0, 0)							,      )
				result.paste(SCREAMING_SNAKE_CASE__							,      (original_slice, 0)     )
				return result







def 			snake_case_   (  SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__     ):







				"""simple docstring"""





				_SCREAMING_SNAKE_CASE :       List[Any]     		=      (original_image_slice * 4, 0, tile.size[0], tile.size[1])
				_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      tile.crop(SCREAMING_SNAKE_CASE__     )
				return tile







def 			snake_case_   (  SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__     ):







				"""simple docstring"""





				_SCREAMING_SNAKE_CASE :       Tuple     		=      n % d
				return n - divisor





class     lowercase__  (						_snake_case					):





							'''simple docstring'''


							def __init__(    self    ,					__snake_case    ,					__snake_case    ,					__snake_case    ,					__snake_case    ,					__snake_case    ,					__snake_case    ,					__snake_case = 350    ,					):
											super().__init__(
											    vae=__snake_case    ,					text_encoder=__snake_case    ,					tokenizer=__snake_case    ,					unet=__snake_case    ,					low_res_scheduler=__snake_case    ,					scheduler=__snake_case    ,					max_noise_level=__snake_case    ,					)






							def UpperCAmelCase_	(    self    ,					__snake_case    ,					__snake_case    ,					__snake_case    ,					__snake_case    ,					__snake_case    ,					__snake_case    ,					__snake_case    ,					**__snake_case  ):
											torch.manual_seed(0  )
											_SCREAMING_SNAKE_CASE :       int     		=      (
											    min(image.size[0] - (tile_size + original_image_slice)    ,					x * tile_size  ),
											    min(image.size[1] - (tile_size + original_image_slice)    ,					y * tile_size  ),
											    min(image.size[0]    ,					(x + 1) * tile_size  ),
											    min(image.size[1]    ,					(y + 1) * tile_size  ),
											)
											_SCREAMING_SNAKE_CASE :       Dict     		=      add_overlap_rect(__snake_case    ,					__snake_case    ,					image.size  )
											_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      image.crop(__snake_case  )
											_SCREAMING_SNAKE_CASE :       List[str]     		=      ((crop_rect[0] + ((crop_rect[2] - crop_rect[0]) / 2)) / image.size[0]) * tile.size[0]
											_SCREAMING_SNAKE_CASE :       Dict     		=      translated_slice_x - (original_image_slice / 2)
											_SCREAMING_SNAKE_CASE :       List[Any]     		=      max(0    ,					__snake_case  )
											_SCREAMING_SNAKE_CASE :       Optional[int]     		=      squeeze_tile(__snake_case    ,					__snake_case    ,					__snake_case    ,					__snake_case  )
											_SCREAMING_SNAKE_CASE :       Optional[int]     		=      to_input.size
											_SCREAMING_SNAKE_CASE :       List[Any]     		=      to_input.resize((tile_size, tile_size)    ,					Image.BICUBIC  )
											_SCREAMING_SNAKE_CASE :       Any     		=      super(__snake_case    ,					self  ).__call__(image=__snake_case    ,					**__snake_case  ).images[0]
											_SCREAMING_SNAKE_CASE :       Optional[int]     		=      upscaled_tile.resize((orig_input_size[0] * 4, orig_input_size[1] * 4)    ,					Image.BICUBIC  )
											_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      unsqueeze_tile(__snake_case    ,					__snake_case  )
											_SCREAMING_SNAKE_CASE :       Any     		=      upscaled_tile.resize((tile.size[0] * 4, tile.size[1] * 4)    ,					Image.BICUBIC  )
											_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      []
											if x == 0:
															remove_borders.append("""l"""  )
											elif crop_rect[2] == image.size[0]:
															remove_borders.append("""r"""  )
											if y == 0:
															remove_borders.append("""t"""  )
											elif crop_rect[3] == image.size[1]:
															remove_borders.append("""b"""  )
											_SCREAMING_SNAKE_CASE :       str     		=      Image.fromarray(
											    make_transparency_mask(
											        (upscaled_tile.size[0], upscaled_tile.size[1])    ,					tile_border * 4    ,					remove_borders=__snake_case  )    ,					mode="""L"""    ,					)
											final_image.paste(
											    __snake_case    ,					(crop_rect_with_overlap[0] * 4, crop_rect_with_overlap[1] * 4)    ,					__snake_case  )






							@torch.no_grad()
							def __call__(    self    ,					__snake_case    ,					__snake_case    ,					__snake_case = 75    ,					__snake_case = 9.0    ,					__snake_case = 50    ,					__snake_case = None    ,					__snake_case = 1    ,					__snake_case = 0.0    ,					__snake_case = None    ,					__snake_case = None    ,					__snake_case = None    ,					__snake_case = 1    ,					__snake_case = 128    ,					__snake_case = 32    ,					__snake_case = 32    ,					):
											_SCREAMING_SNAKE_CASE :       Tuple     		=      Image.new("""RGB"""    ,					(image.size[0] * 4, image.size[1] * 4)  )
											_SCREAMING_SNAKE_CASE :       Optional[int]     		=      math.ceil(image.size[0] / tile_size  )
											_SCREAMING_SNAKE_CASE :       Optional[int]     		=      math.ceil(image.size[1] / tile_size  )
											_SCREAMING_SNAKE_CASE :       int     		=      tcx * tcy
											_SCREAMING_SNAKE_CASE :       Optional[int]     		=      0
											for y in range(__snake_case  ):
															for x in range(__snake_case  ):
																			self._process_tile(
																			    __snake_case    ,					__snake_case    ,					__snake_case    ,					__snake_case    ,					__snake_case    ,					__snake_case    ,					__snake_case    ,					prompt=__snake_case    ,					num_inference_steps=__snake_case    ,					guidance_scale=__snake_case    ,					noise_level=__snake_case    ,					negative_prompt=__snake_case    ,					num_images_per_prompt=__snake_case    ,					eta=__snake_case    ,					generator=__snake_case    ,					latents=__snake_case    ,					)
																			current_count += 1
																			if callback is not None:
																							callback({"""progress""": current_count / total_tile_count, """image""": final_image}  )
											return final_image







def 			snake_case_   (  ):







				"""simple docstring"""





				_SCREAMING_SNAKE_CASE :       Dict     		=      """stabilityai/stable-diffusion-x4-upscaler"""
				_SCREAMING_SNAKE_CASE :       int     		=      StableDiffusionTiledUpscalePipeline.from_pretrained(SCREAMING_SNAKE_CASE__							,      revision="""fp16"""							,      torch_dtype=torch.floataa     )
				_SCREAMING_SNAKE_CASE :       Optional[int]     		=      pipe.to("""cuda"""     )
				_SCREAMING_SNAKE_CASE :       List[Any]     		=      Image.open("""../../docs/source/imgs/diffusers_library.jpg"""     )

				def callback(SCREAMING_SNAKE_CASE__     ):
								print(f"""progress: {obj["progress"]:.4f}"""     )
								obj["image"].save("""diffusers_library_progress.jpg"""     )

				_SCREAMING_SNAKE_CASE :       List[str]     		=      pipe(image=SCREAMING_SNAKE_CASE__							,      prompt="""Black font, white background, vector"""							,      noise_level=40							,      callback=SCREAMING_SNAKE_CASE__     )
				final_image.save("""diffusers_library.jpg"""     )


if __name__ == "__main__":
				main()








'''simple docstring'''


def 			snake_case_   (  SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__     ):







				"""simple docstring"""





				return int((input_a, input_a).count(0     ) != 0     )







def 			snake_case_   (  ):







				"""simple docstring"""





				assert nand_gate(0							,      0     ) == 1
				assert nand_gate(0							,      1     ) == 1
				assert nand_gate(1							,      0     ) == 1
				assert nand_gate(1							,      1     ) == 0


if __name__ == "__main__":
				print(nand_gate(0, 0))
				print(nand_gate(0, 1))
				print(nand_gate(1, 0))
				print(nand_gate(1, 1))




