





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

snake_case_   :   Optional[Any]        =       logging.get_logger(__name__)







@add_end_docstrings(lowerCAmelCase_     )
class       snake_case__  ( lowerCAmelCase_     ):







			def __init__(			self			: Union[str, Any]			,  **lowercase			: int  ):





								'''simple docstring'''



								super().__init__(**lowercase  )

								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			: Optional[int]			,  lowercase			: Tuple			,  **lowercase			: Optional[int]  ):





								'''simple docstring'''



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







			def 			__lowerCAmelCase				(			self			: Optional[Any]			,  **lowercase			: List[str]  ):





								'''simple docstring'''



								UpperCAmelCase			:   Tuple      =				{}
								if "candidate_labels" in kwargs:
													UpperCAmelCase			:   int      =				kwargs["candidate_labels"]
								if "hypothesis_template" in kwargs:
													UpperCAmelCase			:   Optional[int]      =				kwargs["hypothesis_template"]

								return preprocess_params, {}, {}







			def 			__lowerCAmelCase				(			self			: Tuple			,  lowercase			: Union[str, Any]			,  lowercase			: Dict=None			,  lowercase			: int="This is a photo of {}."  ):





								'''simple docstring'''



								UpperCAmelCase			:   Optional[int]      =				load_image(lowercase  )
								UpperCAmelCase			:   int      =				self.image_processor(images=[image]			,  return_tensors=self.framework  )
								UpperCAmelCase			:   Optional[Any]      =				candidate_labels
								UpperCAmelCase			:   Union[str, Any]      =				[hypothesis_template.format(lowercase  ) for x in candidate_labels]
								UpperCAmelCase			:   int      =				self.tokenizer(lowercase			,  return_tensors=self.framework			,  padding=lowercase  )
								UpperCAmelCase			:   Optional[Any]      =				[text_inputs]
								return inputs







			def 			__lowerCAmelCase				(			self			: Optional[Any]			,  lowercase			: Optional[Any]  ):





								'''simple docstring'''



								UpperCAmelCase			:   Dict      =				model_inputs.pop("candidate_labels"  )
								UpperCAmelCase			:   Tuple      =				model_inputs.pop("text_inputs"  )
								if isinstance(text_inputs[0]			,  lowercase  ):
													UpperCAmelCase			:   Optional[int]      =				text_inputs[0]
								else:
													# Batching case.
													UpperCAmelCase			:   Tuple      =				text_inputs[0][0]

								UpperCAmelCase			:   Tuple      =				self.model(**lowercase			,  **lowercase  )

								UpperCAmelCase			:   Optional[int]      =				{
								    "candidate_labels": candidate_labels,
								    "logits": outputs.logits_per_image,
								}
								return model_outputs







			def 			__lowerCAmelCase				(			self			: int			,  lowercase			: str  ):





								'''simple docstring'''



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

								UpperCAmelCase			:   Union[str, Any]      =				[
								    {"score": score, "label": candidate_label}
								    for score, candidate_label in sorted(zip(lowercase			,  lowercase  )			,  key=lambda lowercase  : -x[0]  )
								]
								return result





"""simple docstring"""






import math



def   lowercase_	(    _lowercase       :		int	):




					'''simple docstring'''






					return math.sqrt(_lowercase	) * math.sqrt(_lowercase	) == num



def   lowercase_	(    _lowercase       :		int	):




					'''simple docstring'''






					UpperCAmelCase			:   List[Any]      =				0
					UpperCAmelCase			:   Tuple      =				n
					while left <= right:
										UpperCAmelCase			:   Tuple      =				(left + right) // 2
										if mid**2 == n:
															return True
										elif mid**2 > n:
															UpperCAmelCase			:   List[str]      =				mid - 1
										else:
															UpperCAmelCase			:   Any      =				mid + 1
					return False


if __name__ == "__main__":
	import doctest

	doctest.testmod()


