
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 CLIPImageProcessor, CLIPProcessor




@require_vision
class   __lowercase						(						unittest.TestCase       ):






	def       _a(self				: Union[str, Any]    ) ->			Optional[Any]:
							_lowercase       :	str           =   tempfile.mkdtemp()

							# fmt: off
							_lowercase       :	int           =   ["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
							_lowercase       :	Any           =   dict(zip(snake_case      ,	range(len(snake_case    )    )    )    )
							_lowercase       :	str           =   ["#version: 0.2", "l o", "lo w</w>", "e r</w>", ""]
							_lowercase       :	Dict           =   {"unk_token": "<unk>"}

							_lowercase       :	Any           =   os.path.join(self.tmpdirname      ,	VOCAB_FILES_NAMES["vocab_file"]    )
							_lowercase       :	int           =   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(snake_case    ) + "\n"    )
							with open(self.merges_file      ,	"w"      ,	encoding="utf-8"    ) as fp:
													fp.write("\n".join(snake_case    )    )

							_lowercase       :	Dict           =   {
							    "do_resize": True,
							    "size": 20,
							    "do_center_crop": True,
							    "crop_size": 18,
							    "do_normalize": True,
							    "image_mean": [0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73],
							    "image_std": [0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11],
							}
							_lowercase       :	str           =   os.path.join(self.tmpdirname      ,	snake_case    )
							with open(self.image_processor_file      ,	"w"      ,	encoding="utf-8"    ) as fp:
													json.dump(snake_case      ,	snake_case    )






	def       _a(self				: List[Any]      ,	**snake_case				: Tuple    ) ->			Optional[Any]:
							return CLIPTokenizer.from_pretrained(self.tmpdirname      ,	**snake_case    )






	def       _a(self				: str      ,	**snake_case				: Optional[int]    ) ->			int:
							return CLIPTokenizerFast.from_pretrained(self.tmpdirname      ,	**snake_case    )






	def       _a(self				: Any      ,	**snake_case				: Tuple    ) ->			Tuple:
							return CLIPImageProcessor.from_pretrained(self.tmpdirname      ,	**snake_case    )






	def       _a(self				: Union[str, Any]    ) ->			Union[str, Any]:
							shutil.rmtree(self.tmpdirname    )






	def       _a(self				: Any    ) ->			int:
							_lowercase       :	Tuple           =   [np.random.randint(255      ,	size=(3, 30, 400)      ,	dtype=np.uinta    )]

							_lowercase       :	int           =   [Image.fromarray(np.moveaxis(snake_case      ,	0      ,	-1    )    ) for x in image_inputs]

							return image_inputs






	def       _a(self				: Tuple    ) ->			List[Any]:
							_lowercase       :	List[str]           =   self.get_tokenizer()
							_lowercase       :	Any           =   self.get_rust_tokenizer()
							_lowercase       :	Union[str, Any]           =   self.get_image_processor()

							_lowercase       :	List[str]           =   CLIPProcessor(tokenizer=snake_case      ,	image_processor=snake_case    )
							processor_slow.save_pretrained(self.tmpdirname    )
							_lowercase       :	str           =   CLIPProcessor.from_pretrained(self.tmpdirname      ,	use_fast=snake_case    )

							_lowercase       :	Any           =   CLIPProcessor(tokenizer=snake_case      ,	image_processor=snake_case    )
							processor_fast.save_pretrained(self.tmpdirname    )
							_lowercase       :	int           =   CLIPProcessor.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      ,	snake_case    )
							self.assertIsInstance(processor_fast.tokenizer      ,	snake_case    )

							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      ,	snake_case    )
							self.assertIsInstance(processor_fast.image_processor      ,	snake_case    )






	def       _a(self				: Any    ) ->			Dict:
							_lowercase       :	List[Any]           =   CLIPProcessor(tokenizer=self.get_tokenizer()      ,	image_processor=self.get_image_processor()    )
							processor.save_pretrained(self.tmpdirname    )

							_lowercase       :	Any           =   self.get_tokenizer(bos_token="(BOS)"      ,	eos_token="(EOS)"    )
							_lowercase       :	Optional[Any]           =   self.get_image_processor(do_normalize=snake_case      ,	padding_value=1.0    )

							_lowercase       :	Tuple           =   CLIPProcessor.from_pretrained(
							    self.tmpdirname      ,	bos_token="(BOS)"      ,	eos_token="(EOS)"      ,	do_normalize=snake_case      ,	padding_value=1.0    )

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

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






	def       _a(self				: Any    ) ->			List[str]:
							_lowercase       :	List[str]           =   self.get_image_processor()
							_lowercase       :	Tuple           =   self.get_tokenizer()

							_lowercase       :	int           =   CLIPProcessor(tokenizer=snake_case      ,	image_processor=snake_case    )

							_lowercase       :	Tuple           =   self.prepare_image_inputs()

							_lowercase       :	int           =   image_processor(snake_case      ,	return_tensors="np"    )
							_lowercase       :	Optional[int]           =   processor(images=snake_case      ,	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       _a(self				: Optional[int]    ) ->			List[Any]:
							_lowercase       :	List[Any]           =   self.get_image_processor()
							_lowercase       :	Optional[int]           =   self.get_tokenizer()

							_lowercase       :	Union[str, Any]           =   CLIPProcessor(tokenizer=snake_case      ,	image_processor=snake_case    )

							_lowercase       :	Any           =   "lower newer"

							_lowercase       :	str           =   processor(text=snake_case    )

							_lowercase       :	List[Any]           =   tokenizer(snake_case    )

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






	def       _a(self				: Tuple    ) ->			List[str]:
							_lowercase       :	List[Any]           =   self.get_image_processor()
							_lowercase       :	Union[str, Any]           =   self.get_tokenizer()

							_lowercase       :	List[Any]           =   CLIPProcessor(tokenizer=snake_case      ,	image_processor=snake_case    )

							_lowercase       :	Optional[int]           =   "lower newer"
							_lowercase       :	Dict           =   self.prepare_image_inputs()

							_lowercase       :	Any           =   processor(text=snake_case      ,	images=snake_case    )

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

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






	def       _a(self				: Optional[Any]    ) ->			List[Any]:
							_lowercase       :	Optional[int]           =   self.get_image_processor()
							_lowercase       :	str           =   self.get_tokenizer()

							_lowercase       :	Optional[Any]           =   CLIPProcessor(tokenizer=snake_case      ,	image_processor=snake_case    )

							_lowercase       :	List[Any]           =   [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

							_lowercase       :	Tuple           =   processor.batch_decode(snake_case    )
							_lowercase       :	Any           =   tokenizer.batch_decode(snake_case    )

							self.assertListEqual(snake_case      ,	snake_case    )






	def       _a(self				: str    ) ->			Any:
							_lowercase       :	Tuple           =   self.get_image_processor()
							_lowercase       :	Optional[int]           =   self.get_tokenizer()

							_lowercase       :	Any           =   CLIPProcessor(tokenizer=snake_case      ,	image_processor=snake_case    )

							_lowercase       :	Any           =   "lower newer"
							_lowercase       :	Optional[Any]           =   self.prepare_image_inputs()

							_lowercase       :	List[Any]           =   processor(text=snake_case      ,	images=snake_case    )

							self.assertListEqual(list(inputs.keys()    )      ,	processor.model_input_names    )






from typing import Dict, Iterable, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_DEFAULT_MEAN,
    IMAGENET_DEFAULT_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


UpperCamelCase_						:     Any	       =							logging.get_logger(__name__)




class   __lowercase						(						__snake_case       ):
	_A       =   ["pixel_values"]






	def __init__(self				: Dict      ,	snake_case				: bool = True      ,	snake_case				: Dict[str, int] = None      ,	snake_case				: PILImageResampling = PILImageResampling.BICUBIC      ,	snake_case				: bool = True      ,	snake_case				: Dict[str, int] = None      ,	snake_case				: bool = True      ,	snake_case				: Union[int, float] = 1 / 255      ,	snake_case				: bool = True      ,	snake_case				: Optional[Union[float, Iterable[float]]] = IMAGENET_DEFAULT_MEAN      ,	snake_case				: Optional[Union[float, Iterable[float]]] = IMAGENET_DEFAULT_STD      ,	**snake_case				: Optional[int]      ,	) ->			None:
							super().__init__(**snake_case    )
							_lowercase       :	str           =   size if size is not None else {"shortest_edge": 224}
							_lowercase       :	List[str]           =   get_size_dict(snake_case      ,	default_to_square=snake_case    )
							_lowercase       :	Optional[int]           =   crop_size if crop_size is not None else {"height": 224, "width": 224}
							_lowercase       :	List[Any]           =   get_size_dict(snake_case      ,	param_name="crop_size"    )

							_lowercase       :	Optional[int]           =   do_resize
							_lowercase       :	List[str]           =   size
							_lowercase       :	Tuple           =   resample
							_lowercase       :	Union[str, Any]           =   do_center_crop
							_lowercase       :	List[str]           =   crop_size
							_lowercase       :	Tuple           =   do_rescale
							_lowercase       :	Union[str, Any]           =   rescale_factor
							_lowercase       :	str           =   do_normalize
							_lowercase       :	List[Any]           =   image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN
							_lowercase       :	List[Any]           =   image_std if image_std is not None else IMAGENET_DEFAULT_STD






	def       _a(self				: str      ,	snake_case				: np.ndarray      ,	snake_case				: Dict[str, int]      ,	snake_case				: PILImageResampling = PILImageResampling.BICUBIC      ,	snake_case				: Optional[Union[str, ChannelDimension]] = None      ,	**snake_case				: str      ,	) ->			np.ndarray:
							_lowercase       :	Tuple           =   get_size_dict(snake_case      ,	default_to_square=snake_case    )
							# size_dict is a dict with either keys "height" and "width" or "shortest_edge"
							if "shortest_edge" in size:
													_lowercase       :	List[str]           =   int((256 / 224) * size["shortest_edge"]    )
													_lowercase       :	Dict           =   get_resize_output_image_size(snake_case      ,	size=snake_case      ,	default_to_square=snake_case    )
													_lowercase       :	int           =   {"height": output_size[0], "width": output_size[1]}
							if "height" not in size_dict or "width" not in size_dict:
													raise ValueError(
													    F"""Size dict must have keys 'height' and 'width' or 'shortest_edge'. Got {size_dict.keys()}"""    )
							return resize(
							    snake_case      ,	size=(size_dict["height"], size_dict["width"])      ,	resample=snake_case      ,	data_format=snake_case      ,	**snake_case    )






	def       _a(self				: Any      ,	snake_case				: np.ndarray      ,	snake_case				: Dict[str, int]      ,	snake_case				: Optional[Union[str, ChannelDimension]] = None      ,	**snake_case				: Any      ,	) ->			np.ndarray:
							_lowercase       :	Tuple           =   get_size_dict(snake_case    )
							if "height" not in size or "width" not in size:
													raise ValueError(F"""Size dict must have keys 'height' and 'width'. Got {size.keys()}"""    )
							return center_crop(snake_case      ,	size=(size["height"], size["width"])      ,	data_format=snake_case      ,	**snake_case    )






	def       _a(self				: Dict      ,	snake_case				: np.ndarray      ,	snake_case				: Union[int, float]      ,	snake_case				: Optional[Union[str, ChannelDimension]] = None      ,	**snake_case				: List[str]      ,	) ->			np.ndarray:
							return rescale(snake_case      ,	scale=snake_case      ,	data_format=snake_case      ,	**snake_case    )






	def       _a(self				: str      ,	snake_case				: np.ndarray      ,	snake_case				: Union[float, List[float]]      ,	snake_case				: Union[float, List[float]]      ,	snake_case				: Optional[Union[str, ChannelDimension]] = None      ,	**snake_case				: Optional[int]      ,	) ->			np.ndarray:
							return normalize(snake_case      ,	mean=snake_case      ,	std=snake_case      ,	data_format=snake_case      ,	**snake_case    )






	def       _a(self				: Any      ,	snake_case				: ImageInput      ,	snake_case				: Optional[bool] = None      ,	snake_case				: Optional[Dict[str, int]] = None      ,	snake_case				: PILImageResampling = None      ,	snake_case				: Optional[bool] = None      ,	snake_case				: Optional[Dict[str, int]] = None      ,	snake_case				: Optional[bool] = None      ,	snake_case				: Optional[float] = None      ,	snake_case				: Optional[bool] = None      ,	snake_case				: Optional[Union[float, Iterable[float]]] = None      ,	snake_case				: Optional[Union[float, Iterable[float]]] = None      ,	snake_case				: Optional[TensorType] = None      ,	snake_case				: ChannelDimension = ChannelDimension.FIRST      ,	**snake_case				: Tuple      ,	) ->			BatchFeature:
							_lowercase       :	Optional[Any]           =   do_resize if do_resize is not None else self.do_resize
							_lowercase       :	Any           =   resample if resample is not None else self.resample
							_lowercase       :	Optional[Any]           =   do_center_crop if do_center_crop is not None else self.do_center_crop
							_lowercase       :	Union[str, Any]           =   do_rescale if do_rescale is not None else self.do_rescale
							_lowercase       :	Optional[Any]           =   rescale_factor if rescale_factor is not None else self.rescale_factor
							_lowercase       :	str           =   do_normalize if do_normalize is not None else self.do_normalize
							_lowercase       :	Dict           =   image_mean if image_mean is not None else self.image_mean
							_lowercase       :	Dict           =   image_std if image_std is not None else self.image_std

							_lowercase       :	Optional[int]           =   size if size is not None else self.size
							_lowercase       :	Dict           =   get_size_dict(snake_case      ,	default_to_square=snake_case    )
							_lowercase       :	Dict           =   crop_size if crop_size is not None else self.crop_size
							_lowercase       :	Optional[int]           =   get_size_dict(snake_case      ,	param_name="crop_size"    )

							_lowercase       :	Dict           =   make_list_of_images(snake_case    )

							if not valid_images(snake_case    ):
													raise ValueError(
													    "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, "
													    "torch.Tensor, tf.Tensor or jax.ndarray."    )

							if do_resize and size is None:
													raise ValueError("Size must be specified if do_resize is True."    )

							if do_center_crop and crop_size is None:
													raise ValueError("Crop size must be specified if do_center_crop is True."    )

							if do_rescale and rescale_factor is None:
													raise ValueError("Rescale factor must be specified if do_rescale is True."    )

							if do_normalize and (image_mean is None or image_std is None):
													raise ValueError("Image mean and std must be specified if do_normalize is True."    )

							# All transformations expect numpy arrays.
							_lowercase       :	Dict           =   [to_numpy_array(snake_case    ) for image in images]

							if do_resize:
													_lowercase       :	List[Any]           =   [self.resize(snake_case      ,	snake_case      ,	snake_case    ) for image in images]

							if do_center_crop:
													_lowercase       :	Union[str, Any]           =   [self.center_crop(snake_case      ,	snake_case    ) for image in images]

							if do_rescale:
													_lowercase       :	List[str]           =   [self.rescale(snake_case      ,	snake_case    ) for image in images]

							if do_normalize:
													_lowercase       :	str           =   [self.normalize(snake_case      ,	snake_case      ,	snake_case    ) for image in images]

							_lowercase       :	Any           =   [to_channel_dimension_format(snake_case      ,	snake_case    ) for image in images]

							_lowercase       :	str           =   {"pixel_values": images}
							return BatchFeature(data=snake_case      ,	tensor_type=snake_case    )




