






import unittest

import numpy as np
import requests

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

		from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
		__UpperCAmelCase								=						False

if is_vision_available():
		from PIL import Image

		from transformers import PixaStructImageProcessor



class   lowerCamelCase      (unittest.TestCase			):







		'''simple docstring'''



		def __init__(							self				,		_UpperCamelCase				,		_UpperCamelCase=7				,		_UpperCamelCase=3				,		_UpperCamelCase=1_8				,		_UpperCamelCase=3_0				,		_UpperCamelCase=4_0_0				,		_UpperCamelCase=None				,		_UpperCamelCase=True				,		_UpperCamelCase=True				,		_UpperCamelCase=None				,		)	->							Optional[int]:
							UpperCAmelCase_    :      List[Any]            =						size if size is not None else {'height': 2_0, 'width': 2_0}
							UpperCAmelCase_    :      str            =						parent
							UpperCAmelCase_    :      Tuple            =						batch_size
							UpperCAmelCase_    :      List[str]            =						num_channels
							UpperCAmelCase_    :      Tuple            =						image_size
							UpperCAmelCase_    :      Union[str, Any]            =						min_resolution
							UpperCAmelCase_    :      Union[str, Any]            =						max_resolution
							UpperCAmelCase_    :      Optional[int]            =						size
							UpperCAmelCase_    :      Union[str, Any]            =						do_normalize
							UpperCAmelCase_    :      Union[str, Any]            =						do_convert_rgb
							UpperCAmelCase_    :      Dict            =						[5_1_2, 1_0_2_4, 2_0_4_8, 4_0_9_6]
							UpperCAmelCase_    :      Optional[int]            =						patch_size if patch_size is not None else {'height': 1_6, 'width': 1_6}



		def 					__UpperCAmelCase			(							self   )	->							Optional[Any]:
							return {"do_normalize": self.do_normalize, "do_convert_rgb": self.do_convert_rgb}



		def 					__UpperCAmelCase			(							self   )	->							Any:
							UpperCAmelCase_    :      List[str]            =						'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/australia.jpg'
							UpperCAmelCase_    :      int            =						Image.open(requests.get(_UpperCamelCase				,		stream=_UpperCamelCase   ).raw   ).convert('RGB'   )
							return raw_image





@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11 ,  reason='''`Pix2StructImageProcessor` requires `torch>=1.11.0`.''' ,  )
@require_torch
@require_vision
class   lowerCamelCase      (_snake_case ,  unittest.TestCase			):







		'''simple docstring'''



		_snake_case    :    Dict       						=  PixaStructImageProcessor if is_vision_available() else None



		def 					__UpperCAmelCase			(							self   )	->							str:
							UpperCAmelCase_    :      Union[str, Any]            =						PixaStructImageProcessingTester(self   )



		@property
		def 					__UpperCAmelCase			(							self   )	->							Any:
							return self.image_processor_tester.prepare_image_processor_dict()



		def 					__UpperCAmelCase			(							self   )	->							List[Any]:
							UpperCAmelCase_    :      Dict            =						self.image_processing_class(**self.image_processor_dict   )
							self.assertTrue(hasattr(_UpperCamelCase				,		'do_normalize'   )   )
							self.assertTrue(hasattr(_UpperCamelCase				,		'do_convert_rgb'   )   )



		def 					__UpperCAmelCase			(							self   )	->							int:
							UpperCAmelCase_    :      Union[str, Any]            =						self.image_processor_tester.prepare_dummy_image()

							UpperCAmelCase_    :      List[str]            =						self.image_processing_class(**self.image_processor_dict   )
							UpperCAmelCase_    :      List[str]            =						2_0_4_8

							UpperCAmelCase_    :      List[str]            =						image_processor(_UpperCamelCase				,		return_tensors='pt'				,		max_patches=_UpperCamelCase   )
							self.assertTrue(torch.allclose(inputs.flattened_patches.mean()				,		torch.tensor(0.06_06   )				,		atol=1E-3				,		rtol=1E-3   )   )



		def 					__UpperCAmelCase			(							self   )	->							Union[str, Any]:
							# Initialize image_processor
							UpperCAmelCase_    :      str            =						self.image_processing_class(**self.image_processor_dict   )
							# create random PIL images
							UpperCAmelCase_    :      Tuple            =						prepare_image_inputs(self.image_processor_tester				,		equal_resolution=_UpperCamelCase   )
							for image in image_inputs:
												self.assertIsInstance(_UpperCamelCase				,		Image.Image   )

							# Test not batched input
							UpperCAmelCase_    :      Dict            =						(
							    (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
							    * self.image_processor_tester.num_channels
							) + 2

							for max_patch in self.image_processor_tester.max_patches:
												# Test not batched input
												UpperCAmelCase_    :      Dict            =						image_processor(
												    image_inputs[0]				,		return_tensors='pt'				,		max_patches=_UpperCamelCase   ).flattened_patches
												self.assertEqual(
												    encoded_images.shape				,		(1, max_patch, expected_hidden_dim)				,		)

												# Test batched
												UpperCAmelCase_    :      Optional[Any]            =						image_processor(
												    _UpperCamelCase				,		return_tensors='pt'				,		max_patches=_UpperCamelCase   ).flattened_patches
												self.assertEqual(
												    encoded_images.shape				,		(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)				,		)



		def 					__UpperCAmelCase			(							self   )	->							Tuple:
							# Initialize image_processor
							UpperCAmelCase_    :      Union[str, Any]            =						self.image_processing_class(**self.image_processor_dict   )
							# create random PIL images
							UpperCAmelCase_    :      int            =						prepare_image_inputs(self.image_processor_tester				,		equal_resolution=_UpperCamelCase   )
							for image in image_inputs:
												self.assertIsInstance(_UpperCamelCase				,		Image.Image   )

							# Test not batched input
							UpperCAmelCase_    :      List[str]            =						(
							    (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
							    * self.image_processor_tester.num_channels
							) + 2

							UpperCAmelCase_    :      List[str]            =						True

							for max_patch in self.image_processor_tester.max_patches:
												# Test not batched input
												with self.assertRaises(_UpperCamelCase   ):
																	UpperCAmelCase_    :      List[str]            =						image_processor(
																	    image_inputs[0]				,		return_tensors='pt'				,		max_patches=_UpperCamelCase   ).flattened_patches

												UpperCAmelCase_    :      Any            =						'Hello'

												UpperCAmelCase_    :      Optional[int]            =						image_processor(
												    image_inputs[0]				,		return_tensors='pt'				,		max_patches=_UpperCamelCase				,		header_text=_UpperCamelCase   ).flattened_patches
												self.assertEqual(
												    encoded_images.shape				,		(1, max_patch, expected_hidden_dim)				,		)

												# Test batched
												UpperCAmelCase_    :      Any            =						image_processor(
												    _UpperCamelCase				,		return_tensors='pt'				,		max_patches=_UpperCamelCase				,		header_text=_UpperCamelCase   ).flattened_patches
												self.assertEqual(
												    encoded_images.shape				,		(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)				,		)



		def 					__UpperCAmelCase			(							self   )	->							Any:
							# Initialize image_processor
							UpperCAmelCase_    :      int            =						self.image_processing_class(**self.image_processor_dict   )
							# create random numpy tensors
							UpperCAmelCase_    :      Optional[Any]            =						prepare_image_inputs(self.image_processor_tester				,		equal_resolution=_UpperCamelCase				,		numpify=_UpperCamelCase   )
							for image in image_inputs:
												self.assertIsInstance(_UpperCamelCase				,		np.ndarray   )

							UpperCAmelCase_    :      List[Any]            =						(
							    (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
							    * self.image_processor_tester.num_channels
							) + 2

							for max_patch in self.image_processor_tester.max_patches:
												# Test not batched input
												UpperCAmelCase_    :      Optional[int]            =						image_processor(
												    image_inputs[0]				,		return_tensors='pt'				,		max_patches=_UpperCamelCase   ).flattened_patches
												self.assertEqual(
												    encoded_images.shape				,		(1, max_patch, expected_hidden_dim)				,		)

												# Test batched
												UpperCAmelCase_    :      int            =						image_processor(
												    _UpperCamelCase				,		return_tensors='pt'				,		max_patches=_UpperCamelCase   ).flattened_patches
												self.assertEqual(
												    encoded_images.shape				,		(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)				,		)



		def 					__UpperCAmelCase			(							self   )	->							int:
							# Initialize image_processor
							UpperCAmelCase_    :      Union[str, Any]            =						self.image_processing_class(**self.image_processor_dict   )
							# create random PyTorch tensors
							UpperCAmelCase_    :      Dict            =						prepare_image_inputs(self.image_processor_tester				,		equal_resolution=_UpperCamelCase				,		torchify=_UpperCamelCase   )
							for image in image_inputs:
												self.assertIsInstance(_UpperCamelCase				,		torch.Tensor   )

							# Test not batched input
							UpperCAmelCase_    :      Any            =						(
							    (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
							    * self.image_processor_tester.num_channels
							) + 2

							for max_patch in self.image_processor_tester.max_patches:
												# Test not batched input
												UpperCAmelCase_    :      List[str]            =						image_processor(
												    image_inputs[0]				,		return_tensors='pt'				,		max_patches=_UpperCamelCase   ).flattened_patches
												self.assertEqual(
												    encoded_images.shape				,		(1, max_patch, expected_hidden_dim)				,		)

												# Test batched
												UpperCAmelCase_    :      List[str]            =						image_processor(
												    _UpperCamelCase				,		return_tensors='pt'				,		max_patches=_UpperCamelCase   ).flattened_patches
												self.assertEqual(
												    encoded_images.shape				,		(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)				,		)





@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11 ,  reason='''`Pix2StructImageProcessor` requires `torch>=1.11.0`.''' ,  )
@require_torch
@require_vision
class   lowerCamelCase      (_snake_case ,  unittest.TestCase			):







		'''simple docstring'''



		_snake_case    :    List[str]       						=  PixaStructImageProcessor if is_vision_available() else None



		def 					__UpperCAmelCase			(							self   )	->							Union[str, Any]:
							UpperCAmelCase_    :      Optional[Any]            =						PixaStructImageProcessingTester(self				,		num_channels=4   )
							UpperCAmelCase_    :      int            =						3



		@property
		def 					__UpperCAmelCase			(							self   )	->							Union[str, Any]:
							return self.image_processor_tester.prepare_image_processor_dict()



		def 					__UpperCAmelCase			(							self   )	->							List[Any]:
							UpperCAmelCase_    :      Dict            =						self.image_processing_class(**self.image_processor_dict   )
							self.assertTrue(hasattr(_UpperCamelCase				,		'do_normalize'   )   )
							self.assertTrue(hasattr(_UpperCamelCase				,		'do_convert_rgb'   )   )



		def 					__UpperCAmelCase			(							self   )	->							str:
							# Initialize image_processor
							UpperCAmelCase_    :      List[str]            =						self.image_processing_class(**self.image_processor_dict   )
							# create random PIL images
							UpperCAmelCase_    :      List[Any]            =						prepare_image_inputs(self.image_processor_tester				,		equal_resolution=_UpperCamelCase   )
							for image in image_inputs:
												self.assertIsInstance(_UpperCamelCase				,		Image.Image   )

							# Test not batched input
							UpperCAmelCase_    :      List[str]            =						(
							    (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
							    * (self.image_processor_tester.num_channels - 1)
							) + 2

							for max_patch in self.image_processor_tester.max_patches:
												# Test not batched input
												UpperCAmelCase_    :      List[str]            =						image_processor(
												    image_inputs[0]				,		return_tensors='pt'				,		max_patches=_UpperCamelCase   ).flattened_patches
												self.assertEqual(
												    encoded_images.shape				,		(1, max_patch, expected_hidden_dim)				,		)

												# Test batched
												UpperCAmelCase_    :      Any            =						image_processor(
												    _UpperCamelCase				,		return_tensors='pt'				,		max_patches=_UpperCamelCase   ).flattened_patches
												self.assertEqual(
												    encoded_images.shape				,		(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)				,		)














from typing import Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
		from PIL import Image

		from ..image_utils import load_image

if is_torch_available():
		from ..models.auto.modeling_auto import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING

__UpperCAmelCase								=						logging.get_logger(__name__)



@add_end_docstrings(_snake_case			)
class   lowerCamelCase      (_snake_case			):







		'''simple docstring'''



		def __init__(							self				,		*_UpperCamelCase				,		**_UpperCamelCase   )	->							int:
							super().__init__(*_UpperCamelCase				,		**_UpperCamelCase   )
							self.check_model_type(_UpperCamelCase   )



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase=None				,		_UpperCamelCase=None				,		_UpperCamelCase=None				,		**_UpperCamelCase   )	->							List[Any]:
							UpperCAmelCase_       ,  UpperCAmelCase_    :      Tuple            =						{}, {}
							if padding is not None:
												UpperCAmelCase_    :      List[str]            =						padding
							if truncation is not None:
												UpperCAmelCase_    :      Tuple            =						truncation
							if top_k is not None:
												UpperCAmelCase_    :      Dict            =						top_k
							return preprocess_params, {}, postprocess_params



		def __call__(							self				,		_UpperCamelCase				,		_UpperCamelCase = None				,		**_UpperCamelCase   )	->							int:
							if isinstance(_UpperCamelCase				,		(Image.Image, str)   ) and isinstance(_UpperCamelCase				,		_UpperCamelCase   ):
												UpperCAmelCase_    :      Optional[Any]            =						{'image': image, 'question': question}
							else:
												UpperCAmelCase_    :      List[str]            =						image
							UpperCAmelCase_    :      Optional[Any]            =						super().__call__(_UpperCamelCase				,		**_UpperCamelCase   )
							return results



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase				,		_UpperCamelCase=False				,		_UpperCamelCase=False   )	->							Optional[Any]:
							UpperCAmelCase_    :      List[Any]            =						load_image(inputs['image']   )
							UpperCAmelCase_    :      Dict            =						self.tokenizer(
							    inputs['question']				,		return_tensors=self.framework				,		padding=_UpperCamelCase				,		truncation=_UpperCamelCase   )
							UpperCAmelCase_    :      int            =						self.image_processor(images=_UpperCamelCase				,		return_tensors=self.framework   )
							model_inputs.update(_UpperCamelCase   )
							return model_inputs



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase   )	->							Optional[int]:
							UpperCAmelCase_    :      Any            =						self.model(**_UpperCamelCase   )
							return model_outputs



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase				,		_UpperCamelCase=5   )	->							str:
							if top_k > self.model.config.num_labels:
												UpperCAmelCase_    :      Union[str, Any]            =						self.model.config.num_labels

							if self.framework == "pt":
												UpperCAmelCase_    :      List[str]            =						model_outputs.logits.sigmoid()[0]
												UpperCAmelCase_       ,  UpperCAmelCase_    :      str            =						probs.topk(_UpperCamelCase   )
							else:
												raise ValueError(f"Unsupported framework: {self.framework}"   )

							UpperCAmelCase_    :      Optional[Any]            =						scores.tolist()
							UpperCAmelCase_    :      Tuple            =						ids.tolist()
							return [{"score": score, "answer": self.model.config.idalabel[_id]} for score, _id in zip(_UpperCamelCase				,		_UpperCamelCase   )]






