





'''simple docstring'''







import json
import os
import tempfile

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


if is_torch_available():
	import numpy as np
	import torch

if is_vision_available():
	from PIL import Image





def    _lowerCAmelCase						(						lowercase					,	lowercase=False					,	lowercase=False					,	lowercase=False	)					->					Dict:
			assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

			__lowerCAmelCase											=				[]
			for i in range(image_processor_tester.batch_size	):
						if equal_resolution:
									__lowerCAmelCase											=				__lowerCAmelCase											=				image_processor_tester.max_resolution
						else:
									# To avoid getting image width/height 0
									__lowerCAmelCase											=				image_processor_tester.min_resolution
									if getattr(lowercase					,	"""size_divisor"""					,	lowercase	):
												# If `size_divisor` is defined, the image needs to have width/size >= `size_divisor`
												__lowerCAmelCase											=				max(image_processor_tester.size_divisor					,	lowercase	)
									__lowerCAmelCase						,       __lowerCAmelCase											=				np.random.choice(np.arange(lowercase					,	image_processor_tester.max_resolution	)					,	2	)
						image_inputs.append(
						    np.random.randint(255					,	size=(image_processor_tester.num_channels, width, height)					,	dtype=np.uinta	)	)

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

			if torchify:
						__lowerCAmelCase											=				[torch.from_numpy(lowercase	) for image in image_inputs]

			return image_inputs





def    _lowerCAmelCase						(						lowercase					,	lowercase=10					,	lowercase=10					,	lowercase=False					,	lowercase=False	)					->					Dict:
			__lowerCAmelCase											=				[]
			for i in range(image_processor_tester.num_frames	):
						video.append(np.random.randint(255					,	size=(image_processor_tester.num_channels, width, height)					,	dtype=np.uinta	)	)

			if not numpify and not torchify:
						# PIL expects the channel dimension as last dimension
						__lowerCAmelCase											=				[Image.fromarray(np.moveaxis(lowercase					,	0					,	-1	)	) for frame in video]

			if torchify:
						__lowerCAmelCase											=				[torch.from_numpy(lowercase	) for frame in video]

			return video





def    _lowerCAmelCase						(						lowercase					,	lowercase=False					,	lowercase=False					,	lowercase=False	)					->					Dict:
			assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

			__lowerCAmelCase											=				[]
			for i in range(image_processor_tester.batch_size	):
						if equal_resolution:
									__lowerCAmelCase											=				__lowerCAmelCase											=				image_processor_tester.max_resolution
						else:
									__lowerCAmelCase						,       __lowerCAmelCase											=				np.random.choice(
									    np.arange(image_processor_tester.min_resolution					,	image_processor_tester.max_resolution	)					,	2	)
									__lowerCAmelCase											=				prepare_video(
									    image_processor_tester=lowercase					,	width=lowercase					,	height=lowercase					,	numpify=lowercase					,	torchify=lowercase					,	)
						video_inputs.append(lowercase	)

			return video_inputs







class     _UpperCAmelCase							:
		a   :   int     =None
		def   lowerCamelCase__			(  self			):



					'''simple docstring'''

					__lowerCAmelCase											=				self.image_processing_class(**self.image_processor_dict			)
					__lowerCAmelCase											=				json.loads(image_processor.to_json_string()			)
					for key, value in self.image_processor_dict.items():
								self.assertEqual(obj[key],__SCREAMING_SNAKE_CASE			)
		def   lowerCamelCase__			(  self			):



					'''simple docstring'''

					__lowerCAmelCase											=				self.image_processing_class(**self.image_processor_dict			)

					with tempfile.TemporaryDirectory() as tmpdirname:
								__lowerCAmelCase											=				os.path.join(__SCREAMING_SNAKE_CASE,"""image_processor.json"""			)
								image_processor_first.to_json_file(__SCREAMING_SNAKE_CASE			)
								__lowerCAmelCase											=				self.image_processing_class.from_json_file(__SCREAMING_SNAKE_CASE			)

					self.assertEqual(image_processor_second.to_dict(),image_processor_first.to_dict()			)
		def   lowerCamelCase__			(  self			):



					'''simple docstring'''

					__lowerCAmelCase											=				self.image_processing_class(**self.image_processor_dict			)

					with tempfile.TemporaryDirectory() as tmpdirname:
								__lowerCAmelCase											=				image_processor_first.save_pretrained(__SCREAMING_SNAKE_CASE			)[0]
								check_json_file_has_correct_format(__SCREAMING_SNAKE_CASE			)
								__lowerCAmelCase											=				self.image_processing_class.from_pretrained(__SCREAMING_SNAKE_CASE			)

					self.assertEqual(image_processor_second.to_dict(),image_processor_first.to_dict()			)
		def   lowerCamelCase__			(  self			):



					'''simple docstring'''

					__lowerCAmelCase											=				self.image_processing_class()
					self.assertIsNotNone(__SCREAMING_SNAKE_CASE			)







		@require_torch
		@require_vision
		def   lowerCamelCase__			(  self			):



					'''simple docstring'''

					if self.test_cast_dtype is not None:
								# Initialize image_processor
								__lowerCAmelCase											=				self.image_processing_class(**self.image_processor_dict			)

								# create random PyTorch tensors
								__lowerCAmelCase											=				prepare_image_inputs(self.image_processor_tester,equal_resolution=__SCREAMING_SNAKE_CASE,torchify=__SCREAMING_SNAKE_CASE			)

								__lowerCAmelCase											=				image_processor(__SCREAMING_SNAKE_CASE,return_tensors="""pt"""			)
								# for layoutLM compatiblity
								self.assertEqual(encoding.pixel_values.device,torch.device("""cpu"""			)			)
								self.assertEqual(encoding.pixel_values.dtype,torch.floataa			)

								__lowerCAmelCase											=				image_processor(__SCREAMING_SNAKE_CASE,return_tensors="""pt"""			).to(torch.floataa			)
								self.assertEqual(encoding.pixel_values.device,torch.device("""cpu"""			)			)
								self.assertEqual(encoding.pixel_values.dtype,torch.floataa			)

								__lowerCAmelCase											=				image_processor(__SCREAMING_SNAKE_CASE,return_tensors="""pt"""			).to("""cpu""",torch.bfloataa			)
								self.assertEqual(encoding.pixel_values.device,torch.device("""cpu"""			)			)
								self.assertEqual(encoding.pixel_values.dtype,torch.bfloataa			)

								with self.assertRaises(__SCREAMING_SNAKE_CASE			):
											__lowerCAmelCase											=				image_processor(__SCREAMING_SNAKE_CASE,return_tensors="""pt"""			).to(torch.bfloataa,"""cpu"""			)

								# Try with text + image feature
								__lowerCAmelCase											=				image_processor(__SCREAMING_SNAKE_CASE,return_tensors="""pt"""			)
								encoding.update({"""input_ids""": torch.LongTensor([[1, 2, 3], [4, 5, 6]]			)}			)
								__lowerCAmelCase											=				encoding.to(torch.floataa			)

								self.assertEqual(encoding.pixel_values.device,torch.device("""cpu"""			)			)
								self.assertEqual(encoding.pixel_values.dtype,torch.floataa			)
								self.assertEqual(encoding.input_ids.dtype,torch.long			)








'''simple docstring'''







import unittest
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest.mock import Mock, patch

import diffusers.utils.hub_utils





class     _UpperCAmelCase							(						unittest.TestCase   ):
		@patch("""diffusers.utils.hub_utils.get_full_repo_name"""			)
		def   lowerCamelCase__			(  self,__SCREAMING_SNAKE_CASE			):



					'''simple docstring'''

					__lowerCAmelCase											=				"""full_repo_name"""
					with TemporaryDirectory() as tmpdir:
								# Dummy args values
								__lowerCAmelCase											=				Mock()
								__lowerCAmelCase											=				tmpdir
								__lowerCAmelCase											=				0
								__lowerCAmelCase											=				"""hub_token"""
								__lowerCAmelCase											=				"""dataset_name"""
								__lowerCAmelCase											=				0.01
								__lowerCAmelCase											=				10_00_00
								__lowerCAmelCase											=				1_00_00
								__lowerCAmelCase											=				0.01
								__lowerCAmelCase											=				0.02
								__lowerCAmelCase											=				0.03
								__lowerCAmelCase											=				0.0005
								__lowerCAmelCase											=				0.00_0001
								__lowerCAmelCase											=				1
								__lowerCAmelCase											=				10
								__lowerCAmelCase											=				0.001
								__lowerCAmelCase											=				0.1
								__lowerCAmelCase											=				0.2
								__lowerCAmelCase											=				True

								# Model card mush be rendered and saved
								diffusers.utils.hub_utils.create_model_card(__SCREAMING_SNAKE_CASE,model_name="""model_name"""			)
								self.assertTrue((Path(__SCREAMING_SNAKE_CASE			) / """README.md""").is_file()			)

