



'''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 						a_		(     unittest.TestCase    ):


							def __init__(				self   :						int      ,						a_   :						Union[str, Any]      ,						a_   :						Dict=7      ,						a_   :						Dict=3      ,						a_   :						Tuple=1_8      ,						a_   :						List[Any]=3_0      ,						a_   :						Dict=4_0_0      ,						a_   :						Dict=True      ,						a_   :						str=None      ,						a_   :						int=True      ,						a_   :						List[Any]=None      ,						a_   :						Tuple=True      ,						a_   :						List[Any]=[0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3]      ,						a_   :						Optional[Any]=[0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1]      ,						a_   :						Any=True      ,						)			->					int:
										snake_case:    Dict      							=size if size is not None else {'height': 2_2_4, 'width': 2_2_4}
										snake_case:    List[Any]      							=crop_size if crop_size is not None else {'height': 1_8, 'width': 1_8}
										snake_case:    List[Any]      							=parent
										snake_case:    Optional[Any]      							=batch_size
										snake_case:    int      							=num_channels
										snake_case:    Any      							=image_size
										snake_case:    Dict      							=min_resolution
										snake_case:    str      							=max_resolution
										snake_case:    List[str]      							=do_resize
										snake_case:    List[str]      							=size
										snake_case:    Optional[int]      							=do_center_crop
										snake_case:    Optional[int]      							=crop_size
										snake_case:    Optional[int]      							=do_normalize
										snake_case:    List[Any]      							=image_mean
										snake_case:    List[str]      							=image_std
										snake_case:    Dict      							=do_convert_rgb


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

										if equal_resolution:
													snake_case:    Union[str, Any]      							=[]
													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:    Union[str, Any]      							=[]
													for i in range(self.batch_size			):
																snake_case							,  snake_case:    Optional[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:    int      							=[Image.fromarray(np.moveaxis(a_      ,						0      ,						-1			)			) for x in image_inputs]

										if torchify:
													snake_case:    List[str]      							=[torch.from_numpy(a_			) for x in image_inputs]

										return image_inputs






@require_torch
@require_vision
class 						a_		(     snake_case    ,   unittest.TestCase    ):
							UpperCAmelCase			:		Dict        =				ChineseCLIPImageProcessor if is_vision_available() else None


							def    UpperCamelCase     (				self   :						List[str]			)			->					List[str]:
										snake_case:    Tuple      							=ChineseCLIPImageProcessingTester(self      ,						do_center_crop=a_			)


							@property
							def    UpperCamelCase     (				self   :						List[Any]			)			->					Optional[Any]:
										return self.image_processor_tester.prepare_image_processor_dict()


							def    UpperCamelCase     (				self   :						List[str]			)			->					Dict:
										snake_case:    Dict      							=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			)			->					Any:
										snake_case:    Tuple      							=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:    List[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    UpperCamelCase     (				self   :						List[Any]			)			->					Tuple:
										pass


							def    UpperCamelCase     (				self   :						Optional[Any]			)			->					Optional[int]:
										# Initialize image_processing
										snake_case:    Optional[int]      							=self.image_processing_class(**self.image_processor_dict			)
										# create random PIL images
										snake_case:    Tuple      							=self.image_processor_tester.prepare_inputs(equal_resolution=a_			)
										for image in image_inputs:
													self.assertIsInstance(a_      ,						Image.Image			)

										# Test not batched input
										snake_case:    List[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:    Dict      							=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			)			->					Any:
										# Initialize image_processing
										snake_case:    Any      							=self.image_processing_class(**self.image_processor_dict			)
										# create random numpy tensors
										snake_case:    List[Any]      							=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
										snake_case:    List[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:    Optional[int]      							=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   :						List[Any]			)			->					Any:
										# Initialize image_processing
										snake_case:    Dict      							=self.image_processing_class(**self.image_processor_dict			)
										# create random PyTorch tensors
										snake_case:    Any      							=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
										snake_case:    Tuple      							=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(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 						a_		(     snake_case    ,   unittest.TestCase    ):
							UpperCAmelCase			:		int        =				ChineseCLIPImageProcessor if is_vision_available() else None


							def    UpperCamelCase     (				self   :						Optional[Any]			)			->					Union[str, Any]:
										snake_case:    str      							=ChineseCLIPImageProcessingTester(self      ,						num_channels=4      ,						do_center_crop=a_			)
										snake_case:    Tuple      							=3


							@property
							def    UpperCamelCase     (				self   :						Any			)			->					Optional[Any]:
										return self.image_processor_tester.prepare_image_processor_dict()


							def    UpperCamelCase     (				self   :						int			)			->					int:
										snake_case:    Dict      							=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   :						Union[str, Any]			)			->					str:
										pass




							def    UpperCamelCase     (				self   :						int			)			->					Union[str, Any]:
										# Initialize image_processing
										snake_case:    int      							=self.image_processing_class(**self.image_processor_dict			)
										# create random PIL images
										snake_case:    Any      							=self.image_processor_tester.prepare_inputs(equal_resolution=a_			)
										for image in image_inputs:
													self.assertIsInstance(a_      ,						Image.Image			)

										# Test not batched input
										snake_case:    Optional[Any]      							=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:    List[Any]      							=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'],
										    )      ,						)









'''simple docstring'''

import os
import unittest

from transformers import FunnelTokenizer, FunnelTokenizerFast
from transformers.models.funnel.tokenization_funnel import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin






@require_tokenizers
class 						a_		(     snake_case    ,   unittest.TestCase    ):
							UpperCAmelCase			:		int        =				FunnelTokenizer
							UpperCAmelCase			:		Tuple        =				FunnelTokenizerFast
							UpperCAmelCase			:		List[str]        =				True
							UpperCAmelCase			:		Tuple        =				True


							def    UpperCamelCase     (				self   :						Optional[Any]			)			->					Optional[Any]:
										super().setUp()

										snake_case:    Optional[Any]      							=[
										    '<unk>',
										    '<cls>',
										    '<sep>',
										    'want',
										    '##want',
										    '##ed',
										    'wa',
										    'un',
										    'runn',
										    '##ing',
										    ',',
										    'low',
										    'lowest',
										]
										snake_case:    Any      							=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    UpperCamelCase     (				self   :						Tuple      ,						**a_   :						str			)			->					Any:
										return FunnelTokenizer.from_pretrained(self.tmpdirname      ,						**a_			)


							def    UpperCamelCase     (				self   :						List[Any]      ,						**a_   :						int			)			->					str:
										return FunnelTokenizerFast.from_pretrained(self.tmpdirname      ,						**a_			)


							def    UpperCamelCase     (				self   :						Any      ,						a_   :						Union[str, Any]			)			->					Union[str, Any]:
										snake_case:    List[str]      							='UNwant\u00E9d,running'
										snake_case:    Dict      							='unwanted, running'
										return input_text, output_text


							def    UpperCamelCase     (				self   :						Optional[int]			)			->					List[Any]:
										snake_case:    Union[str, Any]      							=self.tokenizer_class(self.vocab_file			)

										snake_case:    Dict      							=tokenizer.tokenize('UNwant\u00E9d,running'			)
										self.assertListEqual(a_      ,						['un', '##want', '##ed', ',', 'runn', '##ing']			)
										self.assertListEqual(tokenizer.convert_tokens_to_ids(a_			)      ,						[7, 4, 5, 1_0, 8, 9]			)




							def    UpperCamelCase     (				self   :						List[str]			)			->					Dict:
										snake_case:    Optional[Any]      							=self.get_tokenizers(do_lower_case=a_			)
										for tokenizer in tokenizers:
													snake_case:    str      							=tokenizer('UNwant\u00E9d,running'			)
													snake_case:    Union[str, Any]      							=len(inputs['input_ids']			) - 1
													self.assertListEqual(inputs['token_type_ids']      ,						[2] + [0] * sentence_len			)

													snake_case:    List[str]      							=tokenizer('UNwant\u00E9d,running'      ,						'UNwant\u00E9d,running'			)
													self.assertListEqual(inputs['token_type_ids']      ,						[2] + [0] * sentence_len + [1] * sentence_len			)




