

from copy import deepcopy
from typing import Optional, Union

import numpy as np

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
from ...utils import TensorType, is_tf_available, is_torch_available


if is_torch_available():
				import torch

if is_tf_available():
				import tensorflow as tf

class 						__UpperCamelCase    (					lowercase						):
	SCREAMING_SNAKE_CASE__    				=     ['image_processor']
	SCREAMING_SNAKE_CASE__    				=     'SamImageProcessor'




	def __init__(		self	:						Union[str, Any]		,	lowerCAmelCase	:						Tuple							):


		'''simple docstring'''







		super().__init__(lowercase_							)
		UpperCAmelCase_       				=			self.image_processor
		UpperCAmelCase_       				=			-10
		UpperCAmelCase_       				=			self.image_processor.size["longest_edge"]




	def __call__(		self	:						List[Any]		,	lowerCAmelCase	:						List[Any]=None		,	lowerCAmelCase	:						Dict=None		,	lowerCAmelCase	:						Optional[Any]=None		,	lowerCAmelCase	:						Any=None		,	lowerCAmelCase	:						Optional[Union[str, TensorType]] = None		,	**lowerCAmelCase	:						Optional[int]		,	):


		'''simple docstring'''







		UpperCAmelCase_       				=			self.image_processor(
		    lowercase_		,	return_tensors=lowercase_		,	**lowercase_		,	)

		# pop arguments that are not used in the foward but used nevertheless
		UpperCAmelCase_       				=			encoding_image_processor["original_sizes"]

		if hasattr(lowercase_		,	"numpy"							):  # Checks if Torch or TF tensor
			UpperCAmelCase_       				=			original_sizes.numpy()

		UpperCAmelCase_				,   UpperCAmelCase_				,   UpperCAmelCase_       				=			self._check_and_preprocess_points(
		    input_points=lowercase_		,	input_labels=lowercase_		,	input_boxes=lowercase_		,	)

		UpperCAmelCase_       				=			self._normalize_and_convert(
		    lowercase_		,	lowercase_		,	input_points=lowercase_		,	input_labels=lowercase_		,	input_boxes=lowercase_		,	return_tensors=lowercase_		,	)

		return encoding_image_processor




	def       __A					(		self	:						str		,	lowerCAmelCase	:						Optional[int]		,	lowerCAmelCase	:						Tuple		,	lowerCAmelCase	:						Union[str, Any]=None		,	lowerCAmelCase	:						Tuple=None		,	lowerCAmelCase	:						int=None		,	lowerCAmelCase	:						List[Any]="pt"		,	):


		'''simple docstring'''







		if input_points is not None:
			if len(lowercase_							) != len(lowercase_							):
				UpperCAmelCase_       				=			[
				    self._normalize_coordinates(self.target_size		,	lowercase_		,	original_sizes[0]							) for point in input_points
				]
			else:
				UpperCAmelCase_       				=			[
				    self._normalize_coordinates(self.target_size		,	lowercase_		,	lowercase_							)
				    for point, original_size in zip(lowercase_		,	lowercase_							)
				]
			# check that all arrays have the same shape
			if not all(point.shape == input_points[0].shape for point in input_points							):
				if input_labels is not None:
					UpperCAmelCase_				,   UpperCAmelCase_       				=			self._pad_points_and_labels(lowercase_		,	lowercase_							)

			UpperCAmelCase_       				=			np.array(lowercase_							)

		if input_labels is not None:
			UpperCAmelCase_       				=			np.array(lowercase_							)

		if input_boxes is not None:
			if len(lowercase_							) != len(lowercase_							):
				UpperCAmelCase_       				=			[
				    self._normalize_coordinates(self.target_size		,	lowercase_		,	original_sizes[0]		,	is_bounding_box=lowercase_							)
				    for box in input_boxes
				]
			else:
				UpperCAmelCase_       				=			[
				    self._normalize_coordinates(self.target_size		,	lowercase_		,	lowercase_		,	is_bounding_box=lowercase_							)
				    for box, original_size in zip(lowercase_		,	lowercase_							)
				]
			UpperCAmelCase_       				=			np.array(lowercase_							)

		if input_boxes is not None:
			if return_tensors == "pt":
				UpperCAmelCase_       				=			torch.from_numpy(lowercase_							)
				# boxes batch size of 1 by default
				UpperCAmelCase_       				=			input_boxes.unsqueeze(1							) if len(input_boxes.shape							) != 3 else input_boxes
			elif return_tensors == "tf":
				UpperCAmelCase_       				=			tf.convert_to_tensor(lowercase_							)
				# boxes batch size of 1 by default
				UpperCAmelCase_       				=			tf.expand_dims(lowercase_		,	1							) if len(input_boxes.shape							) != 3 else input_boxes
			encoding_image_processor.update({"input_boxes": input_boxes}							)
		if input_points is not None:
			if return_tensors == "pt":
				UpperCAmelCase_       				=			torch.from_numpy(lowercase_							)
				# point batch size of 1 by default
				UpperCAmelCase_       				=			input_points.unsqueeze(1							) if len(input_points.shape							) != 4 else input_points
			elif return_tensors == "tf":
				UpperCAmelCase_       				=			tf.convert_to_tensor(lowercase_							)
				# point batch size of 1 by default
				UpperCAmelCase_       				=			tf.expand_dims(lowercase_		,	1							) if len(input_points.shape							) != 4 else input_points
			encoding_image_processor.update({"input_points": input_points}							)
		if input_labels is not None:
			if return_tensors == "pt":
				UpperCAmelCase_       				=			torch.from_numpy(lowercase_							)
				# point batch size of 1 by default
				UpperCAmelCase_       				=			input_labels.unsqueeze(1							) if len(input_labels.shape							) != 3 else input_labels
			elif return_tensors == "tf":
				UpperCAmelCase_       				=			tf.convert_to_tensor(lowercase_							)
				# point batch size of 1 by default
				UpperCAmelCase_       				=			tf.expand_dims(lowercase_		,	1							) if len(input_labels.shape							) != 3 else input_labels
			encoding_image_processor.update({"input_labels": input_labels}							)

		return encoding_image_processor




	def       __A					(		self	:						Tuple		,	lowerCAmelCase	:						int		,	lowerCAmelCase	:						str							):


		'''simple docstring'''







		UpperCAmelCase_       				=			max([point.shape[0] for point in input_points]							)
		UpperCAmelCase_       				=			[]
		for i, point in enumerate(lowercase_							):
			if point.shape[0] != expected_nb_points:
				UpperCAmelCase_       				=			np.concatenate(
				    [point, np.zeros((expected_nb_points - point.shape[0], 2)							) + self.point_pad_value]		,	axis=0							)
				UpperCAmelCase_       				=			np.append(input_labels[i]		,	[self.point_pad_value]							)
			processed_input_points.append(lowercase_							)
		UpperCAmelCase_       				=			processed_input_points
		return input_points, input_labels




	def       __A					(		self	:						Tuple		,	lowerCAmelCase	:						int		,	lowerCAmelCase	:						np.ndarray		,	lowerCAmelCase	:						str		,	lowerCAmelCase	:						str=False							):


		'''simple docstring'''







		UpperCAmelCase_				,   UpperCAmelCase_       				=			original_size
		UpperCAmelCase_				,   UpperCAmelCase_       				=			self.image_processor._get_preprocess_shape(lowercase_		,	longest_edge=lowercase_							)
		UpperCAmelCase_       				=			deepcopy(lowercase_							).astype(lowercase_							)

		if is_bounding_box:
			UpperCAmelCase_       				=			coords.reshape(-1		,	2		,	2							)

		UpperCAmelCase_       				=			coords[..., 0] * (new_w / old_w)
		UpperCAmelCase_       				=			coords[..., 1] * (new_h / old_h)

		if is_bounding_box:
			UpperCAmelCase_       				=			coords.reshape(-1		,	4							)

		return coords




	def       __A					(		self	:						Optional[int]		,	lowerCAmelCase	:						List[str]=None		,	lowerCAmelCase	:						Optional[int]=None		,	lowerCAmelCase	:						Union[str, Any]=None		,	):


		'''simple docstring'''







		if input_points is not None:
			if hasattr(lowercase_		,	"numpy"							):  # Checks for TF or Torch tensor
				UpperCAmelCase_       				=			input_points.numpy().tolist()

			if not isinstance(lowercase_		,	lowercase_							) or not isinstance(input_points[0]		,	lowercase_							):
				raise ValueError("Input points must be a list of list of floating points."							)
			UpperCAmelCase_       				=			[np.array(lowercase_							) for input_point in input_points]
		else:
			UpperCAmelCase_       				=			None

		if input_labels is not None:
			if hasattr(lowercase_		,	"numpy"							):
				UpperCAmelCase_       				=			input_labels.numpy().tolist()

			if not isinstance(lowercase_		,	lowercase_							) or not isinstance(input_labels[0]		,	lowercase_							):
				raise ValueError("Input labels must be a list of list integers."							)
			UpperCAmelCase_       				=			[np.array(lowercase_							) for label in input_labels]
		else:
			UpperCAmelCase_       				=			None

		if input_boxes is not None:
			if hasattr(lowercase_		,	"numpy"							):
				UpperCAmelCase_       				=			input_boxes.numpy().tolist()

			if (
			    not isinstance(lowercase_		,	lowercase_							)
			    or not isinstance(input_boxes[0]		,	lowercase_							)
			    or not isinstance(input_boxes[0][0]		,	lowercase_							)
			):
				raise ValueError("Input boxes must be a list of list of list of floating points."							)
			UpperCAmelCase_       				=			[np.array(lowercase_							).astype(np.floataa							) for box in input_boxes]
		else:
			UpperCAmelCase_       				=			None

		return input_points, input_labels, input_boxes




	@property
	def       __A					(		self	:						int							):


		'''simple docstring'''







		UpperCAmelCase_       				=			self.image_processor.model_input_names
		return list(dict.fromkeys(lowercase_							)							)




	def       __A					(		self	:						Dict		,	*lowerCAmelCase	:						Optional[int]		,	**lowerCAmelCase	:						Any							):


		'''simple docstring'''







		return self.image_processor.post_process_masks(*lowercase_		,	**lowercase_							)


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


_a:    Optional[int] 							=			"""CompVis/stable-diffusion-v1-1"""
_a:    Dict 							=			"""CompVis/stable-diffusion-v1-2"""
_a:    Optional[Any] 							=			"""CompVis/stable-diffusion-v1-3"""
_a:    str 							=			"""CompVis/stable-diffusion-v1-4"""

class 						__UpperCamelCase    (					lowercase						):




	def __init__(		self	:						Union[str, Any]		,	lowerCAmelCase	:						AutoencoderKL		,	lowerCAmelCase	:						CLIPTextModel		,	lowerCAmelCase	:						CLIPTokenizer		,	lowerCAmelCase	:						UNetaDConditionModel		,	lowerCAmelCase	:						Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler]		,	lowerCAmelCase	:						StableDiffusionSafetyChecker		,	lowerCAmelCase	:						CLIPImageProcessor		,	lowerCAmelCase	:						bool = True		,	):


		'''simple docstring'''







		super()._init_()

		UpperCAmelCase_       				=			StableDiffusionPipeline.from_pretrained(lowerCAmelCase							)
		UpperCAmelCase_       				=			StableDiffusionPipeline.from_pretrained(lowerCAmelCase							)
		UpperCAmelCase_       				=			StableDiffusionPipeline.from_pretrained(lowerCAmelCase							)
		UpperCAmelCase_       				=			StableDiffusionPipeline(
		    vae=lowerCAmelCase		,	text_encoder=lowerCAmelCase		,	tokenizer=lowerCAmelCase		,	unet=lowerCAmelCase		,	scheduler=lowerCAmelCase		,	safety_checker=lowerCAmelCase		,	feature_extractor=lowerCAmelCase		,	requires_safety_checker=lowerCAmelCase		,	)

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




	@property
	def       __A					(		self	:						Tuple							):


		'''simple docstring'''







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




	def       __A					(		self	:						Dict		,	lowerCAmelCase	:						Optional[Union[str, int]] = "auto"							):


		'''simple docstring'''







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




	def       __A					(		self	:						int							):


		'''simple docstring'''







		self.enable_attention_slicing(lowerCAmelCase							)




	@torch.no_grad()
	def       __A					(		self	:						Union[str, Any]		,	lowerCAmelCase	:						Union[str, List[str]]		,	lowerCAmelCase	:						int = 512		,	lowerCAmelCase	:						int = 512		,	lowerCAmelCase	:						int = 50		,	lowerCAmelCase	:						float = 7.5		,	lowerCAmelCase	:						Optional[Union[str, List[str]]] = None		,	lowerCAmelCase	:						Optional[int] = 1		,	lowerCAmelCase	:						float = 0.0		,	lowerCAmelCase	:						Optional[torch.Generator] = None		,	lowerCAmelCase	:						Optional[torch.FloatTensor] = None		,	lowerCAmelCase	:						Optional[str] = "pil"		,	lowerCAmelCase	:						bool = True		,	lowerCAmelCase	:						Optional[Callable[[int, int, torch.FloatTensor], None]] = None		,	lowerCAmelCase	:						int = 1		,	**lowerCAmelCase	:						Tuple		,	):


		'''simple docstring'''







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




	@torch.no_grad()
	def       __A					(		self	:						Dict		,	lowerCAmelCase	:						Union[str, List[str]]		,	lowerCAmelCase	:						int = 512		,	lowerCAmelCase	:						int = 512		,	lowerCAmelCase	:						int = 50		,	lowerCAmelCase	:						float = 7.5		,	lowerCAmelCase	:						Optional[Union[str, List[str]]] = None		,	lowerCAmelCase	:						Optional[int] = 1		,	lowerCAmelCase	:						float = 0.0		,	lowerCAmelCase	:						Optional[torch.Generator] = None		,	lowerCAmelCase	:						Optional[torch.FloatTensor] = None		,	lowerCAmelCase	:						Optional[str] = "pil"		,	lowerCAmelCase	:						bool = True		,	lowerCAmelCase	:						Optional[Callable[[int, int, torch.FloatTensor], None]] = None		,	lowerCAmelCase	:						int = 1		,	**lowerCAmelCase	:						List[Any]		,	):


		'''simple docstring'''







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




	@torch.no_grad()
	def       __A					(		self	:						List[str]		,	lowerCAmelCase	:						Union[str, List[str]]		,	lowerCAmelCase	:						int = 512		,	lowerCAmelCase	:						int = 512		,	lowerCAmelCase	:						int = 50		,	lowerCAmelCase	:						float = 7.5		,	lowerCAmelCase	:						Optional[Union[str, List[str]]] = None		,	lowerCAmelCase	:						Optional[int] = 1		,	lowerCAmelCase	:						float = 0.0		,	lowerCAmelCase	:						Optional[torch.Generator] = None		,	lowerCAmelCase	:						Optional[torch.FloatTensor] = None		,	lowerCAmelCase	:						Optional[str] = "pil"		,	lowerCAmelCase	:						bool = True		,	lowerCAmelCase	:						Optional[Callable[[int, int, torch.FloatTensor], None]] = None		,	lowerCAmelCase	:						int = 1		,	**lowerCAmelCase	:						Union[str, Any]		,	):


		'''simple docstring'''







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




	@torch.no_grad()
	def       __A					(		self	:						Dict		,	lowerCAmelCase	:						Union[str, List[str]]		,	lowerCAmelCase	:						int = 512		,	lowerCAmelCase	:						int = 512		,	lowerCAmelCase	:						int = 50		,	lowerCAmelCase	:						float = 7.5		,	lowerCAmelCase	:						Optional[Union[str, List[str]]] = None		,	lowerCAmelCase	:						Optional[int] = 1		,	lowerCAmelCase	:						float = 0.0		,	lowerCAmelCase	:						Optional[torch.Generator] = None		,	lowerCAmelCase	:						Optional[torch.FloatTensor] = None		,	lowerCAmelCase	:						Optional[str] = "pil"		,	lowerCAmelCase	:						bool = True		,	lowerCAmelCase	:						Optional[Callable[[int, int, torch.FloatTensor], None]] = None		,	lowerCAmelCase	:						int = 1		,	**lowerCAmelCase	:						Optional[Any]		,	):


		'''simple docstring'''







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




	@torch.no_grad()
	def       __A					(		self	:						Union[str, Any]		,	lowerCAmelCase	:						Union[str, List[str]]		,	lowerCAmelCase	:						int = 512		,	lowerCAmelCase	:						int = 512		,	lowerCAmelCase	:						int = 50		,	lowerCAmelCase	:						float = 7.5		,	lowerCAmelCase	:						Optional[Union[str, List[str]]] = None		,	lowerCAmelCase	:						Optional[int] = 1		,	lowerCAmelCase	:						float = 0.0		,	lowerCAmelCase	:						Optional[torch.Generator] = None		,	lowerCAmelCase	:						Optional[torch.FloatTensor] = None		,	lowerCAmelCase	:						Optional[str] = "pil"		,	lowerCAmelCase	:						bool = True		,	lowerCAmelCase	:						Optional[Callable[[int, int, torch.FloatTensor], None]] = None		,	lowerCAmelCase	:						int = 1		,	**lowerCAmelCase	:						Union[str, Any]		,	):


		'''simple docstring'''







		UpperCAmelCase_       				=			"cuda" if torch.cuda.is_available() else "cpu"
		self.to(lowerCAmelCase							)

		# 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
		UpperCAmelCase_       				=			self.textaimg_sda_a(
		    prompt=lowerCAmelCase		,	height=lowerCAmelCase		,	width=lowerCAmelCase		,	num_inference_steps=lowerCAmelCase		,	guidance_scale=lowerCAmelCase		,	negative_prompt=lowerCAmelCase		,	num_images_per_prompt=lowerCAmelCase		,	eta=lowerCAmelCase		,	generator=lowerCAmelCase		,	latents=lowerCAmelCase		,	output_type=lowerCAmelCase		,	return_dict=lowerCAmelCase		,	callback=lowerCAmelCase		,	callback_steps=lowerCAmelCase		,	**lowerCAmelCase		,	)

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

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

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

		# 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]]							)