



'''simple docstring'''




import warnings
from typing import List

import numpy as np

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
from ...utils import is_flax_available, is_tf_available, is_torch_available







class 	_lowerCAmelCase							(			__UpperCAmelCase ):
	__SCREAMING_SNAKE_CASE   :		List[Any]					=					['image_processor', 'tokenizer']
	__SCREAMING_SNAKE_CASE   :		Optional[Any]					=					'OwlViTImageProcessor'
	__SCREAMING_SNAKE_CASE   :		List[str]					=					('CLIPTokenizer', 'CLIPTokenizerFast')


	def __init__(self       ,      lowercase=None       ,      lowercase=None       ,      **lowercase							):
				A_	:						List[str]	=     None
				if "feature_extractor" in kwargs:
							warnings.warn(
							    """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"""
							    """ instead."""       ,      lowercase       ,      )
							A_	:						List[Any]	=     kwargs.pop("""feature_extractor"""							)

				A_	:						Any	=     image_processor if image_processor is not None else feature_extractor
				if image_processor is None:
							raise ValueError("""You need to specify an `image_processor`."""							)
				if tokenizer is None:
							raise ValueError("""You need to specify a `tokenizer`."""							)

				super().__init__(lowercase       ,      lowercase							)


	def __call__(self       ,      lowercase=None       ,      lowercase=None       ,      lowercase=None       ,      lowercase="max_length"       ,      lowercase="np"       ,      **lowercase							):

				if text is None and query_images is None and images is None:
							raise ValueError(
							    """You have to specify at least one text or query image or image. All three cannot be none."""							)

				if text is not None:
							if isinstance(lowercase       ,      lowercase							) or (isinstance(lowercase       ,      lowercase							) and not isinstance(text[0]       ,      lowercase							)):
										A_	:						List[Any]	=     [self.tokenizer(lowercase       ,      padding=lowercase       ,      return_tensors=lowercase       ,      **lowercase							)]

							elif isinstance(lowercase       ,      lowercase							) and isinstance(text[0]       ,      lowercase							):
										A_	:						Union[str, Any]	=     []

										# Maximum number of queries across batch
										A_	:						Dict	=     max([len(lowercase							) for t in text]							)

										# Pad all batch samples to max number of text queries
										for t in text:
													if len(lowercase							) != max_num_queries:
																A_	:						Dict	=     t + [""" """] * (max_num_queries - len(lowercase							))

													A_	:						Optional[Any]	=     self.tokenizer(lowercase       ,      padding=lowercase       ,      return_tensors=lowercase       ,      **lowercase							)
													encodings.append(lowercase							)
							else:
										raise TypeError("""Input text should be a string, a list of strings or a nested list of strings"""							)

							if return_tensors == "np":
										A_	:						List[Any]	=     np.concatenate([encoding["""input_ids"""] for encoding in encodings]       ,      axis=0							)
										A_	:						Optional[Any]	=     np.concatenate([encoding["""attention_mask"""] for encoding in encodings]       ,      axis=0							)

							elif return_tensors == "jax" and is_flax_available():
										import jax.numpy as jnp

										A_	:						Tuple	=     jnp.concatenate([encoding["""input_ids"""] for encoding in encodings]       ,      axis=0							)
										A_	:						Any	=     jnp.concatenate([encoding["""attention_mask"""] for encoding in encodings]       ,      axis=0							)

							elif return_tensors == "pt" and is_torch_available():
										import torch

										A_	:						List[str]	=     torch.cat([encoding["""input_ids"""] for encoding in encodings]       ,      dim=0							)
										A_	:						Dict	=     torch.cat([encoding["""attention_mask"""] for encoding in encodings]       ,      dim=0							)

							elif return_tensors == "tf" and is_tf_available():
										import tensorflow as tf

										A_	:						Tuple	=     tf.stack([encoding["""input_ids"""] for encoding in encodings]       ,      axis=0							)
										A_	:						Dict	=     tf.stack([encoding["""attention_mask"""] for encoding in encodings]       ,      axis=0							)

							else:
										raise ValueError("""Target return tensor type could not be returned"""							)

							A_	:						List[Any]	=     BatchEncoding()
							A_	:						Optional[int]	=     input_ids
							A_	:						Dict	=     attention_mask

				if query_images is not None:
							A_	:						Optional[Any]	=     BatchEncoding()
							A_	:						str	=     self.image_processor(
							    lowercase       ,      return_tensors=lowercase       ,      **lowercase							).pixel_values
							A_	:						List[str]	=     query_pixel_values

				if images is not None:
							A_	:						Union[str, Any]	=     self.image_processor(lowercase       ,      return_tensors=lowercase       ,      **lowercase							)

				if text is not None and images is not None:
							A_	:						Union[str, Any]	=     image_features.pixel_values
							return encoding
				elif query_images is not None and images is not None:
							A_	:						List[str]	=     image_features.pixel_values
							return encoding
				elif text is not None or query_images is not None:
							return encoding
				else:
							return BatchEncoding(data=dict(**lowercase							)       ,      tensor_type=lowercase							)


	def     _a	(self       ,      *lowercase       ,      **lowercase							):
				return self.image_processor.post_process(*lowercase       ,      **lowercase							)


	def     _a	(self       ,      *lowercase       ,      **lowercase							):
				return self.image_processor.post_process_object_detection(*lowercase       ,      **lowercase							)


	def     _a	(self       ,      *lowercase       ,      **lowercase							):
				return self.image_processor.post_process_image_guided_detection(*lowercase       ,      **lowercase							)


	def     _a	(self       ,      *lowercase       ,      **lowercase							):
				return self.tokenizer.batch_decode(*lowercase       ,      **lowercase							)


	def     _a	(self       ,      *lowercase       ,      **lowercase							):
				return self.tokenizer.decode(*lowercase       ,      **lowercase							)


	@property
	def     _a	(self							):
				warnings.warn(
				    """`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."""       ,      lowercase       ,      )
				return self.image_processor_class


	@property
	def     _a	(self							):
				warnings.warn(
				    """`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."""       ,      lowercase       ,      )
				return self.image_processor




'''simple docstring'''




from typing import List, Union

import numpy as np

from ..tokenization_utils import TruncationStrategy
from ..utils import add_end_docstrings, logging
from .base import PIPELINE_INIT_ARGS, ArgumentHandler, ChunkPipeline


lowerCamelCase							:Tuple				    =					logging.get_logger(__name__)







class 	_lowerCAmelCase							(			__UpperCAmelCase ):


	def     _a	(self       ,      lowercase							):
				if isinstance(lowercase       ,      lowercase							):
							A_	:						Tuple	=     [label.strip() for label in labels.split(""","""							) if label.strip()]
				return labels


	def __call__(self       ,      lowercase       ,      lowercase       ,      lowercase							):
				if len(lowercase							) == 0 or len(lowercase							) == 0:
							raise ValueError("""You must include at least one label and at least one sequence."""							)
				if hypothesis_template.format(labels[0]							) == hypothesis_template:
							raise ValueError(
							    (
							        """The provided hypothesis_template \"{}\" was not able to be formatted with the target labels. """
							        """Make sure the passed template includes formatting syntax such as {{}} where the label should go."""
							    ).format(lowercase							)							)

				if isinstance(lowercase       ,      lowercase							):
							A_	:						Optional[Any]	=     [sequences]

				A_	:						List[Any]	=     []
				for sequence in sequences:
							sequence_pairs.extend([[sequence, hypothesis_template.format(lowercase							)] for label in labels]							)

				return sequence_pairs, sequences







@add_end_docstrings(__UpperCAmelCase )
class 	_lowerCAmelCase							(			__UpperCAmelCase ):


	def __init__(self       ,      lowercase=ZeroShotClassificationArgumentHandler()       ,      *lowercase       ,      **lowercase							):
				A_	:						str	=     args_parser
				super().__init__(*lowercase       ,      **lowercase							)
				if self.entailment_id == -1:
							logger.warning(
							    """Failed to determine 'entailment' label id from the label2id mapping in the model config. Setting to """
							    """-1. Define a descriptive label2id mapping in the model config to ensure correct outputs."""							)


	@property
	def     _a	(self							):
				for label, ind in self.model.config.labelaid.items():
							if label.lower().startswith("""entail"""							):
										return ind
				return -1


	def     _a	(self       ,      lowercase       ,      lowercase=True       ,      lowercase=True       ,      lowercase=TruncationStrategy.ONLY_FIRST       ,      **lowercase							):
				A_	:						Optional[int]	=     self.framework
				if self.tokenizer.pad_token is None:
							# Override for tokenizers not supporting padding
							logger.error(
							    """Tokenizer was not supporting padding necessary for zero-shot, attempting to use """
							    """ `pad_token=eos_token`"""							)
							A_	:						Any	=     self.tokenizer.eos_token
				try:
							A_	:						Optional[Any]	=     self.tokenizer(
							    lowercase       ,      add_special_tokens=lowercase       ,      return_tensors=lowercase       ,      padding=lowercase       ,      truncation=lowercase       ,      )
				except Exception as e:
							if "too short" in str(lowercase							):
										# tokenizers might yell that we want to truncate
										# to a value that is not even reached by the input.
										# In that case we don't want to truncate.
										# It seems there's not a really better way to catch that
										# exception.

										A_	:						str	=     self.tokenizer(
										    lowercase       ,      add_special_tokens=lowercase       ,      return_tensors=lowercase       ,      padding=lowercase       ,      truncation=TruncationStrategy.DO_NOT_TRUNCATE       ,      )
							else:
										raise e

				return inputs


	def     _a	(self       ,      **lowercase							):
				if kwargs.get("""multi_class"""       ,      lowercase							) is not None:
							A_	:						Union[str, Any]	=     kwargs["""multi_class"""]
							logger.warning(
							    """The `multi_class` argument has been deprecated and renamed to `multi_label`. """
							    """`multi_class` will be removed in a future version of Transformers."""							)
				A_	:						Tuple	=     {}
				if "candidate_labels" in kwargs:
							A_	:						Optional[int]	=     self._args_parser._parse_labels(kwargs["""candidate_labels"""]							)
				if "hypothesis_template" in kwargs:
							A_	:						List[str]	=     kwargs["""hypothesis_template"""]

				A_	:						Union[str, Any]	=     {}
				if "multi_label" in kwargs:
							A_	:						Optional[int]	=     kwargs["""multi_label"""]
				return preprocess_params, {}, postprocess_params


	def __call__(self       ,      lowercase       ,      *lowercase       ,      **lowercase       ,      ):
				if len(lowercase							) == 0:
							pass
				elif len(lowercase							) == 1 and "candidate_labels" not in kwargs:
							A_	:						Any	=     args[0]
				else:
							raise ValueError(F'Unable to understand extra arguments {args}'							)

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


	def     _a	(self       ,      lowercase       ,      lowercase=None       ,      lowercase="This example is {}."							):
				A_,    A_	:						Any	=     self._args_parser(lowercase       ,      lowercase       ,      lowercase							)

				for i, (candidate_label, sequence_pair) in enumerate(zip(lowercase       ,      lowercase							)							):
							A_	:						List[Any]	=     self._parse_and_tokenize([sequence_pair]							)

							yield {
							    "candidate_label": candidate_label,
							    "sequence": sequences[0],
							    "is_last": i == len(lowercase							) - 1,
							    **model_input,
							}


	def     _a	(self       ,      lowercase							):
				A_	:						Dict	=     inputs["""candidate_label"""]
				A_	:						Any	=     inputs["""sequence"""]
				A_	:						List[Any]	=     {k: inputs[k] for k in self.tokenizer.model_input_names}
				A_	:						Optional[int]	=     self.model(**lowercase							)

				A_	:						Optional[Any]	=     {
				    """candidate_label""": candidate_label,
				    """sequence""": sequence,
				    """is_last""": inputs["""is_last"""],
				    **outputs,
				}
				return model_outputs


	def     _a	(self       ,      lowercase       ,      lowercase=False							):
				A_	:						Tuple	=     [outputs["""candidate_label"""] for outputs in model_outputs]
				A_	:						Optional[int]	=     [outputs["""sequence"""] for outputs in model_outputs]
				A_	:						Union[str, Any]	=     np.concatenate([output["""logits"""].numpy() for output in model_outputs]							)
				A_	:						List[str]	=     logits.shape[0]
				A_	:						Optional[int]	=     len(lowercase							)
				A_	:						int	=     N // n
				A_	:						int	=     logits.reshape((num_sequences, n, -1)							)

				if multi_label or len(lowercase							) == 1:
							# softmax over the entailment vs. contradiction dim for each label independently
							A_	:						Dict	=     self.entailment_id
							A_	:						Dict	=     -1 if entailment_id == 0 else 0
							A_	:						str	=     reshaped_outputs[..., [contradiction_id, entailment_id]]
							A_	:						Optional[Any]	=     np.exp(lowercase							) / np.exp(lowercase							).sum(-1       ,      keepdims=lowercase							)
							A_	:						Tuple	=     scores[..., 1]
				else:
							# softmax the "entailment" logits over all candidate labels
							A_	:						Optional[int]	=     reshaped_outputs[..., self.entailment_id]
							A_	:						Any	=     np.exp(lowercase							) / np.exp(lowercase							).sum(-1       ,      keepdims=lowercase							)

				A_	:						List[str]	=     list(reversed(scores[0].argsort()							)							)
				return {
				    "sequence": sequences[0],
				    "labels": [candidate_labels[i] for i in top_inds],
				    "scores": scores[0, top_inds].tolist(),
				}