


'''simple docstring'''

from __future__ import annotations

import unittest

from transformers import RoFormerConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
     import tensorflow as tf

     from transformers import (
         TFRoFormerForCausalLM,
         TFRoFormerForMaskedLM,
         TFRoFormerForMultipleChoice,
         TFRoFormerForQuestionAnswering,
         TFRoFormerForSequenceClassification,
         TFRoFormerForTokenClassification,
         TFRoFormerModel,
     )
     from transformers.models.roformer.modeling_tf_roformer import (
         TFRoFormerSelfAttention,
         TFRoFormerSinusoidalPositionalEmbedding,
     )





class      snake_case    :





  """simple docstring"""




  def __init__(    self			,  UpperCamelCase			,  UpperCamelCase=13			,  UpperCamelCase=7			,  UpperCamelCase=True			,  UpperCamelCase=True			,  UpperCamelCase=True			,  UpperCamelCase=True			,  UpperCamelCase=99			,  UpperCamelCase=32			,  UpperCamelCase=2			,  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"""
   lowerCamelCase_      =  parent
   lowerCamelCase_      =  13
   lowerCamelCase_      =  7
   lowerCamelCase_      =  True
   lowerCamelCase_      =  True
   lowerCamelCase_      =  True
   lowerCamelCase_      =  True
   lowerCamelCase_      =  99
   lowerCamelCase_      =  32
   lowerCamelCase_      =  2
   lowerCamelCase_      =  4
   lowerCamelCase_      =  37
   lowerCamelCase_      =  "gelu"
   lowerCamelCase_      =  0.1
   lowerCamelCase_      =  0.1
   lowerCamelCase_      =  512
   lowerCamelCase_      =  16
   lowerCamelCase_      =  2
   lowerCamelCase_      =  0.02
   lowerCamelCase_      =  3
   lowerCamelCase_      =  4
   lowerCamelCase_      =  None






  def   snake_case		(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  ids_tensor([self.batch_size, self.seq_length]			,  self.vocab_size     )

   lowerCamelCase_      =  None
   if self.use_input_mask:
    lowerCamelCase_      =  random_attention_mask([self.batch_size, self.seq_length]     )

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

   lowerCamelCase_      =  None
   lowerCamelCase_      =  None
   lowerCamelCase_      =  None
   if self.use_labels:
    lowerCamelCase_      =  ids_tensor([self.batch_size]			,  self.type_sequence_label_size     )
    lowerCamelCase_      =  ids_tensor([self.batch_size, self.seq_length]			,  self.num_labels     )
    lowerCamelCase_      =  ids_tensor([self.batch_size]			,  self.num_choices     )

   lowerCamelCase_      =  RoFormerConfig(
       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			,  return_dict=UpperCamelCase			,  )

   return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels






  def   snake_case		(    self			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase     ):







   """simple docstring"""
   lowerCamelCase_      =  TFRoFormerModel(config=UpperCamelCase     )
   lowerCamelCase_      =  {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

   lowerCamelCase_      =  [input_ids, input_mask]
   lowerCamelCase_      =  model(UpperCamelCase     )

   lowerCamelCase_      =  model(UpperCamelCase     )

   self.parent.assertEqual(result.last_hidden_state.shape			,  (self.batch_size, self.seq_length, self.hidden_size)     )






  def   snake_case		(    self			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase     ):







   """simple docstring"""
   lowerCamelCase_      =  True
   lowerCamelCase_      =  TFRoFormerForCausalLM(config=UpperCamelCase     )
   lowerCamelCase_      =  {
       "input_ids": input_ids,
       "attention_mask": input_mask,
       "token_type_ids": token_type_ids,
   }
   lowerCamelCase_      =  model(UpperCamelCase     )["logits"]
   self.parent.assertListEqual(
       list(prediction_scores.numpy().shape     )			,  [self.batch_size, self.seq_length, self.vocab_size]     )






  def   snake_case		(    self			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase     ):







   """simple docstring"""
   lowerCamelCase_      =  TFRoFormerForMaskedLM(config=UpperCamelCase     )
   lowerCamelCase_      =  {
       "input_ids": input_ids,
       "attention_mask": input_mask,
       "token_type_ids": token_type_ids,
   }
   lowerCamelCase_      =  model(UpperCamelCase     )
   self.parent.assertEqual(result.logits.shape			,  (self.batch_size, self.seq_length, self.vocab_size)     )






  def   snake_case		(    self			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase     ):







   """simple docstring"""
   lowerCamelCase_      =  self.num_labels
   lowerCamelCase_      =  TFRoFormerForSequenceClassification(config=UpperCamelCase     )
   lowerCamelCase_      =  {
       "input_ids": input_ids,
       "attention_mask": input_mask,
       "token_type_ids": token_type_ids,
   }

   lowerCamelCase_      =  model(UpperCamelCase     )
   self.parent.assertEqual(result.logits.shape			,  (self.batch_size, self.num_labels)     )






  def   snake_case		(    self			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase     ):







   """simple docstring"""
   lowerCamelCase_      =  self.num_choices
   lowerCamelCase_      =  TFRoFormerForMultipleChoice(config=UpperCamelCase     )
   lowerCamelCase_      =  tf.tile(tf.expand_dims(UpperCamelCase			,  1     )			,  (1, self.num_choices, 1)     )
   lowerCamelCase_      =  tf.tile(tf.expand_dims(UpperCamelCase			,  1     )			,  (1, self.num_choices, 1)     )
   lowerCamelCase_      =  tf.tile(tf.expand_dims(UpperCamelCase			,  1     )			,  (1, self.num_choices, 1)     )
   lowerCamelCase_      =  {
       "input_ids": multiple_choice_inputs_ids,
       "attention_mask": multiple_choice_input_mask,
       "token_type_ids": multiple_choice_token_type_ids,
   }
   lowerCamelCase_      =  model(UpperCamelCase     )
   self.parent.assertEqual(result.logits.shape			,  (self.batch_size, self.num_choices)     )






  def   snake_case		(    self			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase     ):







   """simple docstring"""
   lowerCamelCase_      =  self.num_labels
   lowerCamelCase_      =  TFRoFormerForTokenClassification(config=UpperCamelCase     )
   lowerCamelCase_      =  {
       "input_ids": input_ids,
       "attention_mask": input_mask,
       "token_type_ids": token_type_ids,
   }
   lowerCamelCase_      =  model(UpperCamelCase     )
   self.parent.assertEqual(result.logits.shape			,  (self.batch_size, self.seq_length, self.num_labels)     )






  def   snake_case		(    self			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase     ):







   """simple docstring"""
   lowerCamelCase_      =  TFRoFormerForQuestionAnswering(config=UpperCamelCase     )
   lowerCamelCase_      =  {
       "input_ids": input_ids,
       "attention_mask": input_mask,
       "token_type_ids": token_type_ids,
   }

   lowerCamelCase_      =  model(UpperCamelCase     )
   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   snake_case		(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  self.prepare_config_and_inputs()
   (
       (
       lowerCamelCase_
   ) ,(
       lowerCamelCase_
   ) ,(
       lowerCamelCase_
   ) ,(
       lowerCamelCase_
   ) ,(
       lowerCamelCase_
   ) ,(
       lowerCamelCase_
   ) ,(
       lowerCamelCase_
   ) ,
   )      =  config_and_inputs
   lowerCamelCase_      =  {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
   return config, inputs_dict





@require_tf
class      snake_case    (    lowercase			,						lowercase			,						unittest.TestCase			):





  """simple docstring"""




  _lowerCamelCase		    =  (
      (
          TFRoFormerModel,
          TFRoFormerForCausalLM,
          TFRoFormerForMaskedLM,
          TFRoFormerForQuestionAnswering,
          TFRoFormerForSequenceClassification,
          TFRoFormerForTokenClassification,
          TFRoFormerForMultipleChoice,
      )
      if is_tf_available()
      else ()
  )
  _lowerCamelCase		    =  (
      {
          "feature-extraction": TFRoFormerModel,
          "fill-mask": TFRoFormerForMaskedLM,
          "question-answering": TFRoFormerForQuestionAnswering,
          "text-classification": TFRoFormerForSequenceClassification,
          "text-generation": TFRoFormerForCausalLM,
          "token-classification": TFRoFormerForTokenClassification,
          "zero-shot": TFRoFormerForSequenceClassification,
      }
      if is_tf_available()
      else {}
  )

  _lowerCamelCase		    =  False
  _lowerCamelCase		    =  False






  def   snake_case		(    self			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase     ):







   """simple docstring"""
   if pipeline_test_casse_name == "TextGenerationPipelineTests":
    return True

   return False






  def   snake_case		(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  TFRoFormerModelTester(self     )
   lowerCamelCase_      =  ConfigTester(self			,  config_class=UpperCamelCase			,  hidden_size=37     )






  def   snake_case		(    self     ):







   """simple docstring"""
   self.config_tester.run_common_tests()






  def   snake_case		(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_model(*UpperCamelCase     )






  def   snake_case		(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_masked_lm(*UpperCamelCase     )






  def   snake_case		(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_lm_head(*UpperCamelCase     )






  def   snake_case		(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_multiple_choice(*UpperCamelCase     )






  def   snake_case		(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_question_answering(*UpperCamelCase     )






  def   snake_case		(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase     )






  def   snake_case		(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_token_classification(*UpperCamelCase     )







  @slow
  def   snake_case		(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  TFRoFormerModel.from_pretrained("junnyu/roformer_chinese_base"     )
   self.assertIsNotNone(UpperCamelCase     )





@require_tf
class      snake_case    (    unittest.TestCase			):





  """simple docstring"""




  @slow
  def   snake_case		(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  TFRoFormerForMaskedLM.from_pretrained("junnyu/roformer_chinese_base"     )
   lowerCamelCase_      =  tf.constant([[0, 1, 2, 3, 4, 5]]     )
   lowerCamelCase_      =  model(UpperCamelCase     )[0]

   # TODO Replace vocab size
   lowerCamelCase_      =  5_0000

   lowerCamelCase_      =  [1, 6, vocab_size]
   self.assertEqual(output.shape			,  UpperCamelCase     )

   print(output[:, :3, :3]     )

   # TODO Replace values below with what was printed above.
   lowerCamelCase_      =  tf.constant(
       [
           [
               [-0.12_053_341, -1.0_264_901, 0.29_221_946],
               [-1.5_133_783, 0.197_433, 0.15_190_607],
               [-5.0_135_403, -3.900_256, -0.84_038_764],
           ]
       ]     )
   tf.debugging.assert_near(output[:, :3, :3]			,  UpperCamelCase			,  atol=1e-4     )





@require_tf
class      snake_case    (    unittest.TestCase			):





  """simple docstring"""




  _lowerCamelCase		    =  1e-4






  def   snake_case		(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  tf.constant([[4, 10]]     )
   lowerCamelCase_      =  TFRoFormerSinusoidalPositionalEmbedding(num_positions=6			,  embedding_dim=6     )

   lowerCamelCase_      =  emba(input_ids.shape     )
   lowerCamelCase_      =  tf.constant(
       [[0.0_000, 0.0_000, 0.0_000, 1.0_000, 1.0_000, 1.0_000], [0.8_415, 0.0_464, 0.0_022, 0.5_403, 0.9_989, 1.0_000]]     )

   tf.debugging.assert_near(UpperCamelCase			,  UpperCamelCase			,  atol=self.tolerance     )







  def   snake_case		(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  tf.constant(
       [
           [0.0_000, 0.0_000, 0.0_000, 0.0_000, 0.0_000],
           [0.8_415, 0.8_219, 0.8_020, 0.7_819, 0.7_617],
           [0.9_093, 0.9_364, 0.9_581, 0.9_749, 0.9_870],
       ]     )
   lowerCamelCase_      =  TFRoFormerSinusoidalPositionalEmbedding(num_positions=512			,  embedding_dim=512     )
   emba([2, 16, 512]     )
   lowerCamelCase_      =  emba.weight[:3, :5]

   tf.debugging.assert_near(UpperCamelCase			,  UpperCamelCase			,  atol=self.tolerance     )





@require_tf
class      snake_case    (    unittest.TestCase			):





  """simple docstring"""




  _lowerCamelCase		    =  1e-4






  def   snake_case		(    self     ):







   """simple docstring"""
   # 2,12,16,64
   lowerCamelCase_      =  tf.reshape(tf.range(2 * 12 * 16 * 64			,  dtype=tf.floataa     )			,  shape=(2, 12, 16, 64)     ) / 100

   lowerCamelCase_      =  -tf.reshape(tf.range(2 * 12 * 16 * 64			,  dtype=tf.floataa     )			,  shape=(2, 12, 16, 64)     ) / 100

   lowerCamelCase_      =  TFRoFormerSinusoidalPositionalEmbedding(num_positions=32			,  embedding_dim=64     )
   lowerCamelCase_      =  embed_positions([2, 16, 768]     )[None, None, :, :]

   lowerCamelCase_ ,lowerCamelCase_      =  TFRoFormerSelfAttention.apply_rotary_position_embeddings(
       UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase     )

   lowerCamelCase_      =  tf.constant(
       [
           [0.0_000, 0.0_100, 0.0_200, 0.0_300, 0.0_400, 0.0_500, 0.0_600, 0.0_700],
           [-0.2_012, 0.8_897, 0.0_263, 0.9_401, 0.2_074, 0.9_463, 0.3_481, 0.9_343],
           [-1.7_057, 0.6_271, -1.2_145, 1.3_897, -0.6_303, 1.7_647, -0.1_173, 1.8_985],
           [-2.1_731, -1.6_397, -2.7_358, 0.2_854, -2.1_840, 1.7_183, -1.3_018, 2.4_871],
           [0.2_717, -3.6_173, -2.9_206, -2.1_988, -3.6_638, 0.3_858, -2.9_155, 2.2_980],
           [3.9_859, -2.1_580, -0.7_984, -4.4_904, -4.1_181, -2.0_252, -4.4_782, 1.1_253],
       ]     )
   lowerCamelCase_      =  tf.constant(
       [
           [0.0_000, -0.0_100, -0.0_200, -0.0_300, -0.0_400, -0.0_500, -0.0_600, -0.0_700],
           [0.2_012, -0.8_897, -0.0_263, -0.9_401, -0.2_074, -0.9_463, -0.3_481, -0.9_343],
           [1.7_057, -0.6_271, 1.2_145, -1.3_897, 0.6_303, -1.7_647, 0.1_173, -1.8_985],
           [2.1_731, 1.6_397, 2.7_358, -0.2_854, 2.1_840, -1.7_183, 1.3_018, -2.4_871],
           [-0.2_717, 3.6_173, 2.9_206, 2.1_988, 3.6_638, -0.3_858, 2.9_155, -2.2_980],
           [-3.9_859, 2.1_580, 0.7_984, 4.4_904, 4.1_181, 2.0_252, 4.4_782, -1.1_253],
       ]     )

   tf.debugging.assert_near(query_layer[0, 0, :6, :8]			,  UpperCamelCase			,  atol=self.tolerance     )
   tf.debugging.assert_near(key_layer[0, 0, :6, :8]			,  UpperCamelCase			,  atol=self.tolerance     )







'''simple docstring'''

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      snake_case    :





       """simple docstring"""




       @staticmethod
       def   snake_case		(    *UpperCamelCase			,  **UpperCamelCase     ):







        """simple docstring"""
        pass





     def __snake_case					(      UpperCAmelCase_						:						List[Any]		):
      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_  :				Dict						= (
    """https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png"""
)





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





  """simple docstring"""




  _lowerCamelCase		    =  MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING






  @require_pytesseract
  @require_vision
  def   snake_case		(    self			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase     ):







   """simple docstring"""
   lowerCamelCase_      =  pipeline(
       "document-question-answering"			,  model=UpperCamelCase			,  tokenizer=UpperCamelCase			,  image_processor=UpperCamelCase     )

   lowerCamelCase_      =  INVOICE_URL
   lowerCamelCase_      =  list(zip(*apply_tesseract(load_image(UpperCamelCase     )			,  UpperCamelCase			,  ""     )     )     )
   lowerCamelCase_      =  "What is the placebo?"
   lowerCamelCase_      =  [
       {
           "image": load_image(UpperCamelCase     ),
           "question": question,
       },
       {
           "image": image,
           "question": question,
       },
       {
           "image": image,
           "question": question,
           "word_boxes": word_boxes,
       },
   ]
   return dqa_pipeline, examples






  def   snake_case		(    self			,  UpperCamelCase			,  UpperCamelCase     ):







   """simple docstring"""
   lowerCamelCase_      =  dqa_pipeline(UpperCamelCase			,  top_k=2     )
   self.assertEqual(
       UpperCamelCase			,  [
           [
               {"score": ANY(UpperCamelCase     ), "answer": ANY(UpperCamelCase     ), "start": ANY(UpperCamelCase     ), "end": ANY(UpperCamelCase     )},
               {"score": ANY(UpperCamelCase     ), "answer": ANY(UpperCamelCase     ), "start": ANY(UpperCamelCase     ), "end": ANY(UpperCamelCase     )},
           ]
       ]
       * 3			,  )






  @require_torch
  @require_detectrona
  @require_pytesseract
  def   snake_case		(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  pipeline("document-question-answering"			,  model="hf-internal-testing/tiny-random-layoutlmv2"     )
   lowerCamelCase_      =  INVOICE_URL
   lowerCamelCase_      =  "How many cats are there?"

   lowerCamelCase_      =  [
       {"score": 0.0_001, "answer": "oy 2312/2019", "start": 38, "end": 39},
       {"score": 0.0_001, "answer": "oy 2312/2019 DUE", "start": 38, "end": 40},
   ]
   lowerCamelCase_      =  dqa_pipeline(image=UpperCamelCase			,  question=UpperCamelCase			,  top_k=2     )
   self.assertEqual(nested_simplify(UpperCamelCase			,  decimals=4     )			,  UpperCamelCase     )

   lowerCamelCase_      =  dqa_pipeline({"image": image, "question": question}			,  top_k=2     )
   self.assertEqual(nested_simplify(UpperCamelCase			,  decimals=4     )			,  UpperCamelCase     )

   # 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=UpperCamelCase			,  question=UpperCamelCase			,  top_k=2     )
   self.assertEqual(UpperCamelCase			,  []     )

   # We can optionnally pass directly the words and bounding boxes
   lowerCamelCase_      =  "./tests/fixtures/tests_samples/COCO/000000039769.png"
   lowerCamelCase_      =  []
   lowerCamelCase_      =  []
   lowerCamelCase_      =  dqa_pipeline(image=UpperCamelCase			,  question=UpperCamelCase			,  words=UpperCamelCase			,  boxes=UpperCamelCase			,  top_k=2     )
   self.assertEqual(UpperCamelCase			,  []     )






  @slow
  @require_torch
  @require_detectrona
  @require_pytesseract
  def   snake_case		(    self     ):







   """simple docstring"""
   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=UpperCamelCase			,  question=UpperCamelCase			,  top_k=2     )
   self.assertEqual(
       nested_simplify(UpperCamelCase			,  decimals=4     )			,  [
           {"score": 0.9_944, "answer": "us-001", "start": 16, "end": 16},
           {"score": 0.0_009, "answer": "us-001", "start": 16, "end": 16},
       ]			,  )

   lowerCamelCase_      =  dqa_pipeline({"image": image, "question": question}			,  top_k=2     )
   self.assertEqual(
       nested_simplify(UpperCamelCase			,  decimals=4     )			,  [
           {"score": 0.9_944, "answer": "us-001", "start": 16, "end": 16},
           {"score": 0.0_009, "answer": "us-001", "start": 16, "end": 16},
       ]			,  )

   lowerCamelCase_      =  dqa_pipeline(
       [{"image": image, "question": question}, {"image": image, "question": question}]			,  top_k=2     )
   self.assertEqual(
       nested_simplify(UpperCamelCase			,  decimals=4     )			,  [
           [
               {"score": 0.9_944, "answer": "us-001", "start": 16, "end": 16},
               {"score": 0.0_009, "answer": "us-001", "start": 16, "end": 16},
           ],
       ]
       * 2			,  )






  @slow
  @require_torch
  @require_detectrona
  @require_pytesseract
  def   snake_case		(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  pipeline(
       "document-question-answering"			,  model="tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"			,  revision="9977165"			,  max_seq_len=50			,  )
   lowerCamelCase_      =  INVOICE_URL
   lowerCamelCase_      =  "What is the invoice number?"

   lowerCamelCase_      =  dqa_pipeline(image=UpperCamelCase			,  question=UpperCamelCase			,  top_k=2     )
   self.assertEqual(
       nested_simplify(UpperCamelCase			,  decimals=4     )			,  [
           {"score": 0.9_974, "answer": "1110212019", "start": 23, "end": 23},
           {"score": 0.9_948, "answer": "us-001", "start": 16, "end": 16},
       ]			,  )

   lowerCamelCase_      =  dqa_pipeline({"image": image, "question": question}			,  top_k=2     )
   self.assertEqual(
       nested_simplify(UpperCamelCase			,  decimals=4     )			,  [
           {"score": 0.9_974, "answer": "1110212019", "start": 23, "end": 23},
           {"score": 0.9_948, "answer": "us-001", "start": 16, "end": 16},
       ]			,  )

   lowerCamelCase_      =  dqa_pipeline(
       [{"image": image, "question": question}, {"image": image, "question": question}]			,  top_k=2     )
   self.assertEqual(
       nested_simplify(UpperCamelCase			,  decimals=4     )			,  [
           [
               {"score": 0.9_974, "answer": "1110212019", "start": 23, "end": 23},
               {"score": 0.9_948, "answer": "us-001", "start": 16, "end": 16},
           ]
       ]
       * 2			,  )






  @slow
  @require_torch
  @require_pytesseract
  @require_vision
  def   snake_case		(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  AutoTokenizer.from_pretrained(
       "impira/layoutlm-document-qa"			,  revision="3dc6de3"			,  add_prefix_space=UpperCamelCase     )
   lowerCamelCase_      =  pipeline(
       "document-question-answering"			,  model="impira/layoutlm-document-qa"			,  tokenizer=UpperCamelCase			,  revision="3dc6de3"			,  )
   lowerCamelCase_      =  INVOICE_URL
   lowerCamelCase_      =  "What is the invoice number?"

   lowerCamelCase_      =  dqa_pipeline(image=UpperCamelCase			,  question=UpperCamelCase			,  top_k=2     )
   self.assertEqual(
       nested_simplify(UpperCamelCase			,  decimals=4     )			,  [
           {"score": 0.4_251, "answer": "us-001", "start": 16, "end": 16},
           {"score": 0.0_819, "answer": "1110212019", "start": 23, "end": 23},
       ]			,  )

   lowerCamelCase_      =  dqa_pipeline({"image": image, "question": question}			,  top_k=2     )
   self.assertEqual(
       nested_simplify(UpperCamelCase			,  decimals=4     )			,  [
           {"score": 0.4_251, "answer": "us-001", "start": 16, "end": 16},
           {"score": 0.0_819, "answer": "1110212019", "start": 23, "end": 23},
       ]			,  )

   lowerCamelCase_      =  dqa_pipeline(
       [{"image": image, "question": question}, {"image": image, "question": question}]			,  top_k=2     )
   self.assertEqual(
       nested_simplify(UpperCamelCase			,  decimals=4     )			,  [
           [
               {"score": 0.4_251, "answer": "us-001", "start": 16, "end": 16},
               {"score": 0.0_819, "answer": "1110212019", "start": 23, "end": 23},
           ]
       ]
       * 2			,  )

   lowerCamelCase_      =  list(zip(*apply_tesseract(load_image(UpperCamelCase     )			,  UpperCamelCase			,  ""     )     )     )

   # 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(UpperCamelCase			,  decimals=4     )			,  [
           {"score": 0.4_251, "answer": "us-001", "start": 16, "end": 16},
           {"score": 0.0_819, "answer": "1110212019", "start": 23, "end": 23},
       ]			,  )






  @slow
  @require_torch
  @require_pytesseract
  @require_vision
  def   snake_case		(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  AutoTokenizer.from_pretrained(
       "impira/layoutlm-document-qa"			,  revision="3dc6de3"			,  add_prefix_space=UpperCamelCase     )
   lowerCamelCase_      =  pipeline(
       "document-question-answering"			,  model="impira/layoutlm-document-qa"			,  tokenizer=UpperCamelCase			,  revision="3dc6de3"			,  max_seq_len=50			,  )
   lowerCamelCase_      =  INVOICE_URL
   lowerCamelCase_      =  "What is the invoice number?"

   lowerCamelCase_      =  dqa_pipeline(image=UpperCamelCase			,  question=UpperCamelCase			,  top_k=2     )
   self.assertEqual(
       nested_simplify(UpperCamelCase			,  decimals=4     )			,  [
           {"score": 0.9_999, "answer": "us-001", "start": 16, "end": 16},
           {"score": 0.9_998, "answer": "us-001", "start": 16, "end": 16},
       ]			,  )

   lowerCamelCase_      =  dqa_pipeline(
       [{"image": image, "question": question}, {"image": image, "question": question}]			,  top_k=2     )
   self.assertEqual(
       nested_simplify(UpperCamelCase			,  decimals=4     )			,  [
           [
               {"score": 0.9_999, "answer": "us-001", "start": 16, "end": 16},
               {"score": 0.9_998, "answer": "us-001", "start": 16, "end": 16},
           ]
       ]
       * 2			,  )

   lowerCamelCase_      =  list(zip(*apply_tesseract(load_image(UpperCamelCase     )			,  UpperCamelCase			,  ""     )     )     )

   # 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(UpperCamelCase			,  decimals=4     )			,  [
           {"score": 0.9_999, "answer": "us-001", "start": 16, "end": 16},
           {"score": 0.9_998, "answer": "us-001", "start": 16, "end": 16},
       ]			,  )






  @slow
  @require_torch
  def   snake_case		(    self     ):







   """simple docstring"""
   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=UpperCamelCase			,  question=UpperCamelCase			,  top_k=2     )
   self.assertEqual(nested_simplify(UpperCamelCase			,  decimals=4     )			,  [{"answer": "us-001"}]     )







  @require_tf
  @unittest.skip("Document question answering not implemented in TF"     )
  def   snake_case		(    self     ):







   """simple docstring"""
   pass



