



"""simple docstring"""







import json
import os
import tempfile
import unittest

import numpy as np
from datasets import load_dataset

from transformers.testing_utils import require_torch, require_vision, slow
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 ImageGPTImageProcessor
class        __A       (	unittest.TestCase    ):







		'''simple docstring'''





		def __init__(	self						:  Optional[int]			,_snake_case						:  Union[str, Any]			,_snake_case						:  Optional[Any]=7			,_snake_case						:  int=3			,_snake_case						:  List[str]=18			,_snake_case						:  Union[str, Any]=30			,_snake_case						:  List[Any]=400			,_snake_case						:  Dict=True			,_snake_case						:  Union[str, Any]=None			,_snake_case						:  Dict=True			,)					->		Union[str, Any]:






				"""simple docstring"""



				lowercase__   :    str				=			size if size is not None else {'height': 18, 'width': 18}
				lowercase__   :    int				=			parent
				lowercase__   :    List[Any]				=			batch_size
				lowercase__   :    int				=			num_channels
				lowercase__   :    Any				=			image_size
				lowercase__   :    Optional[int]				=			min_resolution
				lowercase__   :    Optional[Any]				=			max_resolution
				lowercase__   :    Union[str, Any]				=			do_resize
				lowercase__   :    List[Any]				=			size
				lowercase__   :    int				=			do_normalize







		def 					UpperCAmelCase    (	self						:  Any						)					->		Tuple:






				"""simple docstring"""



				return {
				    # here we create 2 clusters for the sake of simplicity
				    "clusters": np.asarray(
				        [
				            [0.8866_4436_3403_3203, 0.6618_8293_6954_4983, 0.3891_7464_0178_6804],
				            [-0.6042_5591_4688_1104, -0.0_2295_0088_6052_8469, 0.5423_7973_6900_3296],
				        ]						),
				    "do_resize": self.do_resize,
				    "size": self.size,
				    "do_normalize": self.do_normalize,
				}





@require_torch
@require_vision
class        __A       (	a__     ,unittest.TestCase    ):







		'''simple docstring'''





		lowerCAmelCase			:					Any   	=				ImageGPTImageProcessor if is_vision_available() else None







		def 					UpperCAmelCase    (	self						:  Dict						)					->		int:






				"""simple docstring"""



				lowercase__   :    str				=			ImageGPTImageProcessingTester(self						)







		@property
		def 					UpperCAmelCase    (	self						:  Dict						)					->		Optional[Any]:






				"""simple docstring"""



				return self.image_processor_tester.prepare_image_processor_dict()







		def 					UpperCAmelCase    (	self						:  List[str]						)					->		str:






				"""simple docstring"""



				lowercase__   :    int				=			self.image_processing_class(**self.image_processor_dict						)
				self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_			,'''clusters'''						)						)
				self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_			,'''do_resize'''						)						)
				self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_			,'''size'''						)						)
				self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_			,'''do_normalize'''						)						)







		def 					UpperCAmelCase    (	self						:  Union[str, Any]						)					->		str:






				"""simple docstring"""



				lowercase__   :    Tuple				=			self.image_processing_class.from_dict(self.image_processor_dict						)
				self.assertEqual(image_processor.size			,{'''height''': 18, '''width''': 18}						)

				lowercase__   :    Dict				=			self.image_processing_class.from_dict(self.image_processor_dict			,size=42						)
				self.assertEqual(image_processor.size			,{'''height''': 42, '''width''': 42}						)







		def 					UpperCAmelCase    (	self						:  Tuple						)					->		List[str]:






				"""simple docstring"""



				lowercase__   :    str				=			self.image_processing_class(**self.image_processor_dict						)
				lowercase__   :    int				=			json.loads(image_processor.to_json_string()						)
				for key, value in self.image_processor_dict.items():
						if key == "clusters":
								self.assertTrue(np.array_equal(SCREAMING_SNAKE_CASE_			,obj[key]						)						)
						else:
								self.assertEqual(obj[key]			,SCREAMING_SNAKE_CASE_						)







		def 					UpperCAmelCase    (	self						:  Tuple						)					->		Dict:






				"""simple docstring"""



				lowercase__   :    Tuple				=			self.image_processing_class(**self.image_processor_dict						)

				with tempfile.TemporaryDirectory() as tmpdirname:
						lowercase__   :    List[str]				=			os.path.join(SCREAMING_SNAKE_CASE_			,'''image_processor.json'''						)
						image_processor_first.to_json_file(SCREAMING_SNAKE_CASE_						)
						lowercase__   :    Tuple				=			self.image_processing_class.from_json_file(SCREAMING_SNAKE_CASE_						).to_dict()

				lowercase__   :    List[Any]				=			image_processor_first.to_dict()
				for key, value in image_processor_first.items():
						if key == "clusters":
								self.assertTrue(np.array_equal(SCREAMING_SNAKE_CASE_			,image_processor_second[key]						)						)
						else:
								self.assertEqual(image_processor_first[key]			,SCREAMING_SNAKE_CASE_						)







		def 					UpperCAmelCase    (	self						:  List[Any]						)					->		Optional[int]:






				"""simple docstring"""



				lowercase__   :    Optional[Any]				=			self.image_processing_class(**self.image_processor_dict						)

				with tempfile.TemporaryDirectory() as tmpdirname:
						image_processor_first.save_pretrained(SCREAMING_SNAKE_CASE_						)
						lowercase__   :    str				=			self.image_processing_class.from_pretrained(SCREAMING_SNAKE_CASE_						).to_dict()

				lowercase__   :    Union[str, Any]				=			image_processor_first.to_dict()
				for key, value in image_processor_first.items():
						if key == "clusters":
								self.assertTrue(np.array_equal(SCREAMING_SNAKE_CASE_			,image_processor_second[key]						)						)
						else:
								self.assertEqual(image_processor_first[key]			,SCREAMING_SNAKE_CASE_						)







		@unittest.skip('''ImageGPT requires clusters at initialization'''						)
		def 					UpperCAmelCase    (	self						:  List[Any]						)					->		str:






				"""simple docstring"""



				pass





def      __UpperCAmelCase      (    )							->      int:
		lowercase__   :    Optional[int]				=			load_dataset('''hf-internal-testing/fixtures_image_utils'''						,     split='''test'''	)

		lowercase__   :    int				=			Image.open(dataset[4]['''file''']	)
		lowercase__   :    Optional[Any]				=			Image.open(dataset[5]['''file''']	)

		lowercase__   :    str				=			[imagea, imagea]

		return images





@require_vision
@require_torch
class        __A       (	unittest.TestCase    ):







		'''simple docstring'''





		@slow
		def 					UpperCAmelCase    (	self						:  List[Any]						)					->		str:






				"""simple docstring"""



				lowercase__   :    List[str]				=			ImageGPTImageProcessor.from_pretrained('''openai/imagegpt-small'''						)

				lowercase__   :    List[str]				=			prepare_images()

				# test non-batched
				lowercase__   :    int				=			image_processing(images[0]			,return_tensors='''pt'''						)

				self.assertIsInstance(encoding.input_ids			,torch.LongTensor						)
				self.assertEqual(encoding.input_ids.shape			,(1, 1_024)						)

				lowercase__   :    Union[str, Any]				=			[306, 191, 191]
				self.assertEqual(encoding.input_ids[0, :3].tolist()			,SCREAMING_SNAKE_CASE_						)

				# test batched
				lowercase__   :    Tuple				=			image_processing(SCREAMING_SNAKE_CASE_			,return_tensors='''pt'''						)

				self.assertIsInstance(encoding.input_ids			,torch.LongTensor						)
				self.assertEqual(encoding.input_ids.shape			,(2, 1_024)						)

				lowercase__   :    Optional[Any]				=			[303, 13, 13]
				self.assertEqual(encoding.input_ids[1, -3:].tolist()			,SCREAMING_SNAKE_CASE_						)





def 		UpperCamelCase	(   snake_case__       :    int       ,     snake_case__       :    int	)	->     int:
   return int(input_a == input_a == 0	)
def 		UpperCamelCase	(   )	->     None:
   print('Truth Table of NOR Gate:'	)
   print('|   Input 1   |  Input 2  | Output |'	)
   print(F"""|      0      |    0      |  {nor_gate(0       ,     0	)}     |"""	)
   print(F"""|      0      |    1      |  {nor_gate(0       ,     1	)}     |"""	)
   print(F"""|      1      |    0      |  {nor_gate(1       ,     0	)}     |"""	)
   print(F"""|      1      |    1      |  {nor_gate(1       ,     1	)}     |"""	)


if __name__ == "__main__":
    import doctest

    doctest.testmod()
    main()



