


"""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  UpperCamelCase_	( UpperCamelCase):

	"""simple docstring"""





	snake_case__				:  Optional[int]    					=			["image_processor", "tokenizer"]
	snake_case__				:  Union[str, Any]    					=			"OwlViTImageProcessor"
	snake_case__				:  str    					=			("CLIPTokenizer", "CLIPTokenizerFast")




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

						__SCREAMING_SNAKE_CASE						   =				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__(UpperCAmelCase__			,			UpperCAmelCase__   )




	def __call__( self			:				Dict			,			UpperCAmelCase__			:				Dict=None			,			UpperCAmelCase__			:				str=None			,			UpperCAmelCase__			:				List[Any]=None			,			UpperCAmelCase__			:				Optional[Any]="max_length"			,			UpperCAmelCase__			:				Optional[Any]="np"			,			**UpperCAmelCase__			:				Union[str, Any]   )       ->	Optional[int]:

						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(UpperCAmelCase__			,			UpperCAmelCase__   ) or (isinstance(UpperCAmelCase__			,			UpperCAmelCase__   ) and not isinstance(text[0]			,			UpperCAmelCase__   )):
																__SCREAMING_SNAKE_CASE						   =				[self.tokenizer(UpperCAmelCase__			,			padding=UpperCAmelCase__			,			return_tensors=UpperCAmelCase__			,			**UpperCAmelCase__   )]

											elif isinstance(UpperCAmelCase__			,			UpperCAmelCase__   ) and isinstance(text[0]			,			UpperCAmelCase__   ):
																__SCREAMING_SNAKE_CASE						   =				[]

																# Maximum number of queries across batch
																__SCREAMING_SNAKE_CASE						   =				max([len(UpperCAmelCase__   ) for t in text]   )

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

																					__SCREAMING_SNAKE_CASE						   =				self.tokenizer(UpperCAmelCase__			,			padding=UpperCAmelCase__			,			return_tensors=UpperCAmelCase__			,			**UpperCAmelCase__   )
																					encodings.append(UpperCAmelCase__   )
											else:
																raise TypeError("Input text should be a string, a list of strings or a nested list of strings"   )

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

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

																__SCREAMING_SNAKE_CASE						   =				jnp.concatenate([encoding["input_ids"] for encoding in encodings]			,			axis=0   )
																__SCREAMING_SNAKE_CASE						   =				jnp.concatenate([encoding["attention_mask"] for encoding in encodings]			,			axis=0   )

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

																__SCREAMING_SNAKE_CASE						   =				torch.cat([encoding["input_ids"] for encoding in encodings]			,			dim=0   )
																__SCREAMING_SNAKE_CASE						   =				torch.cat([encoding["attention_mask"] for encoding in encodings]			,			dim=0   )

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

																__SCREAMING_SNAKE_CASE						   =				tf.stack([encoding["input_ids"] for encoding in encodings]			,			axis=0   )
																__SCREAMING_SNAKE_CASE						   =				tf.stack([encoding["attention_mask"] for encoding in encodings]			,			axis=0   )

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

											__SCREAMING_SNAKE_CASE						   =				BatchEncoding()
											__SCREAMING_SNAKE_CASE						   =				input_ids
											__SCREAMING_SNAKE_CASE						   =				attention_mask

						if query_images is not None:
											__SCREAMING_SNAKE_CASE						   =				BatchEncoding()
											__SCREAMING_SNAKE_CASE						   =				self.image_processor(
											    UpperCAmelCase__			,			return_tensors=UpperCAmelCase__			,			**UpperCAmelCase__   ).pixel_values
											__SCREAMING_SNAKE_CASE						   =				query_pixel_values

						if images is not None:
											__SCREAMING_SNAKE_CASE						   =				self.image_processor(UpperCAmelCase__			,			return_tensors=UpperCAmelCase__			,			**UpperCAmelCase__   )

						if text is not None and images is not None:
											__SCREAMING_SNAKE_CASE						   =				image_features.pixel_values
											return encoding
						elif query_images is not None and images is not None:
											__SCREAMING_SNAKE_CASE						   =				image_features.pixel_values
											return encoding
						elif text is not None or query_images is not None:
											return encoding
						else:
											return BatchEncoding(data=dict(**UpperCAmelCase__   )			,			tensor_type=UpperCAmelCase__   )




	def   UpperCAmelCase_      ( self			:				Optional[Any]			,			*UpperCAmelCase__			:				Optional[Any]			,			**UpperCAmelCase__			:				str   )       ->	List[str]:
						return self.image_processor.post_process(*UpperCAmelCase__			,			**UpperCAmelCase__   )




	def   UpperCAmelCase_      ( self			:				Optional[int]			,			*UpperCAmelCase__			:				Dict			,			**UpperCAmelCase__			:				Optional[int]   )       ->	Tuple:
						return self.image_processor.post_process_object_detection(*UpperCAmelCase__			,			**UpperCAmelCase__   )




	def   UpperCAmelCase_      ( self			:				str			,			*UpperCAmelCase__			:				int			,			**UpperCAmelCase__			:				Optional[int]   )       ->	List[Any]:
						return self.image_processor.post_process_image_guided_detection(*UpperCAmelCase__			,			**UpperCAmelCase__   )




	def   UpperCAmelCase_      ( self			:				Optional[int]			,			*UpperCAmelCase__			:				Any			,			**UpperCAmelCase__			:				Optional[int]   )       ->	Optional[int]:
						return self.tokenizer.batch_decode(*UpperCAmelCase__			,			**UpperCAmelCase__   )




	def   UpperCAmelCase_      ( self			:				Tuple			,			*UpperCAmelCase__			:				Union[str, Any]			,			**UpperCAmelCase__			:				Optional[int]   )       ->	Tuple:
						return self.tokenizer.decode(*UpperCAmelCase__			,			**UpperCAmelCase__   )




	@property
	def   UpperCAmelCase_      ( self			:				Any   )       ->	str:
						warnings.warn(
						    "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."			,			UpperCAmelCase__			,			)
						return self.image_processor_class




	@property
	def   UpperCAmelCase_      ( self			:				List[str]   )       ->	str:
						warnings.warn(
						    "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."			,			UpperCAmelCase__			,			)
						return self.image_processor




from typing import Optional

from .. import Features, NamedSplit
from ..packaged_modules.text.text import Text
from ..utils.typing import NestedDataStructureLike, PathLike
from .abc import AbstractDatasetReader
class        _lowercase      (			A__       ):






				'''simple docstring'''



				def __init__(       self			:int			,			lowerCAmelCase__			:NestedDataStructureLike[PathLike]			,			lowerCAmelCase__			:Optional[NamedSplit] = None			,			lowerCAmelCase__			:Optional[Features] = None			,			lowerCAmelCase__			:str = None			,			lowerCAmelCase__			:bool = False			,			lowerCAmelCase__			:bool = False			,			lowerCAmelCase__			:Optional[int] = None			,			**lowerCAmelCase__			:Optional[int]			,			)		->			Tuple:
										super().__init__(
										    lowerCAmelCase__			,			split=lowerCAmelCase__			,			features=lowerCAmelCase__			,			cache_dir=lowerCAmelCase__			,			keep_in_memory=lowerCAmelCase__			,			streaming=lowerCAmelCase__			,			num_proc=lowerCAmelCase__			,			**lowerCAmelCase__			,			)
										__SCREAMING_SNAKE_CASE						:     List[str]					=		path_or_paths if isinstance(lowerCAmelCase__			,			lowerCAmelCase__	) else {self.split: path_or_paths}
										__SCREAMING_SNAKE_CASE						:     int					=		Text(
										    cache_dir=lowerCAmelCase__			,			data_files=lowerCAmelCase__			,			features=lowerCAmelCase__			,			**lowerCAmelCase__			,			)



				def 		__magic_name__(       self			:Dict	)		->			Tuple:
										# Build iterable dataset
										if self.streaming:
																__SCREAMING_SNAKE_CASE						:     int					=		self.builder.as_streaming_dataset(split=self.split	)
										# Build regular (map-style) dataset
										else:
																__SCREAMING_SNAKE_CASE						:     List[str]					=		None
																__SCREAMING_SNAKE_CASE						:     str					=		None
																__SCREAMING_SNAKE_CASE						:     Dict					=		None
																__SCREAMING_SNAKE_CASE						:     Tuple					=		None

																self.builder.download_and_prepare(
																    download_config=lowerCAmelCase__			,			download_mode=lowerCAmelCase__			,			verification_mode=lowerCAmelCase__			,			base_path=lowerCAmelCase__			,			num_proc=self.num_proc			,			)
																__SCREAMING_SNAKE_CASE						:     Optional[int]					=		self.builder.as_dataset(
																    split=self.split			,			verification_mode=lowerCAmelCase__			,			in_memory=self.keep_in_memory	)
										return dataset
