



import copy
import inspect
import unittest

from transformers import AutoBackbone
from transformers.configuration_utils import PretrainedConfig
from transformers.testing_utils import require_timm, require_torch, torch_device
from transformers.utils.import_utils import is_torch_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor


if is_torch_available():
 import torch

 from transformers import TimmBackbone, TimmBackboneConfig

from ...test_pipeline_mixin import PipelineTesterMixin



class  __lowerCAmelCase     :




 def __init__(						self	,				__UpperCAmelCase	,				__UpperCAmelCase=None	,				__UpperCAmelCase=None	,				__UpperCAmelCase=None	,				__UpperCAmelCase="resnet50"	,				__UpperCAmelCase=3	,				__UpperCAmelCase=32	,				__UpperCAmelCase=3	,				__UpperCAmelCase=True	,				__UpperCAmelCase=True	,				):



    '''simple docstring'''
    __lowerCamelCase													=    parent
    __lowerCamelCase													=    out_indices if out_indices is not None else [4]
    __lowerCamelCase													=    stage_names
    __lowerCamelCase													=    out_features
    __lowerCamelCase													=    backbone
    __lowerCamelCase													=    batch_size
    __lowerCamelCase													=    image_size
    __lowerCamelCase													=    num_channels
    __lowerCamelCase													=    use_pretrained_backbone
    __lowerCamelCase													=    is_training




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)
    __lowerCamelCase													=    self.get_config()

    return config, pixel_values




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    return TimmBackboneConfig(
        image_size=self.image_size	,				num_channels=self.num_channels	,				out_features=self.out_features	,				out_indices=self.out_indices	,				stage_names=self.stage_names	,				use_pretrained_backbone=self.use_pretrained_backbone	,				backbone=self.backbone	,				)




 def        lowerCamelCase      (						self	,				__UpperCAmelCase	,				__UpperCAmelCase				):



    '''simple docstring'''
    __lowerCamelCase													=    TimmBackbone(config=__UpperCAmelCase				)
    model.to(__UpperCAmelCase				)
    model.eval()
    with torch.no_grad():
       __lowerCamelCase													=    model(__UpperCAmelCase				)
    self.parent.assertEqual(
        result.feature_map[-1].shape	,				(self.batch_size, model.channels[-1], 14, 14)	,				)




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    self.prepare_config_and_inputs()
    __lowerCamelCase							,__lowerCamelCase													=    config_and_inputs
    __lowerCamelCase													=    {'''pixel_values''': pixel_values}
    return config, inputs_dict



@require_torch
@require_timm
class  __lowerCAmelCase     (	lowerCAmelCase__  ,	lowerCAmelCase__  ,	lowerCAmelCase__  ,	unittest.TestCase   ):
 lowerCAmelCase__       					=  (TimmBackbone,) if is_torch_available() else ()
 lowerCAmelCase__       					=  {"""feature-extraction""": TimmBackbone} if is_torch_available() else {}
 lowerCAmelCase__       					=  False
 lowerCAmelCase__       					=  False
 lowerCAmelCase__       					=  False
 lowerCAmelCase__       					=  False




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    TimmBackboneModelTester(self				)
    __lowerCamelCase													=    ConfigTester(self	,				config_class=__UpperCAmelCase	,				has_text_modality=__UpperCAmelCase				)




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    self.config_tester.create_and_test_config_to_json_string()
    self.config_tester.create_and_test_config_to_json_file()
    self.config_tester.create_and_test_config_from_and_save_pretrained()
    self.config_tester.create_and_test_config_with_num_labels()
    self.config_tester.check_config_can_be_init_without_params()
    self.config_tester.check_config_arguments_init()




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    '''resnet18'''
    __lowerCamelCase													=    '''microsoft/resnet-18'''

    __lowerCamelCase													=    AutoBackbone.from_pretrained(__UpperCAmelCase	,				use_timm_backbone=__UpperCAmelCase				)
    __lowerCamelCase													=    AutoBackbone.from_pretrained(__UpperCAmelCase				)

    self.assertEqual(len(timm_model.out_features				)	,				len(transformers_model.out_features				)				)
    self.assertEqual(len(timm_model.stage_names				)	,				len(transformers_model.stage_names				)				)
    self.assertEqual(timm_model.channels	,				transformers_model.channels				)
    # Out indices are set to the last layer by default. For timm models, we don't know
    # the number of layers in advance, so we set it to (-1,), whereas for transformers
    # models, we set it to [len(stage_names) - 1] (kept for backward compatibility).
    self.assertEqual(timm_model.out_indices	,				(-1,)				)
    self.assertEqual(transformers_model.out_indices	,				[len(timm_model.stage_names				) - 1]				)

    __lowerCamelCase													=    AutoBackbone.from_pretrained(__UpperCAmelCase	,				use_timm_backbone=__UpperCAmelCase	,				out_indices=[1, 2, 3]				)
    __lowerCamelCase													=    AutoBackbone.from_pretrained(__UpperCAmelCase	,				out_indices=[1, 2, 3]				)

    self.assertEqual(timm_model.out_indices	,				transformers_model.out_indices				)
    self.assertEqual(len(timm_model.out_features				)	,				len(transformers_model.out_features				)				)
    self.assertEqual(timm_model.channels	,				transformers_model.channels				)




 @unittest.skip('''TimmBackbone doesn\'t support feed forward chunking'''				)
 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    pass




 @unittest.skip('''TimmBackbone doesn\'t have num_hidden_layers attribute'''				)
 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    pass




 @unittest.skip('''TimmBackbone initialization is managed on the timm side'''				)
 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    pass




 @unittest.skip('''TimmBackbone models doesn\'t have inputs_embeds'''				)
 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    pass




 @unittest.skip('''TimmBackbone models doesn\'t have inputs_embeds'''				)
 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    pass




 @unittest.skip('''TimmBackbone model cannot be created without specifying a backbone checkpoint'''				)
 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    pass




 @unittest.skip('''Only checkpoints on timm can be loaded into TimmBackbone'''				)
 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    pass




 @unittest.skip('''model weights aren\'t tied in TimmBackbone.'''				)
 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    pass




 @unittest.skip('''model weights aren\'t tied in TimmBackbone.'''				)
 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    pass




 @unittest.skip('''Only checkpoints on timm can be loaded into TimmBackbone'''				)
 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    pass




 @unittest.skip('''Only checkpoints on timm can be loaded into TimmBackbone'''				)
 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    pass




 @unittest.skip('''TimmBackbone doesn\'t have hidden size info in its configuration.'''				)
 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    pass




 @unittest.skip('''TimmBackbone doesn\'t support output_attentions.'''				)
 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    pass




 @unittest.skip('''Safetensors is not supported by timm.'''				)
 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    pass




 @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''				)
 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    pass




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase							,__lowerCamelCase													=    self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
       __lowerCamelCase													=    model_class(__UpperCAmelCase				)
       __lowerCamelCase													=    inspect.signature(model.forward				)
       # signature.parameters is an OrderedDict => so arg_names order is deterministic
       __lowerCamelCase													=    [*signature.parameters.keys()]

       __lowerCamelCase													=    ['''pixel_values''']
       self.assertListEqual(arg_names[:1]	,				__UpperCAmelCase				)




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase							,__lowerCamelCase													=    self.model_tester.prepare_config_and_inputs_for_common()
    __lowerCamelCase													=    True
    __lowerCamelCase													=    self.has_attentions

    # no need to test all models as different heads yield the same functionality
    __lowerCamelCase													=    self.all_model_classes[0]
    __lowerCamelCase													=    model_class(__UpperCAmelCase				)
    model.to(__UpperCAmelCase				)

    __lowerCamelCase													=    self._prepare_for_class(__UpperCAmelCase	,				__UpperCAmelCase				)
    __lowerCamelCase													=    model(**__UpperCAmelCase				)
    __lowerCamelCase													=    outputs[0][-1]

    # Encoder-/Decoder-only models
    __lowerCamelCase													=    outputs.hidden_states[0]
    hidden_states.retain_grad()

    if self.has_attentions:
       __lowerCamelCase													=    outputs.attentions[0]
       attentions.retain_grad()

    output.flatten()[0].backward(retain_graph=__UpperCAmelCase				)

    self.assertIsNotNone(hidden_states.grad				)

    if self.has_attentions:
       self.assertIsNotNone(attentions.grad				)




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase							,__lowerCamelCase													=    self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
       __lowerCamelCase													=    model_class(__UpperCAmelCase				)
       model.to(__UpperCAmelCase				)
       model.eval()
       __lowerCamelCase													=    model(**__UpperCAmelCase				)

       self.assertEqual(len(result.feature_maps				)	,				len(config.out_indices				)				)
       self.assertEqual(len(model.channels				)	,				len(config.out_indices				)				)

       # Check output of last stage is taken if out_features=None, out_indices=None
       __lowerCamelCase													=    copy.deepcopy(__UpperCAmelCase				)
       __lowerCamelCase													=    None
       __lowerCamelCase													=    model_class(__UpperCAmelCase				)
       model.to(__UpperCAmelCase				)
       model.eval()
       __lowerCamelCase													=    model(**__UpperCAmelCase				)

       self.assertEqual(len(result.feature_maps				)	,				1				)
       self.assertEqual(len(model.channels				)	,				1				)

       # Check backbone can be initialized with fresh weights
       __lowerCamelCase													=    copy.deepcopy(__UpperCAmelCase				)
       __lowerCamelCase													=    False
       __lowerCamelCase													=    model_class(__UpperCAmelCase				)
       model.to(__UpperCAmelCase				)
       model.eval()
       __lowerCamelCase													=    model(**__UpperCAmelCase				)











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     :




   @staticmethod
   def        lowerCamelCase      (						*__UpperCAmelCase	,				**__UpperCAmelCase				):



      '''simple docstring'''
      pass





  def 		a__			(		_UpperCamelCase			: List[str]							):
     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   ):
 lowerCAmelCase__       					=  MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING




 @require_pytesseract
 @require_vision
 def        lowerCamelCase      (						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        lowerCamelCase      (						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        lowerCamelCase      (						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        lowerCamelCase      (						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        lowerCamelCase      (						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        lowerCamelCase      (						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        lowerCamelCase      (						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        lowerCamelCase      (						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        lowerCamelCase      (						self				):



    '''simple docstring'''
    pass






