

'''simple docstring'''





import json
from typing import Dict, List, Optional, Tuple, Union

from tokenizers import pre_tokenizers, processors

from ...tokenization_utils_base import AddedToken, BatchEncoding, EncodedInput
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import PaddingStrategy, logging
from .tokenization_led import LEDTokenizer


UpperCamelCase_    						=       logging.get_logger(__name__)


UpperCamelCase_    						=       {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_file""": """tokenizer.json"""}

UpperCamelCase_    						=       {
    """vocab_file""": {
        """allenai/led-base-16384""": """https://huggingface.co/allenai/led-base-16384/resolve/main/vocab.json""",
    },
    """merges_file""": {
        """allenai/led-base-16384""": """https://huggingface.co/allenai/led-base-16384/resolve/main/merges.txt""",
    },
    """tokenizer_file""": {
        """allenai/led-base-16384""": """https://huggingface.co/allenai/led-base-16384/resolve/main/tokenizer.json""",
    },
}

UpperCamelCase_    						=       {
    """allenai/led-base-16384""": 16384,
}





class 		__SCREAMING_SNAKE_CASE							(			lowercase__      ):
				lowerCamelCase_		       =    VOCAB_FILES_NAMES
				lowerCamelCase_		       =    PRETRAINED_VOCAB_FILES_MAP
				lowerCamelCase_		       =    PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
				lowerCamelCase_		       =    LEDTokenizer
				lowerCamelCase_		       =    ['input_ids', 'attention_mask']







				def __init__(       self     :		Optional[int]		,					UpperCAmelCase__     :		Tuple=None		,					UpperCAmelCase__     :		Optional[Any]=None		,					UpperCAmelCase__     :		List[str]=None		,					UpperCAmelCase__     :		Dict="replace"		,					UpperCAmelCase__     :		Any="<s>"		,					UpperCAmelCase__     :		Any="</s>"		,					UpperCAmelCase__     :		Union[str, Any]="</s>"		,					UpperCAmelCase__     :		Dict="<s>"		,					UpperCAmelCase__     :		List[str]="<unk>"		,					UpperCAmelCase__     :		List[Any]="<pad>"		,					UpperCAmelCase__     :		Tuple="<mask>"		,					UpperCAmelCase__     :		int=False		,					UpperCAmelCase__     :		int=True		,					**UpperCAmelCase__     :		Optional[Any]		,					):
										'''simple docstring'''
										super().__init__(
										    UpperCAmelCase__		,					UpperCAmelCase__		,					tokenizer_file=UpperCAmelCase__		,					errors=UpperCAmelCase__		,					bos_token=UpperCAmelCase__		,					eos_token=UpperCAmelCase__		,					sep_token=UpperCAmelCase__		,					cls_token=UpperCAmelCase__		,					unk_token=UpperCAmelCase__		,					pad_token=UpperCAmelCase__		,					mask_token=UpperCAmelCase__		,					add_prefix_space=UpperCAmelCase__		,					trim_offsets=UpperCAmelCase__		,					**UpperCAmelCase__		,					)

										lowercase :							Optional[int]    		=json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()   )
										if pre_tok_state.get('''add_prefix_space'''		,					UpperCAmelCase__   ) != add_prefix_space:
																lowercase :							Any    		=getattr(UpperCAmelCase__		,					pre_tok_state.pop('''type'''   )   )
																lowercase :							Optional[Any]    		=add_prefix_space
																lowercase :							Union[str, Any]    		=pre_tok_class(**UpperCAmelCase__   )

										lowercase :							str    		=add_prefix_space

										# the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__`
										lowercase :							Tuple    		='''post_processor'''
										lowercase :							Union[str, Any]    		=getattr(self.backend_tokenizer		,					UpperCAmelCase__		,					UpperCAmelCase__   )
										if tokenizer_component_instance:
																lowercase :							List[str]    		=json.loads(tokenizer_component_instance.__getstate__()   )

																# The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class`
																if "sep" in state:
																						lowercase :							int    		=tuple(state['''sep''']   )
																if "cls" in state:
																						lowercase :							Tuple    		=tuple(state['''cls''']   )

																lowercase :							Union[str, Any]    		=False

																if state.get('''add_prefix_space'''		,					UpperCAmelCase__   ) != add_prefix_space:
																						lowercase :							List[Any]    		=add_prefix_space
																						lowercase :							List[str]    		=True

																if state.get('''trim_offsets'''		,					UpperCAmelCase__   ) != trim_offsets:
																						lowercase :							Tuple    		=trim_offsets
																						lowercase :							Optional[int]    		=True

																if changes_to_apply:
																						lowercase :							Any    		=getattr(UpperCAmelCase__		,					state.pop('''type'''   )   )
																						lowercase :							str    		=component_class(**UpperCAmelCase__   )
																						setattr(self.backend_tokenizer		,					UpperCAmelCase__		,					UpperCAmelCase__   )







				@property
				# Copied from transformers.models.bart.tokenization_bart_fast.BartTokenizerFast.mask_token with BART->LED
				def  lowerCamelCase_	(       self     :		List[str]   ):
										'''simple docstring'''
										if self._mask_token is None:
																if self.verbose:
																						logger.error('''Using mask_token, but it is not set yet.'''   )
																return None
										return str(self._mask_token   )







				@mask_token.setter
				def  lowerCamelCase_	(       self     :		List[str]		,					UpperCAmelCase__     :		int   ):
										'''simple docstring'''
										lowercase :							List[Any]    		=AddedToken(UpperCAmelCase__		,					lstrip=UpperCAmelCase__		,					rstrip=UpperCAmelCase__   ) if isinstance(UpperCAmelCase__		,					UpperCAmelCase__   ) else value
										lowercase :							Tuple    		=value







				def  lowerCamelCase_	(       self     :		int		,					*UpperCAmelCase__     :		List[str]		,					**UpperCAmelCase__     :		List[str]   ):
										'''simple docstring'''
										lowercase :							Optional[Any]    		=kwargs.get('''is_split_into_words'''		,					UpperCAmelCase__   )

										if is_split_into_words and not self.add_prefix_space:
																raise ValueError(
																    F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True '''
																    '''to use it with pretokenized inputs.'''   )

										return super()._batch_encode_plus(*UpperCAmelCase__		,					**UpperCAmelCase__   )







				def  lowerCamelCase_	(       self     :		Optional[int]		,					*UpperCAmelCase__     :		Any		,					**UpperCAmelCase__     :		List[str]   ):
										'''simple docstring'''
										lowercase :							Tuple    		=kwargs.get('''is_split_into_words'''		,					UpperCAmelCase__   )

										if is_split_into_words and not self.add_prefix_space:
																raise ValueError(
																    F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True '''
																    '''to use it with pretokenized inputs.'''   )

										return super()._encode_plus(*UpperCAmelCase__		,					**UpperCAmelCase__   )







				def  lowerCamelCase_	(       self     :		Tuple		,					UpperCAmelCase__     :		str		,					UpperCAmelCase__     :		Optional[str] = None   ):
										'''simple docstring'''
										lowercase :							List[Any]    		=self._tokenizer.model.save(UpperCAmelCase__		,					name=UpperCAmelCase__   )
										return tuple(UpperCAmelCase__   )







				def  lowerCamelCase_	(       self     :		int		,					UpperCAmelCase__     :		List[Any]		,					UpperCAmelCase__     :		Tuple=None   ):
										'''simple docstring'''
										lowercase :							str    		=[self.bos_token_id] + token_ids_a + [self.eos_token_id]
										if token_ids_a is None:
																return output

										return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id]







				def  lowerCamelCase_	(       self     :		List[Any]		,					UpperCAmelCase__     :		List[int]		,					UpperCAmelCase__     :		Optional[List[int]] = None   ):
										'''simple docstring'''
										lowercase :							List[str]    		=[self.sep_token_id]
										lowercase :							List[str]    		=[self.cls_token_id]

										if token_ids_a is None:
																return len(cls + token_ids_a + sep   ) * [0]
										return len(cls + token_ids_a + sep + sep + token_ids_a + sep   ) * [0]







				def  lowerCamelCase_	(       self     :		Optional[Any]		,					UpperCAmelCase__     :		Union[Dict[str, EncodedInput], BatchEncoding]		,					UpperCAmelCase__     :		Optional[int] = None		,					UpperCAmelCase__     :		PaddingStrategy = PaddingStrategy.DO_NOT_PAD		,					UpperCAmelCase__     :		Optional[int] = None		,					UpperCAmelCase__     :		Optional[bool] = None		,					):
										'''simple docstring'''
										lowercase :							Optional[int]    		=super()._pad(
										    encoded_inputs=UpperCAmelCase__		,					max_length=UpperCAmelCase__		,					padding_strategy=UpperCAmelCase__		,					pad_to_multiple_of=UpperCAmelCase__		,					return_attention_mask=UpperCAmelCase__		,					)

										# Load from model defaults
										if return_attention_mask is None:
																lowercase :							int    		='''attention_mask''' in self.model_input_names

										if return_attention_mask and "global_attention_mask" in encoded_inputs:
																lowercase :							int    		=encoded_inputs[self.model_input_names[0]]
																# `global_attention_mask` need to have the same length as other (sequential) inputs.
																lowercase :							Optional[int]    		=len(encoded_inputs['''global_attention_mask''']   ) != len(UpperCAmelCase__   )

																if needs_to_be_padded:
																						lowercase :							List[Any]    		=len(UpperCAmelCase__   ) - len(encoded_inputs['''global_attention_mask''']   )

																						if self.padding_side == "right":
																												# Use `-1` since `0` in `global_attention_mask` means `local attention` instead of `not to attend`
																												lowercase :							Dict    		=(
																												    encoded_inputs['''global_attention_mask'''] + [-1] * difference
																												)
																						elif self.padding_side == "left":
																												lowercase :							Union[str, Any]    		=[-1] * difference + encoded_inputs[
																												    '''global_attention_mask'''
																												]
																						else:
																												raise ValueError('''Invalid padding strategy:''' + str(self.padding_side   )   )

										return encoded_inputs






'''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     __UpperCAmelCase       :





   @staticmethod
   def 						UpperCAmelCase_	(	*_lowerCamelCase    ,     **_lowerCamelCase ):
      pass






 def snake_case_      (					__snake_case  :							int)			->					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.
A_     :						Any    							=(
    '''https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png'''
)


@is_pipeline_test
@require_torch
@require_vision
class     __UpperCAmelCase       ( unittest.TestCase		):
  __A :				Union[str, Any]											= MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING





  @require_pytesseract
  @require_vision
  def 						UpperCAmelCase_	(	self    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase ):
     lowerCAmelCase_			=      pipeline(
         '''document-question-answering'''    ,     model=_lowerCamelCase    ,     tokenizer=_lowerCamelCase    ,     image_processor=_lowerCamelCase )

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





  def 						UpperCAmelCase_	(	self    ,     _lowerCamelCase    ,     _lowerCamelCase ):
     lowerCAmelCase_			=      dqa_pipeline(_lowerCamelCase    ,     top_k=2 )
     self.assertEqual(
         _lowerCamelCase    ,     [
             [
                 {'''score''': ANY(_lowerCamelCase ), '''answer''': ANY(_lowerCamelCase ), '''start''': ANY(_lowerCamelCase ), '''end''': ANY(_lowerCamelCase )},
                 {'''score''': ANY(_lowerCamelCase ), '''answer''': ANY(_lowerCamelCase ), '''start''': ANY(_lowerCamelCase ), '''end''': ANY(_lowerCamelCase )},
             ]
         ]
         * 3    ,     )





  @require_torch
  @require_detectrona
  @require_pytesseract
  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      pipeline('''document-question-answering'''    ,     model='''hf-internal-testing/tiny-random-layoutlmv2''' )
     lowerCAmelCase_			=      INVOICE_URL
     lowerCAmelCase_			=      '''How many cats are there?'''

     lowerCAmelCase_			=      [
         {'''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},
     ]
     lowerCAmelCase_			=      dqa_pipeline(image=_lowerCamelCase    ,     question=_lowerCamelCase    ,     top_k=2 )
     self.assertEqual(nested_simplify(_lowerCamelCase    ,     decimals=4 )    ,     _lowerCamelCase )

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

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

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





  @slow
  @require_torch
  @require_detectrona
  @require_pytesseract
  def 						UpperCAmelCase_	(	self ):
     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=_lowerCamelCase    ,     question=_lowerCamelCase    ,     top_k=2 )
     self.assertEqual(
         nested_simplify(_lowerCamelCase    ,     decimals=4 )    ,     [
             {'''score''': 0.99_44, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
             {'''score''': 0.00_09, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
         ]    ,     )

     lowerCAmelCase_			=      dqa_pipeline({'''image''': image, '''question''': question}    ,     top_k=2 )
     self.assertEqual(
         nested_simplify(_lowerCamelCase    ,     decimals=4 )    ,     [
             {'''score''': 0.99_44, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
             {'''score''': 0.00_09, '''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(_lowerCamelCase    ,     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 						UpperCAmelCase_	(	self ):
     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=_lowerCamelCase    ,     question=_lowerCamelCase    ,     top_k=2 )
     self.assertEqual(
         nested_simplify(_lowerCamelCase    ,     decimals=4 )    ,     [
             {'''score''': 0.99_74, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
             {'''score''': 0.99_48, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
         ]    ,     )

     lowerCAmelCase_			=      dqa_pipeline({'''image''': image, '''question''': question}    ,     top_k=2 )
     self.assertEqual(
         nested_simplify(_lowerCamelCase    ,     decimals=4 )    ,     [
             {'''score''': 0.99_74, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
             {'''score''': 0.99_48, '''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(_lowerCamelCase    ,     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 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      AutoTokenizer.from_pretrained(
         '''impira/layoutlm-document-qa'''    ,     revision='''3dc6de3'''    ,     add_prefix_space=_lowerCamelCase )
     lowerCAmelCase_			=      pipeline(
         '''document-question-answering'''    ,     model='''impira/layoutlm-document-qa'''    ,     tokenizer=_lowerCamelCase    ,     revision='''3dc6de3'''    ,     )
     lowerCAmelCase_			=      INVOICE_URL
     lowerCAmelCase_			=      '''What is the invoice number?'''

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

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

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

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

     # 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(_lowerCamelCase    ,     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 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      AutoTokenizer.from_pretrained(
         '''impira/layoutlm-document-qa'''    ,     revision='''3dc6de3'''    ,     add_prefix_space=_lowerCamelCase )
     lowerCAmelCase_			=      pipeline(
         '''document-question-answering'''    ,     model='''impira/layoutlm-document-qa'''    ,     tokenizer=_lowerCamelCase    ,     revision='''3dc6de3'''    ,     max_seq_len=50    ,     )
     lowerCAmelCase_			=      INVOICE_URL
     lowerCAmelCase_			=      '''What is the invoice number?'''

     lowerCAmelCase_			=      dqa_pipeline(image=_lowerCamelCase    ,     question=_lowerCamelCase    ,     top_k=2 )
     self.assertEqual(
         nested_simplify(_lowerCamelCase    ,     decimals=4 )    ,     [
             {'''score''': 0.99_99, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
             {'''score''': 0.99_98, '''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(_lowerCamelCase    ,     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    ,     )

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

     # 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(_lowerCamelCase    ,     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 						UpperCAmelCase_	(	self ):
     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=_lowerCamelCase    ,     question=_lowerCamelCase    ,     top_k=2 )
     self.assertEqual(nested_simplify(_lowerCamelCase    ,     decimals=4 )    ,     [{'''answer''': '''us-001'''}] )







  @require_tf
  @unittest.skip('''Document question answering not implemented in TF''' )
  def 						UpperCAmelCase_	(	self ):
     pass





