






import unittest

from transformers import DebertaVaConfig, is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

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


if is_torch_available():
			import torch

			from transformers import (
			    DebertaVaForMaskedLM,
			    DebertaVaForMultipleChoice,
			    DebertaVaForQuestionAnswering,
			    DebertaVaForSequenceClassification,
			    DebertaVaForTokenClassification,
			    DebertaVaModel,
			)
			from transformers.models.deberta_va.modeling_deberta_va import DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST






class _UpperCamelCase							(							lowercase_   ):
			'''simple docstring'''







			def __init__(			self							: Any      ,      a							: str      ,      a							: Dict=13      ,      a							: List[str]=7      ,      a							: Optional[int]=True      ,      a							: Optional[Any]=True      ,      a							: Tuple=True      ,      a							: List[Any]=True      ,      a							: Union[str, Any]=99      ,      a							: Tuple=32      ,      a							: Any=5      ,      a							: List[str]=4      ,      a							: Tuple=37      ,      a							: Optional[Any]="gelu"      ,      a							: List[Any]=0.1      ,      a							: int=0.1      ,      a							: Tuple=512      ,      a							: Tuple=16      ,      a							: Optional[int]=2      ,      a							: Optional[int]=0.02      ,      a							: int=False      ,      a							: List[str]=True      ,      a							: Tuple="None"      ,      a							: Optional[int]=3      ,      a							: Any=4      ,      a							: List[Any]=None      ,      )							->       List[str]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Any				     =       parent
				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       batch_size
				SCREAMING_SNAKE_CASE   :      int				     =       seq_length
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       is_training
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       use_input_mask
				SCREAMING_SNAKE_CASE   :      str				     =       use_token_type_ids
				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       use_labels
				SCREAMING_SNAKE_CASE   :      Dict				     =       vocab_size
				SCREAMING_SNAKE_CASE   :      List[str]				     =       hidden_size
				SCREAMING_SNAKE_CASE   :      Any				     =       num_hidden_layers
				SCREAMING_SNAKE_CASE   :      Tuple				     =       num_attention_heads
				SCREAMING_SNAKE_CASE   :      Dict				     =       intermediate_size
				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       hidden_act
				SCREAMING_SNAKE_CASE   :      Dict				     =       hidden_dropout_prob
				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       attention_probs_dropout_prob
				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       max_position_embeddings
				SCREAMING_SNAKE_CASE   :      int				     =       type_vocab_size
				SCREAMING_SNAKE_CASE   :      int				     =       type_sequence_label_size
				SCREAMING_SNAKE_CASE   :      Any				     =       initializer_range
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       num_labels
				SCREAMING_SNAKE_CASE   :      int				     =       num_choices
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       relative_attention
				SCREAMING_SNAKE_CASE   :      str				     =       position_biased_input
				SCREAMING_SNAKE_CASE   :      Any				     =       pos_att_type
				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       scope
			def 							__UpperCamelCase (			self							: Union[str, Any]	)							->       Any:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Dict				     =       ids_tensor([self.batch_size, self.seq_length]      ,      self.vocab_size	)

				SCREAMING_SNAKE_CASE   :      List[Any]				     =       None
				if self.use_input_mask:
					SCREAMING_SNAKE_CASE   :      Tuple				     =       ids_tensor([self.batch_size, self.seq_length]      ,      vocab_size=2	)

				SCREAMING_SNAKE_CASE   :      List[str]				     =       None
				if self.use_token_type_ids:
					SCREAMING_SNAKE_CASE   :      str				     =       ids_tensor([self.batch_size, self.seq_length]      ,      self.type_vocab_size	)

				SCREAMING_SNAKE_CASE   :      List[Any]				     =       None
				SCREAMING_SNAKE_CASE   :      Any				     =       None
				SCREAMING_SNAKE_CASE   :      Any				     =       None
				if self.use_labels:
					SCREAMING_SNAKE_CASE   :      List[Any]				     =       ids_tensor([self.batch_size]      ,      self.type_sequence_label_size	)
					SCREAMING_SNAKE_CASE   :      List[Any]				     =       ids_tensor([self.batch_size, self.seq_length]      ,      self.num_labels	)
					SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       ids_tensor([self.batch_size]      ,      self.num_choices	)

				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       self.get_config()

				return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
			def 							__UpperCamelCase (			self							: Dict	)							->       List[Any]:

				"""simple docstring"""

				return DebertaVaConfig(
				    vocab_size=self.vocab_size      ,      hidden_size=self.hidden_size      ,      num_hidden_layers=self.num_hidden_layers      ,      num_attention_heads=self.num_attention_heads      ,      intermediate_size=self.intermediate_size      ,      hidden_act=self.hidden_act      ,      hidden_dropout_prob=self.hidden_dropout_prob      ,      attention_probs_dropout_prob=self.attention_probs_dropout_prob      ,      max_position_embeddings=self.max_position_embeddings      ,      type_vocab_size=self.type_vocab_size      ,      initializer_range=self.initializer_range      ,      relative_attention=self.relative_attention      ,      position_biased_input=self.position_biased_input      ,      pos_att_type=self.pos_att_type      ,      )
			def 							__UpperCamelCase (			self							: Optional[int]      ,      a							: List[Any]	)							->       Any:

				"""simple docstring"""

				self.parent.assertListEqual(list(result.loss.size()	)      ,      []	)
			def 							__UpperCamelCase (			self							: Union[str, Any]      ,      a							: Optional[Any]      ,      a							: Any      ,      a							: List[Any]      ,      a							: Tuple      ,      a							: Dict      ,      a							: List[str]      ,      a							: Tuple	)							->       Dict:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       DebertaVaModel(config=a	)
				model.to(a	)
				model.eval()
				SCREAMING_SNAKE_CASE   :      int				     =       model(a      ,      attention_mask=a      ,      token_type_ids=a	)[0]
				SCREAMING_SNAKE_CASE   :      int				     =       model(a      ,      token_type_ids=a	)[0]
				SCREAMING_SNAKE_CASE   :      Dict				     =       model(a	)[0]

				self.parent.assertListEqual(list(sequence_output.size()	)      ,      [self.batch_size, self.seq_length, self.hidden_size]	)
			def 							__UpperCamelCase (			self							: int      ,      a							: Optional[Any]      ,      a							: int      ,      a							: Any      ,      a							: int      ,      a							: Dict      ,      a							: int      ,      a							: Any	)							->       Tuple:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       DebertaVaForMaskedLM(config=a	)
				model.to(a	)
				model.eval()
				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       model(a      ,      attention_mask=a      ,      token_type_ids=a      ,      labels=a	)

				self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.seq_length, self.vocab_size)	)
			def 							__UpperCamelCase (			self							: Tuple      ,      a							: Optional[int]      ,      a							: str      ,      a							: str      ,      a							: int      ,      a							: Optional[Any]      ,      a							: int      ,      a							: Optional[Any]	)							->       Union[str, Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       self.num_labels
				SCREAMING_SNAKE_CASE   :      Dict				     =       DebertaVaForSequenceClassification(a	)
				model.to(a	)
				model.eval()
				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       model(a      ,      attention_mask=a      ,      token_type_ids=a      ,      labels=a	)
				self.parent.assertListEqual(list(result.logits.size()	)      ,      [self.batch_size, self.num_labels]	)
				self.check_loss_output(a	)
			def 							__UpperCamelCase (			self							: Optional[Any]      ,      a							: List[str]      ,      a							: List[Any]      ,      a							: str      ,      a							: str      ,      a							: List[str]      ,      a							: List[Any]      ,      a							: Optional[Any]	)							->       List[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       self.num_labels
				SCREAMING_SNAKE_CASE   :      Any				     =       DebertaVaForTokenClassification(config=a	)
				model.to(a	)
				model.eval()
				SCREAMING_SNAKE_CASE   :      Tuple				     =       model(a      ,      attention_mask=a      ,      token_type_ids=a      ,      labels=a	)
				self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.seq_length, self.num_labels)	)
			def 							__UpperCamelCase (			self							: List[str]      ,      a							: Optional[int]      ,      a							: int      ,      a							: Tuple      ,      a							: Any      ,      a							: Optional[int]      ,      a							: str      ,      a							: str	)							->       Union[str, Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Dict				     =       DebertaVaForQuestionAnswering(config=a	)
				model.to(a	)
				model.eval()
				SCREAMING_SNAKE_CASE   :      str				     =       model(
				    a      ,      attention_mask=a      ,      token_type_ids=a      ,      start_positions=a      ,      end_positions=a      ,      )
				self.parent.assertEqual(result.start_logits.shape      ,      (self.batch_size, self.seq_length)	)
				self.parent.assertEqual(result.end_logits.shape      ,      (self.batch_size, self.seq_length)	)
			def 							__UpperCamelCase (			self							: Optional[Any]      ,      a							: List[Any]      ,      a							: Union[str, Any]      ,      a							: Union[str, Any]      ,      a							: Optional[Any]      ,      a							: Tuple      ,      a							: Optional[Any]      ,      a							: str	)							->       Optional[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       DebertaVaForMultipleChoice(config=a	)
				model.to(a	)
				model.eval()
				SCREAMING_SNAKE_CASE   :      int				     =       input_ids.unsqueeze(1	).expand(-1      ,      self.num_choices      ,      -1	).contiguous()
				SCREAMING_SNAKE_CASE   :      str				     =       token_type_ids.unsqueeze(1	).expand(-1      ,      self.num_choices      ,      -1	).contiguous()
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       input_mask.unsqueeze(1	).expand(-1      ,      self.num_choices      ,      -1	).contiguous()
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       model(
				    a      ,      attention_mask=a      ,      token_type_ids=a      ,      labels=a      ,      )
				self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.num_choices)	)






			def 							__UpperCamelCase (			self							: Tuple	)							->       str:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      List[str]				     =       self.prepare_config_and_inputs()
				(
				    (
				    SCREAMING_SNAKE_CASE					
				)     ,(
				    SCREAMING_SNAKE_CASE					
				)     ,(
				    SCREAMING_SNAKE_CASE					
				)     ,(
				    SCREAMING_SNAKE_CASE					
				)     ,(
				    SCREAMING_SNAKE_CASE					
				)     ,(
				    SCREAMING_SNAKE_CASE					
				)     ,(
				    SCREAMING_SNAKE_CASE					
				)     ,					
				)   :      List[Any]				     =       config_and_inputs
				SCREAMING_SNAKE_CASE   :      Dict				     =       {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
				return config, inputs_dict






@require_torch
class _UpperCamelCase							(							lowercase_							,	lowercase_							,	unittest.TestCase   ):
			'''simple docstring'''







			lowerCamelCase__           =(
			    (
			        DebertaVaModel,
			        DebertaVaForMaskedLM,
			        DebertaVaForSequenceClassification,
			        DebertaVaForTokenClassification,
			        DebertaVaForQuestionAnswering,
			        DebertaVaForMultipleChoice,
			    )
			    if is_torch_available()
			    else ()
			)
			lowerCamelCase__           =(
			    {
			        '''feature-extraction''': DebertaVaModel,
			        '''fill-mask''': DebertaVaForMaskedLM,
			        '''question-answering''': DebertaVaForQuestionAnswering,
			        '''text-classification''': DebertaVaForSequenceClassification,
			        '''token-classification''': DebertaVaForTokenClassification,
			        '''zero-shot''': DebertaVaForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)

			lowerCamelCase__           =True
			lowerCamelCase__           =False
			lowerCamelCase__           =False
			lowerCamelCase__           =False
			lowerCamelCase__           =False
			def 							__UpperCamelCase (			self							: int	)							->       str:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      int				     =       DebertaVaModelTester(self	)
				SCREAMING_SNAKE_CASE   :      int				     =       ConfigTester(self      ,      config_class=a      ,      hidden_size=37	)
			def 							__UpperCamelCase (			self							: Optional[int]	)							->       int:

				"""simple docstring"""

				self.config_tester.run_common_tests()
			def 							__UpperCamelCase (			self							: str	)							->       Tuple:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      List[Any]				     =       self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_deberta_model(*a	)
			def 							__UpperCamelCase (			self							: Tuple	)							->       List[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      List[str]				     =       self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_deberta_for_sequence_classification(*a	)
			def 							__UpperCamelCase (			self							: str	)							->       Optional[int]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      List[str]				     =       self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_deberta_for_masked_lm(*a	)
			def 							__UpperCamelCase (			self							: Union[str, Any]	)							->       int:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      List[Any]				     =       self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_deberta_for_question_answering(*a	)
			def 							__UpperCamelCase (			self							: str	)							->       Dict:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_deberta_for_token_classification(*a	)
			def 							__UpperCamelCase (			self							: Any	)							->       Tuple:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Dict				     =       self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_deberta_for_multiple_choice(*a	)






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

				"""simple docstring"""

				for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					SCREAMING_SNAKE_CASE   :      str				     =       DebertaVaModel.from_pretrained(a	)
					self.assertIsNotNone(a	)






@require_torch
@require_sentencepiece
@require_tokenizers
class _UpperCamelCase							(							unittest.TestCase   ):
			'''simple docstring'''







			@unittest.skip(reason="Model not available yet"	)
			def 							__UpperCamelCase (			self							: Any	)							->       Tuple:

				"""simple docstring"""

				pass






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

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       DebertaVaModel.from_pretrained("microsoft/deberta-v2-xlarge"	)

				SCREAMING_SNAKE_CASE   :      Any				     =       torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]	)
				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]	)
				with torch.no_grad():
					SCREAMING_SNAKE_CASE   :      Any				     =       model(a      ,      attention_mask=a	)[0]
				# compare the actual values for a slice.
				SCREAMING_SNAKE_CASE   :      Dict				     =       torch.tensor(
				    [[[0.2356, 0.1948, 0.0369], [-0.1063, 0.3586, -0.5152], [-0.6399, -0.0259, -0.2525]]]	)
				self.assertTrue(torch.allclose(output[:, 1:4, 1:4]      ,      a      ,      atol=1e-4	)      ,      F"{output[:, 1:4, 1: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 _UpperCamelCase							:
						'''simple docstring'''







						@staticmethod
						def 							__UpperCamelCase (			*a							: Any      ,      **a							: Optional[int]	)							->       Optional[Any]:

							"""simple docstring"""

							pass

			def  lowerCamelCase__ (					_a):
				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 _UpperCamelCase							(							unittest.TestCase   ):
			'''simple docstring'''







			lowerCamelCase__           =MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING
			@require_pytesseract
			@require_vision
			def 							__UpperCamelCase (			self							: Tuple      ,      a							: Union[str, Any]      ,      a							: Any      ,      a							: Optional[Any]	)							->       List[str]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      int				     =       pipeline(
				    "document-question-answering"      ,      model=a      ,      tokenizer=a      ,      image_processor=a	)

				SCREAMING_SNAKE_CASE   :      Dict				     =       INVOICE_URL
				SCREAMING_SNAKE_CASE   :      int				     =       list(zip(*apply_tesseract(load_image(a	)      ,      a      ,      ""	)	)	)
				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       "What is the placebo?"
				SCREAMING_SNAKE_CASE   :      List[str]				     =       [
				    {
				        "image": load_image(a	),
				        "question": question,
				    },
				    {
				        "image": image,
				        "question": question,
				    },
				    {
				        "image": image,
				        "question": question,
				        "word_boxes": word_boxes,
				    },
				]
				return dqa_pipeline, examples
			def 							__UpperCamelCase (			self							: List[str]      ,      a							: List[Any]      ,      a							: Any	)							->       Optional[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       dqa_pipeline(a      ,      top_k=2	)
				self.assertEqual(
				    a      ,      [
				        [
				            {"score": ANY(a	), "answer": ANY(a	), "start": ANY(a	), "end": ANY(a	)},
				            {"score": ANY(a	), "answer": ANY(a	), "start": ANY(a	), "end": ANY(a	)},
				        ]
				    ]
				    * 3      ,      )
			@require_torch
			@require_detectrona
			@require_pytesseract
			def 							__UpperCamelCase (			self							: Optional[int]	)							->       Optional[int]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      List[Any]				     =       pipeline("document-question-answering"      ,      model="hf-internal-testing/tiny-random-layoutlmv2"	)
				SCREAMING_SNAKE_CASE   :      int				     =       INVOICE_URL
				SCREAMING_SNAKE_CASE   :      int				     =       "How many cats are there?"

				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       [
				    {"score": 0.0001, "answer": "oy 2312/2019", "start": 38, "end": 39},
				    {"score": 0.0001, "answer": "oy 2312/2019 DUE", "start": 38, "end": 40},
				]
				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       dqa_pipeline(image=a      ,      question=a      ,      top_k=2	)
				self.assertEqual(nested_simplify(a      ,      decimals=4	)      ,      a	)

				SCREAMING_SNAKE_CASE   :      Any				     =       dqa_pipeline({"image": image, "question": question}      ,      top_k=2	)
				self.assertEqual(nested_simplify(a      ,      decimals=4	)      ,      a	)

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

				# We can optionnally pass directly the words and bounding boxes
				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       "./tests/fixtures/tests_samples/COCO/000000039769.png"
				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       []
				SCREAMING_SNAKE_CASE   :      int				     =       []
				SCREAMING_SNAKE_CASE   :      str				     =       dqa_pipeline(image=a      ,      question=a      ,      words=a      ,      boxes=a      ,      top_k=2	)
				self.assertEqual(a      ,      []	)
			@slow
			@require_torch
			@require_detectrona
			@require_pytesseract
			def 							__UpperCamelCase (			self							: Optional[int]	)							->       Union[str, Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      List[Any]				     =       pipeline(
				    "document-question-answering"      ,      model="tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"      ,      revision="9977165"      ,      )
				SCREAMING_SNAKE_CASE   :      Any				     =       INVOICE_URL
				SCREAMING_SNAKE_CASE   :      Any				     =       "What is the invoice number?"

				SCREAMING_SNAKE_CASE   :      Dict				     =       dqa_pipeline(image=a      ,      question=a      ,      top_k=2	)
				self.assertEqual(
				    nested_simplify(a      ,      decimals=4	)      ,      [
				        {"score": 0.9944, "answer": "us-001", "start": 16, "end": 16},
				        {"score": 0.0009, "answer": "us-001", "start": 16, "end": 16},
				    ]      ,      )

				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       dqa_pipeline({"image": image, "question": question}      ,      top_k=2	)
				self.assertEqual(
				    nested_simplify(a      ,      decimals=4	)      ,      [
				        {"score": 0.9944, "answer": "us-001", "start": 16, "end": 16},
				        {"score": 0.0009, "answer": "us-001", "start": 16, "end": 16},
				    ]      ,      )

				SCREAMING_SNAKE_CASE   :      Any				     =       dqa_pipeline(
				    [{"image": image, "question": question}, {"image": image, "question": question}]      ,      top_k=2	)
				self.assertEqual(
				    nested_simplify(a      ,      decimals=4	)      ,      [
				        [
				            {"score": 0.9944, "answer": "us-001", "start": 16, "end": 16},
				            {"score": 0.0009, "answer": "us-001", "start": 16, "end": 16},
				        ],
				    ]
				    * 2      ,      )
			@slow
			@require_torch
			@require_detectrona
			@require_pytesseract
			def 							__UpperCamelCase (			self							: Tuple	)							->       Union[str, Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       pipeline(
				    "document-question-answering"      ,      model="tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"      ,      revision="9977165"      ,      max_seq_len=50      ,      )
				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       INVOICE_URL
				SCREAMING_SNAKE_CASE   :      int				     =       "What is the invoice number?"

				SCREAMING_SNAKE_CASE   :      Any				     =       dqa_pipeline(image=a      ,      question=a      ,      top_k=2	)
				self.assertEqual(
				    nested_simplify(a      ,      decimals=4	)      ,      [
				        {"score": 0.9974, "answer": "1110212019", "start": 23, "end": 23},
				        {"score": 0.9948, "answer": "us-001", "start": 16, "end": 16},
				    ]      ,      )

				SCREAMING_SNAKE_CASE   :      Tuple				     =       dqa_pipeline({"image": image, "question": question}      ,      top_k=2	)
				self.assertEqual(
				    nested_simplify(a      ,      decimals=4	)      ,      [
				        {"score": 0.9974, "answer": "1110212019", "start": 23, "end": 23},
				        {"score": 0.9948, "answer": "us-001", "start": 16, "end": 16},
				    ]      ,      )

				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       dqa_pipeline(
				    [{"image": image, "question": question}, {"image": image, "question": question}]      ,      top_k=2	)
				self.assertEqual(
				    nested_simplify(a      ,      decimals=4	)      ,      [
				        [
				            {"score": 0.9974, "answer": "1110212019", "start": 23, "end": 23},
				            {"score": 0.9948, "answer": "us-001", "start": 16, "end": 16},
				        ]
				    ]
				    * 2      ,      )
			@slow
			@require_torch
			@require_pytesseract
			@require_vision
			def 							__UpperCamelCase (			self							: Union[str, Any]	)							->       Optional[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       AutoTokenizer.from_pretrained(
				    "impira/layoutlm-document-qa"      ,      revision="3dc6de3"      ,      add_prefix_space=a	)
				SCREAMING_SNAKE_CASE   :      List[str]				     =       pipeline(
				    "document-question-answering"      ,      model="impira/layoutlm-document-qa"      ,      tokenizer=a      ,      revision="3dc6de3"      ,      )
				SCREAMING_SNAKE_CASE   :      List[str]				     =       INVOICE_URL
				SCREAMING_SNAKE_CASE   :      int				     =       "What is the invoice number?"

				SCREAMING_SNAKE_CASE   :      str				     =       dqa_pipeline(image=a      ,      question=a      ,      top_k=2	)
				self.assertEqual(
				    nested_simplify(a      ,      decimals=4	)      ,      [
				        {"score": 0.4251, "answer": "us-001", "start": 16, "end": 16},
				        {"score": 0.0819, "answer": "1110212019", "start": 23, "end": 23},
				    ]      ,      )

				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       dqa_pipeline({"image": image, "question": question}      ,      top_k=2	)
				self.assertEqual(
				    nested_simplify(a      ,      decimals=4	)      ,      [
				        {"score": 0.4251, "answer": "us-001", "start": 16, "end": 16},
				        {"score": 0.0819, "answer": "1110212019", "start": 23, "end": 23},
				    ]      ,      )

				SCREAMING_SNAKE_CASE   :      Dict				     =       dqa_pipeline(
				    [{"image": image, "question": question}, {"image": image, "question": question}]      ,      top_k=2	)
				self.assertEqual(
				    nested_simplify(a      ,      decimals=4	)      ,      [
				        [
				            {"score": 0.4251, "answer": "us-001", "start": 16, "end": 16},
				            {"score": 0.0819, "answer": "1110212019", "start": 23, "end": 23},
				        ]
				    ]
				    * 2      ,      )

				SCREAMING_SNAKE_CASE   :      int				     =       list(zip(*apply_tesseract(load_image(a	)      ,      a      ,      ""	)	)	)

				# This model should also work if `image` is set to None
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question}      ,      top_k=2	)
				self.assertEqual(
				    nested_simplify(a      ,      decimals=4	)      ,      [
				        {"score": 0.4251, "answer": "us-001", "start": 16, "end": 16},
				        {"score": 0.0819, "answer": "1110212019", "start": 23, "end": 23},
				    ]      ,      )
			@slow
			@require_torch
			@require_pytesseract
			@require_vision
			def 							__UpperCamelCase (			self							: Tuple	)							->       List[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      int				     =       AutoTokenizer.from_pretrained(
				    "impira/layoutlm-document-qa"      ,      revision="3dc6de3"      ,      add_prefix_space=a	)
				SCREAMING_SNAKE_CASE   :      Dict				     =       pipeline(
				    "document-question-answering"      ,      model="impira/layoutlm-document-qa"      ,      tokenizer=a      ,      revision="3dc6de3"      ,      max_seq_len=50      ,      )
				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       INVOICE_URL
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       "What is the invoice number?"

				SCREAMING_SNAKE_CASE   :      Dict				     =       dqa_pipeline(image=a      ,      question=a      ,      top_k=2	)
				self.assertEqual(
				    nested_simplify(a      ,      decimals=4	)      ,      [
				        {"score": 0.9999, "answer": "us-001", "start": 16, "end": 16},
				        {"score": 0.9998, "answer": "us-001", "start": 16, "end": 16},
				    ]      ,      )

				SCREAMING_SNAKE_CASE   :      int				     =       dqa_pipeline(
				    [{"image": image, "question": question}, {"image": image, "question": question}]      ,      top_k=2	)
				self.assertEqual(
				    nested_simplify(a      ,      decimals=4	)      ,      [
				        [
				            {"score": 0.9999, "answer": "us-001", "start": 16, "end": 16},
				            {"score": 0.9998, "answer": "us-001", "start": 16, "end": 16},
				        ]
				    ]
				    * 2      ,      )

				SCREAMING_SNAKE_CASE   :      int				     =       list(zip(*apply_tesseract(load_image(a	)      ,      a      ,      ""	)	)	)

				# This model should also work if `image` is set to None
				SCREAMING_SNAKE_CASE   :      Any				     =       dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question}      ,      top_k=2	)
				self.assertEqual(
				    nested_simplify(a      ,      decimals=4	)      ,      [
				        {"score": 0.9999, "answer": "us-001", "start": 16, "end": 16},
				        {"score": 0.9998, "answer": "us-001", "start": 16, "end": 16},
				    ]      ,      )
			@slow
			@require_torch
			def 							__UpperCamelCase (			self							: str	)							->       int:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      List[str]				     =       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"      ,      )

				SCREAMING_SNAKE_CASE   :      int				     =       INVOICE_URL
				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       "What is the invoice number?"
				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       dqa_pipeline(image=a      ,      question=a      ,      top_k=2	)
				self.assertEqual(nested_simplify(a      ,      decimals=4	)      ,      [{"answer": "us-001"}]	)






			@require_tf
			@unittest.skip("Document question answering not implemented in TF"	)
			def 							__UpperCamelCase (			self							: List[str]	)							->       int:

				"""simple docstring"""

				pass