


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

lowerCamelCase						=		logging.get_logger(__name__)





@add_end_docstrings(_lowercase)
class  _a							(    _lowercase):




	def __init__( self						:		Any		, **_SCREAMING_SNAKE_CASE						:		Optional[int]		)->	List[Any]:
			super().__init__(**_SCREAMING_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						:		List[str]		, _SCREAMING_SNAKE_CASE						:		Union[str, List[str], "Image", List["Image"]]		, **_SCREAMING_SNAKE_CASE						:		List[Any]		)->	Any:
			return super().__call__(_SCREAMING_SNAKE_CASE		, **_SCREAMING_SNAKE_CASE		)




	def 							UpperCAmelCase__( self						:		Tuple		, **_SCREAMING_SNAKE_CASE						:		Any		)->	Union[str, Any]:
			lowerCAmelCase__       :      Tuple   					= {}
			if "candidate_labels" in kwargs:
					lowerCAmelCase__       :      List[str]   					= kwargs['''candidate_labels''']
			if "hypothesis_template" in kwargs:
					lowerCAmelCase__       :      int   					= kwargs['''hypothesis_template''']

			return preprocess_params, {}, {}




	def 							UpperCAmelCase__( self						:		Any		, _SCREAMING_SNAKE_CASE						:		Dict		, _SCREAMING_SNAKE_CASE						:		Union[str, Any]=None		, _SCREAMING_SNAKE_CASE						:		str="This is a photo of {}."		)->	List[Any]:
			lowerCAmelCase__       :      Tuple   					= load_image(_SCREAMING_SNAKE_CASE		)
			lowerCAmelCase__       :      List[Any]   					= self.image_processor(images=[image]		, return_tensors=self.framework		)
			lowerCAmelCase__       :      str   					= candidate_labels
			lowerCAmelCase__       :      List[Any]   					= [hypothesis_template.format(_SCREAMING_SNAKE_CASE		) for x in candidate_labels]
			lowerCAmelCase__       :      str   					= self.tokenizer(_SCREAMING_SNAKE_CASE		, return_tensors=self.framework		, padding=_SCREAMING_SNAKE_CASE		)
			lowerCAmelCase__       :      List[Any]   					= [text_inputs]
			return inputs




	def 							UpperCAmelCase__( self						:		Optional[Any]		, _SCREAMING_SNAKE_CASE						:		List[Any]		)->	List[Any]:
			lowerCAmelCase__       :      int   					= model_inputs.pop('''candidate_labels'''		)
			lowerCAmelCase__       :      Any   					= model_inputs.pop('''text_inputs'''		)
			if isinstance(text_inputs[0]		, _SCREAMING_SNAKE_CASE		):
					lowerCAmelCase__       :      Any   					= text_inputs[0]
			else:
					# Batching case.
					lowerCAmelCase__       :      Optional[int]   					= text_inputs[0][0]

			lowerCAmelCase__       :      str   					= self.model(**_SCREAMING_SNAKE_CASE		, **_SCREAMING_SNAKE_CASE		)

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




	def 							UpperCAmelCase__( self						:		int		, _SCREAMING_SNAKE_CASE						:		List[Any]		)->	Any:
			lowerCAmelCase__       :      Any   					= model_outputs.pop('''candidate_labels'''		)
			lowerCAmelCase__       :      List[str]   					= model_outputs['''logits'''][0]
			if self.framework == "pt":
					lowerCAmelCase__       :      Union[str, Any]   					= logits.softmax(dim=-1		).squeeze(-1		)
					lowerCAmelCase__       :      List[str]   					= probs.tolist()
					if not isinstance(_SCREAMING_SNAKE_CASE		, _SCREAMING_SNAKE_CASE		):
							lowerCAmelCase__       :      List[str]   					= [scores]
			elif self.framework == "tf":
					lowerCAmelCase__       :      Tuple   					= stable_softmax(_SCREAMING_SNAKE_CASE		, axis=-1		)
					lowerCAmelCase__       :      str   					= probs.numpy().tolist()
			else:
					raise ValueError(F'Unsupported framework: {self.framework}'		)

			lowerCAmelCase__       :      Any   					= [
			    {'''score''': score, '''label''': candidate_label}
			    for score, candidate_label in sorted(zip(_SCREAMING_SNAKE_CASE		, _SCREAMING_SNAKE_CASE		)		, key=lambda _SCREAMING_SNAKE_CASE		: -x[0]		)
			]
			return result








import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionPipeline
from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device


lowerCamelCase						=		False





class  _a							(    unittest.TestCase):
	pass





@nightly
@require_torch_gpu
class  _a							(    unittest.TestCase):




	def 							UpperCAmelCase__( self						:		int		)->	int:
			# clean up the VRAM after each test
			super().tearDown()
			gc.collect()
			torch.cuda.empty_cache()




	def 							UpperCAmelCase__( self						:		Any		)->	Dict:
			lowerCAmelCase__       :      int   					= VersatileDiffusionPipeline.from_pretrained('''shi-labs/versatile-diffusion'''		, torch_dtype=torch.floataa		)
			pipe.to(_SCREAMING_SNAKE_CASE		)
			pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE		)

			lowerCAmelCase__       :      List[str]   					= load_image(
			    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'''		)

			lowerCAmelCase__       :      List[str]   					= torch.manual_seed(0		)
			lowerCAmelCase__       :      List[str]   					= pipe.dual_guided(
			    prompt='''first prompt'''		, image=_SCREAMING_SNAKE_CASE		, text_to_image_strength=0.75		, generator=_SCREAMING_SNAKE_CASE		, guidance_scale=7.5		, num_inference_steps=2		, output_type='''numpy'''		, ).images

			with tempfile.TemporaryDirectory() as tmpdirname:
					pipe.save_pretrained(_SCREAMING_SNAKE_CASE		)
					lowerCAmelCase__       :      List[Any]   					= VersatileDiffusionPipeline.from_pretrained(_SCREAMING_SNAKE_CASE		, torch_dtype=torch.floataa		)
			pipe.to(_SCREAMING_SNAKE_CASE		)
			pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE		)

			lowerCAmelCase__       :      List[str]   					= generator.manual_seed(0		)
			lowerCAmelCase__       :      str   					= pipe.dual_guided(
			    prompt='''first prompt'''		, image=_SCREAMING_SNAKE_CASE		, text_to_image_strength=0.75		, generator=_SCREAMING_SNAKE_CASE		, guidance_scale=7.5		, num_inference_steps=2		, output_type='''numpy'''		, ).images

			assert np.abs(image - new_image		).sum() < 1E-5, "Models don't have the same forward pass"




	def 							UpperCAmelCase__( self						:		Tuple		)->	List[Any]:
			lowerCAmelCase__       :      Optional[int]   					= VersatileDiffusionPipeline.from_pretrained('''shi-labs/versatile-diffusion'''		, torch_dtype=torch.floataa		)
			pipe.to(_SCREAMING_SNAKE_CASE		)
			pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE		)

			lowerCAmelCase__       :      int   					= '''cyberpunk 2077'''
			lowerCAmelCase__       :      List[str]   					= load_image(
			    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'''		)
			lowerCAmelCase__       :      List[Any]   					= torch.manual_seed(0		)
			lowerCAmelCase__       :      Union[str, Any]   					= pipe.dual_guided(
			    prompt=_SCREAMING_SNAKE_CASE		, image=_SCREAMING_SNAKE_CASE		, text_to_image_strength=0.75		, generator=_SCREAMING_SNAKE_CASE		, guidance_scale=7.5		, num_inference_steps=50		, output_type='''numpy'''		, ).images

			lowerCAmelCase__       :      List[Any]   					= image[0, 253:256, 253:256, -1]

			assert image.shape == (1, 512, 512, 3)
			lowerCAmelCase__       :      List[str]   					= np.array([0.1448, 0.1619, 0.1741, 0.1086, 0.1147, 0.1128, 0.1199, 0.1165, 0.1001]		)

			assert np.abs(image_slice.flatten() - expected_slice		).max() < 1E-1

			lowerCAmelCase__       :      int   					= '''A painting of a squirrel eating a burger '''
			lowerCAmelCase__       :      Optional[Any]   					= torch.manual_seed(0		)
			lowerCAmelCase__       :      Union[str, Any]   					= pipe.text_to_image(
			    prompt=_SCREAMING_SNAKE_CASE		, generator=_SCREAMING_SNAKE_CASE		, guidance_scale=7.5		, num_inference_steps=50		, output_type='''numpy'''		).images

			lowerCAmelCase__       :      List[Any]   					= image[0, 253:256, 253:256, -1]

			assert image.shape == (1, 512, 512, 3)
			lowerCAmelCase__       :      Optional[Any]   					= np.array([0.3367, 0.3169, 0.2656, 0.3870, 0.4790, 0.3796, 0.4009, 0.4878, 0.4778]		)

			assert np.abs(image_slice.flatten() - expected_slice		).max() < 1E-1

			lowerCAmelCase__       :      Optional[Any]   					= pipe.image_variation(_SCREAMING_SNAKE_CASE		, generator=_SCREAMING_SNAKE_CASE		, output_type='''numpy'''		).images

			lowerCAmelCase__       :      List[Any]   					= image[0, 253:256, 253:256, -1]

			assert image.shape == (1, 512, 512, 3)
			lowerCAmelCase__       :      Any   					= np.array([0.3076, 0.3123, 0.3284, 0.3782, 0.3770, 0.3894, 0.4297, 0.4331, 0.4456]		)

			assert np.abs(image_slice.flatten() - expected_slice		).max() < 1E-1




