


from typing import Dict, List, Optional, Union

import numpy as np

from .feature_extraction_utils import BatchFeature, FeatureExtractionMixin
from .utils import PaddingStrategy, TensorType, is_tf_tensor, is_torch_tensor, logging, to_numpy


__snake_case            =   logging.get_logger(__name__)




class    UpperCAmelCase					(       __snake_case    ):




			def __init__(						self							:		Dict       ,       __magic_name__							:		int       ,       __magic_name__							:		int       ,       __magic_name__							:		float       ,       **__magic_name__							:		Tuple						):







						"""simple docstring"""







						UpperCamelCase         =					feature_size
						UpperCamelCase         =					sampling_rate
						UpperCamelCase         =					padding_value

						UpperCamelCase         =					kwargs.pop("""padding_side"""       ,       """right"""						)
						UpperCamelCase         =					kwargs.pop("""return_attention_mask"""       ,       __magic_name__						)

						super().__init__(**__magic_name__						)




			def   lowerCamelCase_					(						self							:		Any       ,       __magic_name__							:		Union[
			        BatchFeature,
			        List[BatchFeature],
			        Dict[str, BatchFeature],
			        Dict[str, List[BatchFeature]],
			        List[Dict[str, BatchFeature]],
			    ]       ,       __magic_name__							:		Union[bool, str, PaddingStrategy] = True       ,       __magic_name__							:		Optional[int] = None       ,       __magic_name__							:		bool = False       ,       __magic_name__							:		Optional[int] = None       ,       __magic_name__							:		Optional[bool] = None       ,       __magic_name__							:		Optional[Union[str, TensorType]] = None       ,       ):







						"""simple docstring"""







						if isinstance(__magic_name__       ,       (list, tuple)						) and isinstance(processed_features[0]       ,       (dict, BatchFeature)						):
									UpperCamelCase         =					{
									    key: [example[key] for example in processed_features] for key in processed_features[0].keys()
									}

						# The model's main input name, usually `input_values`, has be passed for padding
						if self.model_input_names[0] not in processed_features:
									raise ValueError(
									    """You should supply an instance of `transformers.BatchFeature` or list of `transformers.BatchFeature`"""
									    F' to this method that includes {self.model_input_names[0]}, but you provided'
									    F' {list(processed_features.keys()						)}'						)

						UpperCamelCase         =					processed_features[self.model_input_names[0]]
						UpperCamelCase         =					(
						    return_attention_mask if return_attention_mask is not None else self.return_attention_mask
						)

						if len(__magic_name__						) == 0:
									if return_attention_mask:
												UpperCamelCase         =					[]
									return processed_features

						# If we have PyTorch/TF tensors or lists as inputs, we cast them as Numpy arrays
						# and rebuild them afterwards if no return_tensors is specified
						# Note that we lose the specific device the tensor may be on for PyTorch

						UpperCamelCase         =					required_input[0]
						if isinstance(__magic_name__       ,       (list, tuple)						):
									# first_element might be an empty list/tuple in some edge cases so we grab the first non empty element.
									UpperCamelCase         =					0
									while len(required_input[index]						) == 0:
												index += 1
									if index < len(__magic_name__						):
												UpperCamelCase         =					required_input[index][0]

						if return_tensors is None:
									if is_tf_tensor(__magic_name__						):
												UpperCamelCase         =					"""tf"""
									elif is_torch_tensor(__magic_name__						):
												UpperCamelCase         =					"""pt"""
									elif isinstance(__magic_name__       ,       (int, float, list, tuple, np.ndarray)						):
												UpperCamelCase         =					"""np"""
									else:
												raise ValueError(
												    F'type of {first_element} unknown: {type(__magic_name__						)}. '
												    """Should be one of a python, numpy, pytorch or tensorflow object."""						)

						for key, value in processed_features.items():
									if isinstance(value[0]       ,       (int, float)						):
												UpperCamelCase         =					to_numpy(__magic_name__						)
									else:
												UpperCamelCase         =					[to_numpy(__magic_name__						) for v in value]

        # Convert padding_strategy in PaddingStrategy
						UpperCamelCase         =					self._get_padding_strategies(padding=__magic_name__       ,       max_length=__magic_name__						)

						UpperCamelCase         =					processed_features[self.model_input_names[0]]

						UpperCamelCase         =					len(__magic_name__						)
						if not all(len(__magic_name__						) == batch_size for v in processed_features.values()						):
									raise ValueError("""Some items in the output dictionary have a different batch size than others."""						)

						UpperCamelCase         =					[]
						for i in range(__magic_name__						):
									UpperCamelCase         =					{k: v[i] for k, v in processed_features.items()}
									# truncation
									UpperCamelCase         =					self._truncate(
									    __magic_name__       ,       max_length=__magic_name__       ,       pad_to_multiple_of=__magic_name__       ,       truncation=__magic_name__       ,       )
									truncated_inputs.append(__magic_name__						)

						if padding_strategy == PaddingStrategy.LONGEST:
									# make sure that `max_length` cannot be longer than the longest truncated length
									UpperCamelCase         =					max(len(input_slice[self.model_input_names[0]]						) for input_slice in truncated_inputs						)
									UpperCamelCase         =					PaddingStrategy.MAX_LENGTH

						UpperCamelCase         =					{}
						for i in range(__magic_name__						):
									# padding
									UpperCamelCase         =					self._pad(
									    truncated_inputs[i]       ,       max_length=__magic_name__       ,       padding_strategy=__magic_name__       ,       pad_to_multiple_of=__magic_name__       ,       return_attention_mask=__magic_name__       ,       )

									for key, value in outputs.items():
												if key not in batch_outputs:
															UpperCamelCase         =					[]
												if value.dtype is np.dtype(np.floataa						):
															UpperCamelCase         =					value.astype(np.floataa						)
												batch_outputs[key].append(__magic_name__						)

						return BatchFeature(__magic_name__       ,       tensor_type=__magic_name__						)




			def   lowerCamelCase_					(						self							:		Optional[Any]       ,       __magic_name__							:		Union[Dict[str, np.ndarray], BatchFeature]       ,       __magic_name__							:		Optional[int] = None       ,       __magic_name__							:		PaddingStrategy = PaddingStrategy.DO_NOT_PAD       ,       __magic_name__							:		Optional[int] = None       ,       __magic_name__							:		Optional[bool] = None       ,       ):







						"""simple docstring"""







						UpperCamelCase         =					processed_features[self.model_input_names[0]]

						if padding_strategy == PaddingStrategy.LONGEST:
									UpperCamelCase         =					len(__magic_name__						)

						if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0):
									UpperCamelCase         =					((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of

						UpperCamelCase         =					padding_strategy != PaddingStrategy.DO_NOT_PAD and len(__magic_name__						) < max_length

						if return_attention_mask and "attention_mask" not in processed_features:
									UpperCamelCase         =					np.ones(len(__magic_name__						)       ,       dtype=np.intaa						)

						if needs_to_be_padded:
									UpperCamelCase         =					max_length - len(__magic_name__						)
									if self.padding_side == "right":
												if return_attention_mask:
															UpperCamelCase         =					np.pad(
															    processed_features["""attention_mask"""]       ,       (0, difference)						)
												UpperCamelCase         =					((0, difference), (0, 0)) if self.feature_size > 1 else (0, difference)
												UpperCamelCase         =					np.pad(
												    __magic_name__       ,       __magic_name__       ,       """constant"""       ,       constant_values=self.padding_value						)
									elif self.padding_side == "left":
												if return_attention_mask:
															UpperCamelCase         =					np.pad(
															    processed_features["""attention_mask"""]       ,       (difference, 0)						)
												UpperCamelCase         =					((difference, 0), (0, 0)) if self.feature_size > 1 else (difference, 0)
												UpperCamelCase         =					np.pad(
												    __magic_name__       ,       __magic_name__       ,       """constant"""       ,       constant_values=self.padding_value						)
									else:
												raise ValueError("""Invalid padding strategy:""" + str(self.padding_side						)						)

						return processed_features




			def   lowerCamelCase_					(						self							:		Tuple       ,       __magic_name__							:		Union[Dict[str, np.ndarray], BatchFeature]       ,       __magic_name__							:		Optional[int] = None       ,       __magic_name__							:		Optional[int] = None       ,       __magic_name__							:		Optional[bool] = None       ,       ):







						"""simple docstring"""







						if not truncation:
									return processed_features
						elif truncation and max_length is None:
									raise ValueError("""When setting ``truncation=True``, make sure that ``max_length`` is defined."""						)

						UpperCamelCase         =					processed_features[self.model_input_names[0]]

						# find `max_length` that fits `pad_to_multiple_of`
						if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0):
									UpperCamelCase         =					((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of

						UpperCamelCase         =					len(__magic_name__						) > max_length

						if needs_to_be_truncated:
									UpperCamelCase         =					processed_features[self.model_input_names[0]][:max_length]
									if "attention_mask" in processed_features:
												UpperCamelCase         =					processed_features["""attention_mask"""][:max_length]

						return processed_features





			def   lowerCamelCase_					(						self							:		List[Any]       ,       __magic_name__							:		List[str]=False       ,       __magic_name__							:		List[str]=None						):







						"""simple docstring"""







						if padding is not False:
									if padding is True:
												UpperCamelCase         =					PaddingStrategy.LONGEST  # Default to pad to the longest sequence in the batch
									elif not isinstance(__magic_name__       ,       __magic_name__						):
												UpperCamelCase         =					PaddingStrategy(__magic_name__						)
									elif isinstance(__magic_name__       ,       __magic_name__						):
												UpperCamelCase         =					padding
						else:
									UpperCamelCase         =					PaddingStrategy.DO_NOT_PAD

						# Set max length if needed
						if max_length is None:
									if padding_strategy == PaddingStrategy.MAX_LENGTH:
												raise ValueError(
												    F'When setting ``padding={PaddingStrategy.MAX_LENGTH}``, make sure that max_length is defined'						)

        # Test if we have a padding value
						if padding_strategy != PaddingStrategy.DO_NOT_PAD and (self.padding_value is None):
									raise ValueError(
									    """Asking to pad but the feature_extractor does not have a padding value. Please select a value to use"""
									    """ as `padding_value`. For example: `feature_extractor.padding_value = 0.0`."""						)

						return padding_strategy








import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
			from PIL import Image

			from transformers import OwlViTImageProcessor, OwlViTProcessor




@require_vision
class    UpperCAmelCase					(       unittest.TestCase    ):




			def   lowerCamelCase_					(						self							:		Optional[Any]						):







						"""simple docstring"""







						UpperCamelCase         =					tempfile.mkdtemp()

						# fmt: off
						UpperCamelCase         =					["""""", """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """lo""", """l</w>""", """w</w>""", """r</w>""", """t</w>""", """low</w>""", """er</w>""", """lowest</w>""", """newer</w>""", """wider""", """<unk>""", """<|startoftext|>""", """<|endoftext|>"""]
						# fmt: on
						UpperCamelCase         =					dict(zip(__magic_name__       ,       range(len(__magic_name__						)						)						)						)
						UpperCamelCase         =					["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""]
						UpperCamelCase         =					{"""unk_token""": """<unk>"""}

						UpperCamelCase         =					os.path.join(self.tmpdirname       ,       VOCAB_FILES_NAMES["""vocab_file"""]						)
						UpperCamelCase         =					os.path.join(self.tmpdirname       ,       VOCAB_FILES_NAMES["""merges_file"""]						)
						with open(self.vocab_file       ,       """w"""       ,       encoding="""utf-8"""						) as fp:
									fp.write(json.dumps(__magic_name__						) + """\n"""						)
						with open(self.merges_file       ,       """w"""       ,       encoding="""utf-8"""						) as fp:
									fp.write("""\n""".join(__magic_name__						)						)

						UpperCamelCase         =					{
						    """do_resize""": True,
						    """size""": 2_0,
						    """do_center_crop""": True,
						    """crop_size""": 1_8,
						    """do_normalize""": True,
						    """image_mean""": [0.48_145_466, 0.4_578_275, 0.40_821_073],
						    """image_std""": [0.26_862_954, 0.26_130_258, 0.27_577_711],
						}
						UpperCamelCase         =					os.path.join(self.tmpdirname       ,       __magic_name__						)
						with open(self.image_processor_file       ,       """w"""       ,       encoding="""utf-8"""						) as fp:
									json.dump(__magic_name__       ,       __magic_name__						)




			def   lowerCamelCase_					(						self							:		str       ,       **__magic_name__							:		Tuple						):







						"""simple docstring"""







						return CLIPTokenizer.from_pretrained(self.tmpdirname       ,       pad_token="""!"""       ,       **__magic_name__						)




			def   lowerCamelCase_					(						self							:		Optional[int]       ,       **__magic_name__							:		str						):







						"""simple docstring"""







						return CLIPTokenizerFast.from_pretrained(self.tmpdirname       ,       pad_token="""!"""       ,       **__magic_name__						)




			def   lowerCamelCase_					(						self							:		Optional[int]       ,       **__magic_name__							:		Optional[Any]						):







						"""simple docstring"""







						return OwlViTImageProcessor.from_pretrained(self.tmpdirname       ,       **__magic_name__						)




			def   lowerCamelCase_					(						self							:		int						):







						"""simple docstring"""







						shutil.rmtree(self.tmpdirname						)




			def   lowerCamelCase_					(						self							:		Dict						):







						"""simple docstring"""







						UpperCamelCase         =					[np.random.randint(2_5_5       ,       size=(3, 3_0, 4_0_0)       ,       dtype=np.uinta						)]

						UpperCamelCase         =					[Image.fromarray(np.moveaxis(__magic_name__       ,       0       ,       -1						)						) for x in image_inputs]

						return image_inputs




			def   lowerCamelCase_					(						self							:		Dict						):







						"""simple docstring"""







						UpperCamelCase         =					self.get_tokenizer()
						UpperCamelCase         =					self.get_rust_tokenizer()
						UpperCamelCase         =					self.get_image_processor()

						UpperCamelCase         =					OwlViTProcessor(tokenizer=__magic_name__       ,       image_processor=__magic_name__						)
						processor_slow.save_pretrained(self.tmpdirname						)
						UpperCamelCase         =					OwlViTProcessor.from_pretrained(self.tmpdirname       ,       use_fast=__magic_name__						)

						UpperCamelCase         =					OwlViTProcessor(tokenizer=__magic_name__       ,       image_processor=__magic_name__						)
						processor_fast.save_pretrained(self.tmpdirname						)
						UpperCamelCase         =					OwlViTProcessor.from_pretrained(self.tmpdirname						)

						self.assertEqual(processor_slow.tokenizer.get_vocab()       ,       tokenizer_slow.get_vocab()						)
						self.assertEqual(processor_fast.tokenizer.get_vocab()       ,       tokenizer_fast.get_vocab()						)
						self.assertEqual(tokenizer_slow.get_vocab()       ,       tokenizer_fast.get_vocab()						)
						self.assertIsInstance(processor_slow.tokenizer       ,       __magic_name__						)
						self.assertIsInstance(processor_fast.tokenizer       ,       __magic_name__						)

						self.assertEqual(processor_slow.image_processor.to_json_string()       ,       image_processor.to_json_string()						)
						self.assertEqual(processor_fast.image_processor.to_json_string()       ,       image_processor.to_json_string()						)
						self.assertIsInstance(processor_slow.image_processor       ,       __magic_name__						)
						self.assertIsInstance(processor_fast.image_processor       ,       __magic_name__						)




			def   lowerCamelCase_					(						self							:		int						):







						"""simple docstring"""







						UpperCamelCase         =					OwlViTProcessor(tokenizer=self.get_tokenizer()       ,       image_processor=self.get_image_processor()						)
						processor.save_pretrained(self.tmpdirname						)

						UpperCamelCase         =					self.get_tokenizer(bos_token="""(BOS)"""       ,       eos_token="""(EOS)"""						)
						UpperCamelCase         =					self.get_image_processor(do_normalize=__magic_name__						)

						UpperCamelCase         =					OwlViTProcessor.from_pretrained(
						    self.tmpdirname       ,       bos_token="""(BOS)"""       ,       eos_token="""(EOS)"""       ,       do_normalize=__magic_name__						)

						self.assertEqual(processor.tokenizer.get_vocab()       ,       tokenizer_add_kwargs.get_vocab()						)
						self.assertIsInstance(processor.tokenizer       ,       __magic_name__						)

						self.assertEqual(processor.image_processor.to_json_string()       ,       image_processor_add_kwargs.to_json_string()						)
						self.assertIsInstance(processor.image_processor       ,       __magic_name__						)




			def   lowerCamelCase_					(						self							:		Dict						):







						"""simple docstring"""







						UpperCamelCase         =					self.get_image_processor()
						UpperCamelCase         =					self.get_tokenizer()

						UpperCamelCase         =					OwlViTProcessor(tokenizer=__magic_name__       ,       image_processor=__magic_name__						)

						UpperCamelCase         =					self.prepare_image_inputs()

						UpperCamelCase         =					image_processor(__magic_name__       ,       return_tensors="""np"""						)
						UpperCamelCase         =					processor(images=__magic_name__       ,       return_tensors="""np"""						)

						for key in input_image_proc.keys():
									self.assertAlmostEqual(input_image_proc[key].sum()       ,       input_processor[key].sum()       ,       delta=1e-2						)




			def   lowerCamelCase_					(						self							:		Any						):







						"""simple docstring"""







						UpperCamelCase         =					self.get_image_processor()
						UpperCamelCase         =					self.get_tokenizer()

						UpperCamelCase         =					OwlViTProcessor(tokenizer=__magic_name__       ,       image_processor=__magic_name__						)

						UpperCamelCase         =					"""lower newer"""

						UpperCamelCase         =					processor(text=__magic_name__       ,       return_tensors="""np"""						)

						UpperCamelCase         =					tokenizer(__magic_name__       ,       return_tensors="""np"""						)

						for key in encoded_tok.keys():
									self.assertListEqual(encoded_tok[key][0].tolist()       ,       encoded_processor[key][0].tolist()						)




			def   lowerCamelCase_					(						self							:		Optional[Any]						):







						"""simple docstring"""







						UpperCamelCase         =					self.get_image_processor()
						UpperCamelCase         =					self.get_tokenizer()

						UpperCamelCase         =					OwlViTProcessor(tokenizer=__magic_name__       ,       image_processor=__magic_name__						)

						UpperCamelCase         =					"""lower newer"""
						UpperCamelCase         =					self.prepare_image_inputs()

						UpperCamelCase         =					processor(text=__magic_name__       ,       images=__magic_name__						)

						self.assertListEqual(list(inputs.keys()						)       ,       ["""input_ids""", """attention_mask""", """pixel_values"""]						)

						# test if it raises when no input is passed
						with pytest.raises(__magic_name__						):
									processor()




			def   lowerCamelCase_					(						self							:		int						):







						"""simple docstring"""







						UpperCamelCase         =					"""google/owlvit-base-patch32"""
						UpperCamelCase         =					OwlViTProcessor.from_pretrained(__magic_name__						)

						UpperCamelCase         =					["""cat""", """nasa badge"""]
						UpperCamelCase         =					processor(text=__magic_name__						)

						UpperCamelCase         =					1_6
						self.assertListEqual(list(inputs.keys()						)       ,       ["""input_ids""", """attention_mask"""]						)
						self.assertEqual(inputs["""input_ids"""].shape       ,       (2, seq_length)						)

						# test if it raises when no input is passed
						with pytest.raises(__magic_name__						):
									processor()




			def   lowerCamelCase_					(						self							:		Dict						):







						"""simple docstring"""







						UpperCamelCase         =					"""google/owlvit-base-patch32"""
						UpperCamelCase         =					OwlViTProcessor.from_pretrained(__magic_name__						)

						UpperCamelCase         =					[["""cat""", """nasa badge"""], ["""person"""]]
						UpperCamelCase         =					processor(text=__magic_name__						)

						UpperCamelCase         =					1_6
						UpperCamelCase         =					len(__magic_name__						)
						UpperCamelCase         =					max([len(__magic_name__						) for texts in input_texts]						)

						self.assertListEqual(list(inputs.keys()						)       ,       ["""input_ids""", """attention_mask"""]						)
						self.assertEqual(inputs["""input_ids"""].shape       ,       (batch_size * num_max_text_queries, seq_length)						)

						# test if it raises when no input is passed
						with pytest.raises(__magic_name__						):
									processor()




			def   lowerCamelCase_					(						self							:		List[str]						):







						"""simple docstring"""







						UpperCamelCase         =					"""google/owlvit-base-patch32"""
						UpperCamelCase         =					OwlViTProcessor.from_pretrained(__magic_name__						)

						UpperCamelCase         =					["""cat""", """nasa badge"""]
						UpperCamelCase         =					processor(text=__magic_name__						)

						UpperCamelCase         =					1_6
						UpperCamelCase         =					inputs["""input_ids"""]
						UpperCamelCase         =					[
						    [4_9_4_0_6, 2_3_6_8, 4_9_4_0_7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
						    [4_9_4_0_6, 6_8_4_1, 1_1_3_0_1, 4_9_4_0_7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
						]

						self.assertListEqual(list(inputs.keys()						)       ,       ["""input_ids""", """attention_mask"""]						)
						self.assertEqual(inputs["""input_ids"""].shape       ,       (2, seq_length)						)
						self.assertListEqual(list(input_ids[0]						)       ,       predicted_ids[0]						)
						self.assertListEqual(list(input_ids[1]						)       ,       predicted_ids[1]						)




			def   lowerCamelCase_					(						self							:		str						):







						"""simple docstring"""







						UpperCamelCase         =					self.get_image_processor()
						UpperCamelCase         =					self.get_tokenizer()

						UpperCamelCase         =					OwlViTProcessor(tokenizer=__magic_name__       ,       image_processor=__magic_name__						)

						UpperCamelCase         =					self.prepare_image_inputs()
						UpperCamelCase         =					self.prepare_image_inputs()

						UpperCamelCase         =					processor(images=__magic_name__       ,       query_images=__magic_name__						)

						self.assertListEqual(list(inputs.keys()						)       ,       ["""query_pixel_values""", """pixel_values"""]						)

						# test if it raises when no input is passed
						with pytest.raises(__magic_name__						):
									processor()





			def   lowerCamelCase_					(						self							:		Dict						):







						"""simple docstring"""







						UpperCamelCase         =					self.get_image_processor()
						UpperCamelCase         =					self.get_tokenizer()

						UpperCamelCase         =					OwlViTProcessor(tokenizer=__magic_name__       ,       image_processor=__magic_name__						)

						UpperCamelCase         =					[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

						UpperCamelCase         =					processor.batch_decode(__magic_name__						)
						UpperCamelCase         =					tokenizer.batch_decode(__magic_name__						)

						self.assertListEqual(__magic_name__       ,       __magic_name__						)




