

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

lowerCAmelCase__     :   Optional[int]       = logging.get_logger(__name__)






@add_end_docstrings(_snake_case    )
class 						__magic_name__    (       _snake_case    ):


 def __init__(						self		: Tuple			,						**lowerCAmelCase__		: List[str]    )    ->      int:
   super().__init__(**UpperCamelCase_    )

   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		: int			,						lowerCAmelCase__		: Union[str, List[str], "Image", List["Image"]]			,						**lowerCAmelCase__		: Tuple    )    ->      List[Any]:
   return super().__call__(UpperCamelCase_			,						**UpperCamelCase_    )


 def _UpperCamelCase	(						self		: List[Any]			,						**lowerCAmelCase__		: str    )    ->      List[str]:
   UpperCAmelCase					      =  {}
   if "candidate_labels" in kwargs:
     UpperCAmelCase					      =  kwargs['candidate_labels']
   if "hypothesis_template" in kwargs:
     UpperCAmelCase					      =  kwargs['hypothesis_template']

   return preprocess_params, {}, {}


 def _UpperCamelCase	(						self		: Tuple			,						lowerCAmelCase__		: Dict			,						lowerCAmelCase__		: Optional[Any]=None			,						lowerCAmelCase__		: str="This is a photo of {}."    )    ->      Union[str, Any]:
   UpperCAmelCase					      =  load_image(UpperCamelCase_    )
   UpperCAmelCase					      =  self.image_processor(images=[image]			,						return_tensors=self.framework    )
   UpperCAmelCase					      =  candidate_labels
   UpperCAmelCase					      =  [hypothesis_template.format(UpperCamelCase_    ) for x in candidate_labels]
   UpperCAmelCase					      =  self.tokenizer(UpperCamelCase_			,						return_tensors=self.framework			,						padding=UpperCamelCase_    )
   UpperCAmelCase					      =  [text_inputs]
   return inputs


 def _UpperCamelCase	(						self		: str			,						lowerCAmelCase__		: Union[str, Any]    )    ->      Optional[Any]:
   UpperCAmelCase					      =  model_inputs.pop("candidate_labels"    )
   UpperCAmelCase					      =  model_inputs.pop("text_inputs"    )
   if isinstance(text_inputs[0]			,						UpperCamelCase_    ):
     UpperCAmelCase					      =  text_inputs[0]
   else:
     # Batching case.
     UpperCAmelCase					      =  text_inputs[0][0]

   UpperCAmelCase					      =  self.model(**UpperCamelCase_			,						**UpperCamelCase_    )

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


 def _UpperCamelCase	(						self		: Optional[int]			,						lowerCAmelCase__		: int    )    ->      List[str]:
   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(UpperCamelCase_			,						UpperCamelCase_    ):
       UpperCAmelCase					      =  [scores]
   elif self.framework == "tf":
     UpperCAmelCase					      =  stable_softmax(UpperCamelCase_			,						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(UpperCamelCase_			,						UpperCamelCase_    )			,						key=lambda lowerCAmelCase__    : -x[0]    )
   ]
   return result






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


lowerCAmelCase__       = logging.get_logger(__name__)

lowerCAmelCase__       = {
    "unc-nlp/lxmert-base-uncased": "https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/config.json",
}






class 						__magic_name__    (       _snake_case    ):
 UpperCAmelCase							    =						"""lxmert"""
 UpperCAmelCase							    =						{}


 def __init__(						self		: int			,						lowerCAmelCase__		: Any=3_0_5_2_2			,						lowerCAmelCase__		: List[str]=7_6_8			,						lowerCAmelCase__		: Union[str, Any]=1_2			,						lowerCAmelCase__		: List[Any]=9_5_0_0			,						lowerCAmelCase__		: Any=1_6_0_0			,						lowerCAmelCase__		: Union[str, Any]=4_0_0			,						lowerCAmelCase__		: Tuple=3_0_7_2			,						lowerCAmelCase__		: Dict="gelu"			,						lowerCAmelCase__		: Tuple=0.1			,						lowerCAmelCase__		: Tuple=0.1			,						lowerCAmelCase__		: int=5_1_2			,						lowerCAmelCase__		: List[str]=2			,						lowerCAmelCase__		: List[str]=0.02			,						lowerCAmelCase__		: str=1e-1_2			,						lowerCAmelCase__		: str=9			,						lowerCAmelCase__		: int=5			,						lowerCAmelCase__		: Optional[int]=5			,						lowerCAmelCase__		: List[Any]=2_0_4_8			,						lowerCAmelCase__		: Any=4			,						lowerCAmelCase__		: Dict=6.67			,						lowerCAmelCase__		: Any=True			,						lowerCAmelCase__		: Union[str, Any]=True			,						lowerCAmelCase__		: Any=True			,						lowerCAmelCase__		: Tuple=True			,						lowerCAmelCase__		: Optional[Any]=True			,						lowerCAmelCase__		: Optional[int]=True			,						lowerCAmelCase__		: Tuple=True			,						**lowerCAmelCase__		: List[Any]			,						)    ->      Dict:
   UpperCAmelCase					      =  vocab_size
   UpperCAmelCase					      =  hidden_size
   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					      =  num_qa_labels
   UpperCAmelCase					      =  num_object_labels
   UpperCAmelCase					      =  num_attr_labels
   UpperCAmelCase					      =  l_layers
   UpperCAmelCase					      =  x_layers
   UpperCAmelCase					      =  r_layers
   UpperCAmelCase					      =  visual_feat_dim
   UpperCAmelCase					      =  visual_pos_dim
   UpperCAmelCase					      =  visual_loss_normalizer
   UpperCAmelCase					      =  task_matched
   UpperCAmelCase					      =  task_mask_lm
   UpperCAmelCase					      =  task_obj_predict
   UpperCAmelCase					      =  task_qa
   UpperCAmelCase					      =  visual_obj_loss
   UpperCAmelCase					      =  visual_attr_loss
   UpperCAmelCase					      =  visual_feat_loss
   UpperCAmelCase					      =  {"vision": r_layers, "cross_encoder": x_layers, "language": l_layers}
   super().__init__(**lowerCAmelCase__    )



