




"""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 					A_    (  *lowercase  ,      **lowercase  ):
										pass







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






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






			'''simple docstring'''



			lowerCamelCase__      =			MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING
			@require_pytesseract
			@require_vision
			def 					A_    (  self  ,      lowercase  ,      lowercase  ,      lowercase  ):
							_lowerCamelCase     :			Optional[Any]    =    pipeline(
							    'document-question-answering'  ,      model=lowercase  ,      tokenizer=lowercase  ,      image_processor=lowercase  )

							_lowerCamelCase     :			Dict    =    INVOICE_URL
							_lowerCamelCase     :			List[str]    =    list(zip(*apply_tesseract(load_image(lowercase  )  ,      lowercase  ,      ''  )  )  )
							_lowerCamelCase     :			Optional[Any]    =    'What is the placebo?'
							_lowerCamelCase     :			List[Any]    =    [
							    {
							        'image': load_image(lowercase  ),
							        'question': question,
							    },
							    {
							        'image': image,
							        'question': question,
							    },
							    {
							        'image': image,
							        'question': question,
							        'word_boxes': word_boxes,
							    },
							]
							return dqa_pipeline, examples
			def 					A_    (  self  ,      lowercase  ,      lowercase  ):
							_lowerCamelCase     :			str    =    dqa_pipeline(lowercase  ,      top_k=2  )
							self.assertEqual(
							    lowercase  ,      [
							        [
							            {'score': ANY(lowercase  ), 'answer': ANY(lowercase  ), 'start': ANY(lowercase  ), 'end': ANY(lowercase  )},
							            {'score': ANY(lowercase  ), 'answer': ANY(lowercase  ), 'start': ANY(lowercase  ), 'end': ANY(lowercase  )},
							        ]
							    ]
							    * 3  ,      )
			@require_torch
			@require_detectrona
			@require_pytesseract
			def 					A_    (  self  ):
							_lowerCamelCase     :			List[Any]    =    pipeline('document-question-answering'  ,      model='hf-internal-testing/tiny-random-layoutlmv2'  )
							_lowerCamelCase     :			Optional[Any]    =    INVOICE_URL
							_lowerCamelCase     :			List[Any]    =    'How many cats are there?'

							_lowerCamelCase     :			Optional[int]    =    [
							    {'score': 0.00_01, 'answer': 'oy 2312/2019', 'start': 38, 'end': 39},
							    {'score': 0.00_01, 'answer': 'oy 2312/2019 DUE', 'start': 38, 'end': 40},
							]
							_lowerCamelCase     :			str    =    dqa_pipeline(image=lowercase  ,      question=lowercase  ,      top_k=2  )
							self.assertEqual(nested_simplify(lowercase  ,      decimals=4  )  ,      lowercase  )

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

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

							# We can optionnally pass directly the words and bounding boxes
							_lowerCamelCase     :			Optional[Any]    =    './tests/fixtures/tests_samples/COCO/000000039769.png'
							_lowerCamelCase     :			Dict    =    []
							_lowerCamelCase     :			int    =    []
							_lowerCamelCase     :			Any    =    dqa_pipeline(image=lowercase  ,      question=lowercase  ,      words=lowercase  ,      boxes=lowercase  ,      top_k=2  )
							self.assertEqual(lowercase  ,      []  )
			@slow
			@require_torch
			@require_detectrona
			@require_pytesseract
			def 					A_    (  self  ):
							_lowerCamelCase     :			Optional[int]    =    pipeline(
							    'document-question-answering'  ,      model='tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'  ,      revision='9977165'  ,      )
							_lowerCamelCase     :			Union[str, Any]    =    INVOICE_URL
							_lowerCamelCase     :			str    =    'What is the invoice number?'

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

							_lowerCamelCase     :			Optional[Any]    =    dqa_pipeline({'image': image, 'question': question}  ,      top_k=2  )
							self.assertEqual(
							    nested_simplify(lowercase  ,      decimals=4  )  ,      [
							        {'score': 0.99_44, 'answer': 'us-001', 'start': 16, 'end': 16},
							        {'score': 0.00_09, 'answer': 'us-001', 'start': 16, 'end': 16},
							    ]  ,      )

							_lowerCamelCase     :			Optional[int]    =    dqa_pipeline(
							    [{'image': image, 'question': question}, {'image': image, 'question': question}]  ,      top_k=2  )
							self.assertEqual(
							    nested_simplify(lowercase  ,      decimals=4  )  ,      [
							        [
							            {'score': 0.99_44, 'answer': 'us-001', 'start': 16, 'end': 16},
							            {'score': 0.00_09, 'answer': 'us-001', 'start': 16, 'end': 16},
							        ],
							    ]
							    * 2  ,      )
			@slow
			@require_torch
			@require_detectrona
			@require_pytesseract
			def 					A_    (  self  ):
							_lowerCamelCase     :			Optional[Any]    =    pipeline(
							    'document-question-answering'  ,      model='tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'  ,      revision='9977165'  ,      max_seq_len=50  ,      )
							_lowerCamelCase     :			Optional[int]    =    INVOICE_URL
							_lowerCamelCase     :			List[Any]    =    'What is the invoice number?'

							_lowerCamelCase     :			Tuple    =    dqa_pipeline(image=lowercase  ,      question=lowercase  ,      top_k=2  )
							self.assertEqual(
							    nested_simplify(lowercase  ,      decimals=4  )  ,      [
							        {'score': 0.99_74, 'answer': '1110212019', 'start': 23, 'end': 23},
							        {'score': 0.99_48, 'answer': 'us-001', 'start': 16, 'end': 16},
							    ]  ,      )

							_lowerCamelCase     :			Dict    =    dqa_pipeline({'image': image, 'question': question}  ,      top_k=2  )
							self.assertEqual(
							    nested_simplify(lowercase  ,      decimals=4  )  ,      [
							        {'score': 0.99_74, 'answer': '1110212019', 'start': 23, 'end': 23},
							        {'score': 0.99_48, 'answer': 'us-001', 'start': 16, 'end': 16},
							    ]  ,      )

							_lowerCamelCase     :			List[Any]    =    dqa_pipeline(
							    [{'image': image, 'question': question}, {'image': image, 'question': question}]  ,      top_k=2  )
							self.assertEqual(
							    nested_simplify(lowercase  ,      decimals=4  )  ,      [
							        [
							            {'score': 0.99_74, 'answer': '1110212019', 'start': 23, 'end': 23},
							            {'score': 0.99_48, 'answer': 'us-001', 'start': 16, 'end': 16},
							        ]
							    ]
							    * 2  ,      )
			@slow
			@require_torch
			@require_pytesseract
			@require_vision
			def 					A_    (  self  ):
							_lowerCamelCase     :			List[Any]    =    AutoTokenizer.from_pretrained(
							    'impira/layoutlm-document-qa'  ,      revision='3dc6de3'  ,      add_prefix_space=lowercase  )
							_lowerCamelCase     :			Tuple    =    pipeline(
							    'document-question-answering'  ,      model='impira/layoutlm-document-qa'  ,      tokenizer=lowercase  ,      revision='3dc6de3'  ,      )
							_lowerCamelCase     :			Optional[Any]    =    INVOICE_URL
							_lowerCamelCase     :			Tuple    =    'What is the invoice number?'

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

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

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

							_lowerCamelCase     :			List[Any]    =    list(zip(*apply_tesseract(load_image(lowercase  )  ,      lowercase  ,      ''  )  )  )

							# This model should also work if `image` is set to None
							_lowerCamelCase     :			Optional[Any]    =    dqa_pipeline({'image': None, 'word_boxes': word_boxes, 'question': question}  ,      top_k=2  )
							self.assertEqual(
							    nested_simplify(lowercase  ,      decimals=4  )  ,      [
							        {'score': 0.42_51, 'answer': 'us-001', 'start': 16, 'end': 16},
							        {'score': 0.08_19, 'answer': '1110212019', 'start': 23, 'end': 23},
							    ]  ,      )
			@slow
			@require_torch
			@require_pytesseract
			@require_vision
			def 					A_    (  self  ):
							_lowerCamelCase     :			Union[str, Any]    =    AutoTokenizer.from_pretrained(
							    'impira/layoutlm-document-qa'  ,      revision='3dc6de3'  ,      add_prefix_space=lowercase  )
							_lowerCamelCase     :			Optional[Any]    =    pipeline(
							    'document-question-answering'  ,      model='impira/layoutlm-document-qa'  ,      tokenizer=lowercase  ,      revision='3dc6de3'  ,      max_seq_len=50  ,      )
							_lowerCamelCase     :			Any    =    INVOICE_URL
							_lowerCamelCase     :			Tuple    =    'What is the invoice number?'

							_lowerCamelCase     :			Any    =    dqa_pipeline(image=lowercase  ,      question=lowercase  ,      top_k=2  )
							self.assertEqual(
							    nested_simplify(lowercase  ,      decimals=4  )  ,      [
							        {'score': 0.99_99, 'answer': 'us-001', 'start': 16, 'end': 16},
							        {'score': 0.99_98, 'answer': 'us-001', 'start': 16, 'end': 16},
							    ]  ,      )

							_lowerCamelCase     :			Any    =    dqa_pipeline(
							    [{'image': image, 'question': question}, {'image': image, 'question': question}]  ,      top_k=2  )
							self.assertEqual(
							    nested_simplify(lowercase  ,      decimals=4  )  ,      [
							        [
							            {'score': 0.99_99, 'answer': 'us-001', 'start': 16, 'end': 16},
							            {'score': 0.99_98, 'answer': 'us-001', 'start': 16, 'end': 16},
							        ]
							    ]
							    * 2  ,      )

							_lowerCamelCase     :			Optional[int]    =    list(zip(*apply_tesseract(load_image(lowercase  )  ,      lowercase  ,      ''  )  )  )

							# This model should also work if `image` is set to None
							_lowerCamelCase     :			Union[str, Any]    =    dqa_pipeline({'image': None, 'word_boxes': word_boxes, 'question': question}  ,      top_k=2  )
							self.assertEqual(
							    nested_simplify(lowercase  ,      decimals=4  )  ,      [
							        {'score': 0.99_99, 'answer': 'us-001', 'start': 16, 'end': 16},
							        {'score': 0.99_98, 'answer': 'us-001', 'start': 16, 'end': 16},
							    ]  ,      )
			@slow
			@require_torch
			def 					A_    (  self  ):
							_lowerCamelCase     :			Optional[Any]    =    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     :			List[Any]    =    INVOICE_URL
							_lowerCamelCase     :			Any    =    'What is the invoice number?'
							_lowerCamelCase     :			Dict    =    dqa_pipeline(image=lowercase  ,      question=lowercase  ,      top_k=2  )
							self.assertEqual(nested_simplify(lowercase  ,      decimals=4  )  ,      [{'answer': 'us-001'}]  )






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

from collections import OrderedDict
from typing import Mapping

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


snake_case :		Dict							      =  logging.get_logger(__name__)

snake_case :		List[Any]							      =  {
    "YituTech/conv-bert-base": "https://huggingface.co/YituTech/conv-bert-base/resolve/main/config.json",
    "YituTech/conv-bert-medium-small": (
        "https://huggingface.co/YituTech/conv-bert-medium-small/resolve/main/config.json"
    ),
    "YituTech/conv-bert-small": "https://huggingface.co/YituTech/conv-bert-small/resolve/main/config.json",
    # See all ConvBERT models at https://huggingface.co/models?filter=convbert
}





class       _snake_case					(     snake_case				):
			UpperCamelCase__          =    'convbert'







			def __init__(				self						,   _a=30_522						,   _a=768						,   _a=12						,   _a=12						,   _a=3_072						,   _a="gelu"						,   _a=0.1						,   _a=0.1						,   _a=512						,   _a=2						,   _a=0.02						,   _a=1e-12						,   _a=1						,   _a=0						,   _a=2						,   _a=768						,   _a=2						,   _a=9						,   _a=1						,   _a=None						,   **_a						,   ):
				super().__init__(
				    pad_token_id=_a						,   bos_token_id=_a						,   eos_token_id=_a						,   **_a						,   )

				__magic_name__		:		Tuple	   =     vocab_size
				__magic_name__		:		List[Any]	   =     hidden_size
				__magic_name__		:		Union[str, Any]	   =     num_hidden_layers
				__magic_name__		:		List[Any]	   =     num_attention_heads
				__magic_name__		:		str	   =     intermediate_size
				__magic_name__		:		Any	   =     hidden_act
				__magic_name__		:		List[Any]	   =     hidden_dropout_prob
				__magic_name__		:		Optional[int]	   =     attention_probs_dropout_prob
				__magic_name__		:		Tuple	   =     max_position_embeddings
				__magic_name__		:		str	   =     type_vocab_size
				__magic_name__		:		List[str]	   =     initializer_range
				__magic_name__		:		Tuple	   =     layer_norm_eps
				__magic_name__		:		List[Any]	   =     embedding_size
				__magic_name__		:		List[Any]	   =     head_ratio
				__magic_name__		:		str	   =     conv_kernel_size
				__magic_name__		:		Dict	   =     num_groups
				__magic_name__		:		str	   =     classifier_dropout







class       _snake_case					(     snake_case				):







			@property
			def 			SCREAMING_SNAKE_CASE    (				self		):
				if self.task == "multiple-choice":
					__magic_name__		:		Dict	   =     {0: "batch", 1: "choice", 2: "sequence"}
				else:
					__magic_name__		:		Dict	   =     {0: "batch", 1: "sequence"}
				return OrderedDict(
				    [
				        ("input_ids", dynamic_axis),
				        ("attention_mask", dynamic_axis),
				        ("token_type_ids", dynamic_axis),
				    ]		)
