

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

_a				:		str	   =       logging.get_logger(__name__)
@add_end_docstrings(lowerCAmelCase__		)
class 						__A							(			lowerCAmelCase__		):






     def __init__(		self    ,					**a__  ):
       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    ,					a__    ,					**a__  ):
       return super().__call__(_SCREAMING_SNAKE_CASE    ,					**_SCREAMING_SNAKE_CASE  )






     def    __A  (		self    ,					**a__  ):
       _lowerCAmelCase						:	Union[str, Any]        =					{}
       if "candidate_labels" in kwargs:
         _lowerCAmelCase						:	Optional[Any]        =					kwargs["""candidate_labels"""]
       if "hypothesis_template" in kwargs:
         _lowerCAmelCase						:	Optional[Any]        =					kwargs["""hypothesis_template"""]

       return preprocess_params, {}, {}






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






     def    __A  (		self    ,					a__  ):
       _lowerCAmelCase						:	Any        =					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						:	Union[str, Any]        =					text_inputs[0][0]

       _lowerCAmelCase						:	List[str]        =					self.model(**_SCREAMING_SNAKE_CASE    ,					**_SCREAMING_SNAKE_CASE  )

       _lowerCAmelCase						:	Union[str, Any]        =					{
           """candidate_labels""": candidate_labels,
           """logits""": outputs.logits_per_image,
       }
       return model_outputs






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

       _lowerCAmelCase						:	List[str]        =					[
           {"""score""": score, """label""": candidate_label}
           for score, candidate_label in sorted(zip(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE  )    ,					key=lambda a__  : -x[0]  )
       ]
       return result





"""simple docstring"""




def 							SCREAMING_SNAKE_CASE					(      _lowerCamelCase						:	int = 100     )	->				int:
  _lowerCAmelCase						:	Optional[Any]        =					n * (n + 1) * (2 * n + 1) / 6
  _lowerCAmelCase						:	Tuple        =					(n * (n + 1) / 2) ** 2
  return int(square_of_sum - sum_of_squares     )


if __name__ == "__main__":
     print(F"""{solution() = }""")




