



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

__snake_case						:List[str]      		=     logging.get_logger(__name__)

@add_end_docstrings(__UpperCAmelCase    )
class     _A       (	__UpperCAmelCase    ):



       def __init__(		self							:     Any     ,		**__SCREAMING_SNAKE_CASE							:     Tuple):


           '''simple docstring'''

           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							:     Tuple     ,		__SCREAMING_SNAKE_CASE							:     Union[str, List[str], "Image", List["Image"]]     ,		**__SCREAMING_SNAKE_CASE							:     Tuple):


           '''simple docstring'''

           return super().__call__(__SCREAMING_SNAKE_CASE     ,		**__SCREAMING_SNAKE_CASE)



       def 		_lowerCamelCase			(		self							:     Dict     ,		**__SCREAMING_SNAKE_CASE							:     Optional[int]):


           '''simple docstring'''

           __a						       =				{}
           if "candidate_labels" in kwargs:
               __a						       =				kwargs['''candidate_labels''']
           if "hypothesis_template" in kwargs:
               __a						       =				kwargs['''hypothesis_template''']

           return preprocess_params, {}, {}



       def 		_lowerCamelCase			(		self							:     Optional[Any]     ,		__SCREAMING_SNAKE_CASE							:     Union[str, Any]     ,		__SCREAMING_SNAKE_CASE							:     List[str]=None     ,		__SCREAMING_SNAKE_CASE							:     Dict="This is a photo of {}."):


           '''simple docstring'''

           __a						       =				load_image(__SCREAMING_SNAKE_CASE)
           __a						       =				self.image_processor(images=[image]     ,		return_tensors=self.framework)
           __a						       =				candidate_labels
           __a						       =				[hypothesis_template.format(__SCREAMING_SNAKE_CASE) for x in candidate_labels]
           __a						       =				self.tokenizer(__SCREAMING_SNAKE_CASE     ,		return_tensors=self.framework     ,		padding=__SCREAMING_SNAKE_CASE)
           __a						       =				[text_inputs]
           return inputs



       def 		_lowerCamelCase			(		self							:     Dict     ,		__SCREAMING_SNAKE_CASE							:     Optional[Any]):


           '''simple docstring'''

           __a						       =				model_inputs.pop('''candidate_labels''')
           __a						       =				model_inputs.pop('''text_inputs''')
           if isinstance(text_inputs[0]     ,		__SCREAMING_SNAKE_CASE):
               __a						       =				text_inputs[0]
           else:
               # Batching case.
               __a						       =				text_inputs[0][0]

           __a						       =				self.model(**__SCREAMING_SNAKE_CASE     ,		**__SCREAMING_SNAKE_CASE)

           __a						       =				{
               '''candidate_labels''': candidate_labels,
               '''logits''': outputs.logits_per_image,
           }
           return model_outputs





       def 		_lowerCamelCase			(		self							:     List[Any]     ,		__SCREAMING_SNAKE_CASE							:     Dict):


           '''simple docstring'''

           __a						       =				model_outputs.pop('''candidate_labels''')
           __a						       =				model_outputs['''logits'''][0]
           if self.framework == "pt":
               __a						       =				logits.softmax(dim=-1).squeeze(-1)
               __a						       =				probs.tolist()
               if not isinstance(__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE):
                   __a						       =				[scores]
           elif self.framework == "tf":
               __a						       =				stable_softmax(__SCREAMING_SNAKE_CASE     ,		axis=-1)
               __a						       =				probs.numpy().tolist()
           else:
               raise ValueError(F'Unsupported framework: {self.framework}')

           __a						       =				[
               {'''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 unittest

from diffusers.pipelines.pipeline_utils import is_safetensors_compatible

class     _A       (	unittest.TestCase    ):



       def 		_lowerCamelCase			(		self							:     List[Any]):


           '''simple docstring'''

           __a						       =				[
               '''safety_checker/pytorch_model.bin''',
               '''safety_checker/model.safetensors''',
               '''vae/diffusion_pytorch_model.bin''',
               '''vae/diffusion_pytorch_model.safetensors''',
               '''text_encoder/pytorch_model.bin''',
               '''text_encoder/model.safetensors''',
               '''unet/diffusion_pytorch_model.bin''',
               '''unet/diffusion_pytorch_model.safetensors''',
           ]
           self.assertTrue(is_safetensors_compatible(__SCREAMING_SNAKE_CASE))



       def 		_lowerCamelCase			(		self							:     Optional[Any]):


           '''simple docstring'''

           __a						       =				[
               '''unet/diffusion_pytorch_model.bin''',
               '''unet/diffusion_pytorch_model.safetensors''',
           ]
           self.assertTrue(is_safetensors_compatible(__SCREAMING_SNAKE_CASE))



       def 		_lowerCamelCase			(		self							:     Optional[Any]):


           '''simple docstring'''

           __a						       =				[
               '''safety_checker/pytorch_model.bin''',
               '''safety_checker/model.safetensors''',
               '''vae/diffusion_pytorch_model.bin''',
               '''vae/diffusion_pytorch_model.safetensors''',
               '''text_encoder/pytorch_model.bin''',
               '''text_encoder/model.safetensors''',
               '''unet/diffusion_pytorch_model.bin''',
               # Removed: 'unet/diffusion_pytorch_model.safetensors',
           ]
           self.assertFalse(is_safetensors_compatible(__SCREAMING_SNAKE_CASE))



       def 		_lowerCamelCase			(		self							:     Dict):


           '''simple docstring'''

           __a						       =				[
               '''text_encoder/pytorch_model.bin''',
               '''text_encoder/model.safetensors''',
           ]
           self.assertTrue(is_safetensors_compatible(__SCREAMING_SNAKE_CASE))



       def 		_lowerCamelCase			(		self							:     Union[str, Any]):


           '''simple docstring'''

           __a						       =				[
               '''safety_checker/pytorch_model.bin''',
               '''safety_checker/model.safetensors''',
               '''vae/diffusion_pytorch_model.bin''',
               '''vae/diffusion_pytorch_model.safetensors''',
               '''text_encoder/pytorch_model.bin''',
               # Removed: 'text_encoder/model.safetensors',
               '''unet/diffusion_pytorch_model.bin''',
               '''unet/diffusion_pytorch_model.safetensors''',
           ]
           self.assertFalse(is_safetensors_compatible(__SCREAMING_SNAKE_CASE))



       def 		_lowerCamelCase			(		self							:     Tuple):


           '''simple docstring'''

           __a						       =				[
               '''safety_checker/pytorch_model.fp16.bin''',
               '''safety_checker/model.fp16.safetensors''',
               '''vae/diffusion_pytorch_model.fp16.bin''',
               '''vae/diffusion_pytorch_model.fp16.safetensors''',
               '''text_encoder/pytorch_model.fp16.bin''',
               '''text_encoder/model.fp16.safetensors''',
               '''unet/diffusion_pytorch_model.fp16.bin''',
               '''unet/diffusion_pytorch_model.fp16.safetensors''',
           ]
           __a						       =				'''fp16'''
           self.assertTrue(is_safetensors_compatible(__SCREAMING_SNAKE_CASE     ,		variant=__SCREAMING_SNAKE_CASE))



       def 		_lowerCamelCase			(		self							:     Dict):


           '''simple docstring'''

           __a						       =				[
               '''unet/diffusion_pytorch_model.fp16.bin''',
               '''unet/diffusion_pytorch_model.fp16.safetensors''',
           ]
           __a						       =				'''fp16'''
           self.assertTrue(is_safetensors_compatible(__SCREAMING_SNAKE_CASE     ,		variant=__SCREAMING_SNAKE_CASE))



       def 		_lowerCamelCase			(		self							:     Optional[Any]):


           '''simple docstring'''

           __a						       =				[
               '''unet/diffusion_pytorch_model.bin''',
               '''unet/diffusion_pytorch_model.safetensors''',
           ]
           __a						       =				'''fp16'''
           self.assertTrue(is_safetensors_compatible(__SCREAMING_SNAKE_CASE     ,		variant=__SCREAMING_SNAKE_CASE))



       def 		_lowerCamelCase			(		self							:     Union[str, Any]):


           '''simple docstring'''

           __a						       =				[
               '''safety_checker/pytorch_model.fp16.bin''',
               '''safety_checker/model.fp16.safetensors''',
               '''vae/diffusion_pytorch_model.fp16.bin''',
               '''vae/diffusion_pytorch_model.fp16.safetensors''',
               '''text_encoder/pytorch_model.fp16.bin''',
               '''text_encoder/model.fp16.safetensors''',
               '''unet/diffusion_pytorch_model.fp16.bin''',
               # Removed: 'unet/diffusion_pytorch_model.fp16.safetensors',
           ]
           __a						       =				'''fp16'''
           self.assertFalse(is_safetensors_compatible(__SCREAMING_SNAKE_CASE     ,		variant=__SCREAMING_SNAKE_CASE))



       def 		_lowerCamelCase			(		self							:     Dict):


           '''simple docstring'''

           __a						       =				[
               '''text_encoder/pytorch_model.fp16.bin''',
               '''text_encoder/model.fp16.safetensors''',
           ]
           __a						       =				'''fp16'''
           self.assertTrue(is_safetensors_compatible(__SCREAMING_SNAKE_CASE     ,		variant=__SCREAMING_SNAKE_CASE))



       def 		_lowerCamelCase			(		self							:     List[str]):


           '''simple docstring'''

           __a						       =				[
               '''text_encoder/pytorch_model.bin''',
               '''text_encoder/model.safetensors''',
           ]
           __a						       =				'''fp16'''
           self.assertTrue(is_safetensors_compatible(__SCREAMING_SNAKE_CASE     ,		variant=__SCREAMING_SNAKE_CASE))





       def 		_lowerCamelCase			(		self							:     List[str]):


           '''simple docstring'''

           __a						       =				[
               '''safety_checker/pytorch_model.fp16.bin''',
               '''safety_checker/model.fp16.safetensors''',
               '''vae/diffusion_pytorch_model.fp16.bin''',
               '''vae/diffusion_pytorch_model.fp16.safetensors''',
               '''text_encoder/pytorch_model.fp16.bin''',
               # 'text_encoder/model.fp16.safetensors',
               '''unet/diffusion_pytorch_model.fp16.bin''',
               '''unet/diffusion_pytorch_model.fp16.safetensors''',
           ]
           __a						       =				'''fp16'''
           self.assertFalse(is_safetensors_compatible(__SCREAMING_SNAKE_CASE     ,		variant=__SCREAMING_SNAKE_CASE))





