
import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
					import torch

if is_vision_available():
					from PIL import Image

					from transformers import ChineseCLIPImageProcessor





class 				UpperCAmelCase (    unittest.TestCase    ):




	'''simple docstring'''





	def __init__(			self   :					Any				,A   :					str				,A   :					List[str]=7				,A   :					Optional[Any]=3				,A   :					int=18				,A   :					Union[str, Any]=30				,A   :					Dict=4_00				,A   :					int=True				,A   :					Tuple=None				,A   :					Optional[int]=True				,A   :					str=None				,A   :					Optional[Any]=True				,A   :					List[str]=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73]				,A   :					Dict=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11]				,A   :					Tuple=True				,):
					__A       =					size if size is not None else {"height": 2_24, "width": 2_24}
					__A       =					crop_size if crop_size is not None else {"height": 18, "width": 18}
					__A       =					parent
					__A       =					batch_size
					__A       =					num_channels
					__A       =					image_size
					__A       =					min_resolution
					__A       =					max_resolution
					__A       =					do_resize
					__A       =					size
					__A       =					do_center_crop
					__A       =					crop_size
					__A       =					do_normalize
					__A       =					image_mean
					__A       =					image_std
					__A       =					do_convert_rgb






	def 							UpperCamelCase_    (			self   :					str					):
					return {
					    "do_resize": self.do_resize,
					    "size": self.size,
					    "do_center_crop": self.do_center_crop,
					    "crop_size": self.crop_size,
					    "do_normalize": self.do_normalize,
					    "image_mean": self.image_mean,
					    "image_std": self.image_std,
					    "do_convert_rgb": self.do_convert_rgb,
					}







	def 							UpperCamelCase_    (			self   :					str				,A   :					List[Any]=False				,A   :					int=False				,A   :					Optional[int]=False					):
					assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

					if equal_resolution:
									__A       =					[]
									for i in range(self.batch_size					):
													image_inputs.append(
													    np.random.randint(
													        2_55				,size=(self.num_channels, self.max_resolution, self.max_resolution)				,dtype=np.uinta					)					)
					else:
									__A       =					[]
									for i in range(self.batch_size					):
													__A     ,				__A       =					np.random.choice(np.arange(self.min_resolution				,self.max_resolution					)				,2					)
													image_inputs.append(np.random.randint(2_55				,size=(self.num_channels, width, height)				,dtype=np.uinta					)					)

					if not numpify and not torchify:
									# PIL expects the channel dimension as last dimension
									__A       =					[Image.fromarray(np.moveaxis(A				,0				,-1					)					) for x in image_inputs]

					if torchify:
									__A       =					[torch.from_numpy(A					) for x in image_inputs]

					return image_inputs





@require_torch
@require_vision
class 				UpperCAmelCase (    __SCREAMING_SNAKE_CASE     ,     unittest.TestCase    ):




	'''simple docstring'''





	snake_case_          =	ChineseCLIPImageProcessor if is_vision_available() else None






	def 							UpperCamelCase_    (			self   :					List[Any]					):
					__A       =					ChineseCLIPImageProcessingTester(self				,do_center_crop=A					)






	@property
	def 							UpperCamelCase_    (			self   :					Tuple					):
					return self.image_processor_tester.prepare_image_processor_dict()






	def 							UpperCamelCase_    (			self   :					Union[str, Any]					):
					__A       =					self.image_processing_class(**self.image_processor_dict					)
					self.assertTrue(hasattr(A				,"do_resize"					)					)
					self.assertTrue(hasattr(A				,"size"					)					)
					self.assertTrue(hasattr(A				,"do_center_crop"					)					)
					self.assertTrue(hasattr(A				,"center_crop"					)					)
					self.assertTrue(hasattr(A				,"do_normalize"					)					)
					self.assertTrue(hasattr(A				,"image_mean"					)					)
					self.assertTrue(hasattr(A				,"image_std"					)					)
					self.assertTrue(hasattr(A				,"do_convert_rgb"					)					)






	def 							UpperCamelCase_    (			self   :					Dict					):
					__A       =					self.image_processing_class.from_dict(self.image_processor_dict					)
					self.assertEqual(image_processor.size				,{"height": 2_24, "width": 2_24}					)
					self.assertEqual(image_processor.crop_size				,{"height": 18, "width": 18}					)

					__A       =					self.image_processing_class.from_dict(self.image_processor_dict				,size=42				,crop_size=84					)
					self.assertEqual(image_processor.size				,{"shortest_edge": 42}					)
					self.assertEqual(image_processor.crop_size				,{"height": 84, "width": 84}					)






	def 							UpperCamelCase_    (			self   :					Dict					):
					pass






	def 							UpperCamelCase_    (			self   :					List[Any]					):
					# Initialize image_processing
					__A       =					self.image_processing_class(**self.image_processor_dict					)
					# create random PIL images
					__A       =					self.image_processor_tester.prepare_inputs(equal_resolution=A					)
					for image in image_inputs:
									self.assertIsInstance(A				,Image.Image					)

					# Test not batched input
					__A       =					image_processing(image_inputs[0]				,return_tensors="pt"					).pixel_values
					self.assertEqual(
					    encoded_images.shape				,(
					        1,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.crop_size["height"],
					        self.image_processor_tester.crop_size["width"],
					    )				,)

					# Test batched
					__A       =					image_processing(A				,return_tensors="pt"					).pixel_values
					self.assertEqual(
					    encoded_images.shape				,(
					        self.image_processor_tester.batch_size,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.crop_size["height"],
					        self.image_processor_tester.crop_size["width"],
					    )				,)






	def 							UpperCamelCase_    (			self   :					Any					):
					# Initialize image_processing
					__A       =					self.image_processing_class(**self.image_processor_dict					)
					# create random numpy tensors
					__A       =					self.image_processor_tester.prepare_inputs(equal_resolution=A				,numpify=A					)
					for image in image_inputs:
									self.assertIsInstance(A				,np.ndarray					)

					# Test not batched input
					__A       =					image_processing(image_inputs[0]				,return_tensors="pt"					).pixel_values
					self.assertEqual(
					    encoded_images.shape				,(
					        1,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.crop_size["height"],
					        self.image_processor_tester.crop_size["width"],
					    )				,)

					# Test batched
					__A       =					image_processing(A				,return_tensors="pt"					).pixel_values
					self.assertEqual(
					    encoded_images.shape				,(
					        self.image_processor_tester.batch_size,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.crop_size["height"],
					        self.image_processor_tester.crop_size["width"],
					    )				,)







	def 							UpperCamelCase_    (			self   :					Any					):
					# Initialize image_processing
					__A       =					self.image_processing_class(**self.image_processor_dict					)
					# create random PyTorch tensors
					__A       =					self.image_processor_tester.prepare_inputs(equal_resolution=A				,torchify=A					)
					for image in image_inputs:
									self.assertIsInstance(A				,torch.Tensor					)

					# Test not batched input
					__A       =					image_processing(image_inputs[0]				,return_tensors="pt"					).pixel_values
					self.assertEqual(
					    encoded_images.shape				,(
					        1,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.crop_size["height"],
					        self.image_processor_tester.crop_size["width"],
					    )				,)

					# Test batched
					__A       =					image_processing(A				,return_tensors="pt"					).pixel_values
					self.assertEqual(
					    encoded_images.shape				,(
					        self.image_processor_tester.batch_size,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.crop_size["height"],
					        self.image_processor_tester.crop_size["width"],
					    )				,)





@require_torch
@require_vision
class 				UpperCAmelCase (    __SCREAMING_SNAKE_CASE     ,     unittest.TestCase    ):




	'''simple docstring'''





	snake_case_          =	ChineseCLIPImageProcessor if is_vision_available() else None






	def 							UpperCamelCase_    (			self   :					List[Any]					):
					__A       =					ChineseCLIPImageProcessingTester(self				,num_channels=4				,do_center_crop=A					)
					__A       =					3






	@property
	def 							UpperCamelCase_    (			self   :					Dict					):
					return self.image_processor_tester.prepare_image_processor_dict()






	def 							UpperCamelCase_    (			self   :					Dict					):
					__A       =					self.image_processing_class(**self.image_processor_dict					)
					self.assertTrue(hasattr(A				,"do_resize"					)					)
					self.assertTrue(hasattr(A				,"size"					)					)
					self.assertTrue(hasattr(A				,"do_center_crop"					)					)
					self.assertTrue(hasattr(A				,"center_crop"					)					)
					self.assertTrue(hasattr(A				,"do_normalize"					)					)
					self.assertTrue(hasattr(A				,"image_mean"					)					)
					self.assertTrue(hasattr(A				,"image_std"					)					)
					self.assertTrue(hasattr(A				,"do_convert_rgb"					)					)






	def 							UpperCamelCase_    (			self   :					int					):
					pass







	def 							UpperCamelCase_    (			self   :					Union[str, Any]					):
					# Initialize image_processing
					__A       =					self.image_processing_class(**self.image_processor_dict					)
					# create random PIL images
					__A       =					self.image_processor_tester.prepare_inputs(equal_resolution=A					)
					for image in image_inputs:
									self.assertIsInstance(A				,Image.Image					)

					# Test not batched input
					__A       =					image_processing(image_inputs[0]				,return_tensors="pt"					).pixel_values
					self.assertEqual(
					    encoded_images.shape				,(
					        1,
					        self.expected_encoded_image_num_channels,
					        self.image_processor_tester.crop_size["height"],
					        self.image_processor_tester.crop_size["width"],
					    )				,)

					# Test batched
					__A       =					image_processing(A				,return_tensors="pt"					).pixel_values
					self.assertEqual(
					    encoded_images.shape				,(
					        self.image_processor_tester.batch_size,
					        self.expected_encoded_image_num_channels,
					        self.image_processor_tester.crop_size["height"],
					        self.image_processor_tester.crop_size["width"],
					    )				,)






import warnings
from typing import Dict, List, Optional, Tuple

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


a__		     =     logging.get_logger(__name__)


class    SCREAMING_SNAKE_CASE_				(       _UpperCamelCase   ):



	"""simple docstring"""


	__magic_name__   :       int      	=		['input_ids', 'attention_mask']






	def __init__(					self		:						List[Any]					,     lowerCAmelCase		:						Union[str, Any]="</s>"					,     lowerCAmelCase		:						Tuple="<unk>"					,     lowerCAmelCase		:						Union[str, Any]="<pad>"					,     lowerCAmelCase		:						Tuple=125					,     lowerCAmelCase		:						str=None					,     **lowerCAmelCase		:						Optional[Any]					,     )	->       None:







						"""simple docstring"""







						if extra_ids > 0 and additional_special_tokens is None:
											__UpperCamelCase					:	str             =      [F'''<extra_id_{i}>''' for i in range(lowerCAmelCase	)]
						elif extra_ids > 0 and additional_special_tokens is not None:
											# Check that we have the right number of extra_id special tokens
											__UpperCamelCase					:	Tuple             =      len(set(filter(lambda lowerCAmelCase	: bool("""extra_id""" in str(lowerCAmelCase	)	)					,     lowerCAmelCase	)	)	)
											if extra_tokens != extra_ids:
																raise ValueError(
																    F'''Both extra_ids ({extra_ids}) and additional_special_tokens ({additional_special_tokens}) are'''
																    """ provided to ByT5Tokenizer. In this case the additional_special_tokens must include the"""
																    """ extra_ids tokens"""	)

						__UpperCamelCase					:	Tuple             =      AddedToken(lowerCAmelCase					,     lstrip=lowerCAmelCase					,     rstrip=lowerCAmelCase	) if isinstance(lowerCAmelCase					,     lowerCAmelCase	) else pad_token
						__UpperCamelCase					:	Tuple             =      AddedToken(lowerCAmelCase					,     lstrip=lowerCAmelCase					,     rstrip=lowerCAmelCase	) if isinstance(lowerCAmelCase					,     lowerCAmelCase	) else eos_token
						__UpperCamelCase					:	int             =      AddedToken(lowerCAmelCase					,     lstrip=lowerCAmelCase					,     rstrip=lowerCAmelCase	) if isinstance(lowerCAmelCase					,     lowerCAmelCase	) else unk_token

						super().__init__(
						    eos_token=lowerCAmelCase					,     unk_token=lowerCAmelCase					,     pad_token=lowerCAmelCase					,     extra_ids=lowerCAmelCase					,     additional_special_tokens=lowerCAmelCase					,     **lowerCAmelCase					,     )

						__UpperCamelCase					:	Any             =      extra_ids

						__UpperCamelCase					:	List[Any]             =      2**8  # utf is 8 bits

						# define special tokens dict
						__UpperCamelCase					:	Dict[int, str]             =      {
						    self.pad_token: 0,
						    self.eos_token: 1,
						    self.unk_token: 2,
						}
						__UpperCamelCase					:	int             =      len(self.special_tokens_encoder	)
						__UpperCamelCase					:	Tuple             =      len(lowerCAmelCase	)
						for i, token in enumerate(lowerCAmelCase	):
											__UpperCamelCase					:	Optional[int]             =      self.vocab_size + i - n
						__UpperCamelCase					:	Dict[str, int]             =      {v: k for k, v in self.special_tokens_encoder.items()}






	@property
	def 					lowerCamelCase__     (					self		:						Any	)	->       Optional[int]:







						"""simple docstring"""







						return self._utf_vocab_size + self._num_special_tokens + self._extra_ids






	def 					lowerCamelCase__     (					self		:						Optional[int]					,     lowerCAmelCase		:						List[int]					,     lowerCAmelCase		:						Optional[List[int]] = None					,     lowerCAmelCase		:						bool = False	)	->       List[int]:







						"""simple docstring"""







						if already_has_special_tokens:
											return super().get_special_tokens_mask(
											    token_ids_a=lowerCAmelCase					,     token_ids_a=lowerCAmelCase					,     already_has_special_tokens=lowerCAmelCase	)

						# normal case: some special tokens
						if token_ids_a is None:
											return ([0] * len(lowerCAmelCase	)) + [1]
						return ([0] * len(lowerCAmelCase	)) + [1] + ([0] * len(lowerCAmelCase	)) + [1]






	def 					lowerCamelCase__     (					self		:						Tuple					,     lowerCAmelCase		:						List[int]	)	->       List[int]:







						"""simple docstring"""







						if len(lowerCAmelCase	) > 0 and token_ids[-1] == self.eos_token_id:
											warnings.warn(
											    F'''This sequence already has {self.eos_token}. In future versions this behavior may lead to duplicated'''
											    """ eos tokens being added."""	)
											return token_ids
						else:
											return token_ids + [self.eos_token_id]






	def 					lowerCamelCase__     (					self		:						List[Any]					,     lowerCAmelCase		:						List[int]					,     lowerCAmelCase		:						Optional[List[int]] = None	)	->       List[int]:







						"""simple docstring"""







						__UpperCamelCase					:	Any             =      [self.eos_token_id]

						if token_ids_a is None:
											return len(token_ids_a + eos	) * [0]
						return len(token_ids_a + eos + token_ids_a + eos	) * [0]






	def 					lowerCamelCase__     (					self		:						List[str]					,     lowerCAmelCase		:						List[int]					,     lowerCAmelCase		:						Optional[List[int]] = None	)	->       List[int]:







						"""simple docstring"""







						__UpperCamelCase					:	List[str]             =      self._add_eos_if_not_present(lowerCAmelCase	)
						if token_ids_a is None:
											return token_ids_a
						else:
											__UpperCamelCase					:	List[Any]             =      self._add_eos_if_not_present(lowerCAmelCase	)
											return token_ids_a + token_ids_a






	def 					lowerCamelCase__     (					self		:						str					,     lowerCAmelCase		:						str	)	->       List[str]:







						"""simple docstring"""







						__UpperCamelCase					:	Optional[int]             =      [chr(lowerCAmelCase	) for i in text.encode("""utf-8"""	)]
						return tokens






	def 					lowerCamelCase__     (					self		:						Union[str, Any]					,     lowerCAmelCase		:						Optional[int]	)	->       int:







						"""simple docstring"""







						if token in self.special_tokens_encoder:
											__UpperCamelCase					:	Dict             =      self.special_tokens_encoder[token]
						elif token in self.added_tokens_encoder:
											__UpperCamelCase					:	Tuple             =      self.added_tokens_encoder[token]
						elif len(lowerCAmelCase	) != 1:
											__UpperCamelCase					:	Dict             =      self.unk_token_id
						else:
											__UpperCamelCase					:	Union[str, Any]             =      ord(lowerCAmelCase	) + self._num_special_tokens
						return token_id






	def 					lowerCamelCase__     (					self		:						Union[str, Any]					,     lowerCAmelCase		:						Dict	)	->       Optional[Any]:







						"""simple docstring"""







						if index in self.special_tokens_decoder:
											__UpperCamelCase					:	str             =      self.special_tokens_decoder[index]
						else:
											__UpperCamelCase					:	Any             =      chr(index - self._num_special_tokens	)
						return token






	def 					lowerCamelCase__     (					self		:						str					,     lowerCAmelCase		:						List[Any]	)	->       List[Any]:







						"""simple docstring"""







						__UpperCamelCase					:	Union[str, Any]             =      b""""""
						for token in tokens:
											if token in self.special_tokens_decoder:
																__UpperCamelCase					:	Optional[Any]             =      self.special_tokens_decoder[token].encode("""utf-8"""	)
											elif token in self.added_tokens_decoder:
																__UpperCamelCase					:	Optional[int]             =      self.special_tokens_decoder[token].encode("""utf-8"""	)
											elif token in self.special_tokens_encoder:
																__UpperCamelCase					:	Tuple             =      token.encode("""utf-8"""	)
											elif token in self.added_tokens_encoder:
																__UpperCamelCase					:	Tuple             =      token.encode("""utf-8"""	)
											else:
																__UpperCamelCase					:	Union[str, Any]             =      bytes([ord(lowerCAmelCase	)]	)
											bstring += tok_string
						__UpperCamelCase					:	Optional[int]             =      bstring.decode("""utf-8"""					,     errors="""ignore"""	)
						return string






	def 					lowerCamelCase__     (					self		:						Optional[int]					,     lowerCAmelCase		:						str					,     lowerCAmelCase		:						Optional[str] = None	)	->       Tuple[str]:







						"""simple docstring"""







						return ()






