


import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image

from diffusers import (
    DDIMScheduler,
    KandinskyVaaImgaImgPipeline,
    KandinskyVaaPriorPipeline,
    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  __magic_name__							(		lowerCAmelCase ,unittest.TestCase    ):
       UpperCAmelCase										=KandinskyVaaImgaImgPipeline
       UpperCAmelCase										=["image_embeds", "negative_image_embeds", "image"]
       UpperCAmelCase										=[
           "image_embeds",
           "negative_image_embeds",
           "image",
       ]
       UpperCAmelCase										=[
           "generator",
           "height",
           "width",
           "strength",
           "guidance_scale",
           "num_inference_steps",
           "return_dict",
           "guidance_scale",
           "num_images_per_prompt",
           "output_type",
           "return_dict",
       ]
       UpperCAmelCase										=False






       @property
       def       lowerCAmelCase		(      self)						->  Optional[int]:




          '''simple docstring'''
          return 3_2






       @property
       def       lowerCAmelCase		(      self)						->  Tuple:




          '''simple docstring'''
          return 3_2






       @property
       def       lowerCAmelCase		(      self)						->  Optional[int]:




          '''simple docstring'''
          return self.time_input_dim






       @property
       def       lowerCAmelCase		(      self)						->  List[Any]:




          '''simple docstring'''
          return self.time_input_dim * 4






       @property
       def       lowerCAmelCase		(      self)						->  List[Any]:




          '''simple docstring'''
          return 1_0_0






       @property
       def       lowerCAmelCase		(      self)						->  int:




          '''simple docstring'''
          torch.manual_seed(0)

          _UpperCAmelCase       :       Optional[int]							   ={
              'in_channels': 4,
              # Out channels is double in channels because predicts mean and variance
              'out_channels': 8,
              'addition_embed_type': 'image',
              '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,
          }

          _UpperCAmelCase       :       Union[str, Any]							   =UNetaDConditionModel(**snake_case)
          return model






       @property
       def       lowerCAmelCase		(      self)						->  Union[str, Any]:




          '''simple docstring'''
          return {
              "block_out_channels": [3_2, 6_4],
              "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"],
              "in_channels": 3,
              "latent_channels": 4,
              "layers_per_block": 1,
              "norm_num_groups": 8,
              "norm_type": "spatial",
              "num_vq_embeddings": 1_2,
              "out_channels": 3,
              "up_block_types": [
                  "AttnUpDecoderBlock2D",
                  "UpDecoderBlock2D",
              ],
              "vq_embed_dim": 4,
          }






       @property
       def       lowerCAmelCase		(      self)						->  Union[str, Any]:




          '''simple docstring'''
          torch.manual_seed(0)
          _UpperCAmelCase       :       Dict							   =VQModel(**self.dummy_movq_kwargs)
          return model






       def       lowerCAmelCase		(      self)						->  int:




          '''simple docstring'''
          _UpperCAmelCase       :       Tuple							   =self.dummy_unet
          _UpperCAmelCase       :       Dict							   =self.dummy_movq

          _UpperCAmelCase       :       str							   ={
              'num_train_timesteps': 1_0_0_0,
              'beta_schedule': 'linear',
              'beta_start': 0.0_00_85,
              'beta_end': 0.0_12,
              'clip_sample': False,
              'set_alpha_to_one': False,
              'steps_offset': 0,
              'prediction_type': 'epsilon',
              'thresholding': False,
          }

          _UpperCAmelCase       :       List[Any]							   =DDIMScheduler(**snake_case)

          _UpperCAmelCase       :       List[Any]							   ={
              'unet': unet,
              'scheduler': scheduler,
              'movq': movq,
          }

          return components






       def       lowerCAmelCase		(      self  ,							snake_case  ,							snake_case=0)						->  Any:




          '''simple docstring'''
          _UpperCAmelCase       :       Any							   =floats_tensor((1, self.text_embedder_hidden_size)  ,							rng=random.Random(snake_case)).to(snake_case)
          _UpperCAmelCase       :       Optional[Any]							   =floats_tensor((1, self.text_embedder_hidden_size)  ,							rng=random.Random(seed + 1)).to(
              snake_case)
          # create init_image
          _UpperCAmelCase       :       Dict							   =floats_tensor((1, 3, 6_4, 6_4)  ,							rng=random.Random(snake_case)).to(snake_case)
          _UpperCAmelCase       :       Optional[int]							   =image.cpu().permute(0  ,							2  ,							3  ,							1)[0]
          _UpperCAmelCase       :       str							   =Image.fromarray(np.uinta(snake_case)).convert('RGB').resize((2_5_6, 2_5_6))

          if str(snake_case).startswith('mps'):
             _UpperCAmelCase       :       str							   =torch.manual_seed(snake_case)
          else:
             _UpperCAmelCase       :       Optional[Any]							   =torch.Generator(device=snake_case).manual_seed(snake_case)
          _UpperCAmelCase       :       Optional[Any]							   ={
              'image': init_image,
              'image_embeds': image_embeds,
              'negative_image_embeds': negative_image_embeds,
              'generator': generator,
              'height': 6_4,
              'width': 6_4,
              'num_inference_steps': 1_0,
              'guidance_scale': 7.0,
              'strength': 0.2,
              'output_type': 'np',
          }
          return inputs






       def       lowerCAmelCase		(      self)						->  List[Any]:




          '''simple docstring'''
          _UpperCAmelCase       :       str							   ='cpu'

          _UpperCAmelCase       :       Any							   =self.get_dummy_components()

          _UpperCAmelCase       :       int							   =self.pipeline_class(**snake_case)
          _UpperCAmelCase       :       str							   =pipe.to(snake_case)

          pipe.set_progress_bar_config(disable=snake_case)

          _UpperCAmelCase       :       int							   =pipe(**self.get_dummy_inputs(snake_case))
          _UpperCAmelCase       :       Optional[int]							   =output.images

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

          _UpperCAmelCase       :       Optional[int]							   =image[0, -3:, -3:, -1]
          _UpperCAmelCase       :       List[str]							   =image_from_tuple[0, -3:, -3:, -1]

          assert image.shape == (1, 6_4, 6_4, 3)

          _UpperCAmelCase       :       Tuple							   =np.array(
              [0.6_19_97_78, 0.63_98_44_06, 0.46_14_57_85, 0.62_94_49_84, 0.5_62_22_15, 0.47_30_61_32, 0.47_44_14_56, 0.4_60_76_06, 0.48_71_92_63])
          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  __magic_name__							(		unittest.TestCase    ):






       def       lowerCAmelCase		(      self)						->  List[str]:




          '''simple docstring'''
          super().tearDown()
          gc.collect()
          torch.cuda.empty_cache()






       def       lowerCAmelCase		(      self)						->  List[Any]:




          '''simple docstring'''
          _UpperCAmelCase       :       Union[str, Any]							   =load_numpy(
              'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
              '/kandinskyv22/kandinskyv22_img2img_frog.npy')

          _UpperCAmelCase       :       Optional[int]							   =load_image(
              'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/kandinsky/cat.png')
          _UpperCAmelCase       :       Tuple							   ='A red cartoon frog, 4k'

          _UpperCAmelCase       :       Union[str, Any]							   =KandinskyVaaPriorPipeline.from_pretrained(
              'kandinsky-community/kandinsky-2-2-prior'  ,							torch_dtype=torch.floataa)
          pipe_prior.to(snake_case)

          _UpperCAmelCase       :       str							   =KandinskyVaaImgaImgPipeline.from_pretrained(
              'kandinsky-community/kandinsky-2-2-decoder'  ,							torch_dtype=torch.floataa)
          _UpperCAmelCase       :       Any							   =pipeline.to(snake_case)

          pipeline.set_progress_bar_config(disable=snake_case)

          _UpperCAmelCase       :       Any							   =torch.Generator(device='cpu').manual_seed(0)
          _UpperCAmelCase       :       Tuple							   =pipe_prior(
              snake_case  ,							generator=snake_case  ,							num_inference_steps=5  ,							negative_prompt=''  ,							).to_tuple()

          _UpperCAmelCase       :       Any							   =pipeline(
              image=snake_case  ,							image_embeds=snake_case  ,							negative_image_embeds=snake_case  ,							generator=snake_case  ,							num_inference_steps=1_0_0  ,							height=7_6_8  ,							width=7_6_8  ,							strength=0.2  ,							output_type='np'  ,							)

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

          assert image.shape == (7_6_8, 7_6_8, 3)

          assert_mean_pixel_difference(snake_case  ,							snake_case)





'''simple docstring'''






from collections import UserDict
from typing import List, Union

from ..utils import (
    add_end_docstrings,
    is_tf_available,
    is_torch_available,
    is_vision_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
 from PIL import Image

 from ..image_utils import load_image

if is_torch_available():
 from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING

if is_tf_available():
 from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
 from ..tf_utils import stable_softmax

lowercase							=logging.get_logger(__name__)






@add_end_docstrings(lowerCAmelCase    )
class  __magic_name__							(		lowerCAmelCase    ):






       def __init__(      self  ,							**snake_case)						->  Optional[int]:




          '''simple docstring'''
          super().__init__(**snake_case)

          requires_backends(self  ,							'vision')
          self.check_model_type(
              TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
              if self.framework == 'tf'
              else MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING)






       def __call__(      self  ,							snake_case  ,							**snake_case)						->  str:




          '''simple docstring'''
          return super().__call__(snake_case  ,							**snake_case)






       def       lowerCAmelCase		(      self  ,							**snake_case)						->  int:




          '''simple docstring'''
          _UpperCAmelCase       :       str							   ={}
          if "candidate_labels" in kwargs:
             _UpperCAmelCase       :       Union[str, Any]							   =kwargs['candidate_labels']
          if "hypothesis_template" in kwargs:
             _UpperCAmelCase       :       List[Any]							   =kwargs['hypothesis_template']

          return preprocess_params, {}, {}






       def       lowerCAmelCase		(      self  ,							snake_case  ,							snake_case=None  ,							snake_case="This is a photo of {}.")						->  Any:




          '''simple docstring'''
          _UpperCAmelCase       :       Optional[Any]							   =load_image(snake_case)
          _UpperCAmelCase       :       Union[str, Any]							   =self.image_processor(images=[image]  ,							return_tensors=self.framework)
          _UpperCAmelCase       :       Union[str, Any]							   =candidate_labels
          _UpperCAmelCase       :       List[Any]							   =[hypothesis_template.format(snake_case) for x in candidate_labels]
          _UpperCAmelCase       :       str							   =self.tokenizer(snake_case  ,							return_tensors=self.framework  ,							padding=snake_case)
          _UpperCAmelCase       :       Any							   =[text_inputs]
          return inputs






       def       lowerCAmelCase		(      self  ,							snake_case)						->  str:




          '''simple docstring'''
          _UpperCAmelCase       :       List[str]							   =model_inputs.pop('candidate_labels')
          _UpperCAmelCase       :       Tuple							   =model_inputs.pop('text_inputs')
          if isinstance(text_inputs[0]  ,							snake_case):
             _UpperCAmelCase       :       Any							   =text_inputs[0]
          else:
             # Batching case.
             _UpperCAmelCase       :       str							   =text_inputs[0][0]

          _UpperCAmelCase       :       Any							   =self.model(**snake_case  ,							**snake_case)

          _UpperCAmelCase       :       List[str]							   ={
              'candidate_labels': candidate_labels,
              'logits': outputs.logits_per_image,
          }
          return model_outputs






       def       lowerCAmelCase		(      self  ,							snake_case)						->  Optional[int]:




          '''simple docstring'''
          _UpperCAmelCase       :       str							   =model_outputs.pop('candidate_labels')
          _UpperCAmelCase       :       Union[str, Any]							   =model_outputs['logits'][0]
          if self.framework == "pt":
             _UpperCAmelCase       :       Dict							   =logits.softmax(dim=-1).squeeze(-1)
             _UpperCAmelCase       :       Union[str, Any]							   =probs.tolist()
             if not isinstance(snake_case  ,							snake_case):
                _UpperCAmelCase       :       Union[str, Any]							   =[scores]
          elif self.framework == "tf":
             _UpperCAmelCase       :       Dict							   =stable_softmax(snake_case  ,							axis=-1)
             _UpperCAmelCase       :       str							   =probs.numpy().tolist()
          else:
             raise ValueError(f"Unsupported framework: {self.framework}")

          _UpperCAmelCase       :       List[str]							   =[
              {'score': score, 'label': candidate_label}
              for score, candidate_label in sorted(zip(snake_case  ,							snake_case)  ,							key=lambda snake_case: -x[0])
          ]
          return result

