



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, ChunkPipeline


if is_vision_available():
   from PIL import Image

   from ..image_utils import load_image

if is_torch_available():
   import torch

   from transformers.modeling_outputs import BaseModelOutput

   from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING

__lowerCamelCase         = logging.get_logger(__name__)


@add_end_docstrings(__lowercase  )
class   snake_case_ (__lowercase  ):




       """simple docstring"""






       def __init__(     self     ,**lowercase):

             """simple docstring"""





             super().__init__(**__A)

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

             requires_backends(self     ,"vision")
             self.check_model_type(__A)



       def __call__(     self     ,lowercase     ,lowercase = None     ,**lowercase     ,):

             """simple docstring"""





             if "text_queries" in kwargs:
                   UpperCAmelCase_  :	Dict				      =		kwargs.pop("text_queries")

             if isinstance(__A     ,(str, Image.Image)):
                   UpperCAmelCase_  :	Optional[int]				      =		{"image": image, "candidate_labels": candidate_labels}
             else:
                   UpperCAmelCase_  :	Tuple				      =		image
             UpperCAmelCase_  :	Dict				      =		super().__call__(__A     ,**__A)
             return results



       def  A_   (     self     ,**lowercase):

             """simple docstring"""





             UpperCAmelCase_  :	List[Any]				      =		{}
             if "threshold" in kwargs:
                   UpperCAmelCase_  :	Dict				      =		kwargs["threshold"]
             if "top_k" in kwargs:
                   UpperCAmelCase_  :	Optional[int]				      =		kwargs["top_k"]
             return {}, {}, postprocess_params



       def  A_   (     self     ,lowercase):

             """simple docstring"""





             UpperCAmelCase_  :	Dict				      =		load_image(inputs["image"])
             UpperCAmelCase_  :	Optional[Any]				      =		inputs["candidate_labels"]
             if isinstance(__A     ,__A):
                   UpperCAmelCase_  :	Union[str, Any]				      =		candidate_labels.split(",")

             UpperCAmelCase_  :	Optional[Any]				      =		torch.tensor([[image.height, image.width]]     ,dtype=torch.intaa)
             for i, candidate_label in enumerate(__A):
                   UpperCAmelCase_  :	Union[str, Any]				      =		self.tokenizer(__A     ,return_tensors=self.framework)
                   UpperCAmelCase_  :	List[Any]				      =		self.image_processor(__A     ,return_tensors=self.framework)
                   yield {
                       "is_last": i == len(__A) - 1,
                       "target_size": target_size,
                       "candidate_label": candidate_label,
                       **text_inputs,
                       **image_features,
                   }



       def  A_   (     self     ,lowercase):

             """simple docstring"""





             UpperCAmelCase_  :	int				      =		model_inputs.pop("target_size")
             UpperCAmelCase_  :	Tuple				      =		model_inputs.pop("candidate_label")
             UpperCAmelCase_  :	Any				      =		model_inputs.pop("is_last")

             UpperCAmelCase_  :	Optional[int]				      =		self.model(**__A)

             UpperCAmelCase_  :	str				      =		{"target_size": target_size, "candidate_label": candidate_label, "is_last": is_last, **outputs}
             return model_outputs



       def  A_   (     self     ,lowercase     ,lowercase=0.1     ,lowercase=None):

             """simple docstring"""





             UpperCAmelCase_  :	Dict				      =		[]
             for model_output in model_outputs:
                   UpperCAmelCase_  :	Any				      =		model_output["candidate_label"]
                   UpperCAmelCase_  :	str				      =		BaseModelOutput(__A)
                   UpperCAmelCase_  :	List[str]				      =		self.image_processor.post_process_object_detection(
                       outputs=__A     ,threshold=__A     ,target_sizes=model_output["target_size"])[0]

                   for index in outputs["scores"].nonzero():
                         UpperCAmelCase_  :	int				      =		outputs["scores"][index].item()
                         UpperCAmelCase_  :	Any				      =		self._get_bounding_box(outputs["boxes"][index][0])

                         UpperCAmelCase_  :	Tuple				      =		{"score": score, "label": label, "box": box}
                         results.append(__A)

             UpperCAmelCase_  :	List[str]				      =		sorted(__A     ,key=lambda lowercase: x["score"]     ,reverse=__A)
             if top_k:
                   UpperCAmelCase_  :	Any				      =		results[:top_k]

             return results



       def  A_   (     self     ,lowercase):

             """simple docstring"""





             if self.framework != "pt":
                   raise ValueError("The ZeroShotObjectDetectionPipeline is only available in PyTorch.")
             UpperCAmelCase_      ,      UpperCAmelCase_      ,      UpperCAmelCase_      ,      UpperCAmelCase_  :	List[str]				      =		box.int().tolist()
             UpperCAmelCase_  :	Tuple				      =		{
                 "xmin": xmin,
                 "ymin": ymin,
                 "xmax": xmax,
                 "ymax": ymax,
             }
             return bbox





from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxSeqaSeqConfigWithPast
from ...utils import logging


__lowerCamelCase         =			logging.get_logger(__name__)

__lowerCamelCase         =			{
    '''google/umt5-small''': '''https://huggingface.co/google/umt5-small/resolve/main/config.json''',
    # See all umt5 models at https://huggingface.co/models?filter=umt5
}


class   snake_case_ (lowercase__  ):




       """simple docstring"""






       _lowerCamelCase			    =				"""umt5"""
       _lowerCamelCase			    =				["""past_key_values"""]



       def __init__(     self     ,lowercase=250112     ,lowercase=512     ,lowercase=64     ,lowercase=1024     ,lowercase=8     ,lowercase=None     ,lowercase=6     ,lowercase=32     ,lowercase=128     ,lowercase=0.1     ,lowercase=1E-6     ,lowercase=1.0     ,lowercase="gated-gelu"     ,lowercase=True     ,lowercase=True     ,lowercase="T5Tokenizer"     ,lowercase=True     ,lowercase=0     ,lowercase=1     ,lowercase=0     ,**lowercase     ,):

             """simple docstring"""





             super().__init__(
                 is_encoder_decoder=lowercase     ,tokenizer_class=lowercase     ,tie_word_embeddings=lowercase     ,pad_token_id=lowercase     ,eos_token_id=lowercase     ,decoder_start_token_id=lowercase     ,**lowercase     ,)
             UpperCAmelCase_  :	Optional[int]				      =		vocab_size
             UpperCAmelCase_  :	Any				      =		d_model
             UpperCAmelCase_  :	Any				      =		d_kv
             UpperCAmelCase_  :	int				      =		d_ff
             UpperCAmelCase_  :	Tuple				      =		num_layers
             UpperCAmelCase_  :	int				      =		(
                 num_decoder_layers if num_decoder_layers is not None else self.num_layers
             )  # default = symmetry
             UpperCAmelCase_  :	Optional[int]				      =		num_heads
             UpperCAmelCase_  :	str				      =		relative_attention_num_buckets
             UpperCAmelCase_  :	Any				      =		relative_attention_max_distance
             UpperCAmelCase_  :	Optional[Any]				      =		dropout_rate
             UpperCAmelCase_  :	Union[str, Any]				      =		layer_norm_epsilon
             UpperCAmelCase_  :	Optional[Any]				      =		initializer_factor
             UpperCAmelCase_  :	int				      =		feed_forward_proj
             UpperCAmelCase_  :	str				      =		use_cache

             UpperCAmelCase_  :	List[str]				      =		self.feed_forward_proj.split("-")
             UpperCAmelCase_  :	Any				      =		act_info[-1]
             UpperCAmelCase_  :	Optional[int]				      =		act_info[0] == "gated"

             if len(lowercase) > 1 and act_info[0] != "gated" or len(lowercase) > 2:
                   raise ValueError(
                       F"""`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer."""
                       "Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. "
                       "'gated-gelu' or 'relu'")

             if feed_forward_proj == "gated-gelu":
                   UpperCAmelCase_  :	Tuple				      =		"gelu_new"



       @property
       def  A_   (     self):

             """simple docstring"""





             return self.d_model



       @property
       def  A_   (     self):

             """simple docstring"""





             return self.num_heads



       @property
       def  A_   (     self):

             """simple docstring"""





             return self.num_layers


class   snake_case_ (lowercase__  ):




       """simple docstring"""






       @property
       # Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.inputs
       def  A_   (     self):

             """simple docstring"""





             UpperCAmelCase_  :	int				      =		{
                 "input_ids": {0: "batch", 1: "encoder_sequence"},
                 "attention_mask": {0: "batch", 1: "encoder_sequence"},
             }
             if self.use_past:
                   UpperCAmelCase_  :	Union[str, Any]				      =		"past_encoder_sequence + sequence"
                   UpperCAmelCase_  :	Optional[int]				      =		{0: "batch"}
                   UpperCAmelCase_  :	Union[str, Any]				      =		{0: "batch", 1: "past_decoder_sequence + sequence"}
             else:
                   UpperCAmelCase_  :	Optional[int]				      =		{0: "batch", 1: "decoder_sequence"}
                   UpperCAmelCase_  :	Dict				      =		{0: "batch", 1: "decoder_sequence"}

             if self.use_past:
                   self.fill_with_past_key_values_(lowercase     ,direction="inputs")

             return common_inputs



       @property
       # Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.default_onnx_opset
       def  A_   (     self):

             """simple docstring"""





             return 13



       @property
       def  A_   (     self):

             """simple docstring"""





             return 5E-4
