


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							:					List[Any]     						=     logging.get_logger(__name__)






@add_end_docstrings(_a   )
class     UpperCamelCase(  _a   ):


					def __init__(						self	:		Dict     ,    **SCREAMING_SNAKE_CASE	:		List[str]						)			->	Any:




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




											'''simple docstring'''

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


					def 							SCREAMING_SNAKE_CASE_  (						self	:		Any     ,    **SCREAMING_SNAKE_CASE	:		Any						)			->	Any:




											'''simple docstring'''

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

											return preprocess_params, {}, {}


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




											'''simple docstring'''

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


					def 							SCREAMING_SNAKE_CASE_  (						self	:		str     ,    SCREAMING_SNAKE_CASE	:		Optional[int]						)			->	int:




											'''simple docstring'''

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

											__snake_case				=		self.model(**SCREAMING_SNAKE_CASE     ,    **SCREAMING_SNAKE_CASE						)

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






					def 							SCREAMING_SNAKE_CASE_  (						self	:		List[Any]     ,    SCREAMING_SNAKE_CASE	:		List[str]						)			->	int:




											'''simple docstring'''

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

											__snake_case				=		[
											    {"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









def 	_lowerCAmelCase       (    _lowerCAmelCase )						->	int:


						'''simple docstring'''






						__snake_case				=		abs(_lowerCAmelCase )
						__snake_case				=		0
						while n > 0:
												res += n % 10
												n //= 10
						return res





def 	_lowerCAmelCase       (    _lowerCAmelCase )						->	int:


						'''simple docstring'''






						__snake_case				=		abs(_lowerCAmelCase )
						return n if n < 10 else n % 10 + sum_of_digits(n // 10 )





def 	_lowerCAmelCase       (    _lowerCAmelCase )						->	int:


						'''simple docstring'''






						return sum(int(_lowerCAmelCase ) for c in str(abs(_lowerCAmelCase ) ) )





def 	_lowerCAmelCase       (    )						->	None:


						'''simple docstring'''






						from collections.abc import Callable
						from timeit import timeit

						def benchmark_a_function(_lowerCAmelCase      ,	_lowerCAmelCase ) -> None:
												__snake_case				=		F'''{func.__name__}({value})'''
												__snake_case				=		timeit(F'''__main__.{call}'''      ,	setup="import __main__" )
												print(F'''{call:56} = {func(_lowerCAmelCase )} -- {timing:.4f} seconds''' )

						for value in (26_2144, 1125_8999_0684_2624, 126_7650_6002_2822_9401_4967_0320_5376):
												for func in (sum_of_digits, sum_of_digits_recursion, sum_of_digits_compact):
																		benchmark_a_function(_lowerCAmelCase      ,	_lowerCAmelCase )
												print()


if __name__ == "__main__":
	import doctest

	doctest.testmod()
	benchmark()





