

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

SCREAMING_SNAKE_CASE_					:	List[Any]							  =						logging.get_logger(__name__)



@add_end_docstrings(_lowerCamelCase				)
class 					a						(       _lowerCamelCase				):



    """simple docstring"""



    def __init__(     self: Optional[int]    ,						**UpperCamelCase: int       ):






         """simple docstring"""


         super().__init__(**UpperCamelCase       )

         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: Dict    ,						UpperCamelCase: Union[str, List[str], "Image", List["Image"]]    ,						**UpperCamelCase: List[Any]       ):






         """simple docstring"""


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

    def 				UpperCamelCase	(     self: Union[str, Any]    ,						**UpperCamelCase: str       ):






         """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 				UpperCamelCase	(     self: Optional[int]    ,						UpperCamelCase: Optional[int]    ,						UpperCamelCase: Dict=None    ,						UpperCamelCase: Dict="This is a photo of {}."       ):






         """simple docstring"""


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

    def 				UpperCamelCase	(     self: Union[str, Any]    ,						UpperCamelCase: List[str]       ):






         """simple docstring"""


         A__										=  model_inputs.pop("""candidate_labels"""       )
         A__										=  model_inputs.pop("""text_inputs"""       )
         if isinstance(text_inputs[0]    ,						UpperCamelCase       ):
              A__										=  text_inputs[0]
         else:
              # Batching case.
              A__										=  text_inputs[0][0]

         A__										=  self.model(**UpperCamelCase    ,						**UpperCamelCase       )

         A__										=  {
             """candidate_labels""": candidate_labels,
             """logits""": outputs.logits_per_image,
         }
         return model_outputs





    def 				UpperCamelCase	(     self: List[str]    ,						UpperCamelCase: int       ):






         """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(UpperCamelCase    ,						UpperCamelCase       ):
                   A__										=  [scores]
         elif self.framework == "tf":
              A__										=  stable_softmax(UpperCamelCase    ,						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(UpperCamelCase    ,						UpperCamelCase       )    ,						key=lambda UpperCamelCase       : -x[0]       )
         ]
         return result









"""simple docstring"""



import baseaa





def  _snake_case     (    UpperCAmelCase_  :					str			):
     return baseaa.aaaencode(string.encode("""utf-8"""			)			)





def  _snake_case     (    UpperCAmelCase_  :					bytes			):
     return baseaa.aaadecode(UpperCAmelCase_			).decode("""utf-8"""			)


if __name__ == "__main__":
 import doctest

 doctest.testmod()






