
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 BlipImageProcessor







class 							UpperCAmelCase	(   unittest.TestCase					):

					def __init__(			self      :					int ,      lowerCAmelCase      :					Union[str, Any] ,      lowerCAmelCase      :					str=7 ,      lowerCAmelCase      :					int=3 ,      lowerCAmelCase      :					Any=18 ,      lowerCAmelCase      :					List[Any]=30 ,      lowerCAmelCase      :					List[str]=400 ,      lowerCAmelCase      :					Optional[int]=True ,      lowerCAmelCase      :					Dict=None ,      lowerCAmelCase      :					Any=True ,      lowerCAmelCase      :					Dict=False ,      lowerCAmelCase      :					List[Any]=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73] ,      lowerCAmelCase      :					str=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11] ,      lowerCAmelCase      :					List[Any]=True ,      ):
											lowercase       :    Optional[int]											=  size if size is not None else {'''height''': 20, '''width''': 20}
											lowercase       :    Dict											=  parent
											lowercase       :    Union[str, Any]											=  batch_size
											lowercase       :    List[str]											=  num_channels
											lowercase       :    int											=  image_size
											lowercase       :    int											=  min_resolution
											lowercase       :    Dict											=  max_resolution
											lowercase       :    Union[str, Any]											=  do_resize
											lowercase       :    Optional[Any]											=  size
											lowercase       :    Union[str, Any]											=  do_normalize
											lowercase       :    Dict											=  image_mean
											lowercase       :    int											=  image_std
											lowercase       :    List[Any]											=  do_pad
											lowercase       :    List[Any]											=  do_convert_rgb

					def        _lowerCAmelCase	(			self      :					Union[str, Any]					):
											return {
											    "do_resize": self.do_resize,
											    "size": self.size,
											    "do_normalize": self.do_normalize,
											    "image_mean": self.image_mean,
											    "image_std": self.image_std,
											    "do_convert_rgb": self.do_convert_rgb,
											    "do_pad": self.do_pad,
											}




					def        _lowerCAmelCase	(			self      :					Tuple ,      lowerCAmelCase      :					Optional[int]=False ,      lowerCAmelCase      :					Any=False ,      lowerCAmelCase      :					List[str]=False					):
											assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

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

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

											if torchify:
																	lowercase       :    Any											=  [torch.from_numpy(lowerCAmelCase					) for x in image_inputs]

											return image_inputs







@require_torch
@require_vision
class 							UpperCAmelCase	(   __lowerCamelCase						, unittest.TestCase					):
					a__: Union[str, Any]			  =				BlipImageProcessor if is_vision_available() else None

					def        _lowerCAmelCase	(			self      :					Optional[int]					):
											lowercase       :    str											=  BlipImageProcessingTester(self					)

					@property
					def        _lowerCAmelCase	(			self      :					List[str]					):
											return self.image_processor_tester.prepare_image_processor_dict()

					def        _lowerCAmelCase	(			self      :					str					):
											lowercase       :    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_normalize'''					)					)
											self.assertTrue(hasattr(lowerCAmelCase ,      '''image_mean'''					)					)
											self.assertTrue(hasattr(lowerCAmelCase ,      '''image_std'''					)					)
											self.assertTrue(hasattr(lowerCAmelCase ,      '''do_convert_rgb'''					)					)

					def        _lowerCAmelCase	(			self      :					str					):
											pass

					def        _lowerCAmelCase	(			self      :					Optional[Any]					):
											# Initialize image_processor
											lowercase       :    Any											=  self.image_processing_class(**self.image_processor_dict					)
											# create random PIL images
											lowercase       :    Any											=  self.image_processor_tester.prepare_inputs(equal_resolution=lowerCAmelCase					)
											for image in image_inputs:
																	self.assertIsInstance(lowerCAmelCase ,      Image.Image					)

											# Test not batched input
											lowercase       :    Optional[Any]											=  image_processor(image_inputs[0] ,      return_tensors='''pt'''					).pixel_values
											self.assertEqual(
											    encoded_images.shape ,      (
											        1,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.size['''height'''],
											        self.image_processor_tester.size['''width'''],
											    ) ,      )

											# Test batched
											lowercase       :    int											=  image_processor(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.size['''height'''],
											        self.image_processor_tester.size['''width'''],
											    ) ,      )

					def        _lowerCAmelCase	(			self      :					Optional[int]					):
											# Initialize image_processor
											lowercase       :    int											=  self.image_processing_class(**self.image_processor_dict					)
											# create random numpy tensors
											lowercase       :    Dict											=  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
											lowercase       :    Any											=  image_processor(image_inputs[0] ,      return_tensors='''pt'''					).pixel_values
											self.assertEqual(
											    encoded_images.shape ,      (
											        1,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.size['''height'''],
											        self.image_processor_tester.size['''width'''],
											    ) ,      )

											# Test batched
											lowercase       :    Union[str, Any]											=  image_processor(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.size['''height'''],
											        self.image_processor_tester.size['''width'''],
											    ) ,      )




					def        _lowerCAmelCase	(			self      :					List[Any]					):
											# Initialize image_processor
											lowercase       :    Dict											=  self.image_processing_class(**self.image_processor_dict					)
											# create random PyTorch tensors
											lowercase       :    str											=  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
											lowercase       :    Tuple											=  image_processor(image_inputs[0] ,      return_tensors='''pt'''					).pixel_values
											self.assertEqual(
											    encoded_images.shape ,      (
											        1,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.size['''height'''],
											        self.image_processor_tester.size['''width'''],
											    ) ,      )

											# Test batched
											lowercase       :    Dict											=  image_processor(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.size['''height'''],
											        self.image_processor_tester.size['''width'''],
											    ) ,      )







@require_torch
@require_vision
class 							UpperCAmelCase	(   __lowerCamelCase						, unittest.TestCase					):
					a__: Any			  =				BlipImageProcessor if is_vision_available() else None

					def        _lowerCAmelCase	(			self      :					Union[str, Any]					):
											lowercase       :    Dict											=  BlipImageProcessingTester(self ,      num_channels=4					)
											lowercase       :    Tuple											=  3

					@property
					def        _lowerCAmelCase	(			self      :					Any					):
											return self.image_processor_tester.prepare_image_processor_dict()

					def        _lowerCAmelCase	(			self      :					Tuple					):
											lowercase       :    Tuple											=  self.image_processing_class(**self.image_processor_dict					)
											self.assertTrue(hasattr(lowerCAmelCase ,      '''do_resize'''					)					)
											self.assertTrue(hasattr(lowerCAmelCase ,      '''size'''					)					)
											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        _lowerCAmelCase	(			self      :					Dict					):
											pass




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

											# Test not batched input
											lowercase       :    Any											=  image_processor(image_inputs[0] ,      return_tensors='''pt'''					).pixel_values
											self.assertEqual(
											    encoded_images.shape ,      (
											        1,
											        self.expected_encoded_image_num_channels,
											        self.image_processor_tester.size['''height'''],
											        self.image_processor_tester.size['''width'''],
											    ) ,      )

											# Test batched
											lowercase       :    Union[str, Any]											=  image_processor(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.size['''height'''],
											        self.image_processor_tester.size['''width'''],
											    ) ,      )




from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


snake_case__  							=							{"""configuration_t5""": ["""T5_PRETRAINED_CONFIG_ARCHIVE_MAP""", """T5Config""", """T5OnnxConfig"""]}

try:
				if not is_sentencepiece_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				snake_case__  							=							["""T5Tokenizer"""]

try:
				if not is_tokenizers_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				snake_case__  							=							["""T5TokenizerFast"""]

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				snake_case__  							=							[
				    """T5_PRETRAINED_MODEL_ARCHIVE_LIST""",
				    """T5EncoderModel""",
				    """T5ForConditionalGeneration""",
				    """T5Model""",
				    """T5PreTrainedModel""",
				    """load_tf_weights_in_t5""",
				    """T5ForQuestionAnswering""",
				]

try:
				if not is_tf_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				snake_case__  							=							[
				    """TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST""",
				    """TFT5EncoderModel""",
				    """TFT5ForConditionalGeneration""",
				    """TFT5Model""",
				    """TFT5PreTrainedModel""",
				]

try:
				if not is_flax_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				snake_case__  							=							[
				    """FlaxT5EncoderModel""",
				    """FlaxT5ForConditionalGeneration""",
				    """FlaxT5Model""",
				    """FlaxT5PreTrainedModel""",
				]


if TYPE_CHECKING:
				from .configuration_ta import T5_PRETRAINED_CONFIG_ARCHIVE_MAP, TaConfig, TaOnnxConfig

				try:
								if not is_sentencepiece_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .tokenization_ta import TaTokenizer

				try:
								if not is_tokenizers_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .tokenization_ta_fast import TaTokenizerFast

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_ta import (
								    T5_PRETRAINED_MODEL_ARCHIVE_LIST,
								    TaEncoderModel,
								    TaForConditionalGeneration,
								    TaForQuestionAnswering,
								    TaModel,
								    TaPreTrainedModel,
								    load_tf_weights_in_ta,
								)

				try:
								if not is_tf_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_tf_ta import (
								    TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST,
								    TFTaEncoderModel,
								    TFTaForConditionalGeneration,
								    TFTaModel,
								    TFTaPreTrainedModel,
								)

				try:
								if not is_flax_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_flax_ta import (
								    FlaxTaEncoderModel,
								    FlaxTaForConditionalGeneration,
								    FlaxTaModel,
								    FlaxTaPreTrainedModel,
								)


else:
				import sys

				snake_case__  							=							_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)


