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

      """simple docstring"""







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






    def 						__magic_name__ (    __a			:       str						):



         '''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.
lowerCamelCase_							=     (
    "https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png"
)




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

  """simple docstring"""
  SCREAMING_SNAKE_CASE__			    =			MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING







  @require_pytesseract
  @require_vision
  def        UpperCAmelCase_					(self       ,			SCREAMING_SNAKE_CASE_       ,			SCREAMING_SNAKE_CASE_       ,			SCREAMING_SNAKE_CASE_						):
       UpperCamelCase__				=				pipeline(
           """document-question-answering"""       ,			model=_A       ,			tokenizer=_A       ,			image_processor=_A						)

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







  def        UpperCAmelCase_					(self       ,			SCREAMING_SNAKE_CASE_       ,			SCREAMING_SNAKE_CASE_						):
       UpperCamelCase__				=				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        UpperCAmelCase_					(self						):
       UpperCamelCase__				=				pipeline("""document-question-answering"""       ,			model="""hf-internal-testing/tiny-random-layoutlmv2"""						)
       UpperCamelCase__				=				INVOICE_URL
       UpperCamelCase__				=				"""How many cats are there?"""

       UpperCamelCase__				=				[
           {"""score""": 0.0001, """answer""": """oy 2312/2019""", """start""": 38, """end""": 39},
           {"""score""": 0.0001, """answer""": """oy 2312/2019 DUE""", """start""": 38, """end""": 40},
       ]
       UpperCamelCase__				=				dqa_pipeline(image=_A       ,			question=_A       ,			top_k=2						)
       self.assertEqual(nested_simplify(_A       ,			decimals=4						)       ,			_A						)

       UpperCamelCase__				=				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
       UpperCamelCase__				=				"""./tests/fixtures/tests_samples/COCO/000000039769.png"""
       UpperCamelCase__				=				dqa_pipeline(image=_A       ,			question=_A       ,			top_k=2						)
       self.assertEqual(_A       ,			[]						)

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







  @slow
  @require_torch
  @require_detectrona
  @require_pytesseract
  def        UpperCAmelCase_					(self						):
       UpperCamelCase__				=				pipeline(
           """document-question-answering"""       ,			model="""tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"""       ,			revision="""9977165"""       ,			)
       UpperCamelCase__				=				INVOICE_URL
       UpperCamelCase__				=				"""What is the invoice number?"""

       UpperCamelCase__				=				dqa_pipeline(image=_A       ,			question=_A       ,			top_k=2						)
       self.assertEqual(
           nested_simplify(_A       ,			decimals=4						)       ,			[
               {"""score""": 0.9944, """answer""": """us-001""", """start""": 16, """end""": 16},
               {"""score""": 0.0009, """answer""": """us-001""", """start""": 16, """end""": 16},
           ]       ,			)

       UpperCamelCase__				=				dqa_pipeline({"""image""": image, """question""": question}       ,			top_k=2						)
       self.assertEqual(
           nested_simplify(_A       ,			decimals=4						)       ,			[
               {"""score""": 0.9944, """answer""": """us-001""", """start""": 16, """end""": 16},
               {"""score""": 0.0009, """answer""": """us-001""", """start""": 16, """end""": 16},
           ]       ,			)

       UpperCamelCase__				=				dqa_pipeline(
           [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]       ,			top_k=2						)
       self.assertEqual(
           nested_simplify(_A       ,			decimals=4						)       ,			[
               [
                   {"""score""": 0.9944, """answer""": """us-001""", """start""": 16, """end""": 16},
                   {"""score""": 0.0009, """answer""": """us-001""", """start""": 16, """end""": 16},
               ],
           ]
           * 2       ,			)







  @slow
  @require_torch
  @require_detectrona
  @require_pytesseract
  def        UpperCAmelCase_					(self						):
       UpperCamelCase__				=				pipeline(
           """document-question-answering"""       ,			model="""tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"""       ,			revision="""9977165"""       ,			max_seq_len=50       ,			)
       UpperCamelCase__				=				INVOICE_URL
       UpperCamelCase__				=				"""What is the invoice number?"""

       UpperCamelCase__				=				dqa_pipeline(image=_A       ,			question=_A       ,			top_k=2						)
       self.assertEqual(
           nested_simplify(_A       ,			decimals=4						)       ,			[
               {"""score""": 0.9974, """answer""": """1110212019""", """start""": 23, """end""": 23},
               {"""score""": 0.9948, """answer""": """us-001""", """start""": 16, """end""": 16},
           ]       ,			)

       UpperCamelCase__				=				dqa_pipeline({"""image""": image, """question""": question}       ,			top_k=2						)
       self.assertEqual(
           nested_simplify(_A       ,			decimals=4						)       ,			[
               {"""score""": 0.9974, """answer""": """1110212019""", """start""": 23, """end""": 23},
               {"""score""": 0.9948, """answer""": """us-001""", """start""": 16, """end""": 16},
           ]       ,			)

       UpperCamelCase__				=				dqa_pipeline(
           [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]       ,			top_k=2						)
       self.assertEqual(
           nested_simplify(_A       ,			decimals=4						)       ,			[
               [
                   {"""score""": 0.9974, """answer""": """1110212019""", """start""": 23, """end""": 23},
                   {"""score""": 0.9948, """answer""": """us-001""", """start""": 16, """end""": 16},
               ]
           ]
           * 2       ,			)







  @slow
  @require_torch
  @require_pytesseract
  @require_vision
  def        UpperCAmelCase_					(self						):
       UpperCamelCase__				=				AutoTokenizer.from_pretrained(
           """impira/layoutlm-document-qa"""       ,			revision="""3dc6de3"""       ,			add_prefix_space=_A						)
       UpperCamelCase__				=				pipeline(
           """document-question-answering"""       ,			model="""impira/layoutlm-document-qa"""       ,			tokenizer=_A       ,			revision="""3dc6de3"""       ,			)
       UpperCamelCase__				=				INVOICE_URL
       UpperCamelCase__				=				"""What is the invoice number?"""

       UpperCamelCase__				=				dqa_pipeline(image=_A       ,			question=_A       ,			top_k=2						)
       self.assertEqual(
           nested_simplify(_A       ,			decimals=4						)       ,			[
               {"""score""": 0.4251, """answer""": """us-001""", """start""": 16, """end""": 16},
               {"""score""": 0.0819, """answer""": """1110212019""", """start""": 23, """end""": 23},
           ]       ,			)

       UpperCamelCase__				=				dqa_pipeline({"""image""": image, """question""": question}       ,			top_k=2						)
       self.assertEqual(
           nested_simplify(_A       ,			decimals=4						)       ,			[
               {"""score""": 0.4251, """answer""": """us-001""", """start""": 16, """end""": 16},
               {"""score""": 0.0819, """answer""": """1110212019""", """start""": 23, """end""": 23},
           ]       ,			)

       UpperCamelCase__				=				dqa_pipeline(
           [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]       ,			top_k=2						)
       self.assertEqual(
           nested_simplify(_A       ,			decimals=4						)       ,			[
               [
                   {"""score""": 0.4251, """answer""": """us-001""", """start""": 16, """end""": 16},
                   {"""score""": 0.0819, """answer""": """1110212019""", """start""": 23, """end""": 23},
               ]
           ]
           * 2       ,			)

       UpperCamelCase__				=				list(zip(*apply_tesseract(load_image(_A						)       ,			_A       ,			""""""						)						)						)

       # This model should also work if `image` is set to None
       UpperCamelCase__				=				dqa_pipeline({"""image""": None, """word_boxes""": word_boxes, """question""": question}       ,			top_k=2						)
       self.assertEqual(
           nested_simplify(_A       ,			decimals=4						)       ,			[
               {"""score""": 0.4251, """answer""": """us-001""", """start""": 16, """end""": 16},
               {"""score""": 0.0819, """answer""": """1110212019""", """start""": 23, """end""": 23},
           ]       ,			)







  @slow
  @require_torch
  @require_pytesseract
  @require_vision
  def        UpperCAmelCase_					(self						):
       UpperCamelCase__				=				AutoTokenizer.from_pretrained(
           """impira/layoutlm-document-qa"""       ,			revision="""3dc6de3"""       ,			add_prefix_space=_A						)
       UpperCamelCase__				=				pipeline(
           """document-question-answering"""       ,			model="""impira/layoutlm-document-qa"""       ,			tokenizer=_A       ,			revision="""3dc6de3"""       ,			max_seq_len=50       ,			)
       UpperCamelCase__				=				INVOICE_URL
       UpperCamelCase__				=				"""What is the invoice number?"""

       UpperCamelCase__				=				dqa_pipeline(image=_A       ,			question=_A       ,			top_k=2						)
       self.assertEqual(
           nested_simplify(_A       ,			decimals=4						)       ,			[
               {"""score""": 0.9999, """answer""": """us-001""", """start""": 16, """end""": 16},
               {"""score""": 0.9998, """answer""": """us-001""", """start""": 16, """end""": 16},
           ]       ,			)

       UpperCamelCase__				=				dqa_pipeline(
           [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]       ,			top_k=2						)
       self.assertEqual(
           nested_simplify(_A       ,			decimals=4						)       ,			[
               [
                   {"""score""": 0.9999, """answer""": """us-001""", """start""": 16, """end""": 16},
                   {"""score""": 0.9998, """answer""": """us-001""", """start""": 16, """end""": 16},
               ]
           ]
           * 2       ,			)

       UpperCamelCase__				=				list(zip(*apply_tesseract(load_image(_A						)       ,			_A       ,			""""""						)						)						)

       # This model should also work if `image` is set to None
       UpperCamelCase__				=				dqa_pipeline({"""image""": None, """word_boxes""": word_boxes, """question""": question}       ,			top_k=2						)
       self.assertEqual(
           nested_simplify(_A       ,			decimals=4						)       ,			[
               {"""score""": 0.9999, """answer""": """us-001""", """start""": 16, """end""": 16},
               {"""score""": 0.9998, """answer""": """us-001""", """start""": 16, """end""": 16},
           ]       ,			)







  @slow
  @require_torch
  def        UpperCAmelCase_					(self						):
       UpperCamelCase__				=				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"""       ,			)

       UpperCamelCase__				=				INVOICE_URL
       UpperCamelCase__				=				"""What is the invoice number?"""
       UpperCamelCase__				=				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        UpperCAmelCase_					(self						):
       pass












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

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


if TYPE_CHECKING:
				from ...onnx.config import PatchingSpec
				from ...tokenization_utils_base import PreTrainedTokenizerBase


__A				:    Dict       =   logging.get_logger(__name__)

__A				:    str       =   {
    "allenai/longformer-base-4096": "https://huggingface.co/allenai/longformer-base-4096/resolve/main/config.json",
    "allenai/longformer-large-4096": "https://huggingface.co/allenai/longformer-large-4096/resolve/main/config.json",
    "allenai/longformer-large-4096-finetuned-triviaqa": (
        "https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/config.json"
    ),
    "allenai/longformer-base-4096-extra.pos.embd.only": (
        "https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/config.json"
    ),
    "allenai/longformer-large-4096-extra.pos.embd.only": (
        "https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/config.json"
    ),
}

class 	A_       (a_			):
	UpperCAmelCase__					=			'''longformer'''
	def __init__(    self							,					_A = 5_1_2							,					_A = 2							,					_A = 1							,					_A = 0							,					_A = 2							,					_A = 3_0_5_2_2							,					_A = 7_6_8							,					_A = 1_2							,					_A = 1_2							,					_A = 3_0_7_2							,					_A = "gelu"							,					_A = 0.1							,					_A = 0.1							,					_A = 5_1_2							,					_A = 2							,					_A = 0.02							,					_A = 1E-12							,					_A = False							,					**_A							,					):






				'''simple docstring'''







				super().__init__(pad_token_id=_A							,					**_A		)

				UpperCAmelCase   		=				attention_window
				UpperCAmelCase   		=				sep_token_id
				UpperCAmelCase   		=				bos_token_id
				UpperCAmelCase   		=				eos_token_id
				UpperCAmelCase   		=				vocab_size
				UpperCAmelCase   		=				hidden_size
				UpperCAmelCase   		=				num_hidden_layers
				UpperCAmelCase   		=				num_attention_heads
				UpperCAmelCase   		=				hidden_act
				UpperCAmelCase   		=				intermediate_size
				UpperCAmelCase   		=				hidden_dropout_prob
				UpperCAmelCase   		=				attention_probs_dropout_prob
				UpperCAmelCase   		=				max_position_embeddings
				UpperCAmelCase   		=				type_vocab_size
				UpperCAmelCase   		=				initializer_range
				UpperCAmelCase   		=				layer_norm_eps
				UpperCAmelCase   		=				onnx_export

class 	A_       (a_			):
	def __init__(    self							,					_A							,					_A = "default"							,					_A = None		):






				'''simple docstring'''







				super().__init__(_A							,					_A							,					_A		)
				UpperCAmelCase   		=				True
	@property
	def      _lowercase				(    self		):






				'''simple docstring'''







				if self.task == "multiple-choice":
							UpperCAmelCase   		=				{0: '''batch''', 1: '''choice''', 2: '''sequence'''}
				else:
							UpperCAmelCase   		=				{0: '''batch''', 1: '''sequence'''}
				return OrderedDict(
				    [
				        ('''input_ids''', dynamic_axis),
				        ('''attention_mask''', dynamic_axis),
				        ('''global_attention_mask''', dynamic_axis),
				    ]		)
	@property
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				super().outputs
				if self.task == "default":
							UpperCAmelCase   		=				{0: '''batch'''}
				return outputs
	@property
	def      _lowercase				(    self		):






				'''simple docstring'''







				return 1E-4
	@property
	def      _lowercase				(    self		):






				'''simple docstring'''







				return max(super().default_onnx_opset							,					1_4		)







	def      _lowercase				(    self							,					_A							,					_A = -1							,					_A = -1							,					_A = False							,					_A = None							,					):






				'''simple docstring'''







				UpperCAmelCase   		=				super().generate_dummy_inputs(
				    preprocessor=_A							,					batch_size=_A							,					seq_length=_A							,					is_pair=_A							,					framework=_A		)
				import torch

				# for some reason, replacing this code by inputs["global_attention_mask"] = torch.randint(2, inputs["input_ids"].shape, dtype=torch.int64)
				# makes the export fail randomly
				UpperCAmelCase   		=				torch.zeros_like(inputs['''input_ids''']		)
				# make every second token global
				UpperCAmelCase   		=				1

				return inputs



