

from dataclasses import dataclass
from typing import List, Optional, Union

import numpy as np
import PIL
import torch
from transformers import CLIPImageProcessor, CLIPVisionModel

from ...models import PriorTransformer
from ...pipelines import DiffusionPipeline
from ...schedulers import HeunDiscreteScheduler
from ...utils import (
    BaseOutput,
    is_accelerate_available,
    logging,
    randn_tensor,
    replace_example_docstring,
)
from .renderer import ShapERenderer


A_       : str         =			logging.get_logger(__name__)  # pylint: disable=invalid-name

A_       : Optional[Any]         =			'\n    Examples:\n        ```py\n        >>> from PIL import Image\n        >>> import torch\n        >>> from diffusers import DiffusionPipeline\n        >>> from diffusers.utils import export_to_gif, load_image\n\n        >>> device = torch.device("cuda" if torch.cuda.is_available() else "cpu")\n\n        >>> repo = "openai/shap-e-img2img"\n        >>> pipe = DiffusionPipeline.from_pretrained(repo, torch_dtype=torch.float16)\n        >>> pipe = pipe.to(device)\n\n        >>> guidance_scale = 3.0\n        >>> image_url = "https://hf.co/datasets/diffusers/docs-images/resolve/main/shap-e/corgi.png"\n        >>> image = load_image(image_url).convert("RGB")\n\n        >>> images = pipe(\n        ...     image,\n        ...     guidance_scale=guidance_scale,\n        ...     num_inference_steps=64,\n        ...     frame_size=256,\n        ... ).images\n\n        >>> gif_path = export_to_gif(images[0], "corgi_3d.gif")\n        ```\n'



@dataclass
class     _a						(__magic_name__				):


					'''simple docstring'''






					UpperCAmelCase__:   Union[PIL.Image.Image, np.ndarray]



class     _a						(__magic_name__				):


					'''simple docstring'''






					def __init__( self    ,       A__    ,       A__    ,       A__    ,       A__    ,       A__    ,       ):
						super().__init__()

						self.register_modules(
						    prior=A__    ,       image_encoder=A__    ,       image_processor=A__    ,       scheduler=A__    ,       renderer=A__    ,       )






					def   __A			( self    ,       A__    ,       A__    ,       A__    ,       A__    ,       A__    ,       A__			):
						if latents is None:
							A__							:    int        =		randn_tensor(A__    ,       generator=A__    ,       device=A__    ,       dtype=A__			)
						else:
							if latents.shape != shape:
								raise ValueError(F"""Unexpected latents shape, got {latents.shape}, expected {shape}"""			)
							A__							:    Dict        =		latents.to(A__			)

						A__							:    Union[str, Any]        =		latents * scheduler.init_noise_sigma
						return latents






					def   __A			( self    ,       A__=0			):
						if is_accelerate_available():
							from accelerate import cpu_offload
						else:
							raise ImportError("""Please install accelerate via `pip install accelerate`"""			)

						A__							:    Optional[int]        =		torch.device(F"""cuda:{gpu_id}"""			)

						A__							:    List[str]        =		[self.image_encoder, self.prior]
						for cpu_offloaded_model in models:
							if cpu_offloaded_model is not None:
								cpu_offload(A__    ,       A__			)






					@property
					def   __A			( self			):
						if self.device != torch.device("""meta"""			) or not hasattr(self.image_encoder    ,       """_hf_hook"""			):
							return self.device
						for module in self.image_encoder.modules():
							if (
							    hasattr(A__    ,       """_hf_hook"""			)
							    and hasattr(module._hf_hook    ,       """execution_device"""			)
							    and module._hf_hook.execution_device is not None
							):
								return torch.device(module._hf_hook.execution_device			)
						return self.device






					def   __A			( self    ,       A__    ,       A__    ,       A__    ,       A__    ,       ):
						if isinstance(A__    ,       A__			) and isinstance(image[0]    ,       torch.Tensor			):
							A__							:    str        =		torch.cat(A__    ,       axis=0			) if image[0].ndim == 4 else torch.stack(A__    ,       axis=0			)

						if not isinstance(A__    ,       torch.Tensor			):
							A__							:    Dict        =		self.image_processor(A__    ,       return_tensors="""pt"""			).pixel_values[0].unsqueeze(0			)

						A__							:    Optional[Any]        =		image.to(dtype=self.image_encoder.dtype    ,       device=A__			)

						A__							:    List[Any]        =		self.image_encoder(A__			)["""last_hidden_state"""]
						A__							:    int        =		image_embeds[:, 1:, :].contiguous()  # batch_size, dim, 256

						A__							:    Optional[Any]        =		image_embeds.repeat_interleave(A__    ,       dim=0			)

						if do_classifier_free_guidance:
							A__							:    Optional[int]        =		torch.zeros_like(A__			)

							# For classifier free guidance, we need to do two forward passes.
							# Here we concatenate the unconditional and text embeddings into a single batch
							# to avoid doing two forward passes
							A__							:    Any        =		torch.cat([negative_image_embeds, image_embeds]			)

						return image_embeds






					@torch.no_grad()
					@replace_example_docstring(A__			)
					def __call__( self    ,       A__    ,       A__ = 1    ,       A__ = 25    ,       A__ = None    ,       A__ = None    ,       A__ = 4.0    ,       A__ = 64    ,       A__ = "pil"    ,       A__ = True    ,       ):

						if isinstance(A__    ,       PIL.Image.Image			):
							A__							:    List[Any]        =		1
						elif isinstance(A__    ,       torch.Tensor			):
							A__							:    Any        =		image.shape[0]
						elif isinstance(A__    ,       A__			) and isinstance(image[0]    ,       (torch.Tensor, PIL.Image.Image)			):
							A__							:    Optional[int]        =		len(A__			)
						else:
							raise ValueError(
							    F"""`image` has to be of type `PIL.Image.Image`, `torch.Tensor`, `List[PIL.Image.Image]` or `List[torch.Tensor]` but is {type(A__			)}"""			)

						A__							:    Dict        =		self._execution_device

						A__							:    List[Any]        =		batch_size * num_images_per_prompt

						A__							:    List[str]        =		guidance_scale > 1.0
						A__							:    Optional[Any]        =		self._encode_image(A__    ,       A__    ,       A__    ,       A__			)

						# prior

						self.scheduler.set_timesteps(A__    ,       device=A__			)
						A__							:    Dict        =		self.scheduler.timesteps

						A__							:    int        =		self.prior.config.num_embeddings
						A__							:    List[str]        =		self.prior.config.embedding_dim

						A__							:    List[Any]        =		self.prepare_latents(
						    (batch_size, num_embeddings * embedding_dim)    ,       image_embeds.dtype    ,       A__    ,       A__    ,       A__    ,       self.scheduler    ,       )

						# YiYi notes: for testing only to match ldm, we can directly create a latents with desired shape: batch_size, num_embeddings, embedding_dim
						A__							:    str        =		latents.reshape(latents.shape[0]    ,       A__    ,       A__			)

						for i, t in enumerate(self.progress_bar(A__			)			):
							# expand the latents if we are doing classifier free guidance
							A__							:    List[str]        =		torch.cat([latents] * 2			) if do_classifier_free_guidance else latents
							A__							:    List[str]        =		self.scheduler.scale_model_input(A__    ,       A__			)

							A__							:    Optional[Any]        =		self.prior(
							    A__    ,       timestep=A__    ,       proj_embedding=A__    ,       ).predicted_image_embedding

							# remove the variance
							A__   ,   A__							:    int        =		noise_pred.split(
							    scaled_model_input.shape[2]    ,       dim=2			)  # batch_size, num_embeddings, embedding_dim

							if do_classifier_free_guidance is not None:
								A__   ,   A__							:    List[Any]        =		noise_pred.chunk(2			)
								A__							:    Optional[int]        =		noise_pred_uncond + guidance_scale * (noise_pred - noise_pred_uncond)

							A__							:    Optional[Any]        =		self.scheduler.step(
							    A__    ,       timestep=A__    ,       sample=A__    ,       ).prev_sample

						if output_type == "latent":
							return ShapEPipelineOutput(images=A__			)

						A__							:    Any        =		[]
						for i, latent in enumerate(A__			):
							print()
							A__							:    List[str]        =		self.renderer.decode(
							    latent[None, :]    ,       A__    ,       size=A__    ,       ray_batch_size=4096    ,       n_coarse_samples=64    ,       n_fine_samples=128    ,       )

							images.append(A__			)

						A__							:    Dict        =		torch.stack(A__			)

						if output_type not in ["np", "pil"]:
							raise ValueError(F"""Only the output types `pil` and `np` are supported not output_type={output_type}"""			)

						A__							:    Union[str, Any]        =		images.cpu().numpy()

						if output_type == "pil":
							A__							:    Optional[int]        =		[self.numpy_to_pil(A__			) for image in images]

						# Offload last model to CPU
						if hasattr(self    ,       """final_offload_hook"""			) and self.final_offload_hook is not None:
							self.final_offload_hook.offload()

						if not return_dict:
							return (images,)

						return ShapEPipelineOutput(images=A__			)






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

from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_tf_available():
	import tensorflow as tf

if is_torch_available():
	import torch


A_       : List[Any]         =			logging.get_logger(__name__)



class     _a						:


					'''simple docstring'''






					def __init__( self    ,       A__ = None    ,       A__ = None    ,       A__=None    ,       A__=None			):
						if not conversation_id:
							A__							:    List[Any]        =		uuid.uuida()
						if past_user_inputs is None:
							A__							:    Dict        =		[]
						if generated_responses is None:
							A__							:    int        =		[]

						A__							:    uuid.UUID        =		conversation_id
						A__							:    List[str]        =		past_user_inputs
						A__							:    List[str]        =		generated_responses
						A__							:    Optional[str]        =		text






					def __eq__( self    ,       A__			):
						if not isinstance(A__    ,       A__			):
							return False
						if self.uuid == other.uuid:
							return True
						return (
						    self.new_user_input == other.new_user_input
						    and self.past_user_inputs == other.past_user_inputs
						    and self.generated_responses == other.generated_responses
						)






					def   __A			( self    ,       A__    ,       A__ = False			):
						if self.new_user_input:
							if overwrite:
								logger.warning(
								    F"""User input added while unprocessed input was existing: \"{self.new_user_input}\" was overwritten """
								    F"""with: \"{text}\"."""			)
								A__							:    str        =		text
							else:
								logger.warning(
								    F"""User input added while unprocessed input was existing: \"{self.new_user_input}\" new input """
								    F"""ignored: \"{text}\". Set `overwrite` to True to overwrite unprocessed user input"""			)
						else:
							A__							:    Tuple        =		text






					def   __A			( self			):
						if self.new_user_input:
							self.past_user_inputs.append(self.new_user_input			)
						A__							:    Tuple        =		None






					def   __A			( self    ,       A__			):
						self.generated_responses.append(A__			)






					def   __A			( self			):
						for user_input, generated_response in zip(self.past_user_inputs    ,       self.generated_responses			):
							yield True, user_input
							yield False, generated_response
						if self.new_user_input:
							yield True, self.new_user_input






					def __repr__( self			):
						A__							:    Optional[Any]        =		F"""Conversation id: {self.uuid} \n"""
						for is_user, text in self.iter_texts():
							A__							:    str        =		"""user""" if is_user else """bot"""
							output += F"""{name} >> {text} \n"""
						return output



@add_end_docstrings(
    __magic_name__		,   r'''
        min_length_for_response (`int`, *optional*, defaults to 32):
            The minimum length (in number of tokens) for a response.
        minimum_tokens (`int`, *optional*, defaults to 10):
            The minimum length of tokens to leave for a response.
    '''		,   )
class     _a						(__magic_name__				):


					'''simple docstring'''






					def __init__( self    ,       *A__    ,       **A__			):
						super().__init__(*A__    ,       **A__			)
						if self.tokenizer.pad_token_id is None:
							A__							:    Tuple        =		self.tokenizer.eos_token






					def   __A			( self    ,       A__=None    ,       A__=None    ,       A__=None    ,       **A__			):
						A__							:    Tuple        =		{}
						A__							:    List[str]        =		{}
						A__							:    Union[str, Any]        =		{}

						if min_length_for_response is not None:
							A__							:    str        =		min_length_for_response
						if minimum_tokens is not None:
							A__							:    List[str]        =		minimum_tokens

						if "max_length" in generate_kwargs:
							A__							:    List[Any]        =		generate_kwargs["""max_length"""]
							# self.max_length = generate_kwargs.get("max_length", self.model.config.max_length)
						if clean_up_tokenization_spaces is not None:
							A__							:    Optional[int]        =		clean_up_tokenization_spaces

						if generate_kwargs:
							forward_params.update(A__			)
						return preprocess_params, forward_params, postprocess_params






					def __call__( self    ,       A__    ,       A__=0    ,       **A__			):
						A__							:    Optional[Any]        =		super().__call__(A__    ,       num_workers=A__    ,       **A__			)
						if isinstance(A__    ,       A__			) and len(A__			) == 1:
							return outputs[0]
						return outputs






					def   __A			( self    ,       A__    ,       A__=32			):
						if not isinstance(A__    ,       A__			):
							raise ValueError("""ConversationalPipeline, expects Conversation as inputs"""			)
						if conversation.new_user_input is None:
							raise ValueError(
							    F"""Conversation with UUID {type(conversation.uuid			)} does not contain new user input to process. """
							    """Add user inputs with the conversation's `add_user_input` method"""			)
						if hasattr(self.tokenizer    ,       """_build_conversation_input_ids"""			):
							A__							:    List[str]        =		self.tokenizer._build_conversation_input_ids(A__			)
						else:
							# If the tokenizer cannot handle conversations, we default to only the old version
							A__							:    Tuple        =		self._legacy_parse_and_tokenize(A__			)

						if self.framework == "pt":
							A__							:    List[str]        =		torch.LongTensor([input_ids]			)
						elif self.framework == "tf":
							A__							:    Optional[Any]        =		tf.constant([input_ids]			)
						return {"input_ids": input_ids, "conversation": conversation}






					def   __A			( self    ,       A__    ,       A__=10    ,       **A__			):
						A__							:    List[Any]        =		generate_kwargs.get("""max_length"""    ,       self.model.config.max_length			)

						A__							:    Optional[int]        =		model_inputs["""input_ids"""].shape[1]
						if max_length - minimum_tokens < n:
							logger.warning(F"""Conversation input is to long ({n}), trimming it to ({max_length} - {minimum_tokens})"""			)
							A__							:    Dict        =		max_length - minimum_tokens
							A__							:    Optional[int]        =		model_inputs["""input_ids"""][:, -trim:]
							if "attention_mask" in model_inputs:
								A__							:    str        =		model_inputs["""attention_mask"""][:, -trim:]
						A__							:    List[str]        =		model_inputs.pop("""conversation"""			)
						A__							:    Dict        =		max_length
						A__							:    str        =		self.model.generate(**A__    ,       **A__			)
						if self.model.config.is_encoder_decoder:
							A__							:    Union[str, Any]        =		1
						else:
							A__							:    Optional[Any]        =		n
						return {"output_ids": output_ids[:, start_position:], "conversation": conversation}






					def   __A			( self    ,       A__    ,       A__=True			):
						A__							:    Dict        =		model_outputs["""output_ids"""]
						A__							:    str        =		self.tokenizer.decode(
						    output_ids[0]    ,       skip_special_tokens=A__    ,       clean_up_tokenization_spaces=A__    ,       )
						A__							:    Optional[int]        =		model_outputs["""conversation"""]
						conversation.mark_processed()
						conversation.append_response(A__			)
						return conversation






					def   __A			( self    ,       A__			):
						A__							:    str        =		self.tokenizer.eos_token_id
						A__							:    Tuple        =		[]
						for is_user, text in conversation.iter_texts():
							if eos_token_id is not None:
								input_ids.extend(self.tokenizer.encode(A__    ,       add_special_tokens=A__			) + [eos_token_id]			)
							else:
								input_ids.extend(self.tokenizer.encode(A__    ,       add_special_tokens=A__			)			)

						if len(A__			) > self.tokenizer.model_max_length:
							A__							:    str        =		input_ids[-self.tokenizer.model_max_length :]
						return input_ids



