





"""simple docstring"""





import inspect
import unittest

from huggingface_hub import hf_hub_download

from transformers import ASTConfig
from transformers.testing_utils import require_torch, require_torchaudio, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_torchaudio_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
  import torch
  from torch import nn

  from transformers import ASTForAudioClassification, ASTModel
  from transformers.models.audio_spectrogram_transformer.modeling_audio_spectrogram_transformer import (
      AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
  )


if is_torchaudio_available():
  import torchaudio

  from transformers import ASTFeatureExtractor






class 					_lowerCAmelCase   :
       """simple docstring"""




       def __init__(    self    , __UpperCAmelCase    , __UpperCAmelCase=1_3    , __UpperCAmelCase=2    , __UpperCAmelCase=2_4    , __UpperCAmelCase=1_6    , __UpperCAmelCase=True    , __UpperCAmelCase=True    , __UpperCAmelCase=3_2    , __UpperCAmelCase=5    , __UpperCAmelCase=4    , __UpperCAmelCase=3_7    , __UpperCAmelCase="gelu"    , __UpperCAmelCase=0.1    , __UpperCAmelCase=0.1    , __UpperCAmelCase=1_0    , __UpperCAmelCase=0.02    , __UpperCAmelCase=None    , __UpperCAmelCase=2    , __UpperCAmelCase=2    , ):



         '''simple docstring'''

         lowerCAmelCase__			:List[str]											=							parent
         lowerCAmelCase__			:Any											=							batch_size
         lowerCAmelCase__			:Optional[Any]											=							patch_size
         lowerCAmelCase__			:Optional[int]											=							max_length
         lowerCAmelCase__			:str											=							num_mel_bins
         lowerCAmelCase__			:Union[str, Any]											=							is_training
         lowerCAmelCase__			:Dict											=							use_labels
         lowerCAmelCase__			:Optional[Any]											=							hidden_size
         lowerCAmelCase__			:List[str]											=							num_hidden_layers
         lowerCAmelCase__			:List[Any]											=							num_attention_heads
         lowerCAmelCase__			:Tuple											=							intermediate_size
         lowerCAmelCase__			:Tuple											=							hidden_act
         lowerCAmelCase__			:Union[str, Any]											=							hidden_dropout_prob
         lowerCAmelCase__			:Any											=							attention_probs_dropout_prob
         lowerCAmelCase__			:Any											=							type_sequence_label_size
         lowerCAmelCase__			:Optional[Any]											=							initializer_range
         lowerCAmelCase__			:Optional[Any]											=							scope
         lowerCAmelCase__			:Tuple											=							frequency_stride
         lowerCAmelCase__			:Optional[Any]											=							time_stride

         # in AST, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens)
         lowerCAmelCase__			:List[Any]											=							(self.num_mel_bins - self.patch_size) // self.frequency_stride + 1
         lowerCAmelCase__			:List[str]											=							(self.max_length - self.patch_size) // self.time_stride + 1
         lowerCAmelCase__			:Tuple											=							frequency_out_dimension * time_out_dimension
         lowerCAmelCase__			:Tuple											=							num_patches + 2






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:List[str]											=							floats_tensor([self.batch_size, self.max_length, self.num_mel_bins]      )

         lowerCAmelCase__			:Optional[Any]											=							None
         if self.use_labels:
           lowerCAmelCase__			:List[str]											=							ids_tensor([self.batch_size]    , self.type_sequence_label_size      )

         lowerCAmelCase__			:Dict											=							self.get_config()

         return config, input_values, labels






       def      snake_case						(    self      ):



         '''simple docstring'''

         return ASTConfig(
             patch_size=self.patch_size    , max_length=self.max_length    , num_mel_bins=self.num_mel_bins    , 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    , is_decoder=__UpperCAmelCase    , initializer_range=self.initializer_range    , frequency_stride=self.frequency_stride    , time_stride=self.time_stride    , )






       def      snake_case						(    self    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase      ):



         '''simple docstring'''

         lowerCAmelCase__			:Tuple											=							ASTModel(config=__UpperCAmelCase      )
         model.to(__UpperCAmelCase      )
         model.eval()
         lowerCAmelCase__			:Dict											=							model(__UpperCAmelCase      )
         self.parent.assertEqual(result.last_hidden_state.shape    , (self.batch_size, self.seq_length, self.hidden_size)      )






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:List[Any]											=							self.prepare_config_and_inputs()
         (
             (
             lowerCAmelCase__
         )							,				(
             lowerCAmelCase__
         )							,				(
             lowerCAmelCase__
         )							,				
         )			:int											=							config_and_inputs
         lowerCAmelCase__			:Optional[Any]											=							{'input_values': input_values}
         return config, inputs_dict






@require_torch
class 					_lowerCAmelCase   (     a  ,   a  ,   unittest.TestCase       ):
       """simple docstring"""




       __magic_name__     :List[Any]		  =					(
           (
               ASTModel,
               ASTForAudioClassification,
           )
           if is_torch_available()
           else ()
       )
       __magic_name__     :Tuple		  =					(
           {"""audio-classification""": ASTForAudioClassification, """feature-extraction""": ASTModel}
           if is_torch_available()
           else {}
       )
       __magic_name__     :int		  =					False
       __magic_name__     :Tuple		  =					False
       __magic_name__     :Any		  =					False
       __magic_name__     :List[Any]		  =					False






       def      snake_case						(    self    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase      ):



         '''simple docstring'''

         if pipeline_test_casse_name == "AudioClassificationPipelineTests":
           return True

         return False






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:Dict											=							ASTModelTester(self      )
         lowerCAmelCase__			:List[str]											=							ConfigTester(self    , config_class=__UpperCAmelCase    , has_text_modality=__UpperCAmelCase    , hidden_size=3_7      )






       def      snake_case						(    self      ):



         '''simple docstring'''

         self.config_tester.run_common_tests()






       @unittest.skip(reason='AST does not use inputs_embeds'      )
       def      snake_case						(    self      ):



         '''simple docstring'''

         pass






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__							,				lowerCAmelCase__			:List[str]											=							self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
           lowerCAmelCase__			:str											=							model_class(__UpperCAmelCase      )
           self.assertIsInstance(model.get_input_embeddings()    , (nn.Module)      )
           lowerCAmelCase__			:Optional[Any]											=							model.get_output_embeddings()
           self.assertTrue(x is None or isinstance(__UpperCAmelCase    , nn.Linear      )      )






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__							,				lowerCAmelCase__			:List[str]											=							self.model_tester.prepare_config_and_inputs_for_common()

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

           lowerCAmelCase__			:Optional[Any]											=							['input_values']
           self.assertListEqual(arg_names[:1]    , __UpperCAmelCase      )






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:str											=							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__UpperCAmelCase      )






       @slow
       def      snake_case						(    self      ):



         '''simple docstring'''

         for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           lowerCAmelCase__			:Union[str, Any]											=							ASTModel.from_pretrained(__UpperCAmelCase      )
           self.assertIsNotNone(__UpperCAmelCase      )






def  __A   ()					->Optional[int]:





  """simple docstring"""


  lowerCAmelCase__			:int											=							hf_hub_download(
      repo_id='nielsr/audio-spectogram-transformer-checkpoint'     ,   filename='sample_audio.flac'     ,   repo_type='dataset'			)

  lowerCAmelCase__							,				lowerCAmelCase__			:List[str]											=							torchaudio.load(_SCREAMING_SNAKE_CASE			)

  return audio, sampling_rate






@require_torch
@require_torchaudio
class 					_lowerCAmelCase   (     unittest.TestCase       ):
       """simple docstring"""




       @cached_property
       def      snake_case						(    self      ):



         '''simple docstring'''

         return (
             ASTFeatureExtractor.from_pretrained('MIT/ast-finetuned-audioset-10-10-0.4593'      )
             if is_torchaudio_available()
             else None
         )






       @slow
       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:Tuple											=							self.default_feature_extractor
         lowerCAmelCase__			:Optional[Any]											=							ASTForAudioClassification.from_pretrained('MIT/ast-finetuned-audioset-10-10-0.4593'      ).to(__UpperCAmelCase      )

         lowerCAmelCase__			:int											=							self.default_feature_extractor
         lowerCAmelCase__							,				lowerCAmelCase__			:int											=							prepare_audio()
         lowerCAmelCase__			:Optional[Any]											=							audio.squeeze().numpy()
         lowerCAmelCase__			:Any											=							feature_extractor(__UpperCAmelCase    , sampling_rate=__UpperCAmelCase    , return_tensors='pt'      ).to(__UpperCAmelCase      )

         # forward pass
         with torch.no_grad():
           lowerCAmelCase__			:Tuple											=							model(**__UpperCAmelCase      )

         # verify the logits
         lowerCAmelCase__			:Any											=							torch.Size((1, 5_2_7)      )
         self.assertEqual(outputs.logits.shape    , __UpperCAmelCase      )

         lowerCAmelCase__			:Optional[Any]											=							torch.tensor([-0.87_60, -7.00_42, -8.66_02]      ).to(__UpperCAmelCase      )

         self.assertTrue(torch.allclose(outputs.logits[0, :3]    , __UpperCAmelCase    , atol=1E-4      )      )







"""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 					_lowerCAmelCase   :
         """simple docstring"""




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



           '''simple docstring'''

           pass






  def  __A   (_SCREAMING_SNAKE_CASE			)					->int:





    """simple docstring"""


    return None


# This is a pinned image from a specific revision of a document question answering space, hosted by HuggingFace,
# so we can expect it to be available.
__A											=		(
    """https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png"""
)






@is_pipeline_test
@require_torch
@require_vision
class 					_lowerCAmelCase   (     unittest.TestCase       ):
       """simple docstring"""




       __magic_name__     :str		  =					MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING






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



         '''simple docstring'''

         lowerCAmelCase__			:Dict											=							pipeline(
             'document-question-answering'    , model=__UpperCAmelCase    , tokenizer=__UpperCAmelCase    , image_processor=__UpperCAmelCase      )

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

         lowerCAmelCase__			:List[str]											=							[
             {'score': 0.00_01, 'answer': 'oy 2312/2019', 'start': 3_8, 'end': 3_9},
             {'score': 0.00_01, 'answer': 'oy 2312/2019 DUE', 'start': 3_8, 'end': 4_0},
         ]
         lowerCAmelCase__			:Any											=							dqa_pipeline(image=__UpperCAmelCase    , question=__UpperCAmelCase    , top_k=2      )
         self.assertEqual(nested_simplify(__UpperCAmelCase    , decimals=4      )    , __UpperCAmelCase      )

         lowerCAmelCase__			:Any											=							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__			:List[Any]											=							'./tests/fixtures/tests_samples/COCO/000000039769.png'
         lowerCAmelCase__			:List[Any]											=							dqa_pipeline(image=__UpperCAmelCase    , question=__UpperCAmelCase    , top_k=2      )
         self.assertEqual(__UpperCAmelCase    , []      )

         # We can optionnally pass directly the words and bounding boxes
         lowerCAmelCase__			:Dict											=							'./tests/fixtures/tests_samples/COCO/000000039769.png'
         lowerCAmelCase__			:List[str]											=							[]
         lowerCAmelCase__			:int											=							[]
         lowerCAmelCase__			:List[str]											=							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__			:Union[str, Any]											=							pipeline(
             'document-question-answering'    , model='tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'    , revision='9977165'    , )
         lowerCAmelCase__			:str											=							INVOICE_URL
         lowerCAmelCase__			:List[Any]											=							'What is the invoice number?'

         lowerCAmelCase__			:Tuple											=							dqa_pipeline(image=__UpperCAmelCase    , question=__UpperCAmelCase    , top_k=2      )
         self.assertEqual(
             nested_simplify(__UpperCAmelCase    , decimals=4      )    , [
                 {'score': 0.99_44, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
                 {'score': 0.00_09, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
             ]    , )

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

         lowerCAmelCase__			:Dict											=							dqa_pipeline(
             [{'image': image, 'question': question}, {'image': image, 'question': question}]    , top_k=2      )
         self.assertEqual(
             nested_simplify(__UpperCAmelCase    , decimals=4      )    , [
                 [
                     {'score': 0.99_44, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
                     {'score': 0.00_09, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
                 ],
             ]
             * 2    , )






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



         '''simple docstring'''

         lowerCAmelCase__			:Tuple											=							pipeline(
             'document-question-answering'    , model='tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'    , revision='9977165'    , max_seq_len=5_0    , )
         lowerCAmelCase__			:List[Any]											=							INVOICE_URL
         lowerCAmelCase__			:List[Any]											=							'What is the invoice number?'

         lowerCAmelCase__			:Optional[Any]											=							dqa_pipeline(image=__UpperCAmelCase    , question=__UpperCAmelCase    , top_k=2      )
         self.assertEqual(
             nested_simplify(__UpperCAmelCase    , decimals=4      )    , [
                 {'score': 0.99_74, 'answer': '1110212019', 'start': 2_3, 'end': 2_3},
                 {'score': 0.99_48, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
             ]    , )

         lowerCAmelCase__			:List[str]											=							dqa_pipeline({'image': image, 'question': question}    , top_k=2      )
         self.assertEqual(
             nested_simplify(__UpperCAmelCase    , decimals=4      )    , [
                 {'score': 0.99_74, 'answer': '1110212019', 'start': 2_3, 'end': 2_3},
                 {'score': 0.99_48, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
             ]    , )

         lowerCAmelCase__			:int											=							dqa_pipeline(
             [{'image': image, 'question': question}, {'image': image, 'question': question}]    , top_k=2      )
         self.assertEqual(
             nested_simplify(__UpperCAmelCase    , decimals=4      )    , [
                 [
                     {'score': 0.99_74, 'answer': '1110212019', 'start': 2_3, 'end': 2_3},
                     {'score': 0.99_48, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
                 ]
             ]
             * 2    , )






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



         '''simple docstring'''

         lowerCAmelCase__			:List[Any]											=							AutoTokenizer.from_pretrained(
             'impira/layoutlm-document-qa'    , revision='3dc6de3'    , add_prefix_space=__UpperCAmelCase      )
         lowerCAmelCase__			:Optional[Any]											=							pipeline(
             'document-question-answering'    , model='impira/layoutlm-document-qa'    , tokenizer=__UpperCAmelCase    , revision='3dc6de3'    , )
         lowerCAmelCase__			:List[str]											=							INVOICE_URL
         lowerCAmelCase__			:Any											=							'What is the invoice number?'

         lowerCAmelCase__			:List[Any]											=							dqa_pipeline(image=__UpperCAmelCase    , question=__UpperCAmelCase    , top_k=2      )
         self.assertEqual(
             nested_simplify(__UpperCAmelCase    , decimals=4      )    , [
                 {'score': 0.42_51, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
                 {'score': 0.08_19, 'answer': '1110212019', 'start': 2_3, 'end': 2_3},
             ]    , )

         lowerCAmelCase__			:Optional[int]											=							dqa_pipeline({'image': image, 'question': question}    , top_k=2      )
         self.assertEqual(
             nested_simplify(__UpperCAmelCase    , decimals=4      )    , [
                 {'score': 0.42_51, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
                 {'score': 0.08_19, 'answer': '1110212019', 'start': 2_3, 'end': 2_3},
             ]    , )

         lowerCAmelCase__			:List[str]											=							dqa_pipeline(
             [{'image': image, 'question': question}, {'image': image, 'question': question}]    , top_k=2      )
         self.assertEqual(
             nested_simplify(__UpperCAmelCase    , decimals=4      )    , [
                 [
                     {'score': 0.42_51, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
                     {'score': 0.08_19, 'answer': '1110212019', 'start': 2_3, 'end': 2_3},
                 ]
             ]
             * 2    , )

         lowerCAmelCase__			:Dict											=							list(zip(*apply_tesseract(load_image(__UpperCAmelCase      )    , __UpperCAmelCase    , ''      )      )      )

         # This model should also work if `image` is set to None
         lowerCAmelCase__			:Tuple											=							dqa_pipeline({'image': None, 'word_boxes': word_boxes, 'question': question}    , top_k=2      )
         self.assertEqual(
             nested_simplify(__UpperCAmelCase    , decimals=4      )    , [
                 {'score': 0.42_51, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
                 {'score': 0.08_19, 'answer': '1110212019', 'start': 2_3, 'end': 2_3},
             ]    , )






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



         '''simple docstring'''

         lowerCAmelCase__			:Optional[Any]											=							AutoTokenizer.from_pretrained(
             'impira/layoutlm-document-qa'    , revision='3dc6de3'    , add_prefix_space=__UpperCAmelCase      )
         lowerCAmelCase__			:Tuple											=							pipeline(
             'document-question-answering'    , model='impira/layoutlm-document-qa'    , tokenizer=__UpperCAmelCase    , revision='3dc6de3'    , max_seq_len=5_0    , )
         lowerCAmelCase__			:Dict											=							INVOICE_URL
         lowerCAmelCase__			:List[Any]											=							'What is the invoice number?'

         lowerCAmelCase__			:List[str]											=							dqa_pipeline(image=__UpperCAmelCase    , question=__UpperCAmelCase    , top_k=2      )
         self.assertEqual(
             nested_simplify(__UpperCAmelCase    , decimals=4      )    , [
                 {'score': 0.99_99, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
                 {'score': 0.99_98, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
             ]    , )

         lowerCAmelCase__			:List[str]											=							dqa_pipeline(
             [{'image': image, 'question': question}, {'image': image, 'question': question}]    , top_k=2      )
         self.assertEqual(
             nested_simplify(__UpperCAmelCase    , decimals=4      )    , [
                 [
                     {'score': 0.99_99, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
                     {'score': 0.99_98, 'answer': 'us-001', 'start': 1_6, 'end': 1_6},
                 ]
             ]
             * 2    , )

         lowerCAmelCase__			:Optional[Any]											=							list(zip(*apply_tesseract(load_image(__UpperCAmelCase      )    , __UpperCAmelCase    , ''      )      )      )

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






       @slow
       @require_torch
       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:List[str]											=							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__			:Dict											=							INVOICE_URL
         lowerCAmelCase__			:str											=							'What is the invoice number?'
         lowerCAmelCase__			:Tuple											=							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
