


import json
import pathlib
import unittest

import numpy as np

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


if is_torch_available():
							import torch

if is_vision_available():
							from PIL import Image

							from transformers import DetrImageProcessor

class 						A__							(		unittest.TestCase  ):




						def __init__(       self					:      Optional[Any]				,    _a					:      Optional[int]				,    _a					:      Tuple=7				,    _a					:      List[str]=3				,    _a					:      Optional[Any]=30				,    _a					:      Any=400				,    _a					:      Dict=True				,    _a					:      Dict=None				,    _a					:      Any=True				,    _a					:      Optional[int]=1 / 255				,    _a					:      int=True				,    _a					:      Any=[0.5, 0.5, 0.5]				,    _a					:      Dict=[0.5, 0.5, 0.5]				,    _a					:      List[Any]=True				,    )							->     Tuple:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =size if size is not None else {'''shortest_edge''': 18, '''longest_edge''': 1333}
										_SCREAMING_SNAKE_CASE			  =parent
										_SCREAMING_SNAKE_CASE			  =batch_size
										_SCREAMING_SNAKE_CASE			  =num_channels
										_SCREAMING_SNAKE_CASE			  =min_resolution
										_SCREAMING_SNAKE_CASE			  =max_resolution
										_SCREAMING_SNAKE_CASE			  =do_resize
										_SCREAMING_SNAKE_CASE			  =size
										_SCREAMING_SNAKE_CASE			  =do_rescale
										_SCREAMING_SNAKE_CASE			  =rescale_factor
										_SCREAMING_SNAKE_CASE			  =do_normalize
										_SCREAMING_SNAKE_CASE			  =image_mean
										_SCREAMING_SNAKE_CASE			  =image_std
										_SCREAMING_SNAKE_CASE			  =do_pad




						def 							__UpperCamelCase				(       self					:      Optional[int]  )							->     str:


										"""simple docstring"""





										return {
										    "do_resize": self.do_resize,
										    "size": self.size,
										    "do_rescale": self.do_rescale,
										    "rescale_factor": self.rescale_factor,
										    "do_normalize": self.do_normalize,
										    "image_mean": self.image_mean,
										    "image_std": self.image_std,
										    "do_pad": self.do_pad,
										}




						def 							__UpperCamelCase				(       self					:      str				,    _a					:      str				,    _a					:      Optional[int]=False  )							->     Dict:


										"""simple docstring"""





										if not batched:
														_SCREAMING_SNAKE_CASE			  =image_inputs[0]
														if isinstance(_a				,    Image.Image  ):
																		_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE			  =image.size
														else:
																		_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE			  =image.shape[1], image.shape[2]
														if w < h:
																		_SCREAMING_SNAKE_CASE			  =int(self.size['''shortest_edge'''] * h / w  )
																		_SCREAMING_SNAKE_CASE			  =self.size['''shortest_edge''']
														elif w > h:
																		_SCREAMING_SNAKE_CASE			  =self.size['''shortest_edge''']
																		_SCREAMING_SNAKE_CASE			  =int(self.size['''shortest_edge'''] * w / h  )
														else:
																		_SCREAMING_SNAKE_CASE			  =self.size['''shortest_edge''']
																		_SCREAMING_SNAKE_CASE			  =self.size['''shortest_edge''']

										else:
														_SCREAMING_SNAKE_CASE			  =[]
														for image in image_inputs:
																		_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE			  =self.get_expected_values([image]  )
																		expected_values.append((expected_height, expected_width)  )
														_SCREAMING_SNAKE_CASE			  =max(_a				,    key=lambda _a  : item[0]  )[0]
														_SCREAMING_SNAKE_CASE			  =max(_a				,    key=lambda _a  : item[1]  )[1]

										return expected_height, expected_width

@require_torch
@require_vision
class 						A__							(		UpperCamelCase__   ,     unittest.TestCase  ):
						UpperCAmelCase             =						DetrImageProcessor if is_vision_available() else None




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


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =DetrImageProcessingTester(self  )




						@property
						def 							__UpperCamelCase				(       self					:      Dict  )							->     Dict:


										"""simple docstring"""





										return self.image_processor_tester.prepare_image_processor_dict()




						def 							__UpperCamelCase				(       self					:      Dict  )							->     List[str]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =self.image_processing_class(**self.image_processor_dict  )
										self.assertTrue(hasattr(_a				,    '''image_mean'''  )  )
										self.assertTrue(hasattr(_a				,    '''image_std'''  )  )
										self.assertTrue(hasattr(_a				,    '''do_normalize'''  )  )
										self.assertTrue(hasattr(_a				,    '''do_rescale'''  )  )
										self.assertTrue(hasattr(_a				,    '''rescale_factor'''  )  )
										self.assertTrue(hasattr(_a				,    '''do_resize'''  )  )
										self.assertTrue(hasattr(_a				,    '''size'''  )  )
										self.assertTrue(hasattr(_a				,    '''do_pad'''  )  )




						def 							__UpperCamelCase				(       self					:      str  )							->     Optional[Any]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =self.image_processing_class.from_dict(self.image_processor_dict  )
										self.assertEqual(image_processor.size				,    {'''shortest_edge''': 18, '''longest_edge''': 1333}  )
										self.assertEqual(image_processor.do_pad				,    _a  )

										_SCREAMING_SNAKE_CASE			  =self.image_processing_class.from_dict(
										    self.image_processor_dict				,    size=42				,    max_size=84				,    pad_and_return_pixel_mask=_a  )
										self.assertEqual(image_processor.size				,    {'''shortest_edge''': 42, '''longest_edge''': 84}  )
										self.assertEqual(image_processor.do_pad				,    _a  )




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


										"""simple docstring"""





										pass




						def 							__UpperCamelCase				(       self					:      Dict  )							->     str:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =self.image_processing_class(**self.image_processor_dict  )
										# create random PIL images
										_SCREAMING_SNAKE_CASE			  =prepare_image_inputs(self.image_processor_tester				,    equal_resolution=_a  )
										for image in image_inputs:
														self.assertIsInstance(_a				,    Image.Image  )

										# Test not batched input
										_SCREAMING_SNAKE_CASE			  =image_processing(image_inputs[0]				,    return_tensors='''pt'''  ).pixel_values

										_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE			  =self.image_processor_tester.get_expected_values(_a  )

										self.assertEqual(
										    encoded_images.shape				,    (1, self.image_processor_tester.num_channels, expected_height, expected_width)				,    )

										# Test batched
										_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE			  =self.image_processor_tester.get_expected_values(_a				,    batched=_a  )

										_SCREAMING_SNAKE_CASE			  =image_processing(_a				,    return_tensors='''pt'''  ).pixel_values
										self.assertEqual(
										    encoded_images.shape				,    (
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        expected_height,
										        expected_width,
										    )				,    )




						def 							__UpperCamelCase				(       self					:      Any  )							->     List[Any]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =self.image_processing_class(**self.image_processor_dict  )
										# create random numpy tensors
										_SCREAMING_SNAKE_CASE			  =prepare_image_inputs(self.image_processor_tester				,    equal_resolution=_a				,    numpify=_a  )
										for image in image_inputs:
														self.assertIsInstance(_a				,    np.ndarray  )

										# Test not batched input
										_SCREAMING_SNAKE_CASE			  =image_processing(image_inputs[0]				,    return_tensors='''pt'''  ).pixel_values

										_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE			  =self.image_processor_tester.get_expected_values(_a  )

										self.assertEqual(
										    encoded_images.shape				,    (1, self.image_processor_tester.num_channels, expected_height, expected_width)				,    )

										# Test batched
										_SCREAMING_SNAKE_CASE			  =image_processing(_a				,    return_tensors='''pt'''  ).pixel_values

										_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE			  =self.image_processor_tester.get_expected_values(_a				,    batched=_a  )

										self.assertEqual(
										    encoded_images.shape				,    (
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        expected_height,
										        expected_width,
										    )				,    )




						def 							__UpperCamelCase				(       self					:      Optional[int]  )							->     Tuple:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =self.image_processing_class(**self.image_processor_dict  )
										# create random PyTorch tensors
										_SCREAMING_SNAKE_CASE			  =prepare_image_inputs(self.image_processor_tester				,    equal_resolution=_a				,    torchify=_a  )
										for image in image_inputs:
														self.assertIsInstance(_a				,    torch.Tensor  )

										# Test not batched input
										_SCREAMING_SNAKE_CASE			  =image_processing(image_inputs[0]				,    return_tensors='''pt'''  ).pixel_values

										_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE			  =self.image_processor_tester.get_expected_values(_a  )

										self.assertEqual(
										    encoded_images.shape				,    (1, self.image_processor_tester.num_channels, expected_height, expected_width)				,    )

										# Test batched
										_SCREAMING_SNAKE_CASE			  =image_processing(_a				,    return_tensors='''pt'''  ).pixel_values

										_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE			  =self.image_processor_tester.get_expected_values(_a				,    batched=_a  )

										self.assertEqual(
										    encoded_images.shape				,    (
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        expected_height,
										        expected_width,
										    )				,    )




						@slow
						def 							__UpperCamelCase				(       self					:      Optional[Any]  )							->     Any:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''  )
										with open('''./tests/fixtures/tests_samples/COCO/coco_annotations.txt'''				,    '''r'''  ) as f:
														_SCREAMING_SNAKE_CASE			  =json.loads(f.read()  )

										_SCREAMING_SNAKE_CASE			  ={'''image_id''': 3_9769, '''annotations''': target}

										# encode them
										_SCREAMING_SNAKE_CASE			  =DetrImageProcessor.from_pretrained('''facebook/detr-resnet-50'''  )
										_SCREAMING_SNAKE_CASE			  =image_processing(images=_a				,    annotations=_a				,    return_tensors='''pt'''  )

										# verify pixel values
										_SCREAMING_SNAKE_CASE			  =torch.Size([1, 3, 800, 1066]  )
										self.assertEqual(encoding['''pixel_values'''].shape				,    _a  )

										_SCREAMING_SNAKE_CASE			  =torch.tensor([0.27_96, 0.31_38, 0.34_81]  )
										self.assertTrue(torch.allclose(encoding['''pixel_values'''][0, 0, 0, :3]				,    _a				,    atol=1E-4  )  )

										# verify area
										_SCREAMING_SNAKE_CASE			  =torch.tensor([5887.9600, 1_1250.2061, 48_9353.8438, 83_7122.7500, 14_7967.5156, 16_5732.3438]  )
										self.assertTrue(torch.allclose(encoding['''labels'''][0]['''area''']				,    _a  )  )
										# verify boxes
										_SCREAMING_SNAKE_CASE			  =torch.Size([6, 4]  )
										self.assertEqual(encoding['''labels'''][0]['''boxes'''].shape				,    _a  )
										_SCREAMING_SNAKE_CASE			  =torch.tensor([0.55_03, 0.27_65, 0.06_04, 0.22_15]  )
										self.assertTrue(torch.allclose(encoding['''labels'''][0]['''boxes'''][0]				,    _a				,    atol=1E-3  )  )
										# verify image_id
										_SCREAMING_SNAKE_CASE			  =torch.tensor([3_9769]  )
										self.assertTrue(torch.allclose(encoding['''labels'''][0]['''image_id''']				,    _a  )  )
										# verify is_crowd
										_SCREAMING_SNAKE_CASE			  =torch.tensor([0, 0, 0, 0, 0, 0]  )
										self.assertTrue(torch.allclose(encoding['''labels'''][0]['''iscrowd''']				,    _a  )  )
										# verify class_labels
										_SCREAMING_SNAKE_CASE			  =torch.tensor([75, 75, 63, 65, 17, 17]  )
										self.assertTrue(torch.allclose(encoding['''labels'''][0]['''class_labels''']				,    _a  )  )
										# verify orig_size
										_SCREAMING_SNAKE_CASE			  =torch.tensor([480, 640]  )
										self.assertTrue(torch.allclose(encoding['''labels'''][0]['''orig_size''']				,    _a  )  )
										# verify size
										_SCREAMING_SNAKE_CASE			  =torch.tensor([800, 1066]  )
										self.assertTrue(torch.allclose(encoding['''labels'''][0]['''size''']				,    _a  )  )




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


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''  )
										with open('''./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt'''				,    '''r'''  ) as f:
														_SCREAMING_SNAKE_CASE			  =json.loads(f.read()  )

										_SCREAMING_SNAKE_CASE			  ={'''file_name''': '''000000039769.png''', '''image_id''': 3_9769, '''segments_info''': target}

										_SCREAMING_SNAKE_CASE			  =pathlib.Path('''./tests/fixtures/tests_samples/COCO/coco_panoptic'''  )

										# encode them
										_SCREAMING_SNAKE_CASE			  =DetrImageProcessor.from_pretrained('''facebook/detr-resnet-50-panoptic'''  )
										_SCREAMING_SNAKE_CASE			  =image_processing(images=_a				,    annotations=_a				,    masks_path=_a				,    return_tensors='''pt'''  )

										# verify pixel values
										_SCREAMING_SNAKE_CASE			  =torch.Size([1, 3, 800, 1066]  )
										self.assertEqual(encoding['''pixel_values'''].shape				,    _a  )

										_SCREAMING_SNAKE_CASE			  =torch.tensor([0.27_96, 0.31_38, 0.34_81]  )
										self.assertTrue(torch.allclose(encoding['''pixel_values'''][0, 0, 0, :3]				,    _a				,    atol=1E-4  )  )

										# verify area
										_SCREAMING_SNAKE_CASE			  =torch.tensor([14_7979.6875, 16_5527.0469, 48_4638.5938, 1_1292.9375, 5879.6562, 7634.1147]  )
										self.assertTrue(torch.allclose(encoding['''labels'''][0]['''area''']				,    _a  )  )
										# verify boxes
										_SCREAMING_SNAKE_CASE			  =torch.Size([6, 4]  )
										self.assertEqual(encoding['''labels'''][0]['''boxes'''].shape				,    _a  )
										_SCREAMING_SNAKE_CASE			  =torch.tensor([0.26_25, 0.54_37, 0.46_88, 0.86_25]  )
										self.assertTrue(torch.allclose(encoding['''labels'''][0]['''boxes'''][0]				,    _a				,    atol=1E-3  )  )
										# verify image_id
										_SCREAMING_SNAKE_CASE			  =torch.tensor([3_9769]  )
										self.assertTrue(torch.allclose(encoding['''labels'''][0]['''image_id''']				,    _a  )  )
										# verify is_crowd
										_SCREAMING_SNAKE_CASE			  =torch.tensor([0, 0, 0, 0, 0, 0]  )
										self.assertTrue(torch.allclose(encoding['''labels'''][0]['''iscrowd''']				,    _a  )  )
										# verify class_labels
										_SCREAMING_SNAKE_CASE			  =torch.tensor([17, 17, 63, 75, 75, 93]  )
										self.assertTrue(torch.allclose(encoding['''labels'''][0]['''class_labels''']				,    _a  )  )
										# verify masks
										_SCREAMING_SNAKE_CASE			  =82_2873
										self.assertEqual(encoding['''labels'''][0]['''masks'''].sum().item()				,    _a  )
										# verify orig_size
										_SCREAMING_SNAKE_CASE			  =torch.tensor([480, 640]  )
										self.assertTrue(torch.allclose(encoding['''labels'''][0]['''orig_size''']				,    _a  )  )
										# verify size
										_SCREAMING_SNAKE_CASE			  =torch.tensor([800, 1066]  )
										self.assertTrue(torch.allclose(encoding['''labels'''][0]['''size''']				,    _a  )  )



# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re

from ..models.auto import AutoProcessor
from ..models.vision_encoder_decoder import VisionEncoderDecoderModel
from ..utils import is_vision_available
from .base import PipelineTool


if is_vision_available():
							from PIL import Image

class 						A__							(		UpperCamelCase__  ):
						UpperCAmelCase             =						"naver-clova-ix/donut-base-finetuned-docvqa"
						UpperCAmelCase             =						(
						    "This is a tool that answers a question about an document (pdf). It takes an input named `document` which "
						    "should be the document containing the information, as well as a `question` that is the question about the "
						    "document. It returns a text that contains the answer to the question."
						)
						UpperCAmelCase             =						"document_qa"
						UpperCAmelCase             =						AutoProcessor
						UpperCAmelCase             =						VisionEncoderDecoderModel

						UpperCAmelCase             =						["image", "text"]
						UpperCAmelCase             =						["text"]




						def __init__(       self					:      Any				,    *_a					:      int				,    **_a					:      Dict  )							->     int:


										"""simple docstring"""





										if not is_vision_available():
														raise ValueError('''Pillow must be installed to use the DocumentQuestionAnsweringTool.'''  )

										super().__init__(*_a				,    **_a  )




						def 							__UpperCamelCase				(       self					:      Optional[Any]				,    _a					:      "Image"				,    _a					:      str  )							->     Tuple:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  ='''<s_docvqa><s_question>{user_input}</s_question><s_answer>'''
										_SCREAMING_SNAKE_CASE			  =task_prompt.replace('''{user_input}'''				,    _a  )
										_SCREAMING_SNAKE_CASE			  =self.pre_processor.tokenizer(
										    _a				,    add_special_tokens=_a				,    return_tensors='''pt'''  ).input_ids
										_SCREAMING_SNAKE_CASE			  =self.pre_processor(_a				,    return_tensors='''pt'''  ).pixel_values

										return {"decoder_input_ids": decoder_input_ids, "pixel_values": pixel_values}




						def 							__UpperCamelCase				(       self					:      List[Any]				,    _a					:      Optional[Any]  )							->     int:


										"""simple docstring"""





										return self.model.generate(
										    inputs['''pixel_values'''].to(self.device  )				,    decoder_input_ids=inputs['''decoder_input_ids'''].to(self.device  )				,    max_length=self.model.decoder.config.max_position_embeddings				,    early_stopping=_a				,    pad_token_id=self.pre_processor.tokenizer.pad_token_id				,    eos_token_id=self.pre_processor.tokenizer.eos_token_id				,    use_cache=_a				,    num_beams=1				,    bad_words_ids=[[self.pre_processor.tokenizer.unk_token_id]]				,    return_dict_in_generate=_a				,    ).sequences




						def 							__UpperCamelCase				(       self					:      Any				,    _a					:      int  )							->     Any:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =self.pre_processor.batch_decode(_a  )[0]
										_SCREAMING_SNAKE_CASE			  =sequence.replace(self.pre_processor.tokenizer.eos_token				,    ''''''  )
										_SCREAMING_SNAKE_CASE			  =sequence.replace(self.pre_processor.tokenizer.pad_token				,    ''''''  )
										_SCREAMING_SNAKE_CASE			  =re.sub(R'''<.*?>'''				,    ''''''				,    _a				,    count=1  ).strip()  # remove first task start token
										_SCREAMING_SNAKE_CASE			  =self.pre_processor.tokenajson(_a  )

										return sequence["answer"]