



import warnings

from ...utils import logging
from .image_processing_owlvit import OwlViTImageProcessor


UpperCamelCase__ :			Union[str, Any]												=				logging.get_logger(__name__)






class 		_UpperCamelCase						(							lowerCamelCase__					):


			'''simple docstring'''

			def __init__(							self  :				List[Any]							,  *lowerCAmelCase__  :				Union[str, Any]							,  **lowerCAmelCase__  :				str		):





					"""simple docstring"""






					warnings.warn(
					    """The class OwlViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please"""
					    """ use OwlViTImageProcessor instead."""							,  lowerCAmelCase__							,  )
					super().__init__(*lowerCAmelCase__							,  **lowerCAmelCase__		)




'''simple docstring'''




import inspect
import tempfile
from collections import OrderedDict, UserDict
from collections.abc import MutableMapping
from contextlib import ExitStack, contextmanager
from dataclasses import fields
from enum import Enum
from typing import Any, ContextManager, List, Tuple

import numpy as np

from .import_utils import is_flax_available, is_tf_available, is_torch_available, is_torch_fx_proxy


if is_flax_available():
						import jax.numpy as jnp






class 		_UpperCamelCase						(							lowerCamelCase__					):


			'''simple docstring'''

			def __get__(							self  :				str							,  lowerCAmelCase__  :				Union[str, Any]							,  lowerCAmelCase__  :				List[Any]=None		):





					"""simple docstring"""






					if obj is None:
							return self
					if self.fget is None:
							raise AttributeError("""unreadable attribute"""		)
					__SCREAMING_SNAKE_CASE :	Any					 =	"""__cached_""" + self.fget.__name__
					__SCREAMING_SNAKE_CASE :	str					 =	getattr(lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__		)
					if cached is None:
							__SCREAMING_SNAKE_CASE :	int					 =	self.fget(lowerCAmelCase__		)
							setattr(lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__		)
					return cached


def        lowerCAmelCase_     (			_lowerCamelCase:     Tuple      ):
		__SCREAMING_SNAKE_CASE :	List[str]					 =	val.lower()
		if val in {"y", "yes", "t", "true", "on", "1"}:
				return 1
		if val in {"n", "no", "f", "false", "off", "0"}:
				return 0
		raise ValueError(F"invalid truth value {val!r}"      )


def        lowerCAmelCase_     (			_lowerCamelCase:     Optional[int]      ):
		if is_torch_fx_proxy(_lowerCamelCase      ):
				return True
		if is_torch_available():
				import torch

				if isinstance(_lowerCamelCase ,   torch.Tensor      ):
						return True
		if is_tf_available():
				import tensorflow as tf

				if isinstance(_lowerCamelCase ,   tf.Tensor      ):
						return True

		if is_flax_available():
				import jax.numpy as jnp
				from jax.core import Tracer

				if isinstance(_lowerCamelCase ,   (jnp.ndarray, Tracer)      ):
						return True

		return isinstance(_lowerCamelCase ,   np.ndarray      )


def        lowerCAmelCase_     (			_lowerCamelCase:     Optional[Any]      ):
		return isinstance(_lowerCamelCase ,   np.ndarray      )


def        lowerCAmelCase_     (			_lowerCamelCase:     Dict      ):
		return _is_numpy(_lowerCamelCase      )


def        lowerCAmelCase_     (			_lowerCamelCase:     Any      ):
		import torch

		return isinstance(_lowerCamelCase ,   torch.Tensor      )


def        lowerCAmelCase_     (			_lowerCamelCase:     str      ):
		return False if not is_torch_available() else _is_torch(_lowerCamelCase      )


def        lowerCAmelCase_     (			_lowerCamelCase:     Dict      ):
		import torch

		return isinstance(_lowerCamelCase ,   torch.device      )


def        lowerCAmelCase_     (			_lowerCamelCase:     Any      ):
		return False if not is_torch_available() else _is_torch_device(_lowerCamelCase      )


def        lowerCAmelCase_     (			_lowerCamelCase:     Any      ):
		import torch

		if isinstance(_lowerCamelCase ,   _lowerCamelCase      ):
				if hasattr(_lowerCamelCase ,   _lowerCamelCase      ):
						__SCREAMING_SNAKE_CASE :	Tuple					 =	getattr(_lowerCamelCase ,   _lowerCamelCase      )
				else:
						return False
		return isinstance(_lowerCamelCase ,   torch.dtype      )


def        lowerCAmelCase_     (			_lowerCamelCase:     Optional[Any]      ):
		return False if not is_torch_available() else _is_torch_dtype(_lowerCamelCase      )


def        lowerCAmelCase_     (			_lowerCamelCase:     str      ):
		import tensorflow as tf

		return isinstance(_lowerCamelCase ,   tf.Tensor      )


def        lowerCAmelCase_     (			_lowerCamelCase:     int      ):
		return False if not is_tf_available() else _is_tensorflow(_lowerCamelCase      )


def        lowerCAmelCase_     (			_lowerCamelCase:     Any      ):
		import tensorflow as tf

		# the `is_symbolic_tensor` predicate is only available starting with TF 2.14
		if hasattr(_lowerCamelCase ,   """is_symbolic_tensor"""      ):
				return tf.is_symbolic_tensor(_lowerCamelCase      )
		return type(_lowerCamelCase      ) == tf.Tensor


def        lowerCAmelCase_     (			_lowerCamelCase:     Any      ):
		return False if not is_tf_available() else _is_tf_symbolic_tensor(_lowerCamelCase      )


def        lowerCAmelCase_     (			_lowerCamelCase:     Any      ):
		import jax.numpy as jnp  # noqa: F811

		return isinstance(_lowerCamelCase ,   jnp.ndarray      )


def        lowerCAmelCase_     (			_lowerCamelCase:     List[str]      ):
		return False if not is_flax_available() else _is_jax(_lowerCamelCase      )


def        lowerCAmelCase_     (			_lowerCamelCase:     int      ):
		if isinstance(_lowerCamelCase ,   (dict, UserDict)      ):
				return {k: to_py_obj(_lowerCamelCase      ) for k, v in obj.items()}
		elif isinstance(_lowerCamelCase ,   (list, tuple)      ):
				return [to_py_obj(_lowerCamelCase      ) for o in obj]
		elif is_tf_tensor(_lowerCamelCase      ):
				return obj.numpy().tolist()
		elif is_torch_tensor(_lowerCamelCase      ):
				return obj.detach().cpu().tolist()
		elif is_jax_tensor(_lowerCamelCase      ):
				return np.asarray(_lowerCamelCase      ).tolist()
		elif isinstance(_lowerCamelCase ,   (np.ndarray, np.number)      ):  # tolist also works on 0d np arrays
				return obj.tolist()
		else:
				return obj


def        lowerCAmelCase_     (			_lowerCamelCase:     Any      ):
		if isinstance(_lowerCamelCase ,   (dict, UserDict)      ):
				return {k: to_numpy(_lowerCamelCase      ) for k, v in obj.items()}
		elif isinstance(_lowerCamelCase ,   (list, tuple)      ):
				return np.array(_lowerCamelCase      )
		elif is_tf_tensor(_lowerCamelCase      ):
				return obj.numpy()
		elif is_torch_tensor(_lowerCamelCase      ):
				return obj.detach().cpu().numpy()
		elif is_jax_tensor(_lowerCamelCase      ):
				return np.asarray(_lowerCamelCase      )
		else:
				return obj






class 		_UpperCamelCase						(							lowerCamelCase__					):


			'''simple docstring'''

			def UpperCamelCase__      (							self  :				Dict		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	int					 =	fields(self		)

					# Safety and consistency checks
					if not len(lowerCAmelCase__		):
							raise ValueError(F"{self.__class__.__name__} has no fields."		)
					if not all(field.default is None for field in class_fields[1:]		):
							raise ValueError(F"{self.__class__.__name__} should not have more than one required field."		)

					__SCREAMING_SNAKE_CASE :	Dict					 =	getattr(self							,  class_fields[0].name		)
					__SCREAMING_SNAKE_CASE :	Dict					 =	all(getattr(self							,  field.name		) is None for field in class_fields[1:]		)

					if other_fields_are_none and not is_tensor(lowerCAmelCase__		):
							if isinstance(lowerCAmelCase__							,  lowerCAmelCase__		):
									__SCREAMING_SNAKE_CASE :	Any					 =	first_field.items()
									__SCREAMING_SNAKE_CASE :	Dict					 =	True
							else:
									try:
											__SCREAMING_SNAKE_CASE :	List[Any]					 =	iter(lowerCAmelCase__		)
											__SCREAMING_SNAKE_CASE :	Optional[int]					 =	True
									except TypeError:
											__SCREAMING_SNAKE_CASE :	int					 =	False

            # if we provided an iterator as first field and the iterator is a (key, value) iterator
            # set the associated fields
							if first_field_iterator:
									for idx, element in enumerate(lowerCAmelCase__		):
											if (
											    not isinstance(lowerCAmelCase__							,  (list, tuple)		)
											    or not len(lowerCAmelCase__		) == 2
											    or not isinstance(element[0]							,  lowerCAmelCase__		)
											):
													if idx == 0:
															# If we do not have an iterator of key/values, set it as attribute
															__SCREAMING_SNAKE_CASE :	str					 =	first_field
													else:
															# If we have a mixed iterator, raise an error
															raise ValueError(
															    F"Cannot set key/value for {element}. It needs to be a tuple (key, value)."		)
													break
											setattr(self							,  element[0]							,  element[1]		)
											if element[1] is not None:
													__SCREAMING_SNAKE_CASE :	Optional[int]					 =	element[1]
							elif first_field is not None:
									__SCREAMING_SNAKE_CASE :	Optional[int]					 =	first_field
					else:
							for field in class_fields:
									__SCREAMING_SNAKE_CASE :	List[Any]					 =	getattr(self							,  field.name		)
									if v is not None:
											__SCREAMING_SNAKE_CASE :	Optional[int]					 =	v

			def __delitem__(							self  :				Optional[Any]							,  *lowerCAmelCase__  :				int							,  **lowerCAmelCase__  :				Tuple		):





					"""simple docstring"""






					raise Exception(F"You cannot use ``__delitem__`` on a {self.__class__.__name__} instance."		)

			def UpperCamelCase__      (							self  :				str							,  *lowerCAmelCase__  :				Optional[Any]							,  **lowerCAmelCase__  :				Dict		):





					"""simple docstring"""






					raise Exception(F"You cannot use ``setdefault`` on a {self.__class__.__name__} instance."		)

			def UpperCamelCase__      (							self  :				str							,  *lowerCAmelCase__  :				Any							,  **lowerCAmelCase__  :				List[Any]		):





					"""simple docstring"""






					raise Exception(F"You cannot use ``pop`` on a {self.__class__.__name__} instance."		)

			def UpperCamelCase__      (							self  :				int							,  *lowerCAmelCase__  :				List[str]							,  **lowerCAmelCase__  :				List[Any]		):





					"""simple docstring"""






					raise Exception(F"You cannot use ``update`` on a {self.__class__.__name__} instance."		)

			def __getitem__(							self  :				Union[str, Any]							,  lowerCAmelCase__  :				Optional[Any]		):





					"""simple docstring"""






					if isinstance(lowerCAmelCase__							,  lowerCAmelCase__		):
							__SCREAMING_SNAKE_CASE :	Tuple					 =	dict(self.items()		)
							return inner_dict[k]
					else:
							return self.to_tuple()[k]

			def __setattr__(							self  :				str							,  lowerCAmelCase__  :				Optional[int]							,  lowerCAmelCase__  :				Any		):





					"""simple docstring"""






					if name in self.keys() and value is not None:
							# Don't call self.__setitem__ to avoid recursion errors
							super().__setitem__(lowerCAmelCase__							,  lowerCAmelCase__		)
					super().__setattr__(lowerCAmelCase__							,  lowerCAmelCase__		)

			def __setitem__(							self  :				Tuple							,  lowerCAmelCase__  :				List[str]							,  lowerCAmelCase__  :				str		):





					"""simple docstring"""






					super().__setitem__(lowerCAmelCase__							,  lowerCAmelCase__		)
					# Don't call self.__setattr__ to avoid recursion errors
					super().__setattr__(lowerCAmelCase__							,  lowerCAmelCase__		)



			def UpperCamelCase__      (							self  :				Optional[Any]		):





					"""simple docstring"""






					return tuple(self[k] for k in self.keys()		)






class 		_UpperCamelCase						(							lowerCamelCase__					,      lowerCamelCase__					):


			'''simple docstring'''

			@classmethod
			def UpperCamelCase__      (							cls  :				List[Any]							,  lowerCAmelCase__  :				Tuple		):





					"""simple docstring"""






					raise ValueError(
					    F"{value} is not a valid {cls.__name__}, please select one of {list(cls._valueamember_map_.keys()		)}"		)






class 		_UpperCamelCase						(							lowerCamelCase__					):


			'''simple docstring'''

			_A   :	Dict    					=	'''longest'''
			_A   :	Optional[Any]    					=	'''max_length'''
			_A   :	Tuple    					=	'''do_not_pad'''






class 		_UpperCamelCase						(							lowerCamelCase__					):


			'''simple docstring'''

			_A   :	Optional[int]    					=	'''pt'''
			_A   :	Union[str, Any]    					=	'''tf'''
			_A   :	Union[str, Any]    					=	'''np'''
			_A   :	Dict    					=	'''jax'''






class 		_UpperCamelCase						:


			'''simple docstring'''

			def __init__(							self  :				str							,  lowerCAmelCase__  :				List[ContextManager]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	int					 =	context_managers
					__SCREAMING_SNAKE_CASE :	Dict					 =	ExitStack()

			def __enter__(							self  :				Union[str, Any]		):





					"""simple docstring"""






					for context_manager in self.context_managers:
							self.stack.enter_context(lowerCAmelCase__		)



			def __exit__(							self  :				Any							,  *lowerCAmelCase__  :				Any							,  **lowerCAmelCase__  :				List[str]		):





					"""simple docstring"""






					self.stack.__exit__(*lowerCAmelCase__							,  **lowerCAmelCase__		)


def        lowerCAmelCase_     (			_lowerCamelCase:     Optional[int]      ):
		__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	infer_framework(_lowerCamelCase      )
		if framework == "tf":
				__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	inspect.signature(model_class.call      )  # TensorFlow models
		elif framework == "pt":
				__SCREAMING_SNAKE_CASE :	int					 =	inspect.signature(model_class.forward      )  # PyTorch models
		else:
				__SCREAMING_SNAKE_CASE :	List[Any]					 =	inspect.signature(model_class.__call__      )  # Flax models

		for p in signature.parameters:
				if p == "return_loss" and signature.parameters[p].default is True:
						return True

		return False


def        lowerCAmelCase_     (			_lowerCamelCase:     Tuple      ):
		__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	model_class.__name__
		__SCREAMING_SNAKE_CASE :	List[Any]					 =	infer_framework(_lowerCamelCase      )
		if framework == "tf":
				__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	inspect.signature(model_class.call      )  # TensorFlow models
		elif framework == "pt":
				__SCREAMING_SNAKE_CASE :	Dict					 =	inspect.signature(model_class.forward      )  # PyTorch models
		else:
				__SCREAMING_SNAKE_CASE :	List[Any]					 =	inspect.signature(model_class.__call__      )  # Flax models

		if "QuestionAnswering" in model_name:
				return [p for p in signature.parameters if "label" in p or p in ("start_positions", "end_positions")]
		else:
				return [p for p in signature.parameters if "label" in p]


def        lowerCAmelCase_     (			_lowerCamelCase:     MutableMapping ,   _lowerCamelCase:     str = "" ,   _lowerCamelCase:     str = "."      ):

		def _flatten_dict(_lowerCamelCase:     str ,   _lowerCamelCase:     Any="" ,   _lowerCamelCase:     List[Any]="."      ):
				for k, v in d.items():
						__SCREAMING_SNAKE_CASE :	Tuple					 =	str(_lowerCamelCase      ) + delimiter + str(_lowerCamelCase      ) if parent_key else k
						if v and isinstance(_lowerCamelCase ,   _lowerCamelCase      ):
								yield from flatten_dict(_lowerCamelCase ,   _lowerCamelCase ,   delimiter=_lowerCamelCase      ).items()
						else:
								yield key, v

		return dict(_flatten_dict(_lowerCamelCase ,   _lowerCamelCase ,   _lowerCamelCase      )      )


@contextmanager
def        lowerCAmelCase_     (			_lowerCamelCase:     Dict ,   _lowerCamelCase:     bool = False      ):
		if use_temp_dir:
				with tempfile.TemporaryDirectory() as tmp_dir:
						yield tmp_dir
		else:
				yield working_dir


def        lowerCAmelCase_     (			_lowerCamelCase:     Union[str, Any] ,   _lowerCamelCase:     Optional[Any]=None      ):
		if is_numpy_array(_lowerCamelCase      ):
				return np.transpose(_lowerCamelCase ,   axes=_lowerCamelCase      )
		elif is_torch_tensor(_lowerCamelCase      ):
				return array.T if axes is None else array.permute(*_lowerCamelCase      )
		elif is_tf_tensor(_lowerCamelCase      ):
				import tensorflow as tf

				return tf.transpose(_lowerCamelCase ,   perm=_lowerCamelCase      )
		elif is_jax_tensor(_lowerCamelCase      ):
				return jnp.transpose(_lowerCamelCase ,   axes=_lowerCamelCase      )
		else:
				raise ValueError(F"Type not supported for transpose: {type(_lowerCamelCase      )}."      )


def        lowerCAmelCase_     (			_lowerCamelCase:     Union[str, Any] ,   _lowerCamelCase:     Dict      ):
		if is_numpy_array(_lowerCamelCase      ):
				return np.reshape(_lowerCamelCase ,   _lowerCamelCase      )
		elif is_torch_tensor(_lowerCamelCase      ):
				return array.reshape(*_lowerCamelCase      )
		elif is_tf_tensor(_lowerCamelCase      ):
				import tensorflow as tf

				return tf.reshape(_lowerCamelCase ,   _lowerCamelCase      )
		elif is_jax_tensor(_lowerCamelCase      ):
				return jnp.reshape(_lowerCamelCase ,   _lowerCamelCase      )
		else:
				raise ValueError(F"Type not supported for reshape: {type(_lowerCamelCase      )}."      )


def        lowerCAmelCase_     (			_lowerCamelCase:     Optional[int] ,   _lowerCamelCase:     Dict=None      ):
		if is_numpy_array(_lowerCamelCase      ):
				return np.squeeze(_lowerCamelCase ,   axis=_lowerCamelCase      )
		elif is_torch_tensor(_lowerCamelCase      ):
				return array.squeeze() if axis is None else array.squeeze(dim=_lowerCamelCase      )
		elif is_tf_tensor(_lowerCamelCase      ):
				import tensorflow as tf

				return tf.squeeze(_lowerCamelCase ,   axis=_lowerCamelCase      )
		elif is_jax_tensor(_lowerCamelCase      ):
				return jnp.squeeze(_lowerCamelCase ,   axis=_lowerCamelCase      )
		else:
				raise ValueError(F"Type not supported for squeeze: {type(_lowerCamelCase      )}."      )


def        lowerCAmelCase_     (			_lowerCamelCase:     List[str] ,   _lowerCamelCase:     List[str]      ):
		if is_numpy_array(_lowerCamelCase      ):
				return np.expand_dims(_lowerCamelCase ,   _lowerCamelCase      )
		elif is_torch_tensor(_lowerCamelCase      ):
				return array.unsqueeze(dim=_lowerCamelCase      )
		elif is_tf_tensor(_lowerCamelCase      ):
				import tensorflow as tf

				return tf.expand_dims(_lowerCamelCase ,   axis=_lowerCamelCase      )
		elif is_jax_tensor(_lowerCamelCase      ):
				return jnp.expand_dims(_lowerCamelCase ,   axis=_lowerCamelCase      )
		else:
				raise ValueError(F"Type not supported for expand_dims: {type(_lowerCamelCase      )}."      )


def        lowerCAmelCase_     (			_lowerCamelCase:     int      ):
		if is_numpy_array(_lowerCamelCase      ):
				return np.size(_lowerCamelCase      )
		elif is_torch_tensor(_lowerCamelCase      ):
				return array.numel()
		elif is_tf_tensor(_lowerCamelCase      ):
				import tensorflow as tf

				return tf.size(_lowerCamelCase      )
		elif is_jax_tensor(_lowerCamelCase      ):
				return array.size
		else:
				raise ValueError(F"Type not supported for expand_dims: {type(_lowerCamelCase      )}."      )


def        lowerCAmelCase_     (			_lowerCamelCase:     str ,   _lowerCamelCase:     Any      ):
		for key, value in auto_map.items():
				if isinstance(_lowerCamelCase ,   (tuple, list)      ):
						__SCREAMING_SNAKE_CASE :	Dict					 =	[F"{repo_id}--{v}" if (v is not None and """--""" not in v) else v for v in value]
				elif value is not None and "--" not in value:
						__SCREAMING_SNAKE_CASE :	Any					 =	F"{repo_id}--{value}"

		return auto_map


def        lowerCAmelCase_     (			_lowerCamelCase:     Optional[int]      ):
		for base_class in inspect.getmro(_lowerCamelCase      ):
				__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	base_class.__module__
				__SCREAMING_SNAKE_CASE :	Any					 =	base_class.__name__
				if module.startswith("""tensorflow"""      ) or module.startswith("""keras"""      ) or name == "TFPreTrainedModel":
						return "tf"
				elif module.startswith("""torch"""      ) or name == "PreTrainedModel":
						return "pt"
				elif module.startswith("""flax"""      ) or module.startswith("""jax"""      ) or name == "FlaxPreTrainedModel":
						return "flax"
		else:
				raise TypeError(F"Could not infer framework from class {model_class}."      )