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



	"""simple docstring"""




	def __init__(       self  :			Any       ,							SCREAMING_SNAKE_CASE_  :			Optional[int]       ,							SCREAMING_SNAKE_CASE_  :			Optional[Any]=7       ,							SCREAMING_SNAKE_CASE_  :			Tuple=3       ,							SCREAMING_SNAKE_CASE_  :			Optional[Any]=1_8       ,							SCREAMING_SNAKE_CASE_  :			Any=3_0       ,							SCREAMING_SNAKE_CASE_  :			Dict=4_0_0       ,							SCREAMING_SNAKE_CASE_  :			Dict=True       ,							SCREAMING_SNAKE_CASE_  :			Optional[Any]=None       ,							SCREAMING_SNAKE_CASE_  :			Tuple=True       ,							SCREAMING_SNAKE_CASE_  :			Tuple=None       ,							SCREAMING_SNAKE_CASE_  :			Optional[Any]=True       ,							SCREAMING_SNAKE_CASE_  :			str=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73]       ,							SCREAMING_SNAKE_CASE_  :			Optional[int]=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11]       ,							SCREAMING_SNAKE_CASE_  :			Optional[int]=True       ,							)       ->     Optional[int]:
					lowercase_       =					size if size is not None else {'''height''': 2_2_4, '''width''': 2_2_4}
					lowercase_       =					crop_size if crop_size is not None else {'''height''': 1_8, '''width''': 1_8}
					lowercase_       =					parent
					lowercase_       =					batch_size
					lowercase_       =					num_channels
					lowercase_       =					image_size
					lowercase_       =					min_resolution
					lowercase_       =					max_resolution
					lowercase_       =					do_resize
					lowercase_       =					size
					lowercase_       =					do_center_crop
					lowercase_       =					crop_size
					lowercase_       =					do_normalize
					lowercase_       =					image_mean
					lowercase_       =					image_std
					lowercase_       =					do_convert_rgb
	def 						_lowercase  (       self  :			Any       )       ->     List[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 						_lowercase  (       self  :			Optional[Any]       ,							SCREAMING_SNAKE_CASE_  :			Optional[int]=False       ,							SCREAMING_SNAKE_CASE_  :			List[Any]=False       ,							SCREAMING_SNAKE_CASE_  :			Dict=False       )       ->     List[Any]:
					assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

					if equal_resolution:
									lowercase_       =					[]
									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:
									lowercase_       =					[]
									for i in range(self.batch_size       ):
													lowercase_  ,	lowercase_       =					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
									lowercase_       =					[Image.fromarray(np.moveaxis(SCREAMING_SNAKE_CASE_       ,							0       ,							-1       )       ) for x in image_inputs]

					if torchify:
									lowercase_       =					[torch.from_numpy(SCREAMING_SNAKE_CASE_       ) for x in image_inputs]

					return image_inputs

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



	"""simple docstring"""




	a			:Any         =	ChineseCLIPImageProcessor if is_vision_available() else None
	def 						_lowercase  (       self  :			Union[str, Any]       )       ->     List[Any]:
					lowercase_       =					ChineseCLIPImageProcessingTester(self       ,							do_center_crop=SCREAMING_SNAKE_CASE_       )
	@property
	def 						_lowercase  (       self  :			Any       )       ->     int:
					return self.image_processor_tester.prepare_image_processor_dict()
	def 						_lowercase  (       self  :			str       )       ->     Union[str, Any]:
					lowercase_       =					self.image_processing_class(**self.image_processor_dict       )
					self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_       ,							'''do_resize'''       )       )
					self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_       ,							'''size'''       )       )
					self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_       ,							'''do_center_crop'''       )       )
					self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_       ,							'''center_crop'''       )       )
					self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_       ,							'''do_normalize'''       )       )
					self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_       ,							'''image_mean'''       )       )
					self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_       ,							'''image_std'''       )       )
					self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_       ,							'''do_convert_rgb'''       )       )
	def 						_lowercase  (       self  :			int       )       ->     Optional[int]:
					lowercase_       =					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}       )

					lowercase_       =					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 						_lowercase  (       self  :			List[str]       )       ->     List[Any]:
					pass
	def 						_lowercase  (       self  :			List[Any]       )       ->     List[str]:
					# Initialize image_processing
					lowercase_       =					self.image_processing_class(**self.image_processor_dict       )
					# create random PIL images
					lowercase_       =					self.image_processor_tester.prepare_inputs(equal_resolution=SCREAMING_SNAKE_CASE_       )
					for image in image_inputs:
									self.assertIsInstance(SCREAMING_SNAKE_CASE_       ,							Image.Image       )

					# Test not batched input
					lowercase_       =					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
					lowercase_       =					image_processing(SCREAMING_SNAKE_CASE_       ,							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 						_lowercase  (       self  :			int       )       ->     int:
					# Initialize image_processing
					lowercase_       =					self.image_processing_class(**self.image_processor_dict       )
					# create random numpy tensors
					lowercase_       =					self.image_processor_tester.prepare_inputs(equal_resolution=SCREAMING_SNAKE_CASE_       ,							numpify=SCREAMING_SNAKE_CASE_       )
					for image in image_inputs:
									self.assertIsInstance(SCREAMING_SNAKE_CASE_       ,							np.ndarray       )

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

					# Test not batched input
					lowercase_       =					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
					lowercase_       =					image_processing(SCREAMING_SNAKE_CASE_       ,							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  lowercase__(				UpperCAmelCase ,						unittest.TestCase							):



	"""simple docstring"""




	a			:Union[str, Any]         =	ChineseCLIPImageProcessor if is_vision_available() else None
	def 						_lowercase  (       self  :			int       )       ->     int:
					lowercase_       =					ChineseCLIPImageProcessingTester(self       ,							num_channels=4       ,							do_center_crop=SCREAMING_SNAKE_CASE_       )
					lowercase_       =					3
	@property
	def 						_lowercase  (       self  :			Optional[int]       )       ->     Any:
					return self.image_processor_tester.prepare_image_processor_dict()
	def 						_lowercase  (       self  :			Optional[int]       )       ->     Optional[Any]:
					lowercase_       =					self.image_processing_class(**self.image_processor_dict       )
					self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_       ,							'''do_resize'''       )       )
					self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_       ,							'''size'''       )       )
					self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_       ,							'''do_center_crop'''       )       )
					self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_       ,							'''center_crop'''       )       )
					self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_       ,							'''do_normalize'''       )       )
					self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_       ,							'''image_mean'''       )       )
					self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_       ,							'''image_std'''       )       )
					self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_       ,							'''do_convert_rgb'''       )       )
	def 						_lowercase  (       self  :			Dict       )       ->     List[Any]:
					pass




	def 						_lowercase  (       self  :			Optional[Any]       )       ->     List[Any]:
					# Initialize image_processing
					lowercase_       =					self.image_processing_class(**self.image_processor_dict       )
					# create random PIL images
					lowercase_       =					self.image_processor_tester.prepare_inputs(equal_resolution=SCREAMING_SNAKE_CASE_       )
					for image in image_inputs:
									self.assertIsInstance(SCREAMING_SNAKE_CASE_       ,							Image.Image       )

					# Test not batched input
					lowercase_       =					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
					lowercase_       =					image_processing(SCREAMING_SNAKE_CASE_       ,							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 argparse
from collections import defaultdict

import yaml


__a  			=					'docs/source/en/_toctree.yml'




def 				a						(							snake_case__:					Dict					):


				'''simple docstring'''
				lowercase_       =					defaultdict(snake_case__					)
				for doc in model_doc:
								counts[doc["local"]] += 1
				lowercase_       =					[key for key, value in counts.items() if value > 1]

				lowercase_       =					[]
				for duplicate_key in duplicates:
								lowercase_       =					list({doc['''title'''] for doc in model_doc if doc['''local'''] == duplicate_key}					)
								if len(snake_case__					) > 1:
												raise ValueError(
												    F'''{duplicate_key} is present several times in the documentation table of content at '''
												    '''`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the '''
												    '''others.'''					)
								# Only add this once
								new_doc.append({'''local''': duplicate_key, '''title''': titles[0]}					)

				# Add none duplicate-keys
				new_doc.extend([doc for doc in model_doc if counts[doc['''local''']] == 1]					)

				# Sort
				return sorted(snake_case__						,   key=lambda snake_case__					: s["title"].lower()					)




def 				a						(							snake_case__:					List[Any]=False					):


				'''simple docstring'''
				with open(snake_case__						,   encoding='''utf-8'''					) as f:
								lowercase_       =					yaml.safe_load(f.read()					)

				# Get to the API doc
				lowercase_       =					0
				while content[api_idx]["title"] != "API":
								api_idx += 1
				lowercase_       =					content[api_idx]['''sections''']

				# Then to the model doc
				lowercase_       =					0
				while api_doc[model_idx]["title"] != "Models":
								model_idx += 1

				lowercase_       =					api_doc[model_idx]['''sections''']

				lowercase_       =					[(idx, section) for idx, section in enumerate(snake_case__					) if '''sections''' in section]
				lowercase_       =					False
				for idx, modality_doc in modalities_docs:
								lowercase_       =					modality_doc['''sections''']
								lowercase_       =					clean_model_doc_toc(snake_case__					)

								if old_modality_doc != new_modality_doc:
												lowercase_       =					True
												if overwrite:
																lowercase_       =					new_modality_doc

				if diff:
								if overwrite:
												lowercase_       =					model_doc
												lowercase_       =					api_doc
												with open(snake_case__						,   '''w'''						,   encoding='''utf-8'''					) as f:
																f.write(yaml.dump(snake_case__						,   allow_unicode=snake_case__					)					)
								else:
												raise ValueError(
												    '''The model doc part of the table of content is not properly sorted, run `make style` to fix this.'''					)


if __name__ == "__main__":
						__a  			=					argparse.ArgumentParser()
						parser.add_argument('--fix_and_overwrite', action='store_true', help='Whether to fix inconsistencies.')
						__a  			=					parser.parse_args()

						check_model_doc(args.fix_and_overwrite)




