






"""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(snake_case__   )
class    lowercase__			(							snake_case__   ):


			def __init__(      self     :  Tuple   ,       **snake_case__     :  Any					):
										super().__init__(**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     :  Union[str, Any]   ,       snake_case__     :  Union[str, List[str], "Image", List["Image"]]   ,       **snake_case__     :  List[str]					):
										return super().__call__(snake_case__   ,       **snake_case__					)


			def 	UpperCAmelCase__						(      self     :  Tuple   ,       **snake_case__     :  int					):
										lowerCamelCase_       :							int									={}
										if "candidate_labels" in kwargs:
																	lowerCamelCase_       :							Any									=kwargs["""candidate_labels"""]
										if "hypothesis_template" in kwargs:
																	lowerCamelCase_       :							List[Any]									=kwargs["""hypothesis_template"""]

										return preprocess_params, {}, {}


			def 	UpperCAmelCase__						(      self     :  int   ,       snake_case__     :  Optional[int]   ,       snake_case__     :  List[str]=None   ,       snake_case__     :  List[str]="This is a photo of {}."					):
										lowerCamelCase_       :							int									=load_image(snake_case__					)
										lowerCamelCase_       :							Union[str, Any]									=self.image_processor(images=[image]   ,       return_tensors=self.framework					)
										lowerCamelCase_       :							Union[str, Any]									=candidate_labels
										lowerCamelCase_       :							str									=[hypothesis_template.format(snake_case__					) for x in candidate_labels]
										lowerCamelCase_       :							str									=self.tokenizer(snake_case__   ,       return_tensors=self.framework   ,       padding=snake_case__					)
										lowerCamelCase_       :							List[str]									=[text_inputs]
										return inputs


			def 	UpperCAmelCase__						(      self     :  int   ,       snake_case__     :  str					):
										lowerCamelCase_       :							List[Any]									=model_inputs.pop("candidate_labels"					)
										lowerCamelCase_       :							str									=model_inputs.pop("text_inputs"					)
										if isinstance(text_inputs[0]   ,       snake_case__					):
																	lowerCamelCase_       :							Dict									=text_inputs[0]
										else:
																	# Batching case.
																	lowerCamelCase_       :							int									=text_inputs[0][0]

										lowerCamelCase_       :							Dict									=self.model(**snake_case__   ,       **snake_case__					)

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


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

										lowerCamelCase_       :							int									=[
										    {"""score""": score, """label""": candidate_label}
										    for score, candidate_label in sorted(zip(snake_case__   ,       snake_case__					)   ,       key=lambda snake_case__					: -x[0]					)
										]
										return result







"""simple docstring"""






import numpy as np

UpperCAmelCase    :     Optional[Any]   						=		[
    ['a', 'b', 'c', 'd', 'e'],
    ['f', 'g', 'h', 'i', 'k'],
    ['l', 'm', 'n', 'o', 'p'],
    ['q', 'r', 's', 't', 'u'],
    ['v', 'w', 'x', 'y', 'z'],
]





class      lowerCamelCase__   :







					"""simple docstring"""


					def __init__(       self    : Union[str, Any]  ):
												'''simple docstring'''






												__UpperCAmelCase   :						List[Any]      							=					np.array(UpperCamelCase  )






					def lowerCamelCase__							(       self    : int ,       UpperCamelCase    : str  ):
												'''simple docstring'''






												__UpperCAmelCase							,__UpperCAmelCase   :						Optional[Any]      							=					np.where(letter == self.SQUARE  )
												__UpperCAmelCase   :						Optional[Any]      							=					np.concatenate([indexa + 1, indexa + 1]  )
												return indexes






					def lowerCamelCase__							(       self    : Any ,       UpperCamelCase    : int ,       UpperCamelCase    : int  ):
												'''simple docstring'''






												__UpperCAmelCase   :						Tuple      							=					self.SQUARE[indexa - 1, indexa - 1]
												return letter






					def lowerCamelCase__							(       self    : Optional[int] ,       UpperCamelCase    : str  ):
												'''simple docstring'''






												__UpperCAmelCase   :						str      							=					message.lower()
												__UpperCAmelCase   :						List[Any]      							=					message.replace(""" """ ,       """"""  )
												__UpperCAmelCase   :						List[Any]      							=					message.replace("""j""" ,       """i"""  )

												__UpperCAmelCase   :						Optional[int]      							=					np.empty((2, len(UpperCamelCase  ))  )
												for letter_index in range(len(UpperCamelCase  )  ):
																			__UpperCAmelCase   :						List[Any]      							=					self.letter_to_numbers(message[letter_index]  )

																			__UpperCAmelCase   :						str      							=					numbers[0]
																			__UpperCAmelCase   :						int      							=					numbers[1]

												__UpperCAmelCase   :						Union[str, Any]      							=					first_step.reshape(2 * len(UpperCamelCase  )  )
												__UpperCAmelCase   :						Optional[Any]      							=					""""""
												for numbers_index in range(len(UpperCamelCase  )  ):
																			__UpperCAmelCase   :						Any      							=					int(second_step[numbers_index * 2]  )
																			__UpperCAmelCase   :						Any      							=					int(second_step[(numbers_index * 2) + 1]  )
																			__UpperCAmelCase   :						str      							=					self.numbers_to_letter(UpperCamelCase ,       UpperCamelCase  )
																			__UpperCAmelCase   :						Optional[Any]      							=					encoded_message + letter

												return encoded_message






					def lowerCamelCase__							(       self    : Optional[int] ,       UpperCamelCase    : str  ):
												'''simple docstring'''






												__UpperCAmelCase   :						Any      							=					message.lower()
												message.replace(""" """ ,       """"""  )
												__UpperCAmelCase   :						int      							=					np.empty(2 * len(UpperCamelCase  )  )
												for letter_index in range(len(UpperCamelCase  )  ):
																			__UpperCAmelCase   :						Any      							=					self.letter_to_numbers(message[letter_index]  )
																			__UpperCAmelCase   :						Any      							=					numbers[0]
																			__UpperCAmelCase   :						Dict      							=					numbers[1]

												__UpperCAmelCase   :						str      							=					first_step.reshape((2, len(UpperCamelCase  ))  )
												__UpperCAmelCase   :						Union[str, Any]      							=					""""""
												for numbers_index in range(len(UpperCamelCase  )  ):
																			__UpperCAmelCase   :						Optional[int]      							=					int(second_step[0, numbers_index]  )
																			__UpperCAmelCase   :						Tuple      							=					int(second_step[1, numbers_index]  )
																			__UpperCAmelCase   :						Tuple      							=					self.numbers_to_letter(UpperCamelCase ,       UpperCamelCase  )
																			__UpperCAmelCase   :						Any      							=					decoded_message + letter

												return decoded_message
