





from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Mapping, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig, OnnxSeqaSeqConfigWithPast
from ...utils import logging


if TYPE_CHECKING:
    from ...feature_extraction_utils import FeatureExtractionMixin
    from ...tokenization_utils_base import PreTrainedTokenizerBase
    from ...utils import TensorType

snake_case_				=			logging.get_logger(__name__)

snake_case_				=			{
    'openai/whisper-base': 'https://huggingface.co/openai/whisper-base/resolve/main/config.json',
}

# fmt: off
snake_case_				=			[
    1, 2, 7, 8, 9, 10, 14, 25,
    26, 27, 28, 29, 31, 58, 59, 60, 61, 62,
    63, 90, 91, 92, 93, 357, 366, 438, 532, 685,
    705, 796, 930, 1058, 1220, 1267, 1279, 1303, 1343, 1377,
    1391, 1635, 1782, 1875, 2162, 2361, 2488, 3467, 4008, 4211,
    4600, 4808, 5299, 5855, 6329, 7203, 9609, 9959, 10563, 10786,
    11420, 11709, 11907, 13163, 13697, 13700, 14808, 15306, 16410, 16791,
    17992, 19203, 19510, 20724, 22305, 22935, 27007, 30109, 30420, 33409,
    34949, 40283, 40493, 40549, 47282, 49146, 50257, 50359, 50360, 50361
]
snake_case_				=			[
    1, 2, 7, 8, 9, 10, 14, 25,
    26, 27, 28, 29, 31, 58, 59, 60, 61, 62,
    63, 90, 91, 92, 93, 359, 503, 522, 542, 873,
    893, 902, 918, 922, 931, 1350, 1853, 1982, 2460, 2627,
    3246, 3253, 3268, 3536, 3846, 3961, 4183, 4667, 6585, 6647,
    7273, 9061, 9383, 10428, 10929, 11938, 12033, 12331, 12562, 13793,
    14157, 14635, 15265, 15618, 16553, 16604, 18362, 18956, 20075, 21675,
    22520, 26130, 26161, 26435, 28279, 29464, 31650, 32302, 32470, 36865,
    42863, 47425, 49870, 50254, 50258, 50360, 50361, 50362
]

class 		SCREAMING_SNAKE_CASE__				(       _UpperCAmelCase  ):
      A_						:				int							=    'whisper'
      A_						:				int							=    ['past_key_values']
      A_						:				Union[str, Any]							=    {'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}


      def __init__(self       :      int				,			a__       :      List[str]=5_1865				,			a__       :      int=80				,			a__       :      Optional[int]=6				,			a__       :      List[Any]=4				,			a__       :      Optional[Any]=6				,			a__       :      List[str]=4				,			a__       :      Optional[int]=1536				,			a__       :      Tuple=1536				,			a__       :      Tuple=0.0				,			a__       :      List[Any]=0.0				,			a__       :      Optional[int]=5_0257				,			a__       :      Any=True				,			a__       :      Union[str, Any]=True				,			a__       :      Any="gelu"				,			a__       :      Union[str, Any]=256				,			a__       :      Dict=0.0				,			a__       :      Dict=0.0				,			a__       :      Any=0.0				,			a__       :      Dict=0.0_2				,			a__       :      Optional[Any]=False				,			a__       :      List[Any]=1500				,			a__       :      Dict=448				,			a__       :      Optional[int]=5_0256				,			a__       :      Any=5_0256				,			a__       :      Tuple=5_0256				,			a__       :      Union[str, Any]=None				,			a__       :      Any=[220, 5_0256]				,			a__       :      Tuple=False				,			a__       :      Any=256				,			a__       :      int=False				,			a__       :      Optional[int]=0.0_5				,			a__       :      Dict=10				,			a__       :      List[Any]=2				,			a__       :      List[str]=0.0				,			a__       :      Optional[int]=10				,			a__       :      Dict=0				,			a__       :      List[Any]=7				,			**a__       :      Union[str, Any]				,			):





         """simple docstring"""



         __snake_case							       =   vocab_size
         __snake_case							       =   num_mel_bins
         __snake_case							       =   d_model
         __snake_case							       =   encoder_layers
         __snake_case							       =   encoder_attention_heads
         __snake_case							       =   decoder_layers
         __snake_case							       =   decoder_attention_heads
         __snake_case							       =   decoder_ffn_dim
         __snake_case							       =   encoder_ffn_dim
         __snake_case							       =   dropout
         __snake_case							       =   attention_dropout
         __snake_case							       =   activation_dropout
         __snake_case							       =   activation_function
         __snake_case							       =   init_std
         __snake_case							       =   encoder_layerdrop
         __snake_case							       =   decoder_layerdrop
         __snake_case							       =   use_cache
         __snake_case							       =   encoder_layers
         __snake_case							       =   scale_embedding  # scale factor will be sqrt(d_model) if True
         __snake_case							       =   max_source_positions
         __snake_case							       =   max_target_positions

         # Audio Classification-specific parameters. Feel free to ignore for other classes.
         __snake_case							       =   classifier_proj_size
         __snake_case							       =   use_weighted_layer_sum

         # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
         __snake_case							       =   apply_spec_augment
         __snake_case							       =   mask_time_prob
         __snake_case							       =   mask_time_length
         __snake_case							       =   mask_time_min_masks
         __snake_case							       =   mask_feature_prob
         __snake_case							       =   mask_feature_length
         __snake_case							       =   mask_feature_min_masks

         __snake_case							       =   median_filter_width

         super().__init__(
             pad_token_id=a__				,			bos_token_id=a__				,			eos_token_id=a__				,			is_encoder_decoder=a__				,			decoder_start_token_id=a__				,			suppress_tokens=a__				,			begin_suppress_tokens=a__				,			**a__				,			)






class 		SCREAMING_SNAKE_CASE__				(       _UpperCAmelCase  ):


      @property
      def    a (self       :      List[str]      ):





         """simple docstring"""



         __snake_case							       =   OrderedDict(
             [
                 ('''input_features''', {0: '''batch''', 1: '''feature_size''', 2: '''encoder_sequence'''}),
             ]      )
         if self.use_past:
            __snake_case							       =   {0: '''batch'''}
         else:
            __snake_case							       =   {0: '''batch''', 1: '''decoder_sequence'''}

         if self.use_past:
            self.fill_with_past_key_values_(a__				,			direction='''inputs'''      )

         return common_inputs


      def    a (self       :      Dict				,			a__       :      Union["PreTrainedTokenizerBase", "FeatureExtractionMixin"]				,			a__       :      int = -1				,			a__       :      int = -1				,			a__       :      bool = False				,			a__       :      Optional["TensorType"] = None				,			a__       :      int = 2_2050				,			a__       :      float = 5.0				,			a__       :      int = 220				,			):





         """simple docstring"""



         __snake_case							       =   OrderedDict()
         __snake_case							       =   OnnxConfig.generate_dummy_inputs(
             self				,			preprocessor=preprocessor.feature_extractor				,			batch_size=a__				,			framework=a__				,			sampling_rate=a__				,			time_duration=a__				,			frequency=a__				,			)
         __snake_case							       =   encoder_inputs['''input_features'''].shape[2]
         __snake_case							       =   encoder_sequence_length // 2 if self.use_past else seq_length

         __snake_case							       =   super().generate_dummy_inputs(
             preprocessor.tokenizer				,			a__				,			a__				,			a__				,			a__      )

         __snake_case							       =   encoder_inputs.pop('''input_features'''      )
         __snake_case							       =   decoder_inputs.pop('''decoder_input_ids'''      )

         if "past_key_values" in decoder_inputs:
            __snake_case							       =   decoder_inputs.pop('''past_key_values'''      )

         return dummy_inputs


      @property
      def    a (self       :      Union[str, Any]      ):





         """simple docstring"""



         return 1E-3








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


          @staticmethod
          def    a (*a__       :      List[str]				,			**a__       :      List[str]      ):





             """simple docstring"""



             pass






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

@is_pipeline_test
@require_torch
@require_vision
class 		SCREAMING_SNAKE_CASE__				(       unittest.TestCase  ):
      A_						:				Optional[Any]							=    MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING


      @require_pytesseract
      @require_vision
      def    a (self       :      List[Any]				,			a__       :      Tuple				,			a__       :      Union[str, Any]				,			a__       :      Any      ):





         """simple docstring"""



         __snake_case							       =   pipeline(
             '''document-question-answering'''				,			model=a__				,			tokenizer=a__				,			image_processor=a__      )

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


      def    a (self       :      Union[str, Any]				,			a__       :      Optional[int]				,			a__       :      Dict      ):





         """simple docstring"""



         __snake_case							       =   dqa_pipeline(a__				,			top_k=2      )
         self.assertEqual(
             a__				,			[
                 [
                     {'''score''': ANY(a__      ), '''answer''': ANY(a__      ), '''start''': ANY(a__      ), '''end''': ANY(a__      )},
                     {'''score''': ANY(a__      ), '''answer''': ANY(a__      ), '''start''': ANY(a__      ), '''end''': ANY(a__      )},
                 ]
             ]
             * 3				,			)


      @require_torch
      @require_detectrona
      @require_pytesseract
      def    a (self       :      Dict      ):





         """simple docstring"""



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

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

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

         # This image does not detect ANY text in it, meaning layoutlmv2 should fail.
         # Empty answer probably
         __snake_case							       =   '''./tests/fixtures/tests_samples/COCO/000000039769.png'''
         __snake_case							       =   dqa_pipeline(image=a__				,			question=a__				,			top_k=2      )
         self.assertEqual(a__				,			[]      )

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


      @slow
      @require_torch
      @require_detectrona
      @require_pytesseract
      def    a (self       :      str      ):





         """simple docstring"""



         __snake_case							       =   pipeline(
             '''document-question-answering'''				,			model='''tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'''				,			revision='''9977165'''				,			)
         __snake_case							       =   INVOICE_URL
         __snake_case							       =   '''What is the invoice number?'''

         __snake_case							       =   dqa_pipeline(image=a__				,			question=a__				,			top_k=2      )
         self.assertEqual(
             nested_simplify(a__				,			decimals=4      )				,			[
                 {'''score''': 0.9_9_4_4, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
                 {'''score''': 0.0_0_0_9, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
             ]				,			)

         __snake_case							       =   dqa_pipeline({'''image''': image, '''question''': question}				,			top_k=2      )
         self.assertEqual(
             nested_simplify(a__				,			decimals=4      )				,			[
                 {'''score''': 0.9_9_4_4, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
                 {'''score''': 0.0_0_0_9, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
             ]				,			)

         __snake_case							       =   dqa_pipeline(
             [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]				,			top_k=2      )
         self.assertEqual(
             nested_simplify(a__				,			decimals=4      )				,			[
                 [
                     {'''score''': 0.9_9_4_4, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
                     {'''score''': 0.0_0_0_9, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
                 ],
             ]
             * 2				,			)


      @slow
      @require_torch
      @require_detectrona
      @require_pytesseract
      def    a (self       :      List[Any]      ):





         """simple docstring"""



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

         __snake_case							       =   dqa_pipeline(image=a__				,			question=a__				,			top_k=2      )
         self.assertEqual(
             nested_simplify(a__				,			decimals=4      )				,			[
                 {'''score''': 0.9_9_7_4, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
                 {'''score''': 0.9_9_4_8, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
             ]				,			)

         __snake_case							       =   dqa_pipeline({'''image''': image, '''question''': question}				,			top_k=2      )
         self.assertEqual(
             nested_simplify(a__				,			decimals=4      )				,			[
                 {'''score''': 0.9_9_7_4, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
                 {'''score''': 0.9_9_4_8, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
             ]				,			)

         __snake_case							       =   dqa_pipeline(
             [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]				,			top_k=2      )
         self.assertEqual(
             nested_simplify(a__				,			decimals=4      )				,			[
                 [
                     {'''score''': 0.9_9_7_4, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
                     {'''score''': 0.9_9_4_8, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
                 ]
             ]
             * 2				,			)


      @slow
      @require_torch
      @require_pytesseract
      @require_vision
      def    a (self       :      Tuple      ):





         """simple docstring"""



         __snake_case							       =   AutoTokenizer.from_pretrained(
             '''impira/layoutlm-document-qa'''				,			revision='''3dc6de3'''				,			add_prefix_space=a__      )
         __snake_case							       =   pipeline(
             '''document-question-answering'''				,			model='''impira/layoutlm-document-qa'''				,			tokenizer=a__				,			revision='''3dc6de3'''				,			)
         __snake_case							       =   INVOICE_URL
         __snake_case							       =   '''What is the invoice number?'''

         __snake_case							       =   dqa_pipeline(image=a__				,			question=a__				,			top_k=2      )
         self.assertEqual(
             nested_simplify(a__				,			decimals=4      )				,			[
                 {'''score''': 0.4_2_5_1, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
                 {'''score''': 0.0_8_1_9, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
             ]				,			)

         __snake_case							       =   dqa_pipeline({'''image''': image, '''question''': question}				,			top_k=2      )
         self.assertEqual(
             nested_simplify(a__				,			decimals=4      )				,			[
                 {'''score''': 0.4_2_5_1, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
                 {'''score''': 0.0_8_1_9, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
             ]				,			)

         __snake_case							       =   dqa_pipeline(
             [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]				,			top_k=2      )
         self.assertEqual(
             nested_simplify(a__				,			decimals=4      )				,			[
                 [
                     {'''score''': 0.4_2_5_1, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
                     {'''score''': 0.0_8_1_9, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
                 ]
             ]
             * 2				,			)

         __snake_case							       =   list(zip(*apply_tesseract(load_image(a__      )				,			a__				,			''''''      )      )      )

         # This model should also work if `image` is set to None
         __snake_case							       =   dqa_pipeline({'''image''': None, '''word_boxes''': word_boxes, '''question''': question}				,			top_k=2      )
         self.assertEqual(
             nested_simplify(a__				,			decimals=4      )				,			[
                 {'''score''': 0.4_2_5_1, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
                 {'''score''': 0.0_8_1_9, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
             ]				,			)


      @slow
      @require_torch
      @require_pytesseract
      @require_vision
      def    a (self       :      Dict      ):





         """simple docstring"""



         __snake_case							       =   AutoTokenizer.from_pretrained(
             '''impira/layoutlm-document-qa'''				,			revision='''3dc6de3'''				,			add_prefix_space=a__      )
         __snake_case							       =   pipeline(
             '''document-question-answering'''				,			model='''impira/layoutlm-document-qa'''				,			tokenizer=a__				,			revision='''3dc6de3'''				,			max_seq_len=50				,			)
         __snake_case							       =   INVOICE_URL
         __snake_case							       =   '''What is the invoice number?'''

         __snake_case							       =   dqa_pipeline(image=a__				,			question=a__				,			top_k=2      )
         self.assertEqual(
             nested_simplify(a__				,			decimals=4      )				,			[
                 {'''score''': 0.9_9_9_9, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
                 {'''score''': 0.9_9_9_8, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
             ]				,			)

         __snake_case							       =   dqa_pipeline(
             [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]				,			top_k=2      )
         self.assertEqual(
             nested_simplify(a__				,			decimals=4      )				,			[
                 [
                     {'''score''': 0.9_9_9_9, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
                     {'''score''': 0.9_9_9_8, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
                 ]
             ]
             * 2				,			)

         __snake_case							       =   list(zip(*apply_tesseract(load_image(a__      )				,			a__				,			''''''      )      )      )

         # This model should also work if `image` is set to None
         __snake_case							       =   dqa_pipeline({'''image''': None, '''word_boxes''': word_boxes, '''question''': question}				,			top_k=2      )
         self.assertEqual(
             nested_simplify(a__				,			decimals=4      )				,			[
                 {'''score''': 0.9_9_9_9, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
                 {'''score''': 0.9_9_9_8, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
             ]				,			)


      @slow
      @require_torch
      def    a (self       :      Tuple      ):





         """simple docstring"""



         __snake_case							       =   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'''				,			)

         __snake_case							       =   INVOICE_URL
         __snake_case							       =   '''What is the invoice number?'''
         __snake_case							       =   dqa_pipeline(image=a__				,			question=a__				,			top_k=2      )
         self.assertEqual(nested_simplify(a__				,			decimals=4      )				,			[{'''answer''': '''us-001'''}]      )


      @require_tf
      @unittest.skip('''Document question answering not implemented in TF'''      )
      def    a (self       :      List[str]      ):





         """simple docstring"""



         pass

