"""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

_UpperCamelCase   :						Optional[int]      =    logging.get_logger(__name__)


@add_end_docstrings(_a)
class      UpperCAmelCase_	(    _a):


      def __init__(   self		,			**a							)       ->					int:
             super().__init__(**a							)

             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		,			a		,			**a							)       ->					str:
             return super().__call__(a		,			**a							)


      def 		_UpperCAmelCase       (   self		,			**a							)       ->					Any:
             lowercase__			:		str       =	{}
             if "candidate_labels" in kwargs:
                    lowercase__			:		List[str]       =	kwargs['candidate_labels']
             if "hypothesis_template" in kwargs:
                    lowercase__			:		Optional[Any]       =	kwargs['hypothesis_template']

             return preprocess_params, {}, {}


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


      def 		_UpperCAmelCase       (   self		,			a							)       ->					str:
             lowercase__			:		Dict       =	model_inputs.pop('candidate_labels'							)
             lowercase__			:		int       =	model_inputs.pop('text_inputs'							)
             if isinstance(text_inputs[0]		,			a							):
                    lowercase__			:		str       =	text_inputs[0]
             else:
                    # Batching case.
                    lowercase__			:		Dict       =	text_inputs[0][0]

             lowercase__			:		Optional[Any]       =	self.model(**a		,			**a							)

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


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

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


"""simple docstring"""






from ..utils import DummyObject, requires_backends





class  _A						(       metaclass=lowerCAmelCase	):
      snake_case__			:   str							  =	['torch', 'transformers', 'onnx']

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







            """simple docstring"""



            requires_backends(self		,       ["""torch""", """transformers""", """onnx"""]						)

      @classmethod
      def 				A__       (						cls		,       *__lowerCAmelCase		,       **__lowerCAmelCase						):







            """simple docstring"""



            requires_backends(cls		,       ["""torch""", """transformers""", """onnx"""]						)







      @classmethod
      def 				A__       (						cls		,       *__lowerCAmelCase		,       **__lowerCAmelCase						):







            """simple docstring"""



            requires_backends(cls		,       ["""torch""", """transformers""", """onnx"""]						)






class  _A						(       metaclass=lowerCAmelCase	):
      snake_case__			:   List[Any]							  =	['torch', 'transformers', 'onnx']

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







            """simple docstring"""



            requires_backends(self		,       ["""torch""", """transformers""", """onnx"""]						)

      @classmethod
      def 				A__       (						cls		,       *__lowerCAmelCase		,       **__lowerCAmelCase						):







            """simple docstring"""



            requires_backends(cls		,       ["""torch""", """transformers""", """onnx"""]						)







      @classmethod
      def 				A__       (						cls		,       *__lowerCAmelCase		,       **__lowerCAmelCase						):







            """simple docstring"""



            requires_backends(cls		,       ["""torch""", """transformers""", """onnx"""]						)






class  _A						(       metaclass=lowerCAmelCase	):
      snake_case__			:   Optional[Any]							  =	['torch', 'transformers', 'onnx']

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







            """simple docstring"""



            requires_backends(self		,       ["""torch""", """transformers""", """onnx"""]						)

      @classmethod
      def 				A__       (						cls		,       *__lowerCAmelCase		,       **__lowerCAmelCase						):







            """simple docstring"""



            requires_backends(cls		,       ["""torch""", """transformers""", """onnx"""]						)







      @classmethod
      def 				A__       (						cls		,       *__lowerCAmelCase		,       **__lowerCAmelCase						):







            """simple docstring"""



            requires_backends(cls		,       ["""torch""", """transformers""", """onnx"""]						)






class  _A						(       metaclass=lowerCAmelCase	):
      snake_case__			:   int							  =	['torch', 'transformers', 'onnx']

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







            """simple docstring"""



            requires_backends(self		,       ["""torch""", """transformers""", """onnx"""]						)

      @classmethod
      def 				A__       (						cls		,       *__lowerCAmelCase		,       **__lowerCAmelCase						):







            """simple docstring"""



            requires_backends(cls		,       ["""torch""", """transformers""", """onnx"""]						)







      @classmethod
      def 				A__       (						cls		,       *__lowerCAmelCase		,       **__lowerCAmelCase						):







            """simple docstring"""



            requires_backends(cls		,       ["""torch""", """transformers""", """onnx"""]						)






class  _A						(       metaclass=lowerCAmelCase	):
      snake_case__			:   List[str]							  =	['torch', 'transformers', 'onnx']

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







            """simple docstring"""



            requires_backends(self		,       ["""torch""", """transformers""", """onnx"""]						)

      @classmethod
      def 				A__       (						cls		,       *__lowerCAmelCase		,       **__lowerCAmelCase						):







            """simple docstring"""



            requires_backends(cls		,       ["""torch""", """transformers""", """onnx"""]						)







      @classmethod
      def 				A__       (						cls		,       *__lowerCAmelCase		,       **__lowerCAmelCase						):







            """simple docstring"""



            requires_backends(cls		,       ["""torch""", """transformers""", """onnx"""]						)






class  _A						(       metaclass=lowerCAmelCase	):
      snake_case__			:   Optional[Any]							  =	['torch', 'transformers', 'onnx']

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







            """simple docstring"""



            requires_backends(self		,       ["""torch""", """transformers""", """onnx"""]						)

      @classmethod
      def 				A__       (						cls		,       *__lowerCAmelCase		,       **__lowerCAmelCase						):







            """simple docstring"""



            requires_backends(cls		,       ["""torch""", """transformers""", """onnx"""]						)







      @classmethod
      def 				A__       (						cls		,       *__lowerCAmelCase		,       **__lowerCAmelCase						):







            """simple docstring"""



            requires_backends(cls		,       ["""torch""", """transformers""", """onnx"""]						)



