





'''simple docstring'''




import unittest

from parameterized import parameterized

from transformers import AutoTokenizer, GPTNeoXConfig, is_torch_available, set_seed
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
  import torch

  from transformers import (
      GPTNeoXForCausalLM,
      GPTNeoXForQuestionAnswering,
      GPTNeoXForSequenceClassification,
      GPTNeoXForTokenClassification,
      GPTNeoXModel,
  )


class 					SCREAMING_SNAKE_CASE       :


     def __init__(		self		,      _UpperCAmelCase		,      _UpperCAmelCase=13		,      _UpperCAmelCase=7		,      _UpperCAmelCase=True		,      _UpperCAmelCase=True		,      _UpperCAmelCase=True		,      _UpperCAmelCase=True		,      _UpperCAmelCase=99		,      _UpperCAmelCase=64		,      _UpperCAmelCase=5		,      _UpperCAmelCase=4		,      _UpperCAmelCase=37		,      _UpperCAmelCase="gelu"		,      _UpperCAmelCase=0.1		,      _UpperCAmelCase=0.1		,      _UpperCAmelCase=512		,      _UpperCAmelCase=16		,      _UpperCAmelCase=2		,      _UpperCAmelCase=0.02		,      _UpperCAmelCase=3		,      _UpperCAmelCase=4		,      _UpperCAmelCase=None		,      ):

          '''simple docstring'''






          __A		:					Optional[Any]						=			parent
          __A		:					int						=			batch_size
          __A		:					Optional[int]						=			seq_length
          __A		:					Union[str, Any]						=			is_training
          __A		:					Dict						=			use_input_mask
          __A		:					Optional[Any]						=			use_token_type_ids
          __A		:					int						=			use_labels
          __A		:					int						=			vocab_size
          __A		:					str						=			hidden_size
          __A		:					Optional[int]						=			num_hidden_layers
          __A		:					Any						=			num_attention_heads
          __A		:					Optional[Any]						=			intermediate_size
          __A		:					Tuple						=			hidden_act
          __A		:					Any						=			hidden_dropout_prob
          __A		:					Dict						=			attention_probs_dropout_prob
          __A		:					Any						=			max_position_embeddings
          __A		:					List[Any]						=			type_vocab_size
          __A		:					Optional[int]						=			type_sequence_label_size
          __A		:					str						=			initializer_range
          __A		:					Optional[Any]						=			num_labels
          __A		:					Any						=			num_choices
          __A		:					List[str]						=			scope
          __A		:					Optional[Any]						=			vocab_size - 1


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					int						=			ids_tensor([self.batch_size, self.seq_length]		,      self.vocab_size)

          __A		:					List[str]						=			None
          if self.use_input_mask:
               __A		:					str						=			random_attention_mask([self.batch_size, self.seq_length])

          __A		:					Union[str, Any]						=			None
          if self.use_labels:
               __A		:					Dict						=			ids_tensor([self.batch_size, self.seq_length]		,      self.num_labels)

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

          return config, input_ids, input_mask, token_labels


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          return GPTNeoXConfig(
              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		,      is_decoder=snake_case__		,      initializer_range=self.initializer_range		,      pad_token_id=self.pad_token_id		,      )


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A					,__A					,__A					,__A		:					Tuple						=			self.prepare_config_and_inputs()

          __A		:					List[Any]						=			True

          return config, input_ids, input_mask, token_labels


     def 				SCREAMING_SNAKE_CASE    (		self		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase):

          '''simple docstring'''






          __A		:					Union[str, Any]						=			GPTNeoXModel(config=snake_case__)
          model.to(snake_case__)
          model.eval()
          __A		:					Any						=			model(snake_case__		,      attention_mask=snake_case__)
          __A		:					List[str]						=			model(snake_case__)
          self.parent.assertEqual(result.last_hidden_state.shape		,      (self.batch_size, self.seq_length, self.hidden_size))


     def 				SCREAMING_SNAKE_CASE    (		self		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase):

          '''simple docstring'''






          __A		:					Any						=			True
          __A		:					int						=			GPTNeoXModel(snake_case__)
          model.to(snake_case__)
          model.eval()
          __A		:					Dict						=			model(snake_case__		,      attention_mask=snake_case__)
          self.parent.assertEqual(result.last_hidden_state.shape		,      (self.batch_size, self.seq_length, self.hidden_size))


     def 				SCREAMING_SNAKE_CASE    (		self		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase):

          '''simple docstring'''






          __A		:					Union[str, Any]						=			GPTNeoXForCausalLM(config=snake_case__)
          model.to(snake_case__)
          model.eval()
          __A		:					Any						=			model(snake_case__		,      attention_mask=snake_case__		,      labels=snake_case__)
          self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.seq_length, self.vocab_size))


     def 				SCREAMING_SNAKE_CASE    (		self		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase):

          '''simple docstring'''






          __A		:					Dict						=			self.num_labels
          __A		:					Optional[int]						=			GPTNeoXForQuestionAnswering(snake_case__)
          model.to(snake_case__)
          model.eval()
          __A		:					List[str]						=			model(snake_case__		,      attention_mask=snake_case__)
          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 				SCREAMING_SNAKE_CASE    (		self		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase):

          '''simple docstring'''






          __A		:					str						=			self.num_labels
          __A		:					Optional[Any]						=			GPTNeoXForSequenceClassification(snake_case__)
          model.to(snake_case__)
          model.eval()
          __A		:					Tuple						=			ids_tensor([self.batch_size]		,      self.type_sequence_label_size)
          __A		:					str						=			model(snake_case__		,      attention_mask=snake_case__		,      labels=snake_case__)
          self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.num_labels))


     def 				SCREAMING_SNAKE_CASE    (		self		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase):

          '''simple docstring'''






          __A		:					str						=			self.num_labels
          __A		:					Any						=			GPTNeoXForTokenClassification(snake_case__)
          model.to(snake_case__)
          model.eval()
          __A		:					int						=			model(snake_case__		,      attention_mask=snake_case__		,      labels=snake_case__)
          self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.seq_length, self.num_labels))


     def 				SCREAMING_SNAKE_CASE    (		self		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase):

          '''simple docstring'''






          __A		:					List[Any]						=			True
          __A		:					Optional[int]						=			GPTNeoXForCausalLM(config=snake_case__)
          model.to(snake_case__)
          model.eval()

          # first forward pass
          __A		:					List[Any]						=			model(snake_case__		,      attention_mask=snake_case__		,      use_cache=snake_case__)
          __A		:					Optional[int]						=			outputs.past_key_values

          # create hypothetical multiple next token and extent to next_input_ids
          __A		:					List[Any]						=			ids_tensor((self.batch_size, 3)		,      config.vocab_size)
          __A		:					str						=			ids_tensor((self.batch_size, 3)		,      vocab_size=2)

          # append to next input_ids and
          __A		:					Dict						=			torch.cat([input_ids, next_tokens]		,      dim=-1)
          __A		:					Optional[Any]						=			torch.cat([input_mask, next_mask]		,      dim=-1)

          __A		:					int						=			model(snake_case__		,      attention_mask=snake_case__		,      output_hidden_states=snake_case__)
          __A		:					Optional[int]						=			output_from_no_past['hidden_states'][0]
          __A		:					Union[str, Any]						=			model(
              snake_case__		,      attention_mask=snake_case__		,      past_key_values=snake_case__		,      output_hidden_states=snake_case__		,      )['hidden_states'][0]

          # select random slice
          __A		:					int						=			ids_tensor((1,)		,      output_from_past.shape[-1]).item()
          __A		:					str						=			output_from_no_past[:, -3:, random_slice_idx].detach()
          __A		:					List[str]						=			output_from_past[:, :, random_slice_idx].detach()

          self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1])

          # test that outputs are equal for slice
          self.parent.assertTrue(torch.allclose(snake_case__		,      snake_case__		,      atol=1e-3))


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					Dict						=			self.prepare_config_and_inputs()
          __A					,__A					,__A					,__A		:					Dict						=			config_and_inputs
          __A		:					str						=			{'input_ids': input_ids, 'attention_mask': input_mask}
          return config, inputs_dict




@require_torch
class 					SCREAMING_SNAKE_CASE       (SCREAMING_SNAKE_CASE_   ,						SCREAMING_SNAKE_CASE_   ,						SCREAMING_SNAKE_CASE_   ,						unittest.TestCase				):
     lowerCAmelCase						     =			(
         (
             GPTNeoXModel,
             GPTNeoXForCausalLM,
             GPTNeoXForQuestionAnswering,
             GPTNeoXForSequenceClassification,
             GPTNeoXForTokenClassification,
         )
         if is_torch_available()
         else ()
     )
     lowerCAmelCase						     =			(GPTNeoXForCausalLM,) if is_torch_available() else ()
     lowerCAmelCase						     =			(
         {
             '''feature-extraction''': GPTNeoXModel,
             '''question-answering''': GPTNeoXForQuestionAnswering,
             '''text-classification''': GPTNeoXForSequenceClassification,
             '''text-generation''': GPTNeoXForCausalLM,
             '''token-classification''': GPTNeoXForTokenClassification,
             '''zero-shot''': GPTNeoXForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )
     lowerCAmelCase						     =			False
     lowerCAmelCase						     =			False
     lowerCAmelCase						     =			False
     lowerCAmelCase						     =			False


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					Optional[Any]						=			GPTNeoXModelTester(self)
          __A		:					int						=			ConfigTester(self		,      config_class=snake_case__		,      hidden_size=64		,      num_attention_heads=8)


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          self.config_tester.run_common_tests()


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A					,__A					,__A					,__A		:					List[Any]						=			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(snake_case__		,      snake_case__		,      snake_case__)


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A					,__A					,__A					,__A		:					Dict						=			self.model_tester.prepare_config_and_inputs_for_decoder()
          self.model_tester.create_and_check_model_as_decoder(snake_case__		,      snake_case__		,      snake_case__)


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A					,__A					,__A					,__A		:					str						=			self.model_tester.prepare_config_and_inputs_for_decoder()

          __A		:					Optional[int]						=			None

          self.model_tester.create_and_check_model_as_decoder(snake_case__		,      snake_case__		,      snake_case__)


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A					,__A					,__A					,__A		:					Tuple						=			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_decoder_model_past_large_inputs(snake_case__		,      snake_case__		,      snake_case__)


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					int						=			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_causal_lm(*snake_case__)


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					Union[str, Any]						=			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_question_answering(*snake_case__)


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					Tuple						=			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_sequence_classification(*snake_case__)


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					str						=			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_token_classification(*snake_case__)


     @unittest.skip(reason='Feed forward chunking is not implemented')
     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          pass


     @parameterized.expand([('linear',), ('dynamic',)])
     def 				SCREAMING_SNAKE_CASE    (		self		,      _UpperCAmelCase):

          '''simple docstring'''






          __A					,__A		:					Optional[int]						=			self.model_tester.prepare_config_and_inputs_for_common()
          __A		:					List[str]						=			ids_tensor([1, 10]		,      config.vocab_size)
          __A		:					Union[str, Any]						=			ids_tensor([1, int(config.max_position_embeddings * 1.5)]		,      config.vocab_size)

          set_seed(42)  # Fixed seed at init time so the two models get the same random weights
          __A		:					str						=			GPTNeoXModel(snake_case__)
          original_model.to(snake_case__)
          original_model.eval()
          __A		:					Any						=			original_model(snake_case__).last_hidden_state
          __A		:					Tuple						=			original_model(snake_case__).last_hidden_state

          set_seed(42)  # Fixed seed at init time so the two models get the same random weights
          __A		:					List[Any]						=			{'type': scaling_type, 'factor': 10.0}
          __A		:					str						=			GPTNeoXModel(snake_case__)
          scaled_model.to(snake_case__)
          scaled_model.eval()
          __A		:					Dict						=			scaled_model(snake_case__).last_hidden_state
          __A		:					Any						=			scaled_model(snake_case__).last_hidden_state

          # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original
          # maximum sequence length, so the outputs for the short input should match.
          if scaling_type == "dynamic":
               self.assertTrue(torch.allclose(snake_case__		,      snake_case__		,      atol=1e-5))
          else:
               self.assertFalse(torch.allclose(snake_case__		,      snake_case__		,      atol=1e-5))

          # The output should be different for long inputs
          self.assertFalse(torch.allclose(snake_case__		,      snake_case__		,      atol=1e-5))




@require_torch
class 					SCREAMING_SNAKE_CASE       (unittest.TestCase				):


     @slow
     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					int						=			AutoTokenizer.from_pretrained('EleutherAI/pythia-410m-deduped')
          for checkpointing in [True, False]:
               __A		:					Optional[Any]						=			GPTNeoXForCausalLM.from_pretrained('EleutherAI/pythia-410m-deduped')

               if checkpointing:
                    model.gradient_checkpointing_enable()
               else:
                    model.gradient_checkpointing_disable()
               model.to(snake_case__)

               __A		:					int						=			tokenizer('My favorite food is'		,      return_tensors='pt').to(snake_case__)
               # The hub repo. is updated on 2023-04-04, resulting in poor outputs.
               # See: https://github.com/huggingface/transformers/pull/24193
               __A		:					str						=			'My favorite food is a good old-fashioned, old-fashioned, old-fashioned.\n\nI\'m not sure'

               __A		:					int						=			model.generate(**snake_case__		,      do_sample=snake_case__		,      max_new_tokens=20)
               __A		:					Optional[Any]						=			tokenizer.batch_decode(snake_case__)[0]

               self.assertEqual(snake_case__		,      snake_case__)
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