



'''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, prepare_image_inputs


if is_torch_available():
						import torch

if is_vision_available():
						from PIL import Image

						from transformers import PoolFormerImageProcessor







class 	_lowerCAmelCase							(			unittest.TestCase ):


	def __init__(self       ,      lowercase       ,      lowercase=7       ,      lowercase=3       ,      lowercase=30       ,      lowercase=400       ,      lowercase=True       ,      lowercase=None       ,      lowercase=0.9       ,      lowercase=None       ,      lowercase=True       ,      lowercase=[0.5, 0.5, 0.5]       ,      lowercase=[0.5, 0.5, 0.5]       ,      ):
				A_	:						Union[str, Any]	=     size if size is not None else {"""shortest_edge""": 30}
				A_	:						str	=     crop_size if crop_size is not None else {"""height""": 30, """width""": 30}
				A_	:						Union[str, Any]	=     parent
				A_	:						List[Any]	=     batch_size
				A_	:						str	=     num_channels
				A_	:						int	=     min_resolution
				A_	:						str	=     max_resolution
				A_	:						Tuple	=     do_resize_and_center_crop
				A_	:						Tuple	=     size
				A_	:						int	=     crop_pct
				A_	:						Tuple	=     crop_size
				A_	:						Optional[int]	=     do_normalize
				A_	:						int	=     image_mean
				A_	:						Optional[int]	=     image_std


	def     _a	(self							):
				return {
				    "size": self.size,
				    "do_resize_and_center_crop": self.do_resize_and_center_crop,
				    "crop_pct": self.crop_pct,
				    "crop_size": self.crop_size,
				    "do_normalize": self.do_normalize,
				    "image_mean": self.image_mean,
				    "image_std": self.image_std,
				}







@require_torch
@require_vision
class 	_lowerCAmelCase							(			__UpperCAmelCase      ,			unittest.TestCase ):
	__SCREAMING_SNAKE_CASE   :		Any					=					PoolFormerImageProcessor if is_vision_available() else None


	def     _a	(self							):
				A_	:						int	=     PoolFormerImageProcessingTester(self							)


	@property
	def     _a	(self							):
				return self.image_processor_tester.prepare_image_processor_dict()


	def     _a	(self							):
				A_	:						Optional[int]	=     self.image_processing_class(**self.image_processor_dict							)
				self.assertTrue(hasattr(lowercase       ,      """do_resize_and_center_crop"""							)							)
				self.assertTrue(hasattr(lowercase       ,      """size"""							)							)
				self.assertTrue(hasattr(lowercase       ,      """crop_pct"""							)							)
				self.assertTrue(hasattr(lowercase       ,      """do_normalize"""							)							)
				self.assertTrue(hasattr(lowercase       ,      """image_mean"""							)							)
				self.assertTrue(hasattr(lowercase       ,      """image_std"""							)							)


	def     _a	(self							):
				A_	:						int	=     self.image_processing_class.from_dict(self.image_processor_dict							)
				self.assertEqual(image_processor.size       ,      {"""shortest_edge""": 30}							)
				self.assertEqual(image_processor.crop_size       ,      {"""height""": 30, """width""": 30}							)

				A_	:						Union[str, Any]	=     self.image_processing_class.from_dict(self.image_processor_dict       ,      size=42       ,      crop_size=84							)
				self.assertEqual(image_processor.size       ,      {"""shortest_edge""": 42}							)
				self.assertEqual(image_processor.crop_size       ,      {"""height""": 84, """width""": 84}							)


	def     _a	(self							):
				pass


	def     _a	(self							):
				# Initialize image_processing
				A_	:						Tuple	=     self.image_processing_class(**self.image_processor_dict							)
				# create random PIL images
				A_	:						Union[str, Any]	=     prepare_image_inputs(self.image_processor_tester       ,      equal_resolution=lowercase							)
				for image in image_inputs:
							self.assertIsInstance(lowercase       ,      Image.Image							)

				# Test not batched input
				A_	:						Any	=     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
				A_	:						int	=     image_processing(lowercase       ,      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     _a	(self							):
				# Initialize image_processing
				A_	:						Optional[int]	=     self.image_processing_class(**self.image_processor_dict							)
				# create random numpy tensors
				A_	:						str	=     prepare_image_inputs(self.image_processor_tester       ,      equal_resolution=lowercase       ,      numpify=lowercase							)
				for image in image_inputs:
							self.assertIsInstance(lowercase       ,      np.ndarray							)

				# Test not batched input
				A_	:						int	=     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
				A_	:						List[str]	=     image_processing(lowercase       ,      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     _a	(self							):
				# Initialize image_processing
				A_	:						Tuple	=     self.image_processing_class(**self.image_processor_dict							)
				# create random PyTorch tensors
				A_	:						int	=     prepare_image_inputs(self.image_processor_tester       ,      equal_resolution=lowercase       ,      torchify=lowercase							)
				for image in image_inputs:
							self.assertIsInstance(lowercase       ,      torch.Tensor							)

				# Test not batched input
				A_	:						List[Any]	=     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
				A_	:						Optional[int]	=     image_processing(lowercase       ,      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"""],
				    )       ,      )




'''simple docstring'''




from typing import List, Optional, Union

from ...image_utils import ImageInput
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType







class 	_lowerCAmelCase							(			__UpperCAmelCase ):
	__SCREAMING_SNAKE_CASE   :		Any					=					['image_processor', 'tokenizer']
	__SCREAMING_SNAKE_CASE   :		Union[str, Any]					=					'BlipImageProcessor'
	__SCREAMING_SNAKE_CASE   :		List[Any]					=					('BertTokenizer', 'BertTokenizerFast')


	def __init__(self       ,      lowercase       ,      lowercase							):
				A_	:						List[Any]	=     False
				super().__init__(lowercase       ,      lowercase							)
				A_	:						Tuple	=     self.image_processor


	def __call__(self       ,      lowercase = None       ,      lowercase = None       ,      lowercase = True       ,      lowercase = False       ,      lowercase = None       ,      lowercase = None       ,      lowercase = 0       ,      lowercase = None       ,      lowercase = None       ,      lowercase = False       ,      lowercase = False       ,      lowercase = False       ,      lowercase = False       ,      lowercase = False       ,      lowercase = True       ,      lowercase = None       ,      **lowercase       ,      ):
				if images is None and text is None:
							raise ValueError("""You have to specify either images or text."""							)

				# Get only text
				if images is None:
							A_	:						Optional[Any]	=     self.tokenizer
							A_	:						Tuple	=     self.tokenizer(
							    text=lowercase       ,      add_special_tokens=lowercase       ,      padding=lowercase       ,      truncation=lowercase       ,      max_length=lowercase       ,      stride=lowercase       ,      pad_to_multiple_of=lowercase       ,      return_attention_mask=lowercase       ,      return_overflowing_tokens=lowercase       ,      return_special_tokens_mask=lowercase       ,      return_offsets_mapping=lowercase       ,      return_token_type_ids=lowercase       ,      return_length=lowercase       ,      verbose=lowercase       ,      return_tensors=lowercase       ,      **lowercase       ,      )
							return text_encoding

				# add pixel_values
				A_	:						int	=     self.image_processor(lowercase       ,      return_tensors=lowercase							)

				if text is not None:
							A_	:						Optional[Any]	=     self.tokenizer(
							    text=lowercase       ,      add_special_tokens=lowercase       ,      padding=lowercase       ,      truncation=lowercase       ,      max_length=lowercase       ,      stride=lowercase       ,      pad_to_multiple_of=lowercase       ,      return_attention_mask=lowercase       ,      return_overflowing_tokens=lowercase       ,      return_special_tokens_mask=lowercase       ,      return_offsets_mapping=lowercase       ,      return_token_type_ids=lowercase       ,      return_length=lowercase       ,      verbose=lowercase       ,      return_tensors=lowercase       ,      **lowercase       ,      )
				else:
							A_	:						List[str]	=     None

				if text_encoding is not None:
							encoding_image_processor.update(lowercase							)

				return encoding_image_processor


	def     _a	(self       ,      *lowercase       ,      **lowercase							):
				return self.tokenizer.batch_decode(*lowercase       ,      **lowercase							)


	def     _a	(self       ,      *lowercase       ,      **lowercase							):
				return self.tokenizer.decode(*lowercase       ,      **lowercase							)


	@property
	def     _a	(self							):
				A_	:						int	=     self.tokenizer.model_input_names
				A_	:						Any	=     self.image_processor.model_input_names
				return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names							)							)