
import copy
import os
from typing import TYPE_CHECKING, List, Union


if TYPE_CHECKING:
				pass

from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowercase__							:							Dict									=			logging.get_logger(__name__)

lowercase__							:							Optional[int]									=			{
    "kakaobrain/align-base": "https://huggingface.co/kakaobrain/align-base/resolve/main/config.json",
}






class        SCREAMING_SNAKE_CASE__    (							SCREAMING_SNAKE_CASE_    ):

							"""simple docstring"""






							_snake_case     		=  'align_text_model'



							def __init__(  self     ,	SCREAMING_SNAKE_CASE_=30522     ,	SCREAMING_SNAKE_CASE_=768     ,	SCREAMING_SNAKE_CASE_=12     ,	SCREAMING_SNAKE_CASE_=12     ,	SCREAMING_SNAKE_CASE_=3072     ,	SCREAMING_SNAKE_CASE_="gelu"     ,	SCREAMING_SNAKE_CASE_=0.1     ,	SCREAMING_SNAKE_CASE_=0.1     ,	SCREAMING_SNAKE_CASE_=512     ,	SCREAMING_SNAKE_CASE_=2     ,	SCREAMING_SNAKE_CASE_=0.0_2     ,	SCREAMING_SNAKE_CASE_=1E-12     ,	SCREAMING_SNAKE_CASE_=0     ,	SCREAMING_SNAKE_CASE_="absolute"     ,	SCREAMING_SNAKE_CASE_=True     ,	**SCREAMING_SNAKE_CASE_     ,	)->   Tuple:





													'''simple docstring'''






													super().__init__(**SCREAMING_SNAKE_CASE_			)

													__UpperCamelCase       =			vocab_size
													__UpperCamelCase       =			hidden_size
													__UpperCamelCase       =			num_hidden_layers
													__UpperCamelCase       =			num_attention_heads
													__UpperCamelCase       =			hidden_act
													__UpperCamelCase       =			intermediate_size
													__UpperCamelCase       =			hidden_dropout_prob
													__UpperCamelCase       =			attention_probs_dropout_prob
													__UpperCamelCase       =			max_position_embeddings
													__UpperCamelCase       =			type_vocab_size
													__UpperCamelCase       =			initializer_range
													__UpperCamelCase       =			layer_norm_eps
													__UpperCamelCase       =			position_embedding_type
													__UpperCamelCase       =			use_cache
													__UpperCamelCase       =			pad_token_id



							@classmethod
							def     A__		(  cls     ,	SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_			)->   "PretrainedConfig":





													'''simple docstring'''






													cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE_			)

													__UpperCamelCase  ,							__UpperCamelCase       =			cls.get_config_dict(SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_			)

													# get the text config dict if we are loading from AlignConfig
													if config_dict.get('''model_type'''			) == "align":
																			__UpperCamelCase       =			config_dict['''text_config''']

													if "model_type" in config_dict and hasattr(cls     ,	'''model_type'''			) and config_dict["model_type"] != cls.model_type:
																			logger.warning(
																			    F"You are using a model of type {config_dict['model_type']} to instantiate a model of type "
																			    F"{cls.model_type}. This is not supported for all configurations of models and can yield errors."			)

													return cls.from_dict(SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_			)






class        SCREAMING_SNAKE_CASE__    (							SCREAMING_SNAKE_CASE_    ):

							"""simple docstring"""






							_snake_case     		=  'align_vision_model'



							def __init__(  self     ,	SCREAMING_SNAKE_CASE_ = 3     ,	SCREAMING_SNAKE_CASE_ = 600     ,	SCREAMING_SNAKE_CASE_ = 2.0     ,	SCREAMING_SNAKE_CASE_ = 3.1     ,	SCREAMING_SNAKE_CASE_ = 8     ,	SCREAMING_SNAKE_CASE_ = [3, 3, 5, 3, 5, 5, 3]     ,	SCREAMING_SNAKE_CASE_ = [32, 16, 24, 40, 80, 112, 192]     ,	SCREAMING_SNAKE_CASE_ = [16, 24, 40, 80, 112, 192, 320]     ,	SCREAMING_SNAKE_CASE_ = []     ,	SCREAMING_SNAKE_CASE_ = [1, 2, 2, 2, 1, 2, 1]     ,	SCREAMING_SNAKE_CASE_ = [1, 2, 2, 3, 3, 4, 1]     ,	SCREAMING_SNAKE_CASE_ = [1, 6, 6, 6, 6, 6, 6]     ,	SCREAMING_SNAKE_CASE_ = 0.2_5     ,	SCREAMING_SNAKE_CASE_ = "swish"     ,	SCREAMING_SNAKE_CASE_ = 2560     ,	SCREAMING_SNAKE_CASE_ = "mean"     ,	SCREAMING_SNAKE_CASE_ = 0.0_2     ,	SCREAMING_SNAKE_CASE_ = 0.0_0_1     ,	SCREAMING_SNAKE_CASE_ = 0.9_9     ,	SCREAMING_SNAKE_CASE_ = 0.2     ,	**SCREAMING_SNAKE_CASE_     ,	)->   Dict:





													'''simple docstring'''






													super().__init__(**SCREAMING_SNAKE_CASE_			)

													__UpperCamelCase       =			num_channels
													__UpperCamelCase       =			image_size
													__UpperCamelCase       =			width_coefficient
													__UpperCamelCase       =			depth_coefficient
													__UpperCamelCase       =			depth_divisor
													__UpperCamelCase       =			kernel_sizes
													__UpperCamelCase       =			in_channels
													__UpperCamelCase       =			out_channels
													__UpperCamelCase       =			depthwise_padding
													__UpperCamelCase       =			strides
													__UpperCamelCase       =			num_block_repeats
													__UpperCamelCase       =			expand_ratios
													__UpperCamelCase       =			squeeze_expansion_ratio
													__UpperCamelCase       =			hidden_act
													__UpperCamelCase       =			hidden_dim
													__UpperCamelCase       =			pooling_type
													__UpperCamelCase       =			initializer_range
													__UpperCamelCase       =			batch_norm_eps
													__UpperCamelCase       =			batch_norm_momentum
													__UpperCamelCase       =			drop_connect_rate
													__UpperCamelCase       =			sum(SCREAMING_SNAKE_CASE_			) * 4



							@classmethod
							def     A__		(  cls     ,	SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_			)->   "PretrainedConfig":





													'''simple docstring'''






													cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE_			)

													__UpperCamelCase  ,							__UpperCamelCase       =			cls.get_config_dict(SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_			)

													# get the vision config dict if we are loading from AlignConfig
													if config_dict.get('''model_type'''			) == "align":
																			__UpperCamelCase       =			config_dict['''vision_config''']

													if "model_type" in config_dict and hasattr(cls     ,	'''model_type'''			) and config_dict["model_type"] != cls.model_type:
																			logger.warning(
																			    F"You are using a model of type {config_dict['model_type']} to instantiate a model of type "
																			    F"{cls.model_type}. This is not supported for all configurations of models and can yield errors."			)

													return cls.from_dict(SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_			)






class        SCREAMING_SNAKE_CASE__    (							SCREAMING_SNAKE_CASE_    ):

							"""simple docstring"""






							_snake_case     		=  'align'
							_snake_case     		=  True



							def __init__(  self     ,	SCREAMING_SNAKE_CASE_=None     ,	SCREAMING_SNAKE_CASE_=None     ,	SCREAMING_SNAKE_CASE_=640     ,	SCREAMING_SNAKE_CASE_=1.0     ,	SCREAMING_SNAKE_CASE_=0.0_2     ,	**SCREAMING_SNAKE_CASE_     ,	)->   List[Any]:





													'''simple docstring'''






													super().__init__(**SCREAMING_SNAKE_CASE_			)

													if text_config is None:
																			__UpperCamelCase       =			{}
																			logger.info('''text_config is None. Initializing the AlignTextConfig with default values.'''			)

													if vision_config is None:
																			__UpperCamelCase       =			{}
																			logger.info('''vision_config is None. Initializing the AlignVisionConfig with default values.'''			)

													__UpperCamelCase       =			AlignTextConfig(**SCREAMING_SNAKE_CASE_			)
													__UpperCamelCase       =			AlignVisionConfig(**SCREAMING_SNAKE_CASE_			)

													__UpperCamelCase       =			projection_dim
													__UpperCamelCase       =			temperature_init_value
													__UpperCamelCase       =			initializer_range



							@classmethod
							def     A__		(  cls     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_			)->   str:





													'''simple docstring'''






													return cls(text_config=text_config.to_dict()     ,	vision_config=vision_config.to_dict()     ,	**SCREAMING_SNAKE_CASE_			)



							def     A__		(  self			)->   Union[str, Any]:





													'''simple docstring'''






													__UpperCamelCase       =			copy.deepcopy(self.__dict__			)
													__UpperCamelCase       =			self.text_config.to_dict()
													__UpperCamelCase       =			self.vision_config.to_dict()
													__UpperCamelCase       =			self.__class__.model_type
													return output





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




