import copy
import os
from typing import Union

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


UpperCamelCase__    							= logging.get_logger(__name__)

UpperCamelCase__    							= {
    'BAAI/AltCLIP': 'https://huggingface.co/BAAI/AltCLIP/resolve/main/config.json',
    # See all AltCLIP models at https://huggingface.co/models?filter=altclip
}





class 		A							(    UpperCAmelCase_					):
   __UpperCAmelCase			:			List[Any]              =      'altclip_text_model'


   def __init__(self					:						List[str]	,  __UpperCAmelCase					:						Union[str, Any]=2_5_0_0_0_2	,  __UpperCAmelCase					:						List[Any]=1_0_2_4	,  __UpperCAmelCase					:						List[Any]=2_4	,  __UpperCAmelCase					:						Dict=1_6	,  __UpperCAmelCase					:						List[str]=4_0_9_6	,  __UpperCAmelCase					:						Optional[Any]="gelu"	,  __UpperCAmelCase					:						List[str]=0.1	,  __UpperCAmelCase					:						str=0.1	,  __UpperCAmelCase					:						Optional[int]=5_1_4	,  __UpperCAmelCase					:						Any=1	,  __UpperCAmelCase					:						Optional[int]=0.02	,  __UpperCAmelCase					:						Optional[Any]=0.02	,  __UpperCAmelCase					:						Dict=1E-05	,  __UpperCAmelCase					:						Tuple=1	,  __UpperCAmelCase					:						Any=0	,  __UpperCAmelCase					:						List[str]=2	,  __UpperCAmelCase					:						Dict="absolute"	,  __UpperCAmelCase					:						List[str]=True	,  __UpperCAmelCase					:						List[Any]=7_6_8	,  **__UpperCAmelCase					:						Optional[Any]	,  )						->    int:
         """simple docstring"""



         super().__init__(pad_token_id=__UpperCAmelCase	,  bos_token_id=__UpperCAmelCase	,  eos_token_id=__UpperCAmelCase	,  **__UpperCAmelCase		)

         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__								=  initializer_factor
         UpperCAmelCase__								=  layer_norm_eps
         UpperCAmelCase__								=  position_embedding_type
         UpperCAmelCase__								=  use_cache
         UpperCAmelCase__								=  project_dim





class 		A							(    UpperCAmelCase_					):
   __UpperCAmelCase			:			List[str]              =      'altclip_vision_model'


   def __init__(self					:						Optional[Any]	,  __UpperCAmelCase					:						List[Any]=7_6_8	,  __UpperCAmelCase					:						List[str]=3_0_7_2	,  __UpperCAmelCase					:						List[Any]=5_1_2	,  __UpperCAmelCase					:						Any=1_2	,  __UpperCAmelCase					:						Optional[Any]=1_2	,  __UpperCAmelCase					:						List[str]=3	,  __UpperCAmelCase					:						Dict=2_2_4	,  __UpperCAmelCase					:						Dict=3_2	,  __UpperCAmelCase					:						Dict="quick_gelu"	,  __UpperCAmelCase					:						List[str]=1E-5	,  __UpperCAmelCase					:						int=0.0	,  __UpperCAmelCase					:						int=0.02	,  __UpperCAmelCase					:						Union[str, Any]=1.0	,  **__UpperCAmelCase					:						Optional[Any]	,  )						->    List[Any]:
         """simple docstring"""



         super().__init__(**__UpperCAmelCase		)

         UpperCAmelCase__								=  hidden_size
         UpperCAmelCase__								=  intermediate_size
         UpperCAmelCase__								=  projection_dim
         UpperCAmelCase__								=  num_hidden_layers
         UpperCAmelCase__								=  num_attention_heads
         UpperCAmelCase__								=  num_channels
         UpperCAmelCase__								=  patch_size
         UpperCAmelCase__								=  image_size
         UpperCAmelCase__								=  initializer_range
         UpperCAmelCase__								=  initializer_factor
         UpperCAmelCase__								=  attention_dropout
         UpperCAmelCase__								=  layer_norm_eps
         UpperCAmelCase__								=  hidden_act





   @classmethod
   def       lowercase_				(cls					:						Dict	,  __UpperCAmelCase					:						Dict	,  **__UpperCAmelCase					:						List[str]		)						->    "PretrainedConfig":
         """simple docstring"""



         cls._set_token_in_kwargs(__UpperCAmelCase		)

         UpperCAmelCase__								=  cls.get_config_dict(__UpperCAmelCase	,  **__UpperCAmelCase		)

         # get the vision config dict if we are loading from AltCLIPConfig
         if config_dict.get("model_type"		) == "altclip":
               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(__UpperCAmelCase	,  **__UpperCAmelCase		)





class 		A							(    UpperCAmelCase_					):
   __UpperCAmelCase			:			List[str]              =      'altclip'
   __UpperCAmelCase			:			Dict              =      True


   def __init__(self					:						List[Any]	,  __UpperCAmelCase					:						Optional[Any]=None	,  __UpperCAmelCase					:						Any=None	,  __UpperCAmelCase					:						Any=7_6_8	,  __UpperCAmelCase					:						List[Any]=2.6592	,  **__UpperCAmelCase					:						Union[str, Any]		)						->    Any:
         """simple docstring"""



         UpperCAmelCase__								=  kwargs.pop("text_config_dict"	,  __UpperCAmelCase		)
         UpperCAmelCase__								=  kwargs.pop("vision_config_dict"	,  __UpperCAmelCase		)

         super().__init__(**__UpperCAmelCase		)

         # Instead of simply assigning `[text|vision]_config_dict` to `[text|vision]_config`, we use the values in
         # `[text|vision]_config_dict` to update the values in `[text|vision]_config`. The values should be same in most
         # cases, but we don't want to break anything regarding `_config_dict` that existed before commit `8827e1b2`.
         if text_config_dict is not None:
               if text_config is None:
                     UpperCAmelCase__								=  {}

               # This is the complete result when using `text_config_dict`.
               UpperCAmelCase__								=  AltCLIPTextConfig(**__UpperCAmelCase		).to_dict()

               # Give a warning if the values exist in both `_text_config_dict` and `text_config` but being different.
               for key, value in _text_config_dict.items():
                     if key in text_config and value != text_config[key] and key not in ["transformers_version"]:
                           # If specified in `text_config_dict`
                           if key in text_config_dict:
                                 UpperCAmelCase__								=  (
                                     f"""`{key}` is found in both `text_config_dict` and `text_config` but with different values. """
                                     f"""The value `text_config_dict[\"{key}\"]` will be used instead."""
                                 )
                           # If inferred from default argument values (just to be super careful)
                           else:
                                 UpperCAmelCase__								=  (
                                     f"""`text_config_dict` is provided which will be used to initialize `AltCLIPTextConfig`. The """
                                     f"""value `text_config[\"{key}\"]` will be overriden."""
                                 )
                           logger.warning(__UpperCAmelCase		)

            # Update all values in `text_config` with the ones in `_text_config_dict`.
               text_config.update(_text_config_dict		)

         if vision_config_dict is not None:
               if vision_config is None:
                     UpperCAmelCase__								=  {}

               # This is the complete result when using `vision_config_dict`.
               UpperCAmelCase__								=  AltCLIPVisionConfig(**__UpperCAmelCase		).to_dict()
               # convert keys to string instead of integer
               if "id2label" in _vision_config_dict:
                     UpperCAmelCase__								=  {
                         str(__UpperCAmelCase		): value for key, value in _vision_config_dict['''id2label'''].items()
                     }

               # Give a warning if the values exist in both `_vision_config_dict` and `vision_config` but being different.
               for key, value in _vision_config_dict.items():
                     if key in vision_config and value != vision_config[key] and key not in ["transformers_version"]:
                           # If specified in `vision_config_dict`
                           if key in vision_config_dict:
                                 UpperCAmelCase__								=  (
                                     f"""`{key}` is found in both `vision_config_dict` and `vision_config` but with different """
                                     f"""values. The value `vision_config_dict[\"{key}\"]` will be used instead."""
                                 )
                           # If inferred from default argument values (just to be super careful)
                           else:
                                 UpperCAmelCase__								=  (
                                     f"""`vision_config_dict` is provided which will be used to initialize `AltCLIPVisionConfig`. """
                                     f"""The value `vision_config[\"{key}\"]` will be overriden."""
                                 )
                           logger.warning(__UpperCAmelCase		)

            # Update all values in `vision_config` with the ones in `_vision_config_dict`.
               vision_config.update(_vision_config_dict		)

         if text_config is None:
               UpperCAmelCase__								=  {}
               logger.info("`text_config` is `None`. Initializing the `AltCLIPTextConfig` with default values."		)

         if vision_config is None:
               UpperCAmelCase__								=  {}
               logger.info("`vision_config` is `None`. initializing the `AltCLIPVisionConfig` with default values."		)

         UpperCAmelCase__								=  AltCLIPTextConfig(**__UpperCAmelCase		)
         UpperCAmelCase__								=  AltCLIPVisionConfig(**__UpperCAmelCase		)

         UpperCAmelCase__								=  projection_dim
         UpperCAmelCase__								=  logit_scale_init_value
         UpperCAmelCase__								=  1.0


   @classmethod
   def       lowercase_				(cls					:						Dict	,  __UpperCAmelCase					:						int	,  __UpperCAmelCase					:						Union[str, Any]	,  **__UpperCAmelCase					:						str		)						->    Any:
         """simple docstring"""



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





   def       lowercase_				(self					:						List[Any]		)						->    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 typing import Any, Dict, List, Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
      from ..image_utils import load_image


if is_torch_available():
      import torch

      from ..models.auto.modeling_auto import MODEL_FOR_OBJECT_DETECTION_MAPPING, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING

UpperCamelCase__    							= logging.get_logger(__name__)


UpperCamelCase__    							= Dict[str, Any]
UpperCamelCase__    							= List[Prediction]





@add_end_docstrings(UpperCAmelCase_					)
class 					A							(    UpperCAmelCase_					):


   def __init__(self					:						Any	,  *__UpperCAmelCase					:						List[str]	,  **__UpperCAmelCase					:						Any		)						->    Union[str, Any]:
         """simple docstring"""



         super().__init__(*__UpperCAmelCase	,  **__UpperCAmelCase		)

         if self.framework == "tf":
               raise ValueError(f"""The {self.__class__} is only available in PyTorch."""		)

         requires_backends(self	,  "vision"		)
         self.check_model_type(
             dict(MODEL_FOR_OBJECT_DETECTION_MAPPING.items() + MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING.items()		)		)


   def       lowercase_				(self					:						Optional[Any]	,  **__UpperCAmelCase					:						Dict		)						->    Any:
         """simple docstring"""



         UpperCAmelCase__								=  {}
         if "threshold" in kwargs:
               UpperCAmelCase__								=  kwargs["threshold"]
         return {}, {}, postprocess_kwargs


   def __call__(self					:						Optional[int]	,  *__UpperCAmelCase					:						List[Any]	,  **__UpperCAmelCase					:						Dict		)						->    Union[Predictions, List[Prediction]]:
         """simple docstring"""



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


   def       lowercase_				(self					:						Optional[int]	,  __UpperCAmelCase					:						Any		)						->    Optional[Any]:
         """simple docstring"""



         UpperCAmelCase__								=  load_image(__UpperCAmelCase		)
         UpperCAmelCase__								=  torch.IntTensor([[image.height, image.width]]		)
         UpperCAmelCase__								=  self.image_processor(images=[image]	,  return_tensors="pt"		)
         if self.tokenizer is not None:
               UpperCAmelCase__								=  self.tokenizer(text=inputs["words"]	,  boxes=inputs["boxes"]	,  return_tensors="pt"		)
         UpperCAmelCase__								=  target_size
         return inputs


   def       lowercase_				(self					:						List[str]	,  __UpperCAmelCase					:						str		)						->    Optional[Any]:
         """simple docstring"""



         UpperCAmelCase__								=  model_inputs.pop("target_size"		)
         UpperCAmelCase__								=  self.model(**__UpperCAmelCase		)
         UpperCAmelCase__								=  outputs.__class__({"target_size": target_size, **outputs}		)
         if self.tokenizer is not None:
               UpperCAmelCase__								=  model_inputs["bbox"]
         return model_outputs


   def       lowercase_				(self					:						List[Any]	,  __UpperCAmelCase					:						Any	,  __UpperCAmelCase					:						Dict=0.9		)						->    List[Any]:
         """simple docstring"""



         UpperCAmelCase__								=  model_outputs["target_size"]
         if self.tokenizer is not None:
               # This is a LayoutLMForTokenClassification variant.
               # The OCR got the boxes and the model classified the words.
               UpperCAmelCase__		,				UpperCAmelCase__								=  target_size[0].tolist()

               def unnormalize(__UpperCAmelCase					:						List[str]		):
                     return self._get_bounding_box(
                         torch.Tensor(
                             [
                                 (width * bbox[0] / 1_0_0_0),
                                 (height * bbox[1] / 1_0_0_0),
                                 (width * bbox[2] / 1_0_0_0),
                                 (height * bbox[3] / 1_0_0_0),
                             ]		)		)

               UpperCAmelCase__		,				UpperCAmelCase__								=  model_outputs["logits"].squeeze(0		).softmax(dim=-1		).max(dim=-1		)
               UpperCAmelCase__								=  [self.model.config.idalabel[prediction] for prediction in classes.tolist()]
               UpperCAmelCase__								=  [unnormalize(__UpperCAmelCase		) for bbox in model_outputs["bbox"].squeeze(0		)]
               UpperCAmelCase__								=  ["score", "label", "box"]
               UpperCAmelCase__								=  [dict(zip(__UpperCAmelCase	,  __UpperCAmelCase		)		) for vals in zip(scores.tolist()	,  __UpperCAmelCase	,  __UpperCAmelCase		) if vals[0] > threshold]
         else:
               # This is a regular ForObjectDetectionModel
               UpperCAmelCase__								=  self.image_processor.post_process_object_detection(__UpperCAmelCase	,  __UpperCAmelCase	,  __UpperCAmelCase		)
               UpperCAmelCase__								=  raw_annotations[0]
               UpperCAmelCase__								=  raw_annotation["scores"]
               UpperCAmelCase__								=  raw_annotation["labels"]
               UpperCAmelCase__								=  raw_annotation["boxes"]

               UpperCAmelCase__								=  scores.tolist()
               UpperCAmelCase__								=  [self.model.config.idalabel[label.item()] for label in labels]
               UpperCAmelCase__								=  [self._get_bounding_box(__UpperCAmelCase		) for box in boxes]

               # {"scores": [...], ...} --> [{"score":x, ...}, ...]
               UpperCAmelCase__								=  ["score", "label", "box"]
               UpperCAmelCase__								=  [
                   dict(zip(__UpperCAmelCase	,  __UpperCAmelCase		)		)
                   for vals in zip(raw_annotation["scores"]	,  raw_annotation["labels"]	,  raw_annotation["boxes"]		)
               ]

         return annotation





   def       lowercase_				(self					:						List[str]	,  __UpperCAmelCase					:						"torch.Tensor"		)						->    Dict[str, int]:
         """simple docstring"""



         if self.framework != "pt":
               raise ValueError("The ObjectDetectionPipeline is only available in PyTorch."		)
         UpperCAmelCase__		,				UpperCAmelCase__		,				UpperCAmelCase__		,				UpperCAmelCase__								=  box.int().tolist()
         UpperCAmelCase__								=  {
             "xmin": xmin,
             "ymin": ymin,
             "xmax": xmax,
             "ymax": ymax,
         }
         return bbox



