


'''simple docstring'''






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							):





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





							def 		a__   (  self		:int			):
									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 		a__   (  self		:Optional[int]  ,_UpperCamelCase		:str=False  ,_UpperCamelCase		:Any=False  ,_UpperCamelCase		:Optional[Any]=False			):
									assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

									if equal_resolution:
											snake_case_		:							int			       =						[]
											for i in range(self.batch_size			):
													image_inputs.append(
													    np.random.randint(
													        2_5_5  ,size=(self.num_channels, self.max_resolution, self.max_resolution)  ,dtype=np.uinta			)			)
									else:
											snake_case_		:							List[Any]			       =						[]
											for i in range(self.batch_size			):
													snake_case_       ,      snake_case_		:							Union[str, Any]			       =						np.random.choice(np.arange(self.min_resolution  ,self.max_resolution			)  ,2			)
													image_inputs.append(np.random.randint(2_5_5  ,size=(self.num_channels, width, height)  ,dtype=np.uinta			)			)

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

									if torchify:
											snake_case_		:							Optional[int]			       =						[torch.from_numpy(_lowerCAmelCase			) for x in image_inputs]

									return image_inputs





@require_torch
@require_vision
class 						__UpperCamelCase	(						lowercase__						,  unittest.TestCase							):
							lowercase			:		List[Any]   		=  ChineseCLIPImageProcessor if is_vision_available() else None





							def 		a__   (  self		:str			):
									snake_case_		:							Dict			       =						ChineseCLIPImageProcessingTester(self  ,do_center_crop=_lowerCAmelCase			)





							@property
							def 		a__   (  self		:Optional[Any]			):
									return self.image_processor_tester.prepare_image_processor_dict()





							def 		a__   (  self		:Tuple			):
									snake_case_		:							str			       =						self.image_processing_class(**self.image_processor_dict			)
									self.assertTrue(hasattr(_lowerCAmelCase  ,"""do_resize"""			)			)
									self.assertTrue(hasattr(_lowerCAmelCase  ,"""size"""			)			)
									self.assertTrue(hasattr(_lowerCAmelCase  ,"""do_center_crop"""			)			)
									self.assertTrue(hasattr(_lowerCAmelCase  ,"""center_crop"""			)			)
									self.assertTrue(hasattr(_lowerCAmelCase  ,"""do_normalize"""			)			)
									self.assertTrue(hasattr(_lowerCAmelCase  ,"""image_mean"""			)			)
									self.assertTrue(hasattr(_lowerCAmelCase  ,"""image_std"""			)			)
									self.assertTrue(hasattr(_lowerCAmelCase  ,"""do_convert_rgb"""			)			)





							def 		a__   (  self		:Dict			):
									snake_case_		:							Any			       =						self.image_processing_class.from_dict(self.image_processor_dict			)
									self.assertEqual(image_processor.size  ,{"""height""": 2_2_4, """width""": 2_2_4}			)
									self.assertEqual(image_processor.crop_size  ,{"""height""": 1_8, """width""": 1_8}			)

									snake_case_		:							Any			       =						self.image_processing_class.from_dict(self.image_processor_dict  ,size=4_2  ,crop_size=8_4			)
									self.assertEqual(image_processor.size  ,{"""shortest_edge""": 4_2}			)
									self.assertEqual(image_processor.crop_size  ,{"""height""": 8_4, """width""": 8_4}			)





							def 		a__   (  self		:List[Any]			):
									pass





							def 		a__   (  self		:List[Any]			):
									# Initialize image_processing
									snake_case_		:							int			       =						self.image_processing_class(**self.image_processor_dict			)
									# create random PIL images
									snake_case_		:							Tuple			       =						self.image_processor_tester.prepare_inputs(equal_resolution=_lowerCAmelCase			)
									for image in image_inputs:
											self.assertIsInstance(_lowerCAmelCase  ,Image.Image			)

									# Test not batched input
									snake_case_		:							str			       =						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
									snake_case_		:							List[Any]			       =						image_processing(_lowerCAmelCase  ,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 		a__   (  self		:Union[str, Any]			):
									# Initialize image_processing
									snake_case_		:							Any			       =						self.image_processing_class(**self.image_processor_dict			)
									# create random numpy tensors
									snake_case_		:							Any			       =						self.image_processor_tester.prepare_inputs(equal_resolution=_lowerCAmelCase  ,numpify=_lowerCAmelCase			)
									for image in image_inputs:
											self.assertIsInstance(_lowerCAmelCase  ,np.ndarray			)

									# Test not batched input
									snake_case_		:							Dict			       =						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
									snake_case_		:							List[str]			       =						image_processing(_lowerCAmelCase  ,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 		a__   (  self		:int			):
									# Initialize image_processing
									snake_case_		:							Union[str, Any]			       =						self.image_processing_class(**self.image_processor_dict			)
									# create random PyTorch tensors
									snake_case_		:							Union[str, Any]			       =						self.image_processor_tester.prepare_inputs(equal_resolution=_lowerCAmelCase  ,torchify=_lowerCAmelCase			)
									for image in image_inputs:
											self.assertIsInstance(_lowerCAmelCase  ,torch.Tensor			)

									# Test not batched input
									snake_case_		:							Union[str, Any]			       =						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
									snake_case_		:							Dict			       =						image_processing(_lowerCAmelCase  ,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	(						lowercase__						,  unittest.TestCase							):
							lowercase			:		Optional[Any]   		=  ChineseCLIPImageProcessor if is_vision_available() else None





							def 		a__   (  self		:Optional[int]			):
									snake_case_		:							int			       =						ChineseCLIPImageProcessingTester(self  ,num_channels=4  ,do_center_crop=_lowerCAmelCase			)
									snake_case_		:							Optional[int]			       =						3





							@property
							def 		a__   (  self		:Optional[Any]			):
									return self.image_processor_tester.prepare_image_processor_dict()





							def 		a__   (  self		:Tuple			):
									snake_case_		:							Union[str, Any]			       =						self.image_processing_class(**self.image_processor_dict			)
									self.assertTrue(hasattr(_lowerCAmelCase  ,"""do_resize"""			)			)
									self.assertTrue(hasattr(_lowerCAmelCase  ,"""size"""			)			)
									self.assertTrue(hasattr(_lowerCAmelCase  ,"""do_center_crop"""			)			)
									self.assertTrue(hasattr(_lowerCAmelCase  ,"""center_crop"""			)			)
									self.assertTrue(hasattr(_lowerCAmelCase  ,"""do_normalize"""			)			)
									self.assertTrue(hasattr(_lowerCAmelCase  ,"""image_mean"""			)			)
									self.assertTrue(hasattr(_lowerCAmelCase  ,"""image_std"""			)			)
									self.assertTrue(hasattr(_lowerCAmelCase  ,"""do_convert_rgb"""			)			)





							def 		a__   (  self		:Optional[Any]			):
									pass





							def 		a__   (  self		:Any			):
									# Initialize image_processing
									snake_case_		:							int			       =						self.image_processing_class(**self.image_processor_dict			)
									# create random PIL images
									snake_case_		:							int			       =						self.image_processor_tester.prepare_inputs(equal_resolution=_lowerCAmelCase			)
									for image in image_inputs:
											self.assertIsInstance(_lowerCAmelCase  ,Image.Image			)

									# Test not batched input
									snake_case_		:							int			       =						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
									snake_case_		:							Optional[Any]			       =						image_processing(_lowerCAmelCase  ,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"""],
									    )  ,)



'''simple docstring'''






import os
import unittest

from transformers.models.transfo_xl.tokenization_transfo_xl import VOCAB_FILES_NAMES, TransfoXLTokenizer

from ...test_tokenization_common import TokenizerTesterMixin
class 						__UpperCamelCase	(						lowercase__						,  unittest.TestCase							):
							lowercase			:		Dict   		=  TransfoXLTokenizer
							lowercase			:		Optional[Any]   		=  False
							lowercase			:		Dict   		=  False





							def 		a__   (  self		:Union[str, Any]			):
									super().setUp()

									snake_case_		:							Optional[int]			       =						[
									    """<unk>""",
									    """[CLS]""",
									    """[SEP]""",
									    """want""",
									    """unwanted""",
									    """wa""",
									    """un""",
									    """running""",
									    """,""",
									    """low""",
									    """l""",
									]
									snake_case_		:							Optional[int]			       =						os.path.join(self.tmpdirname  ,VOCAB_FILES_NAMES["""vocab_file"""]			)
									with open(self.vocab_file  ,"""w"""  ,encoding="""utf-8"""			) as vocab_writer:
											vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens]			)			)





							def 		a__   (  self		:List[Any]  ,**_UpperCamelCase		:Optional[Any]			):
									snake_case_		:							Tuple			       =						True
									return TransfoXLTokenizer.from_pretrained(self.tmpdirname  ,**_UpperCamelCase			)





							def 		a__   (  self		:Tuple  ,_UpperCamelCase		:Union[str, Any]			):
									snake_case_		:							Any			       =						"""<unk> UNwanted , running"""
									snake_case_		:							Optional[int]			       =						"""<unk> unwanted, running"""
									return input_text, output_text





							def 		a__   (  self		:Dict			):
									snake_case_		:							Dict			       =						TransfoXLTokenizer(vocab_file=self.vocab_file  ,lower_case=_UpperCamelCase			)

									snake_case_		:							Dict			       =						tokenizer.tokenize("""<unk> UNwanted , running"""			)
									self.assertListEqual(_UpperCamelCase  ,["""<unk>""", """unwanted""", """,""", """running"""]			)

									self.assertListEqual(tokenizer.convert_tokens_to_ids(_UpperCamelCase			)  ,[0, 4, 8, 7]			)





							def 		a__   (  self		:Optional[Any]			):
									snake_case_		:							Dict			       =						TransfoXLTokenizer(lower_case=_UpperCamelCase			)

									self.assertListEqual(
									    tokenizer.tokenize(""" \tHeLLo ! how  \n Are yoU ?  """			)  ,["""hello""", """!""", """how""", """are""", """you""", """?"""]			)





							def 		a__   (  self		:Any			):
									snake_case_		:							List[Any]			       =						TransfoXLTokenizer(lower_case=_UpperCamelCase			)

									self.assertListEqual(
									    tokenizer.tokenize(""" \tHeLLo ! how  \n Are yoU ?  """			)  ,["""HeLLo""", """!""", """how""", """Are""", """yoU""", """?"""]			)





							def 		a__   (  self		:List[str]			):
									snake_case_		:							str			       =						TransfoXLTokenizer(lower_case=_UpperCamelCase			)
									snake_case_		:							List[str]			       =						"""Hello (bracket) and side-scrolled [and] Henry's $5,000 with 3.34 m. What's up!?"""
									snake_case_		:							Optional[int]			       =						[
									    """Hello""",
									    """(""",
									    """bracket""",
									    """)""",
									    """and""",
									    """side""",
									    """@-@""",
									    """scrolled""",
									    """[""",
									    """and""",
									    """]""",
									    """Henry""",
									    """'s""",
									    """$""",
									    """5""",
									    """@,@""",
									    """000""",
									    """with""",
									    """3""",
									    """@.@""",
									    """34""",
									    """m""",
									    """.""",
									    """What""",
									    """'s""",
									    """up""",
									    """!""",
									    """?""",
									]

									self.assertListEqual(tokenizer.tokenize(_UpperCamelCase			)  ,_UpperCamelCase			)

									self.assertEqual(tokenizer.convert_tokens_to_string(_UpperCamelCase			)  ,_UpperCamelCase			)





							def 		a__   (  self		:Dict			):
									snake_case_		:							Union[str, Any]			       =						self.get_tokenizer()
									snake_case_		:							Dict			       =						len(_UpperCamelCase			)

									tokenizer.add_tokens(["""new1""", """new2"""]			)
									tokenizer.move_added_token("""new1"""  ,1			)

									# Check that moved token is not copied (duplicate)
									self.assertEqual(len(_UpperCamelCase			)  ,original_len + 2			)
									# Check that token is moved to specified id
									self.assertEqual(tokenizer.encode("""new1"""			)  ,[1]			)
									self.assertEqual(tokenizer.decode([1]			)  ,"""new1"""			)