





'''simple docstring'''







import json
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
from transformers import (
    CONFIG_MAPPING,
    IMAGE_PROCESSOR_MAPPING,
    AutoConfig,
    AutoImageProcessor,
    CLIPConfig,
    CLIPImageProcessor,
)
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER


sys.path.append(str(Path(__file__).parent.parent.parent.parent / 'utils'))

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_image_processing import CustomImageProcessor  # noqa E402
class 				__SCREAMING_SNAKE_CASE  (unittest.TestCase      ):






				"""simple docstring"""







				def   UpperCamelCase__   (  self     :					Tuple					):
								_a								= 0




				def   UpperCamelCase__   (  self     :					List[Any]					):
								_a								= AutoImageProcessor.from_pretrained("openai/clip-vit-base-patch32"					)
								self.assertIsInstance(__a					,				__a					)




				def   UpperCamelCase__   (  self     :					Union[str, Any]					):
								with tempfile.TemporaryDirectory() as tmpdirname:
												_a								= Path(__a					) / "preprocessor_config.json"
												_a								= Path(__a					) / "config.json"
												json.dump(
												    {"image_processor_type": "CLIPImageProcessor", "processor_class": "CLIPProcessor"}					,				open(__a					,				"w"					)					,				)
												json.dump({"model_type": "clip"}					,				open(__a					,				"w"					)					)

												_a								= AutoImageProcessor.from_pretrained(__a					)
												self.assertIsInstance(__a					,				__a					)




				def   UpperCamelCase__   (  self     :					str					):
								# Ensure we can load the image processor from the feature extractor config
								with tempfile.TemporaryDirectory() as tmpdirname:
												_a								= Path(__a					) / "preprocessor_config.json"
												_a								= Path(__a					) / "config.json"
												json.dump(
												    {"feature_extractor_type": "CLIPFeatureExtractor", "processor_class": "CLIPProcessor"}					,				open(__a					,				"w"					)					,				)
												json.dump({"model_type": "clip"}					,				open(__a					,				"w"					)					)

												_a								= AutoImageProcessor.from_pretrained(__a					)
												self.assertIsInstance(__a					,				__a					)




				def   UpperCamelCase__   (  self     :					Tuple					):
								with tempfile.TemporaryDirectory() as tmpdirname:
												_a								= CLIPConfig()

												# Create a dummy config file with image_proceesor_type
												_a								= Path(__a					) / "preprocessor_config.json"
												_a								= Path(__a					) / "config.json"
												json.dump(
												    {"image_processor_type": "CLIPImageProcessor", "processor_class": "CLIPProcessor"}					,				open(__a					,				"w"					)					,				)
												json.dump({"model_type": "clip"}					,				open(__a					,				"w"					)					)

												# remove image_processor_type to make sure config.json alone is enough to load image processor locally
												_a								= AutoImageProcessor.from_pretrained(__a					).to_dict()

												config_dict.pop("image_processor_type"					)
												_a								= CLIPImageProcessor(**__a					)

												# save in new folder
												model_config.save_pretrained(__a					)
												config.save_pretrained(__a					)

												_a								= AutoImageProcessor.from_pretrained(__a					)

												# make sure private variable is not incorrectly saved
												_a								= json.loads(config.to_json_string()					)
												self.assertTrue("_processor_class" not in dict_as_saved					)

								self.assertIsInstance(__a					,				__a					)




				def   UpperCamelCase__   (  self     :					str					):
								with tempfile.TemporaryDirectory() as tmpdirname:
												_a								= Path(__a					) / "preprocessor_config.json"
												json.dump(
												    {"image_processor_type": "CLIPImageProcessor", "processor_class": "CLIPProcessor"}					,				open(__a					,				"w"					)					,				)

												_a								= AutoImageProcessor.from_pretrained(__a					)
												self.assertIsInstance(__a					,				__a					)




				def   UpperCamelCase__   (  self     :					Optional[Any]					):
								with self.assertRaisesRegex(
								    __a					,				"clip-base is not a local folder and is not a valid model identifier"					):
												_a								= AutoImageProcessor.from_pretrained("clip-base"					)




				def   UpperCamelCase__   (  self     :					Tuple					):
								with self.assertRaisesRegex(
								    __a					,				r"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"					):
												_a								= AutoImageProcessor.from_pretrained(__a					,				revision="aaaaaa"					)




				def   UpperCamelCase__   (  self     :					List[str]					):
								with self.assertRaisesRegex(
								    __a					,				"hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json."					,				):
												_a								= AutoImageProcessor.from_pretrained("hf-internal-testing/config-no-model"					)




				def   UpperCamelCase__   (  self     :					Any					):
								# If remote code is not set, we will time out when asking whether to load the model.
								with self.assertRaises(__a					):
												_a								= AutoImageProcessor.from_pretrained("hf-internal-testing/test_dynamic_image_processor"					)
								# If remote code is disabled, we can't load this config.
								with self.assertRaises(__a					):
												_a								= AutoImageProcessor.from_pretrained(
												    "hf-internal-testing/test_dynamic_image_processor"					,				trust_remote_code=__a					)

								_a								= AutoImageProcessor.from_pretrained(
								    "hf-internal-testing/test_dynamic_image_processor"					,				trust_remote_code=__a					)
								self.assertEqual(image_processor.__class__.__name__					,				"NewImageProcessor"					)

								# Test image processor can be reloaded.
								with tempfile.TemporaryDirectory() as tmp_dir:
												image_processor.save_pretrained(__a					)
												_a								= AutoImageProcessor.from_pretrained(__a					,				trust_remote_code=__a					)
								self.assertEqual(reloaded_image_processor.__class__.__name__					,				"NewImageProcessor"					)




				def   UpperCamelCase__   (  self     :					Optional[Any]					):
								try:
												AutoConfig.register("custom"					,				__a					)
												AutoImageProcessor.register(__a					,				__a					)
												# Trying to register something existing in the Transformers library will raise an error
												with self.assertRaises(__a					):
																AutoImageProcessor.register(__a					,				__a					)

												with tempfile.TemporaryDirectory() as tmpdirname:
																_a								= Path(__a					) / "preprocessor_config.json"
																_a								= Path(__a					) / "config.json"
																json.dump(
																    {"feature_extractor_type": "CLIPFeatureExtractor", "processor_class": "CLIPProcessor"}					,				open(__a					,				"w"					)					,				)
																json.dump({"model_type": "clip"}					,				open(__a					,				"w"					)					)

																_a								= CustomImageProcessor.from_pretrained(__a					)

												# Now that the config is registered, it can be used as any other config with the auto-API
												with tempfile.TemporaryDirectory() as tmp_dir:
																image_processor.save_pretrained(__a					)
																_a								= AutoImageProcessor.from_pretrained(__a					)
																self.assertIsInstance(__a					,				__a					)

								finally:
												if "custom" in CONFIG_MAPPING._extra_content:
																del CONFIG_MAPPING._extra_content["custom"]
												if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content:
																del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]




				def   UpperCamelCase__   (  self     :					Optional[Any]					):
								class 				__SCREAMING_SNAKE_CASE  (lowerCamelCase_      ):






												"""simple docstring"""







												__a            =True

								try:
												AutoConfig.register("custom"					,				__a					)
												AutoImageProcessor.register(__a					,				__a					)
												# If remote code is not set, the default is to use local
												_a								= AutoImageProcessor.from_pretrained("hf-internal-testing/test_dynamic_image_processor"					)
												self.assertEqual(image_processor.__class__.__name__					,				"NewImageProcessor"					)
												self.assertTrue(image_processor.is_local					)

												# If remote code is disabled, we load the local one.
												_a								= AutoImageProcessor.from_pretrained(
												    "hf-internal-testing/test_dynamic_image_processor"					,				trust_remote_code=__a					)
												self.assertEqual(image_processor.__class__.__name__					,				"NewImageProcessor"					)
												self.assertTrue(image_processor.is_local					)

												# If remote is enabled, we load from the Hub
												_a								= AutoImageProcessor.from_pretrained(
												    "hf-internal-testing/test_dynamic_image_processor"					,				trust_remote_code=__a					)
												self.assertEqual(image_processor.__class__.__name__					,				"NewImageProcessor"					)
												self.assertTrue(not hasattr(__a					,				"is_local"					)					)

								finally:
												if "custom" in CONFIG_MAPPING._extra_content:
																del CONFIG_MAPPING._extra_content["custom"]
												if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content:
																del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]







'''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 				__SCREAMING_SNAKE_CASE  (lowerCamelCase_      ):






				"""simple docstring"""







				__a            =['image_processor', 'tokenizer']
				__a            ='OwlViTImageProcessor'
				__a            =('CLIPTokenizer', 'CLIPTokenizerFast')




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

								_a								= 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__(__a					,				__a					)




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

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

												elif isinstance(__a					,				__a					) and isinstance(text[0]					,				__a					):
																_a								= []

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

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

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

												if return_tensors == "np":
																_a								= np.concatenate([encoding["input_ids"] for encoding in encodings]					,				axis=0					)
																_a								= 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								= jnp.concatenate([encoding["input_ids"] for encoding in encodings]					,				axis=0					)
																_a								= jnp.concatenate([encoding["attention_mask"] for encoding in encodings]					,				axis=0					)

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

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

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

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

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

												_a								= BatchEncoding()
												_a								= input_ids
												_a								= attention_mask

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

								if images is not None:
												_a								= self.image_processor(__a					,				return_tensors=__a					,				**__a					)

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




				def   UpperCamelCase__   (  self     :					List[str]					,				*__a     :					Union[str, Any]					,				**__a     :					int					):
								return self.image_processor.post_process(*__a					,				**__a					)




				def   UpperCamelCase__   (  self     :					Optional[int]					,				*__a     :					Optional[Any]					,				**__a     :					List[str]					):
								return self.image_processor.post_process_object_detection(*__a					,				**__a					)




				def   UpperCamelCase__   (  self     :					Optional[Any]					,				*__a     :					Dict					,				**__a     :					Union[str, Any]					):
								return self.image_processor.post_process_image_guided_detection(*__a					,				**__a					)




				def   UpperCamelCase__   (  self     :					str					,				*__a     :					Tuple					,				**__a     :					Tuple					):
								return self.tokenizer.batch_decode(*__a					,				**__a					)




				def   UpperCamelCase__   (  self     :					List[str]					,				*__a     :					List[Any]					,				**__a     :					Optional[int]					):
								return self.tokenizer.decode(*__a					,				**__a					)




				@property
				def   UpperCamelCase__   (  self     :					List[str]					):
								warnings.warn(
								    "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."					,				__a					,				)
								return self.image_processor_class




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