



'''simple docstring'''





import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image

from diffusers import (
    DDIMScheduler,
    KandinskyVaaControlnetImgaImgPipeline,
    KandinskyVaaPriorEmbaEmbPipeline,
    UNetaDConditionModel,
    VQModel,
)
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference


enable_full_determinism()







class     UpperCAmelCase_       (       SCREAMING_SNAKE_CASE__		,		unittest.TestCase	):
    """simple docstring"""







    lowercase					=		KandinskyVaaControlnetImgaImgPipeline
    lowercase					=		["image_embeds", "negative_image_embeds", "image", "hint"]
    lowercase					=		["image_embeds", "negative_image_embeds", "image", "hint"]
    lowercase					=		[
        "generator",
        "height",
        "width",
        "strength",
        "guidance_scale",
        "num_inference_steps",
        "return_dict",
        "guidance_scale",
        "num_images_per_prompt",
        "output_type",
        "return_dict",
    ]
    lowercase					=		False




    @property
    def lowerCamelCase       (    self					:     Optional[Any]						):
          return 32




    @property
    def lowerCamelCase       (    self					:     str						):
          return 32




    @property
    def lowerCamelCase       (    self					:     int						):
          return self.time_input_dim




    @property
    def lowerCamelCase       (    self					:     Optional[Any]						):
          return self.time_input_dim * 4




    @property
    def lowerCamelCase       (    self					:     Any						):
          return 100




    @property
    def lowerCamelCase       (    self					:     List[str]						):
          torch.manual_seed(0						)

          snake_case__  :    Tuple    						=      {
              '''in_channels''': 8,
              # Out channels is double in channels because predicts mean and variance
              '''out_channels''': 8,
              '''addition_embed_type''': '''image_hint''',
              '''down_block_types''': ('''ResnetDownsampleBlock2D''', '''SimpleCrossAttnDownBlock2D'''),
              '''up_block_types''': ('''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D'''),
              '''mid_block_type''': '''UNetMidBlock2DSimpleCrossAttn''',
              '''block_out_channels''': (self.block_out_channels_a, self.block_out_channels_a * 2),
              '''layers_per_block''': 1,
              '''encoder_hid_dim''': self.text_embedder_hidden_size,
              '''encoder_hid_dim_type''': '''image_proj''',
              '''cross_attention_dim''': self.cross_attention_dim,
              '''attention_head_dim''': 4,
              '''resnet_time_scale_shift''': '''scale_shift''',
              '''class_embed_type''': None,
          }

          snake_case__  :    Union[str, Any]    						=      UNetaDConditionModel(**__lowerCamelCase						)
          return model




    @property
    def lowerCamelCase       (    self					:     Union[str, Any]						):
          return {
              "block_out_channels": [32, 32, 64, 64],
              "down_block_types": [
                  "DownEncoderBlock2D",
                  "DownEncoderBlock2D",
                  "DownEncoderBlock2D",
                  "AttnDownEncoderBlock2D",
              ],
              "in_channels": 3,
              "latent_channels": 4,
              "layers_per_block": 1,
              "norm_num_groups": 8,
              "norm_type": "spatial",
              "num_vq_embeddings": 12,
              "out_channels": 3,
              "up_block_types": ["AttnUpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D"],
              "vq_embed_dim": 4,
          }




    @property
    def lowerCamelCase       (    self					:     List[Any]						):
          torch.manual_seed(0						)
          snake_case__  :    Any    						=      VQModel(**self.dummy_movq_kwargs						)
          return model




    def lowerCamelCase       (    self					:     Optional[Any]						):
          snake_case__  :    Any    						=      self.dummy_unet
          snake_case__  :    Union[str, Any]    						=      self.dummy_movq

          snake_case__  :    Optional[Any]    						=      {
              '''num_train_timesteps''': 1_000,
              '''beta_schedule''': '''linear''',
              '''beta_start''': 0.00085,
              '''beta_end''': 0.012,
              '''clip_sample''': False,
              '''set_alpha_to_one''': False,
              '''steps_offset''': 0,
              '''prediction_type''': '''epsilon''',
              '''thresholding''': False,
          }

          snake_case__  :    Optional[int]    						=      DDIMScheduler(**__lowerCamelCase						)

          snake_case__  :    Tuple    						=      {
              '''unet''': unet,
              '''scheduler''': scheduler,
              '''movq''': movq,
          }

          return components




    def lowerCamelCase       (    self					:     Tuple ,     snake_case_					:     int ,     snake_case_					:     str=0						):
          snake_case__  :    Tuple    						=      floats_tensor((1, self.text_embedder_hidden_size) ,     rng=random.Random(__lowerCamelCase						)						).to(__lowerCamelCase						)
          snake_case__  :    str    						=      floats_tensor((1, self.text_embedder_hidden_size) ,     rng=random.Random(seed + 1						)						).to(
              __lowerCamelCase						)
          # create init_image
          snake_case__  :    List[Any]    						=      floats_tensor((1, 3, 64, 64) ,     rng=random.Random(__lowerCamelCase						)						).to(__lowerCamelCase						)
          snake_case__  :    List[str]    						=      image.cpu().permute(0 ,     2 ,     3 ,     1						)[0]
          snake_case__  :    Optional[Any]    						=      Image.fromarray(np.uinta(__lowerCamelCase						)						).convert("""RGB"""						).resize((256, 256)						)
          # create hint
          snake_case__  :    Union[str, Any]    						=      floats_tensor((1, 3, 64, 64) ,     rng=random.Random(__lowerCamelCase						)						).to(__lowerCamelCase						)

          if str(__lowerCamelCase						).startswith("""mps"""						):
                snake_case__  :    Any    						=      torch.manual_seed(__lowerCamelCase						)
          else:
                snake_case__  :    Union[str, Any]    						=      torch.Generator(device=__lowerCamelCase						).manual_seed(__lowerCamelCase						)
          snake_case__  :    Union[str, Any]    						=      {
              '''image''': init_image,
              '''image_embeds''': image_embeds,
              '''negative_image_embeds''': negative_image_embeds,
              '''hint''': hint,
              '''generator''': generator,
              '''height''': 64,
              '''width''': 64,
              '''num_inference_steps''': 10,
              '''guidance_scale''': 7.0,
              '''strength''': 0.2,
              '''output_type''': '''np''',
          }
          return inputs




    def lowerCamelCase       (    self					:     Dict						):
          snake_case__  :    List[str]    						=      '''cpu'''

          snake_case__  :    int    						=      self.get_dummy_components()

          snake_case__  :    List[Any]    						=      self.pipeline_class(**__lowerCamelCase						)
          snake_case__  :    str    						=      pipe.to(__lowerCamelCase						)

          pipe.set_progress_bar_config(disable=__lowerCamelCase						)

          snake_case__  :    str    						=      pipe(**self.get_dummy_inputs(__lowerCamelCase						)						)
          snake_case__  :    Any    						=      output.images

          snake_case__  :    List[str]    						=      pipe(
              **self.get_dummy_inputs(__lowerCamelCase						) ,     return_dict=__lowerCamelCase ,     )[0]

          snake_case__  :    Union[str, Any]    						=      image[0, -3:, -3:, -1]
          snake_case__  :    Tuple    						=      image_from_tuple[0, -3:, -3:, -1]

          assert image.shape == (1, 64, 64, 3)

          snake_case__  :    Dict    						=      np.array(
              [0.54985034, 0.55509365, 0.52561504, 0.5570494, 0.5593818, 0.5263979, 0.50285643, 0.5069846, 0.51196736]						)
          assert (
              np.abs(image_slice.flatten() - expected_slice						).max() < 1E-2
          ), f" expected_slice {expected_slice}, but got {image_slice.flatten()}"
          assert (
              np.abs(image_from_tuple_slice.flatten() - expected_slice						).max() < 1E-2
          ), f" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}"







@slow
@require_torch_gpu
class     UpperCAmelCase_       (       unittest.TestCase	):
    """simple docstring"""







    def lowerCamelCase       (    self					:     Any						):
          super().tearDown()
          gc.collect()
          torch.cuda.empty_cache()




    def lowerCamelCase       (    self					:     List[str]						):
          snake_case__  :    str    						=      load_numpy(
              """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
              """/kandinskyv22/kandinskyv22_controlnet_img2img_robotcat_fp16.npy"""						)

          snake_case__  :    List[str]    						=      load_image(
              """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/kandinsky/cat.png"""						)
          snake_case__  :    Union[str, Any]    						=      init_image.resize((512, 512)						)

          snake_case__  :    int    						=      load_image(
              """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
              """/kandinskyv22/hint_image_cat.png"""						)
          snake_case__  :    Any    						=      torch.from_numpy(np.array(__lowerCamelCase						)						).float() / 255.0
          snake_case__  :    int    						=      hint.permute(2 ,     0 ,     1						).unsqueeze(0						)

          snake_case__  :    List[str]    						=      '''A robot, 4k photo'''

          snake_case__  :    str    						=      KandinskyVaaPriorEmbaEmbPipeline.from_pretrained(
              """kandinsky-community/kandinsky-2-2-prior""" ,     torch_dtype=torch.floataa						)
          pipe_prior.to(__lowerCamelCase						)

          snake_case__  :    Dict    						=      KandinskyVaaControlnetImgaImgPipeline.from_pretrained(
              """kandinsky-community/kandinsky-2-2-controlnet-depth""" ,     torch_dtype=torch.floataa						)
          snake_case__  :    Dict    						=      pipeline.to(__lowerCamelCase						)

          pipeline.set_progress_bar_config(disable=__lowerCamelCase						)

          snake_case__  :    Union[str, Any]    						=      torch.Generator(device="""cpu"""						).manual_seed(0						)

          snake_case__  :    Optional[Any]    						=      pipe_prior(
              __lowerCamelCase ,     image=__lowerCamelCase ,     strength=0.85 ,     generator=__lowerCamelCase ,     negative_prompt="""""" ,     ).to_tuple()

          snake_case__  :    Any    						=      pipeline(
              image=__lowerCamelCase ,     image_embeds=__lowerCamelCase ,     negative_image_embeds=__lowerCamelCase ,     hint=__lowerCamelCase ,     generator=__lowerCamelCase ,     num_inference_steps=100 ,     height=512 ,     width=512 ,     strength=0.5 ,     output_type="""np""" ,     )

          snake_case__  :    Optional[Any]    						=      output.images[0]

          assert image.shape == (512, 512, 3)

          assert_mean_pixel_difference(__lowerCamelCase ,     __lowerCamelCase						)







"""simple docstring"""



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_ =			logging.get_logger(__name__)






class 		__snake_case					:





 """simple docstring"""


 def __init__(    self					,						__lowerCamelCase = None					,						__lowerCamelCase = None					,						__lowerCamelCase=None					,						__lowerCamelCase=None  ):



     '''simple docstring'''




     if not conversation_id:
         __A    :						List[Any]						 =     uuid.uuida()
     if past_user_inputs is None:
         __A    :						List[str]						 =     []
     if generated_responses is None:
         __A    :						Tuple						 =     []

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

 def __eq__(    self					,						__lowerCamelCase  ):



     '''simple docstring'''




     if not isinstance(__lowerCamelCase					,						__lowerCamelCase  ):
         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 					UpperCamelCase__(    self					,						__lowerCamelCase					,						__lowerCamelCase = False  ):



     '''simple docstring'''




     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    :						Union[str, Any]						 =     text

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     if self.new_user_input:
         self.past_user_inputs.append(self.new_user_input  )
     __A    :						List[Any]						 =     None

 def 					UpperCamelCase__(    self					,						__lowerCamelCase  ):



     '''simple docstring'''




     self.generated_responses.append(__lowerCamelCase  )

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     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  ):



     '''simple docstring'''




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






@add_end_docstrings(
    SCREAMING_SNAKE_CASE__							,							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 		__snake_case					(		SCREAMING_SNAKE_CASE__     ):





 """simple docstring"""


 def __init__(    self					,						*__lowerCamelCase					,						**__lowerCamelCase  ):



     '''simple docstring'''




     super().__init__(*__lowerCamelCase					,						**__lowerCamelCase  )
     if self.tokenizer.pad_token_id is None:
         __A    :						Union[str, Any]						 =     self.tokenizer.eos_token

 def 					UpperCamelCase__(    self					,						__lowerCamelCase=None					,						__lowerCamelCase=None					,						__lowerCamelCase=None					,						**__lowerCamelCase  ):



     '''simple docstring'''




     __A    :						str						 =     {}
     __A    :						List[str]						 =     {}
     __A    :						Any						 =     {}

     if min_length_for_response is not None:
         __A    :						int						 =     min_length_for_response
     if minimum_tokens is not None:
         __A    :						Any						 =     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    :						str						 =     clean_up_tokenization_spaces

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

 def __call__(    self					,						__lowerCamelCase					,						__lowerCamelCase=0					,						**__lowerCamelCase  ):



     '''simple docstring'''




     __A    :						Any						 =     super().__call__(__lowerCamelCase					,						num_workers=__lowerCamelCase					,						**__lowerCamelCase  )
     if isinstance(__lowerCamelCase					,						__lowerCamelCase  ) and len(__lowerCamelCase  ) == 1:
         return outputs[0]
     return outputs

 def 					UpperCamelCase__(    self					,						__lowerCamelCase					,						__lowerCamelCase=32  ):



     '''simple docstring'''




     if not isinstance(__lowerCamelCase					,						__lowerCamelCase  ):
         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[Any]						 =     self.tokenizer._build_conversation_input_ids(__lowerCamelCase  )
     else:
         # If the tokenizer cannot handle conversations, we default to only the old version
         __A    :						int						 =     self._legacy_parse_and_tokenize(__lowerCamelCase  )

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

 def 					UpperCamelCase__(    self					,						__lowerCamelCase					,						__lowerCamelCase=10					,						**__lowerCamelCase  ):



     '''simple docstring'''




     __A    :						Tuple						 =     generate_kwargs.get('''max_length'''					,						self.model.config.max_length  )

     __A    :						str						 =     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    :						str						 =     max_length - minimum_tokens
         __A    :						Any						 =     model_inputs['''input_ids'''][:, -trim:]
         if "attention_mask" in model_inputs:
             __A    :						Union[str, Any]						 =     model_inputs['''attention_mask'''][:, -trim:]
     __A    :						Dict						 =     model_inputs.pop('''conversation'''  )
     __A    :						List[str]						 =     max_length
     __A    :						Dict						 =     self.model.generate(**__lowerCamelCase					,						**__lowerCamelCase  )
     if self.model.config.is_encoder_decoder:
         __A    :						Any						 =     1
     else:
         __A    :						List[Any]						 =     n
     return {"output_ids": output_ids[:, start_position:], "conversation": conversation}

 def 					UpperCamelCase__(    self					,						__lowerCamelCase					,						__lowerCamelCase=True  ):



     '''simple docstring'''




     __A    :						int						 =     model_outputs['''output_ids''']
     __A    :						Optional[int]						 =     self.tokenizer.decode(
         output_ids[0]					,						skip_special_tokens=__lowerCamelCase					,						clean_up_tokenization_spaces=__lowerCamelCase					,						)
     __A    :						Dict						 =     model_outputs['''conversation''']
     conversation.mark_processed()
     conversation.append_response(__lowerCamelCase  )
     return conversation







 def 					UpperCamelCase__(    self					,						__lowerCamelCase  ):



     '''simple docstring'''




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

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




