



import inspect
import unittest

from transformers import MobileNetVaConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation, MobileNetVaModel
							from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
							from PIL import Image

							from transformers import MobileNetVaImageProcessor


class   __lowerCAmelCase  (		UpperCAmelCase_   ):
					"""simple docstring"""






					def      _a				(  self       :							Any       ):






								"""simple docstring"""


								A__	  =     self.config_class(**self.inputs_dict       )
								self.parent.assertTrue(hasattr(_snake_case					,       'tf_padding'       )       )
								self.parent.assertTrue(hasattr(_snake_case					,       'depth_multiplier'       )       )







class   __lowerCAmelCase  :
					"""simple docstring"""






					def __init__(  self       :							Optional[Any]					,       _snake_case       :							Optional[int]					,       _snake_case       :							Tuple=13					,       _snake_case       :							int=3					,       _snake_case       :							Optional[Any]=32					,       _snake_case       :							List[str]=0.25					,       _snake_case       :							Optional[int]=8					,       _snake_case       :							List[Any]=8					,       _snake_case       :							Dict=6					,       _snake_case       :							List[str]=32					,       _snake_case       :							Tuple=True					,       _snake_case       :							Union[str, Any]=True					,       _snake_case       :							Any=True					,       _snake_case       :							List[str]="relu6"					,       _snake_case       :							Optional[int]=12_80					,       _snake_case       :							List[Any]=0.1					,       _snake_case       :							Optional[int]=0.02					,       _snake_case       :							List[Any]=True					,       _snake_case       :							List[Any]=True					,       _snake_case       :							List[str]=10					,       _snake_case       :							str=None					,       ):






								"""simple docstring"""


								A__	  =     parent
								A__	  =     batch_size
								A__	  =     num_channels
								A__	  =     image_size
								A__	  =     depth_multiplier
								A__	  =     depth_divisible_by
								A__	  =     min_depth
								A__	  =     expand_ratio
								A__	  =     tf_padding
								A__	  =     output_stride
								A__	  =     first_layer_is_expansion
								A__	  =     finegrained_output
								A__	  =     hidden_act
								A__	  =     last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier       )
								A__	  =     classifier_dropout_prob
								A__	  =     use_labels
								A__	  =     is_training
								A__	  =     num_labels
								A__	  =     initializer_range
								A__	  =     scope



					def      _a				(  self       :							Optional[Any]       ):






								"""simple docstring"""


								A__	  =     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

								A__	  =     None
								A__	  =     None
								if self.use_labels:
											A__	  =     ids_tensor([self.batch_size]					,       self.num_labels       )
											A__	  =     ids_tensor([self.batch_size, self.image_size, self.image_size]					,       self.num_labels       )

								A__	  =     self.get_config()

								return config, pixel_values, labels, pixel_labels



					def      _a				(  self       :							Optional[Any]       ):






								"""simple docstring"""


								return MobileNetVaConfig(
								    num_channels=self.num_channels					,       image_size=self.image_size					,       depth_multiplier=self.depth_multiplier					,       depth_divisible_by=self.depth_divisible_by					,       min_depth=self.min_depth					,       expand_ratio=self.expand_ratio					,       output_stride=self.output_stride					,       first_layer_is_expansion=self.first_layer_is_expansion					,       finegrained_output=self.finegrained_output					,       hidden_act=self.hidden_act					,       tf_padding=self.tf_padding					,       classifier_dropout_prob=self.classifier_dropout_prob					,       initializer_range=self.initializer_range					,       )



					def      _a				(  self       :							Tuple					,       _snake_case       :							List[Any]					,       _snake_case       :							Tuple					,       _snake_case       :							Optional[int]					,       _snake_case       :							Optional[Any]       ):






								"""simple docstring"""


								A__	  =     MobileNetVaModel(config=_snake_case       )
								model.to(_snake_case       )
								model.eval()
								A__	  =     model(_snake_case       )
								self.parent.assertEqual(
								    result.last_hidden_state.shape					,       (
								        self.batch_size,
								        self.last_hidden_size,
								        self.image_size // self.output_stride,
								        self.image_size // self.output_stride,
								    )					,       )
								self.parent.assertEqual(
								    result.pooler_output.shape					,       (self.batch_size, self.last_hidden_size)					,       )



					def      _a				(  self       :							List[str]					,       _snake_case       :							Dict					,       _snake_case       :							Optional[Any]					,       _snake_case       :							Optional[Any]					,       _snake_case       :							List[str]       ):






								"""simple docstring"""


								A__	  =     self.num_labels
								A__	  =     MobileNetVaForImageClassification(_snake_case       )
								model.to(_snake_case       )
								model.eval()
								A__	  =     model(_snake_case					,       labels=_snake_case       )
								self.parent.assertEqual(result.logits.shape					,       (self.batch_size, self.num_labels)       )



					def      _a				(  self       :							Optional[Any]					,       _snake_case       :							List[str]					,       _snake_case       :							Optional[int]					,       _snake_case       :							Tuple					,       _snake_case       :							Optional[Any]       ):






								"""simple docstring"""


								A__	  =     self.num_labels
								A__	  =     MobileNetVaForSemanticSegmentation(_snake_case       )
								model.to(_snake_case       )
								model.eval()
								A__	  =     model(_snake_case       )
								self.parent.assertEqual(
								    result.logits.shape					,       (
								        self.batch_size,
								        self.num_labels,
								        self.image_size // self.output_stride,
								        self.image_size // self.output_stride,
								    )					,       )
								A__	  =     model(_snake_case					,       labels=_snake_case       )
								self.parent.assertEqual(
								    result.logits.shape					,       (
								        self.batch_size,
								        self.num_labels,
								        self.image_size // self.output_stride,
								        self.image_size // self.output_stride,
								    )					,       )



					def      _a				(  self       :							Optional[Any]       ):






								"""simple docstring"""


								A__	  =     self.prepare_config_and_inputs()
								A__  ,			A__  ,			A__  ,			A__	  =     config_and_inputs
								A__	  =     {'pixel_values': pixel_values}
								return config, inputs_dict







@require_torch
class   __lowerCAmelCase  (		UpperCAmelCase_				,  UpperCAmelCase_				,  unittest.TestCase   ):
					"""simple docstring"""






					A__					:		Optional[Any]      							=     (
					    (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation)
					    if is_torch_available()
					    else ()
					)
					A__					:		str      							=     (
					    {
					        "feature-extraction": MobileNetVaModel,
					        "image-classification": MobileNetVaForImageClassification,
					        "image-segmentation": MobileNetVaForSemanticSegmentation,
					    }
					    if is_torch_available()
					    else {}
					)

					A__					:		List[Any]      							=     False
					A__					:		Any      							=     False
					A__					:		Optional[Any]      							=     False
					A__					:		str      							=     False



					def      _a				(  self       :							List[str]       ):






								"""simple docstring"""


								A__	  =     MobileNetVaModelTester(self       )
								A__	  =     MobileNetVaConfigTester(self					,       config_class=_snake_case					,       has_text_modality=_snake_case       )



					def      _a				(  self       :							Union[str, Any]       ):






								"""simple docstring"""


								self.config_tester.run_common_tests()



					@unittest.skip(reason='MobileNetV2 does not use inputs_embeds'       )
					def      _a				(  self       :							Optional[Any]       ):






								"""simple docstring"""


								pass



					@unittest.skip(reason='MobileNetV2 does not support input and output embeddings'       )
					def      _a				(  self       :							List[str]       ):






								"""simple docstring"""


								pass



					@unittest.skip(reason='MobileNetV2 does not output attentions'       )
					def      _a				(  self       :							Any       ):






								"""simple docstring"""


								pass



					def      _a				(  self       :							str       ):






								"""simple docstring"""


								A__  ,			A__	  =     self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
											A__	  =     model_class(_snake_case       )
											A__	  =     inspect.signature(model.forward       )
											# signature.parameters is an OrderedDict => so arg_names order is deterministic
											A__	  =     [*signature.parameters.keys()]

											A__	  =     ['pixel_values']
											self.assertListEqual(arg_names[:1]					,       _snake_case       )



					def      _a				(  self       :							str       ):






								"""simple docstring"""


								A__	  =     self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*_snake_case       )



					def      _a				(  self       :							str       ):






								"""simple docstring"""


								def check_hidden_states_output(_snake_case       :							Any					,       _snake_case       :							List[str]					,       _snake_case       :							Any       ):
											A__	  =     model_class(_snake_case       )
											model.to(_snake_case       )
											model.eval()

											with torch.no_grad():
														A__	  =     model(**self._prepare_for_class(_snake_case					,       _snake_case       )       )

											A__	  =     outputs.hidden_states

											A__	  =     16
											self.assertEqual(len(_snake_case       )					,       _snake_case       )

								A__  ,			A__	  =     self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
											A__	  =     True
											check_hidden_states_output(_snake_case					,       _snake_case					,       _snake_case       )

											# check that output_hidden_states also work using config
											del inputs_dict["output_hidden_states"]
											A__	  =     True

											check_hidden_states_output(_snake_case					,       _snake_case					,       _snake_case       )



					def      _a				(  self       :							Tuple       ):






								"""simple docstring"""


								A__	  =     self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*_snake_case       )



					def      _a				(  self       :							List[str]       ):






								"""simple docstring"""


								A__	  =     self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_semantic_segmentation(*_snake_case       )



					@slow
					def      _a				(  self       :							Dict       ):






								"""simple docstring"""


								for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											A__	  =     MobileNetVaModel.from_pretrained(_snake_case       )
											self.assertIsNotNone(_snake_case       )







def A    (							)    ->   Tuple:
			A__	  =     Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'       )
			return image






@require_torch
@require_vision
class   __lowerCAmelCase  (		unittest.TestCase   ):
					"""simple docstring"""






					@cached_property
					def      _a				(  self       :							Optional[int]       ):






								"""simple docstring"""


								return (
								    MobileNetVaImageProcessor.from_pretrained('google/mobilenet_v2_1.0_224'       ) if is_vision_available() else None
								)



					@slow
					def      _a				(  self       :							Any       ):






								"""simple docstring"""


								A__	  =     MobileNetVaForImageClassification.from_pretrained('google/mobilenet_v2_1.0_224'       ).to(_snake_case       )

								A__	  =     self.default_image_processor
								A__	  =     prepare_img()
								A__	  =     image_processor(images=_snake_case					,       return_tensors='pt'       ).to(_snake_case       )

								# forward pass
								with torch.no_grad():
											A__	  =     model(**_snake_case       )

								# verify the logits
								A__	  =     torch.Size((1, 10_01)       )
								self.assertEqual(outputs.logits.shape					,       _snake_case       )

								A__	  =     torch.tensor([0.2445, -1.1993, 0.1905]       ).to(_snake_case       )

								self.assertTrue(torch.allclose(outputs.logits[0, :3]					,       _snake_case					,       atol=1E-4       )       )



					@slow
					def      _a				(  self       :							List[Any]       ):






								"""simple docstring"""


								A__	  =     MobileNetVaForSemanticSegmentation.from_pretrained('google/deeplabv3_mobilenet_v2_1.0_513'       )
								A__	  =     model.to(_snake_case       )

								A__	  =     MobileNetVaImageProcessor.from_pretrained('google/deeplabv3_mobilenet_v2_1.0_513'       )

								A__	  =     prepare_img()
								A__	  =     image_processor(images=_snake_case					,       return_tensors='pt'       ).to(_snake_case       )

								# forward pass
								with torch.no_grad():
											A__	  =     model(**_snake_case       )
								A__	  =     outputs.logits

								# verify the logits
								A__	  =     torch.Size((1, 21, 65, 65)       )
								self.assertEqual(logits.shape					,       _snake_case       )

								A__	  =     torch.tensor(
								    [
								        [[17.5790, 17.7581, 18.3355], [18.3257, 18.4230, 18.8973], [18.6169, 18.8650, 19.2187]],
								        [[-2.1595, -2.0977, -2.3741], [-2.4226, -2.3028, -2.6835], [-2.7819, -2.5991, -2.7706]],
								        [[4.2058, 4.8317, 4.7638], [4.4136, 5.0361, 4.9383], [4.5028, 4.9644, 4.8734]],
								    ]					,       device=_snake_case					,       )

								self.assertTrue(torch.allclose(logits[0, :3, :3, :3]					,       _snake_case					,       atol=1E-4       )       )





import unittest

from transformers import MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING, AutoTokenizer, is_vision_available
from transformers.pipelines import pipeline
from transformers.pipelines.document_question_answering import apply_tesseract
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_detectrona,
    require_pytesseract,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
				from PIL import Image

				from transformers.image_utils import load_image
else:





				class     lowerCAmelCase__     :


						'''simple docstring'''






						@staticmethod
						def   _snake_case       (       *snake_case__     :     Optional[int]     ,  **snake_case__     :     Tuple		)      ->	Optional[Any]:
								pass



				def 	lowerCamelCase					(	UpperCamelCase     :				str							)   ->    Tuple:
						return None


# This is a pinned image from a specific revision of a document question answering space, hosted by HuggingFace,
# so we can expect it to be available.
A						=			(
    'https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png'
)





@is_pipeline_test
@require_torch
@require_vision
class     lowerCAmelCase__     (				unittest.TestCase			):


		'''simple docstring'''






		lowerCAmelCase_     =   MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING
		@require_pytesseract
		@require_vision
		def   _snake_case       (       self     :     str     ,  snake_case__     :     Optional[Any]     ,  snake_case__     :     Any     ,  snake_case__     :     Union[str, Any]		)      ->	Dict:
				_lowerCamelCase				   =  pipeline(
				    'document-question-answering'     ,  model=snake_case__     ,  tokenizer=snake_case__     ,  image_processor=snake_case__		)

				_lowerCamelCase				   =  INVOICE_URL
				_lowerCamelCase				   =  list(zip(*apply_tesseract(load_image(snake_case__		)     ,  snake_case__     ,  ''		)		)		)
				_lowerCamelCase				   =  'What is the placebo?'
				_lowerCamelCase				   =  [
				    {
				        'image': load_image(snake_case__		),
				        'question': question,
				    },
				    {
				        'image': image,
				        'question': question,
				    },
				    {
				        'image': image,
				        'question': question,
				        'word_boxes': word_boxes,
				    },
				]
				return dqa_pipeline, examples
		def   _snake_case       (       self     :     Union[str, Any]     ,  snake_case__     :     Union[str, Any]     ,  snake_case__     :     List[Any]		)      ->	Tuple:
				_lowerCamelCase				   =  dqa_pipeline(snake_case__     ,  top_k=2		)
				self.assertEqual(
				    snake_case__     ,  [
				        [
				            {'score': ANY(snake_case__		), 'answer': ANY(snake_case__		), 'start': ANY(snake_case__		), 'end': ANY(snake_case__		)},
				            {'score': ANY(snake_case__		), 'answer': ANY(snake_case__		), 'start': ANY(snake_case__		), 'end': ANY(snake_case__		)},
				        ]
				    ]
				    * 3     ,  )
		@require_torch
		@require_detectrona
		@require_pytesseract
		def   _snake_case       (       self     :     Union[str, Any]		)      ->	Optional[int]:
				_lowerCamelCase				   =  pipeline('document-question-answering'     ,  model='hf-internal-testing/tiny-random-layoutlmv2'		)
				_lowerCamelCase				   =  INVOICE_URL
				_lowerCamelCase				   =  'How many cats are there?'

				_lowerCamelCase				   =  [
				    {'score': 0.0001, 'answer': 'oy 2312/2019', 'start': 3_8, 'end': 3_9},
				    {'score': 0.0001, 'answer': 'oy 2312/2019 DUE', 'start': 3_8, 'end': 4_0},
				]
				_lowerCamelCase				   =  dqa_pipeline(image=snake_case__     ,  question=snake_case__     ,  top_k=2		)
				self.assertEqual(nested_simplify(snake_case__     ,  decimals=4		)     ,  snake_case__		)

				_lowerCamelCase				   =  dqa_pipeline({'image': image, 'question': question}     ,  top_k=2		)
				self.assertEqual(nested_simplify(snake_case__     ,  decimals=4		)     ,  snake_case__		)

				# This image does not detect ANY text in it, meaning layoutlmv2 should fail.
				# Empty answer probably
				_lowerCamelCase				   =  './tests/fixtures/tests_samples/COCO/000000039769.png'
				_lowerCamelCase				   =  dqa_pipeline(image=snake_case__     ,  question=snake_case__     ,  top_k=2		)
				self.assertEqual(snake_case__     ,  []		)

				# We can optionnally pass directly the words and bounding boxes
				_lowerCamelCase				   =  './tests/fixtures/tests_samples/COCO/000000039769.png'
				_lowerCamelCase				   =  []
				_lowerCamelCase				   =  []
				_lowerCamelCase				   =  dqa_pipeline(image=snake_case__     ,  question=snake_case__     ,  words=snake_case__     ,  boxes=snake_case__     ,  top_k=2		)
				self.assertEqual(snake_case__     ,  []		)
		@slow
		@require_torch
		@require_detectrona
		@require_pytesseract
		def   _snake_case       (       self     :     Optional[int]		)      ->	List[Any]:
				_lowerCamelCase				   =  pipeline(
				    'document-question-answering'     ,  model='tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'     ,  revision='9977165'     ,  )
				_lowerCamelCase				   =  INVOICE_URL
				_lowerCamelCase				   =  'What is the invoice number?'

				_lowerCamelCase				   =  dqa_pipeline(image=snake_case__     ,  question=snake_case__     ,  top_k=2		)
				self.assertEqual(
				    nested_simplify(snake_case__     ,  decimals=4		)     ,  [
				        {'score': 0.9944, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
				        {'score': 0.0009, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
				    ]     ,  )

				_lowerCamelCase				   =  dqa_pipeline({'image': image, 'question': question}     ,  top_k=2		)
				self.assertEqual(
				    nested_simplify(snake_case__     ,  decimals=4		)     ,  [
				        {'score': 0.9944, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
				        {'score': 0.0009, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
				    ]     ,  )

				_lowerCamelCase				   =  dqa_pipeline(
				    [{'image': image, 'question': question}, {'image': image, 'question': question}]     ,  top_k=2		)
				self.assertEqual(
				    nested_simplify(snake_case__     ,  decimals=4		)     ,  [
				        [
				            {'score': 0.9944, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
				            {'score': 0.0009, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
				        ],
				    ]
				    * 2     ,  )
		@slow
		@require_torch
		@require_detectrona
		@require_pytesseract
		def   _snake_case       (       self     :     int		)      ->	Optional[Any]:
				_lowerCamelCase				   =  pipeline(
				    'document-question-answering'     ,  model='tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'     ,  revision='9977165'     ,  max_seq_len=5_0     ,  )
				_lowerCamelCase				   =  INVOICE_URL
				_lowerCamelCase				   =  'What is the invoice number?'

				_lowerCamelCase				   =  dqa_pipeline(image=snake_case__     ,  question=snake_case__     ,  top_k=2		)
				self.assertEqual(
				    nested_simplify(snake_case__     ,  decimals=4		)     ,  [
				        {'score': 0.9974, 'answer': '1110212019', 'start': 2_3, 'end': 2_3},
				        {'score': 0.9948, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
				    ]     ,  )

				_lowerCamelCase				   =  dqa_pipeline({'image': image, 'question': question}     ,  top_k=2		)
				self.assertEqual(
				    nested_simplify(snake_case__     ,  decimals=4		)     ,  [
				        {'score': 0.9974, 'answer': '1110212019', 'start': 2_3, 'end': 2_3},
				        {'score': 0.9948, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
				    ]     ,  )

				_lowerCamelCase				   =  dqa_pipeline(
				    [{'image': image, 'question': question}, {'image': image, 'question': question}]     ,  top_k=2		)
				self.assertEqual(
				    nested_simplify(snake_case__     ,  decimals=4		)     ,  [
				        [
				            {'score': 0.9974, 'answer': '1110212019', 'start': 2_3, 'end': 2_3},
				            {'score': 0.9948, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
				        ]
				    ]
				    * 2     ,  )
		@slow
		@require_torch
		@require_pytesseract
		@require_vision
		def   _snake_case       (       self     :     int		)      ->	List[Any]:
				_lowerCamelCase				   =  AutoTokenizer.from_pretrained(
				    'impira/layoutlm-document-qa'     ,  revision='3dc6de3'     ,  add_prefix_space=snake_case__		)
				_lowerCamelCase				   =  pipeline(
				    'document-question-answering'     ,  model='impira/layoutlm-document-qa'     ,  tokenizer=snake_case__     ,  revision='3dc6de3'     ,  )
				_lowerCamelCase				   =  INVOICE_URL
				_lowerCamelCase				   =  'What is the invoice number?'

				_lowerCamelCase				   =  dqa_pipeline(image=snake_case__     ,  question=snake_case__     ,  top_k=2		)
				self.assertEqual(
				    nested_simplify(snake_case__     ,  decimals=4		)     ,  [
				        {'score': 0.4251, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
				        {'score': 0.0819, 'answer': '1110212019', 'start': 2_3, 'end': 2_3},
				    ]     ,  )

				_lowerCamelCase				   =  dqa_pipeline({'image': image, 'question': question}     ,  top_k=2		)
				self.assertEqual(
				    nested_simplify(snake_case__     ,  decimals=4		)     ,  [
				        {'score': 0.4251, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
				        {'score': 0.0819, 'answer': '1110212019', 'start': 2_3, 'end': 2_3},
				    ]     ,  )

				_lowerCamelCase				   =  dqa_pipeline(
				    [{'image': image, 'question': question}, {'image': image, 'question': question}]     ,  top_k=2		)
				self.assertEqual(
				    nested_simplify(snake_case__     ,  decimals=4		)     ,  [
				        [
				            {'score': 0.4251, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
				            {'score': 0.0819, 'answer': '1110212019', 'start': 2_3, 'end': 2_3},
				        ]
				    ]
				    * 2     ,  )

				_lowerCamelCase				   =  list(zip(*apply_tesseract(load_image(snake_case__		)     ,  snake_case__     ,  ''		)		)		)

				# This model should also work if `image` is set to None
				_lowerCamelCase				   =  dqa_pipeline({'image': None, 'word_boxes': word_boxes, 'question': question}     ,  top_k=2		)
				self.assertEqual(
				    nested_simplify(snake_case__     ,  decimals=4		)     ,  [
				        {'score': 0.4251, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
				        {'score': 0.0819, 'answer': '1110212019', 'start': 2_3, 'end': 2_3},
				    ]     ,  )
		@slow
		@require_torch
		@require_pytesseract
		@require_vision
		def   _snake_case       (       self     :     Dict		)      ->	List[str]:
				_lowerCamelCase				   =  AutoTokenizer.from_pretrained(
				    'impira/layoutlm-document-qa'     ,  revision='3dc6de3'     ,  add_prefix_space=snake_case__		)
				_lowerCamelCase				   =  pipeline(
				    'document-question-answering'     ,  model='impira/layoutlm-document-qa'     ,  tokenizer=snake_case__     ,  revision='3dc6de3'     ,  max_seq_len=5_0     ,  )
				_lowerCamelCase				   =  INVOICE_URL
				_lowerCamelCase				   =  'What is the invoice number?'

				_lowerCamelCase				   =  dqa_pipeline(image=snake_case__     ,  question=snake_case__     ,  top_k=2		)
				self.assertEqual(
				    nested_simplify(snake_case__     ,  decimals=4		)     ,  [
				        {'score': 0.9999, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
				        {'score': 0.9998, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
				    ]     ,  )

				_lowerCamelCase				   =  dqa_pipeline(
				    [{'image': image, 'question': question}, {'image': image, 'question': question}]     ,  top_k=2		)
				self.assertEqual(
				    nested_simplify(snake_case__     ,  decimals=4		)     ,  [
				        [
				            {'score': 0.9999, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
				            {'score': 0.9998, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
				        ]
				    ]
				    * 2     ,  )

				_lowerCamelCase				   =  list(zip(*apply_tesseract(load_image(snake_case__		)     ,  snake_case__     ,  ''		)		)		)

				# This model should also work if `image` is set to None
				_lowerCamelCase				   =  dqa_pipeline({'image': None, 'word_boxes': word_boxes, 'question': question}     ,  top_k=2		)
				self.assertEqual(
				    nested_simplify(snake_case__     ,  decimals=4		)     ,  [
				        {'score': 0.9999, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
				        {'score': 0.9998, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
				    ]     ,  )
		@slow
		@require_torch
		def   _snake_case       (       self     :     Dict		)      ->	int:
				_lowerCamelCase				   =  pipeline(
				    'document-question-answering'     ,  model='naver-clova-ix/donut-base-finetuned-docvqa'     ,  tokenizer=AutoTokenizer.from_pretrained('naver-clova-ix/donut-base-finetuned-docvqa'		)     ,  feature_extractor='naver-clova-ix/donut-base-finetuned-docvqa'     ,  )

				_lowerCamelCase				   =  INVOICE_URL
				_lowerCamelCase				   =  'What is the invoice number?'
				_lowerCamelCase				   =  dqa_pipeline(image=snake_case__     ,  question=snake_case__     ,  top_k=2		)
				self.assertEqual(nested_simplify(snake_case__     ,  decimals=4		)     ,  [{'answer': 'us-001'}]		)



		@require_tf
		@unittest.skip('Document question answering not implemented in TF'		)
		def   _snake_case       (       self     :     Optional[Any]		)      ->	Any:
				pass