



"""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   =	logging.get_logger(__name__)
@add_end_docstrings(SCREAMING_SNAKE_CASE_				)
class     a		(     SCREAMING_SNAKE_CASE_				):



  def __init__(  self       :    Optional[int]				,						**lowerCAmelCase       :    List[Any]						)   ->    Tuple:


     '''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       :    List[Any]				,						lowerCAmelCase       :    Tuple				,						**lowerCAmelCase       :    Optional[int]						)   ->    str:


     '''simple docstring'''




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



  def 		lowerCamelCase__				(  self       :    Optional[Any]				,						**lowerCAmelCase       :    Any						)   ->    str:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:       List[Any]   	={}
     if "candidate_labels" in kwargs:
        SCREAMING_SNAKE_CASE_:       int   	=kwargs["""candidate_labels"""]
     if "hypothesis_template" in kwargs:
        SCREAMING_SNAKE_CASE_:       Optional[int]   	=kwargs["""hypothesis_template"""]

     return preprocess_params, {}, {}



  def 		lowerCamelCase__				(  self       :    Union[str, Any]				,						lowerCAmelCase       :    Optional[Any]				,						lowerCAmelCase       :    Tuple=None				,						lowerCAmelCase       :    List[Any]="This is a photo of {}."						)   ->    int:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:       str   	=load_image(__UpperCAmelCase						)
     SCREAMING_SNAKE_CASE_:       Dict   	=self.image_processor(images=[image]				,						return_tensors=self.framework						)
     SCREAMING_SNAKE_CASE_:       List[Any]   	=candidate_labels
     SCREAMING_SNAKE_CASE_:       List[str]   	=[hypothesis_template.format(__UpperCAmelCase						) for x in candidate_labels]
     SCREAMING_SNAKE_CASE_:       Optional[Any]   	=self.tokenizer(__UpperCAmelCase				,						return_tensors=self.framework				,						padding=__UpperCAmelCase						)
     SCREAMING_SNAKE_CASE_:       Tuple   	=[text_inputs]
     return inputs



  def 		lowerCamelCase__				(  self       :    str				,						lowerCAmelCase       :    int						)   ->    Union[str, Any]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:       Tuple   	=model_inputs.pop("""candidate_labels"""						)
     SCREAMING_SNAKE_CASE_:       Union[str, Any]   	=model_inputs.pop("""text_inputs"""						)
     if isinstance(text_inputs[0]				,						__UpperCAmelCase						):
        SCREAMING_SNAKE_CASE_:       int   	=text_inputs[0]
     else:
        # Batching case.
        SCREAMING_SNAKE_CASE_:       Dict   	=text_inputs[0][0]

     SCREAMING_SNAKE_CASE_:       Any   	=self.model(**__UpperCAmelCase				,						**__UpperCAmelCase						)

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





  def 		lowerCamelCase__				(  self       :    int				,						lowerCAmelCase       :    Optional[Any]						)   ->    Any:


     '''simple docstring'''




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

     SCREAMING_SNAKE_CASE_:       Tuple   	=[
         {"""score""": score, """label""": candidate_label}
         for score, candidate_label in sorted(zip(__UpperCAmelCase				,						__UpperCAmelCase						)				,						key=lambda lowerCAmelCase						: -x[0]						)
     ]
     return result








'''simple docstring'''





from maths.prime_factors import prime_factors
def    _SCREAMING_SNAKE_CASE  (       UpperCamelCase			):



    """simple docstring"""





    if not isinstance(UpperCamelCase							,    UpperCamelCase			):
        lowerCAmelCase__ :  int							      =					f"""Input value of [number={number}] must be an integer"""
        raise TypeError(UpperCamelCase			)
    if number < 1:
        raise ValueError("""Input must be a positive integer"""			)
    return -1 if len(prime_factors(UpperCamelCase			)			) % 2 else 1


if __name__ == "__main__":
 import doctest

 doctest.testmod()


