
import math
import sys




def 	A_ (							snake_case							:   int )      ->    int:



						'''simple docstring'''

						if number != int(__A ):
												raise ValueError('''the value of input must be a natural number''' )
						if number < 0:
												raise ValueError('''the value of input must not be a negative number''' )
						if number == 0:
												return 1
						__UpperCamelCase       =			[-1] * (number + 1)
						__UpperCamelCase       =			0
						for i in range(1 ,      number + 1 ):
												__UpperCamelCase       =			sys.maxsize
												__UpperCamelCase       =			int(math.sqrt(__A ) )
												for j in range(1 ,      root + 1 ):
																		__UpperCamelCase       =			1 + answers[i - (j**2)]
																		__UpperCamelCase       =			min(__A ,      __A )
												__UpperCamelCase       =			answer
						return answers[number]


if __name__ == "__main__":
				import doctest

				doctest.testmod()





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

lowercase__							:							int									=			logging.get_logger(__name__)






@add_end_docstrings(SCREAMING_SNAKE_CASE_    )
class        SCREAMING_SNAKE_CASE__    (							SCREAMING_SNAKE_CASE_    ):

							"""simple docstring"""






							def __init__(  self     ,	**SCREAMING_SNAKE_CASE_			)->   Tuple:





													'''simple docstring'''






													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     ,	SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_			)->   str:





													'''simple docstring'''






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



							def     A__		(  self     ,	**SCREAMING_SNAKE_CASE_			)->   Tuple:





													'''simple docstring'''






													__UpperCamelCase       =			{}
													if "candidate_labels" in kwargs:
																			__UpperCamelCase       =			kwargs['''candidate_labels''']
													if "hypothesis_template" in kwargs:
																			__UpperCamelCase       =			kwargs['''hypothesis_template''']

													return preprocess_params, {}, {}



							def     A__		(  self     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_=None     ,	SCREAMING_SNAKE_CASE_="This is a photo of {}."			)->   Optional[int]:





													'''simple docstring'''






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



							def     A__		(  self     ,	SCREAMING_SNAKE_CASE_			)->   Dict:





													'''simple docstring'''






													__UpperCamelCase       =			model_inputs.pop('''candidate_labels'''			)
													__UpperCamelCase       =			model_inputs.pop('''text_inputs'''			)
													if isinstance(text_inputs[0]     ,	SCREAMING_SNAKE_CASE_			):
																			__UpperCamelCase       =			text_inputs[0]
													else:
																			# Batching case.
																			__UpperCamelCase       =			text_inputs[0][0]

													__UpperCamelCase       =			self.model(**SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_			)

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



							def     A__		(  self     ,	SCREAMING_SNAKE_CASE_			)->   int:





													'''simple docstring'''






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

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




