
import argparse
from pathlib import Path

import requests
import torch
from PIL import Image

from transformers import (
    RobertaTokenizer,
    TrOCRConfig,
    TrOCRForCausalLM,
    TrOCRProcessor,
    VisionEncoderDecoderModel,
    ViTConfig,
    ViTImageProcessor,
    ViTModel,
)
from transformers.utils import logging


logging.set_verbosity_info()
__lowerCAmelCase					:			List[str]			      =		logging.get_logger(__name__)


def     UpperCAmelCase_  (						__lowerCAmelCase	,					__lowerCAmelCase	) ->						Union[str, Any]:
    __lowercase		:			List[Any]	    =       []
    for i in range(encoder_config.num_hidden_layers	):
        # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms
        rename_keys.append(
            (F'encoder.deit.blocks.{i}.norm1.weight', F'encoder.encoder.layer.{i}.layernorm_before.weight')	)
        rename_keys.append((F'encoder.deit.blocks.{i}.norm1.bias', F'encoder.encoder.layer.{i}.layernorm_before.bias')	)
        rename_keys.append(
            (F'encoder.deit.blocks.{i}.attn.proj.weight', F'encoder.encoder.layer.{i}.attention.output.dense.weight')	)
        rename_keys.append(
            (F'encoder.deit.blocks.{i}.attn.proj.bias', F'encoder.encoder.layer.{i}.attention.output.dense.bias')	)
        rename_keys.append(
            (F'encoder.deit.blocks.{i}.norm2.weight', F'encoder.encoder.layer.{i}.layernorm_after.weight')	)
        rename_keys.append((F'encoder.deit.blocks.{i}.norm2.bias', F'encoder.encoder.layer.{i}.layernorm_after.bias')	)
        rename_keys.append(
            (F'encoder.deit.blocks.{i}.mlp.fc1.weight', F'encoder.encoder.layer.{i}.intermediate.dense.weight')	)
        rename_keys.append(
            (F'encoder.deit.blocks.{i}.mlp.fc1.bias', F'encoder.encoder.layer.{i}.intermediate.dense.bias')	)
        rename_keys.append(
            (F'encoder.deit.blocks.{i}.mlp.fc2.weight', F'encoder.encoder.layer.{i}.output.dense.weight')	)
        rename_keys.append((F'encoder.deit.blocks.{i}.mlp.fc2.bias', F'encoder.encoder.layer.{i}.output.dense.bias')	)

    # cls token, position embeddings and patch embeddings of encoder
    rename_keys.extend(
        [
            ('''encoder.deit.cls_token''', '''encoder.embeddings.cls_token'''),
            ('''encoder.deit.pos_embed''', '''encoder.embeddings.position_embeddings'''),
            ('''encoder.deit.patch_embed.proj.weight''', '''encoder.embeddings.patch_embeddings.projection.weight'''),
            ('''encoder.deit.patch_embed.proj.bias''', '''encoder.embeddings.patch_embeddings.projection.bias'''),
            ('''encoder.deit.norm.weight''', '''encoder.layernorm.weight'''),
            ('''encoder.deit.norm.bias''', '''encoder.layernorm.bias'''),
        ]	)

    return rename_keys


def     UpperCAmelCase_  (						__lowerCAmelCase	,					__lowerCAmelCase	) ->						Any:
    for i in range(encoder_config.num_hidden_layers	):
        # queries, keys and values (only weights, no biases)
        __lowercase		:			int	    =       state_dict.pop(F'encoder.deit.blocks.{i}.attn.qkv.weight'	)

        __lowercase		:			Any	    =       in_proj_weight[
            : encoder_config.hidden_size, :
        ]
        __lowercase		:			str	    =       in_proj_weight[
            encoder_config.hidden_size : encoder_config.hidden_size * 2, :
        ]
        __lowercase		:			Dict	    =       in_proj_weight[
            -encoder_config.hidden_size :, :
        ]


def     UpperCAmelCase_  (						__lowerCAmelCase	,					__lowerCAmelCase	,					__lowerCAmelCase	) ->						Union[str, Any]:
    __lowercase		:			Optional[int]	    =       dct.pop(__lowerCAmelCase	)
    __lowercase		:			Tuple	    =       val


def     UpperCAmelCase_  (						__lowerCAmelCase	) ->						Tuple:
    if "handwritten" in checkpoint_url:
        __lowercase		:			int	    =       '''https://fki.tic.heia-fr.ch/static/img/a01-122-02-00.jpg'''  # industry
        # url = "https://fki.tic.heia-fr.ch/static/img/a01-122-02-12.jpg" # have
        # url = "https://fki.tic.heia-fr.ch/static/img/a01-122-02-10.jpg" # let
        # url = "https://fki.tic.heia-fr.ch/static/img/a01-122-02.jpg"  #
        # url = "https://fki.tic.heia-fr.ch/static/img/a01-122.jpg"
    elif "printed" in checkpoint_url or "stage1" in checkpoint_url:
        __lowercase		:			List[str]	    =       '''https://www.researchgate.net/profile/Dinh-Sang/publication/338099565/figure/fig8/AS:840413229350922@1577381536857/An-receipt-example-in-the-SROIE-2019-dataset_Q640.jpg'''
    __lowercase		:			Any	    =       Image.open(requests.get(__lowerCAmelCase	,					stream=__lowerCAmelCase	).raw	).convert('''RGB'''	)
    return im


@torch.no_grad()
def     UpperCAmelCase_  (						__lowerCAmelCase	,					__lowerCAmelCase	) ->						Any:
    __lowercase		:			Any	    =       ViTConfig(image_size=384	,					qkv_bias=__lowerCAmelCase	)
    __lowercase		:			Dict	    =       TrOCRConfig()

    # size of the architecture
    if "base" in checkpoint_url:
        __lowercase		:			Optional[Any]	    =       768
    elif "large" in checkpoint_url:
        # use ViT-large encoder
        __lowercase		:			List[Any]	    =       1_024
        __lowercase		:			Optional[int]	    =       4_096
        __lowercase		:			Optional[int]	    =       24
        __lowercase		:			List[str]	    =       16
        __lowercase		:			str	    =       1_024
    else:
        raise ValueError('''Should either find \'base\' or \'large\' in checkpoint URL'''	)

    # the large-printed + stage1 checkpoints uses sinusoidal position embeddings, no layernorm afterwards
    if "large-printed" in checkpoint_url or "stage1" in checkpoint_url:
        __lowercase		:			int	    =       False
        __lowercase		:			int	    =       '''relu'''
        __lowercase		:			Tuple	    =       1_024
        __lowercase		:			Tuple	    =       True
        __lowercase		:			Optional[int]	    =       False
        __lowercase		:			Tuple	    =       False

    # load HuggingFace model
    __lowercase		:			str	    =       ViTModel(__lowerCAmelCase	,					add_pooling_layer=__lowerCAmelCase	)
    __lowercase		:			Dict	    =       TrOCRForCausalLM(__lowerCAmelCase	)
    __lowercase		:			Tuple	    =       VisionEncoderDecoderModel(encoder=__lowerCAmelCase	,					decoder=__lowerCAmelCase	)
    model.eval()

    # load state_dict of original model, rename some keys
    __lowercase		:			int	    =       torch.hub.load_state_dict_from_url(__lowerCAmelCase	,					map_location='''cpu'''	,					check_hash=__lowerCAmelCase	)['''model''']

    __lowercase		:			Dict	    =       create_rename_keys(__lowerCAmelCase	,					__lowerCAmelCase	)
    for src, dest in rename_keys:
        rename_key(__lowerCAmelCase	,					__lowerCAmelCase	,					__lowerCAmelCase	)
    read_in_q_k_v(__lowerCAmelCase	,					__lowerCAmelCase	)

    # remove parameters we don't need
    del state_dict["encoder.deit.head.weight"]
    del state_dict["encoder.deit.head.bias"]
    del state_dict["decoder.version"]

    # add prefix to decoder keys
    for key, val in state_dict.copy().items():
        __lowercase		:			str	    =       state_dict.pop(__lowerCAmelCase	)
        if key.startswith('''decoder'''	) and "output_projection" not in key:
            __lowercase		:			Tuple	    =       val
        else:
            __lowercase		:			Union[str, Any]	    =       val

    # load state dict
    model.load_state_dict(__lowerCAmelCase	)

    # Check outputs on an image
    __lowercase		:			Union[str, Any]	    =       ViTImageProcessor(size=encoder_config.image_size	)
    __lowercase		:			Optional[Any]	    =       RobertaTokenizer.from_pretrained('''roberta-large'''	)
    __lowercase		:			int	    =       TrOCRProcessor(__lowerCAmelCase	,					__lowerCAmelCase	)

    __lowercase		:			List[Any]	    =       processor(images=prepare_img(__lowerCAmelCase	)	,					return_tensors='''pt'''	).pixel_values

    # verify logits
    __lowercase		:			Dict	    =       torch.tensor([[model.config.decoder.decoder_start_token_id]]	)
    __lowercase		:			Tuple	    =       model(pixel_values=__lowerCAmelCase	,					decoder_input_ids=__lowerCAmelCase	)
    __lowercase		:			int	    =       outputs.logits

    __lowercase		:			List[Any]	    =       torch.Size([1, 1, 50_265]	)
    if "trocr-base-handwritten" in checkpoint_url:
        __lowercase		:			Optional[int]	    =       torch.tensor(
            [-1.4502, -4.6683, -0.5347, -2.9291, 9.1435, -3.0571, 8.9764, 1.7560, 8.7358, -1.5311]	)
    elif "trocr-large-handwritten" in checkpoint_url:
        __lowercase		:			Optional[int]	    =       torch.tensor(
            [-2.6437, -1.3129, -2.2596, -5.3455, 6.3539, 1.7604, 5.4991, 1.4702, 5.6113, 2.0170]	)
    elif "trocr-base-printed" in checkpoint_url:
        __lowercase		:			Union[str, Any]	    =       torch.tensor(
            [-5.6816, -5.8388, 1.1398, -6.9034, 6.8505, -2.4393, 1.2284, -1.0232, -1.9661, -3.9210]	)
    elif "trocr-large-printed" in checkpoint_url:
        __lowercase		:			Dict	    =       torch.tensor(
            [-6.0162, -7.0959, 4.4155, -5.1063, 7.0468, -3.1631, 2.6466, -0.3081, -0.8106, -1.7535]	)

    if "stage1" not in checkpoint_url:
        assert logits.shape == expected_shape, "Shape of logits not as expected"
        assert torch.allclose(logits[0, 0, :10]	,					__lowerCAmelCase	,					atol=1E-3	), "First elements of logits not as expected"

    Path(__lowerCAmelCase	).mkdir(exist_ok=__lowerCAmelCase	)
    print(F'Saving model to {pytorch_dump_folder_path}'	)
    model.save_pretrained(__lowerCAmelCase	)
    print(F'Saving processor to {pytorch_dump_folder_path}'	)
    processor.save_pretrained(__lowerCAmelCase	)


if __name__ == "__main__":
 __lowerCAmelCase					:			str			      =		argparse.ArgumentParser()

 parser.add_argument(
     "--checkpoint_url",
     default="https://layoutlm.blob.core.windows.net/trocr/model_zoo/fairseq/trocr-base-handwritten.pt",
     type=str,
     help="URL to the original PyTorch checkpoint (.pth file).",
 )
 parser.add_argument(
     "--pytorch_dump_folder_path", default=None, type=str, help="Path to the folder to output PyTorch model."
 )
 __lowerCAmelCase					:			Union[str, Any]			      =		parser.parse_args()
 convert_tr_ocr_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)




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		:       Union[str, Any]				,       **_snake_case		:       str				):
           pass






 def     UpperCAmelCase_  (						__lowerCAmelCase	) ->						Union[str, 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.
__lowerCAmelCase					:			Dict			      =		(
    "https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png"
)


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



      """simple docstring"""







      A__	:			int        =    MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING




      @require_pytesseract
      @require_vision
      def 		snake_case_			(      self		:       List[Any]				,       _snake_case		:       Any				,       _snake_case		:       Dict				,       _snake_case		:       Any				):
          __lowercase		:			List[str]	    =       pipeline(
              '''document-question-answering'''				,       model=_snake_case				,       tokenizer=_snake_case				,       image_processor=_snake_case				)

          __lowercase		:			int	    =       INVOICE_URL
          __lowercase		:			Optional[int]	    =       list(zip(*apply_tesseract(load_image(_snake_case				)				,       _snake_case				,       ''''''				)				)				)
          __lowercase		:			List[str]	    =       '''What is the placebo?'''
          __lowercase		:			Tuple	    =       [
              {
                  '''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		:       List[str]				,       _snake_case		:       int				,       _snake_case		:       List[Any]				):
          __lowercase		:			Optional[Any]	    =       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		:       Optional[int]				):
          __lowercase		:			List[str]	    =       pipeline('''document-question-answering'''				,       model='''hf-internal-testing/tiny-random-layoutlmv2'''				)
          __lowercase		:			Any	    =       INVOICE_URL
          __lowercase		:			str	    =       '''How many cats are there?'''

          __lowercase		:			Optional[int]	    =       [
              {'''score''': 0.00_01, '''answer''': '''oy 2312/2019''', '''start''': 38, '''end''': 39},
              {'''score''': 0.00_01, '''answer''': '''oy 2312/2019 DUE''', '''start''': 38, '''end''': 40},
          ]
          __lowercase		:			Optional[Any]	    =       dqa_pipeline(image=_snake_case				,       question=_snake_case				,       top_k=2				)
          self.assertEqual(nested_simplify(_snake_case				,       decimals=4				)				,       _snake_case				)

          __lowercase		:			Optional[Any]	    =       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
          __lowercase		:			Optional[int]	    =       '''./tests/fixtures/tests_samples/COCO/000000039769.png'''
          __lowercase		:			str	    =       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
          __lowercase		:			List[Any]	    =       '''./tests/fixtures/tests_samples/COCO/000000039769.png'''
          __lowercase		:			Dict	    =       []
          __lowercase		:			List[str]	    =       []
          __lowercase		:			Tuple	    =       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		:       int				):
          __lowercase		:			List[Any]	    =       pipeline(
              '''document-question-answering'''				,       model='''tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'''				,       revision='''9977165'''				,       )
          __lowercase		:			List[str]	    =       INVOICE_URL
          __lowercase		:			Optional[Any]	    =       '''What is the invoice number?'''

          __lowercase		:			int	    =       dqa_pipeline(image=_snake_case				,       question=_snake_case				,       top_k=2				)
          self.assertEqual(
              nested_simplify(_snake_case				,       decimals=4				)				,       [
                  {'''score''': 0.99_44, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
                  {'''score''': 0.00_09, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
              ]				,       )

          __lowercase		:			Union[str, Any]	    =       dqa_pipeline({'''image''': image, '''question''': question}				,       top_k=2				)
          self.assertEqual(
              nested_simplify(_snake_case				,       decimals=4				)				,       [
                  {'''score''': 0.99_44, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
                  {'''score''': 0.00_09, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
              ]				,       )

          __lowercase		:			List[str]	    =       dqa_pipeline(
              [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]				,       top_k=2				)
          self.assertEqual(
              nested_simplify(_snake_case				,       decimals=4				)				,       [
                  [
                      {'''score''': 0.99_44, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
                      {'''score''': 0.00_09, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
                  ],
              ]
              * 2				,       )




      @slow
      @require_torch
      @require_detectrona
      @require_pytesseract
      def 		snake_case_			(      self		:       int				):
          __lowercase		:			Optional[Any]	    =       pipeline(
              '''document-question-answering'''				,       model='''tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'''				,       revision='''9977165'''				,       max_seq_len=50				,       )
          __lowercase		:			Union[str, Any]	    =       INVOICE_URL
          __lowercase		:			Any	    =       '''What is the invoice number?'''

          __lowercase		:			Any	    =       dqa_pipeline(image=_snake_case				,       question=_snake_case				,       top_k=2				)
          self.assertEqual(
              nested_simplify(_snake_case				,       decimals=4				)				,       [
                  {'''score''': 0.99_74, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
                  {'''score''': 0.99_48, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
              ]				,       )

          __lowercase		:			Dict	    =       dqa_pipeline({'''image''': image, '''question''': question}				,       top_k=2				)
          self.assertEqual(
              nested_simplify(_snake_case				,       decimals=4				)				,       [
                  {'''score''': 0.99_74, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
                  {'''score''': 0.99_48, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
              ]				,       )

          __lowercase		:			List[Any]	    =       dqa_pipeline(
              [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]				,       top_k=2				)
          self.assertEqual(
              nested_simplify(_snake_case				,       decimals=4				)				,       [
                  [
                      {'''score''': 0.99_74, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
                      {'''score''': 0.99_48, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
                  ]
              ]
              * 2				,       )




      @slow
      @require_torch
      @require_pytesseract
      @require_vision
      def 		snake_case_			(      self		:       str				):
          __lowercase		:			List[Any]	    =       AutoTokenizer.from_pretrained(
              '''impira/layoutlm-document-qa'''				,       revision='''3dc6de3'''				,       add_prefix_space=_snake_case				)
          __lowercase		:			Union[str, Any]	    =       pipeline(
              '''document-question-answering'''				,       model='''impira/layoutlm-document-qa'''				,       tokenizer=_snake_case				,       revision='''3dc6de3'''				,       )
          __lowercase		:			Tuple	    =       INVOICE_URL
          __lowercase		:			Any	    =       '''What is the invoice number?'''

          __lowercase		:			Optional[Any]	    =       dqa_pipeline(image=_snake_case				,       question=_snake_case				,       top_k=2				)
          self.assertEqual(
              nested_simplify(_snake_case				,       decimals=4				)				,       [
                  {'''score''': 0.42_51, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
                  {'''score''': 0.08_19, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
              ]				,       )

          __lowercase		:			List[Any]	    =       dqa_pipeline({'''image''': image, '''question''': question}				,       top_k=2				)
          self.assertEqual(
              nested_simplify(_snake_case				,       decimals=4				)				,       [
                  {'''score''': 0.42_51, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
                  {'''score''': 0.08_19, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
              ]				,       )

          __lowercase		:			int	    =       dqa_pipeline(
              [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]				,       top_k=2				)
          self.assertEqual(
              nested_simplify(_snake_case				,       decimals=4				)				,       [
                  [
                      {'''score''': 0.42_51, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
                      {'''score''': 0.08_19, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
                  ]
              ]
              * 2				,       )

          __lowercase		:			Union[str, Any]	    =       list(zip(*apply_tesseract(load_image(_snake_case				)				,       _snake_case				,       ''''''				)				)				)

          # This model should also work if `image` is set to None
          __lowercase		:			Union[str, Any]	    =       dqa_pipeline({'''image''': None, '''word_boxes''': word_boxes, '''question''': question}				,       top_k=2				)
          self.assertEqual(
              nested_simplify(_snake_case				,       decimals=4				)				,       [
                  {'''score''': 0.42_51, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
                  {'''score''': 0.08_19, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
              ]				,       )




      @slow
      @require_torch
      @require_pytesseract
      @require_vision
      def 		snake_case_			(      self		:       List[str]				):
          __lowercase		:			Optional[int]	    =       AutoTokenizer.from_pretrained(
              '''impira/layoutlm-document-qa'''				,       revision='''3dc6de3'''				,       add_prefix_space=_snake_case				)
          __lowercase		:			List[str]	    =       pipeline(
              '''document-question-answering'''				,       model='''impira/layoutlm-document-qa'''				,       tokenizer=_snake_case				,       revision='''3dc6de3'''				,       max_seq_len=50				,       )
          __lowercase		:			Tuple	    =       INVOICE_URL
          __lowercase		:			Optional[int]	    =       '''What is the invoice number?'''

          __lowercase		:			Any	    =       dqa_pipeline(image=_snake_case				,       question=_snake_case				,       top_k=2				)
          self.assertEqual(
              nested_simplify(_snake_case				,       decimals=4				)				,       [
                  {'''score''': 0.99_99, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
                  {'''score''': 0.99_98, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
              ]				,       )

          __lowercase		:			Any	    =       dqa_pipeline(
              [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]				,       top_k=2				)
          self.assertEqual(
              nested_simplify(_snake_case				,       decimals=4				)				,       [
                  [
                      {'''score''': 0.99_99, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
                      {'''score''': 0.99_98, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
                  ]
              ]
              * 2				,       )

          __lowercase		:			int	    =       list(zip(*apply_tesseract(load_image(_snake_case				)				,       _snake_case				,       ''''''				)				)				)

          # This model should also work if `image` is set to None
          __lowercase		:			Tuple	    =       dqa_pipeline({'''image''': None, '''word_boxes''': word_boxes, '''question''': question}				,       top_k=2				)
          self.assertEqual(
              nested_simplify(_snake_case				,       decimals=4				)				,       [
                  {'''score''': 0.99_99, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
                  {'''score''': 0.99_98, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
              ]				,       )




      @slow
      @require_torch
      def 		snake_case_			(      self		:       List[Any]				):
          __lowercase		:			int	    =       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'''				,       )

          __lowercase		:			List[Any]	    =       INVOICE_URL
          __lowercase		:			List[Any]	    =       '''What is the invoice number?'''
          __lowercase		:			int	    =       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		:       int				):
          pass


