






"""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        __UpperCAmelCase:




									"""simple docstring"""




									@staticmethod
									def 						UpperCAmelCase_			(  *snake_case__			,    **snake_case__     ):
															'''simple docstring'''
															pass





			def 		lowercase__(A		)							->List[Any]:




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


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




						"""simple docstring"""




						__lowerCamelCase										=   MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING




						@require_pytesseract
						@require_vision
						def 						UpperCAmelCase_			(  self			,    snake_case__			,    snake_case__			,    snake_case__     ):
												'''simple docstring'''
												lowercase__							: int=			pipeline(
												    "document-question-answering"			,    model=lowerCAmelCase_			,    tokenizer=lowerCAmelCase_			,    image_processor=lowerCAmelCase_     )

												lowercase__							: int=			INVOICE_URL
												lowercase__							: Union[str, Any]=			list(zip(*apply_tesseract(load_image(lowerCAmelCase_     )			,    lowerCAmelCase_			,    ""     )     )     )
												lowercase__							: Optional[Any]=			"What is the placebo?"
												lowercase__							: Tuple=			[
												    {
												        "image": load_image(lowerCAmelCase_     ),
												        "question": question,
												    },
												    {
												        "image": image,
												        "question": question,
												    },
												    {
												        "image": image,
												        "question": question,
												        "word_boxes": word_boxes,
												    },
												]
												return dqa_pipeline, examples




						def 						UpperCAmelCase_			(  self			,    snake_case__			,    snake_case__     ):
												'''simple docstring'''
												lowercase__							: Union[str, Any]=			dqa_pipeline(lowerCAmelCase_			,    top_k=2     )
												self.assertEqual(
												    lowerCAmelCase_			,    [
												        [
												            {"score": ANY(lowerCAmelCase_     ), "answer": ANY(lowerCAmelCase_     ), "start": ANY(lowerCAmelCase_     ), "end": ANY(lowerCAmelCase_     )},
												            {"score": ANY(lowerCAmelCase_     ), "answer": ANY(lowerCAmelCase_     ), "start": ANY(lowerCAmelCase_     ), "end": ANY(lowerCAmelCase_     )},
												        ]
												    ]
												    * 3			,    )




						@require_torch
						@require_detectrona
						@require_pytesseract
						def 						UpperCAmelCase_			(  self     ):
												'''simple docstring'''
												lowercase__							: Tuple=			pipeline("document-question-answering"			,    model="hf-internal-testing/tiny-random-layoutlmv2"     )
												lowercase__							: Dict=			INVOICE_URL
												lowercase__							: int=			"How many cats are there?"

												lowercase__							: Any=			[
												    {"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},
												]
												lowercase__							: List[str]=			dqa_pipeline(image=lowerCAmelCase_			,    question=lowerCAmelCase_			,    top_k=2     )
												self.assertEqual(nested_simplify(lowerCAmelCase_			,    decimals=4     )			,    lowerCAmelCase_     )

												lowercase__							: Dict=			dqa_pipeline({"image": image, "question": question}			,    top_k=2     )
												self.assertEqual(nested_simplify(lowerCAmelCase_			,    decimals=4     )			,    lowerCAmelCase_     )

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

												# We can optionnally pass directly the words and bounding boxes
												lowercase__							: int=			"./tests/fixtures/tests_samples/COCO/000000039769.png"
												lowercase__							: Optional[Any]=			[]
												lowercase__							: Dict=			[]
												lowercase__							: Optional[Any]=			dqa_pipeline(image=lowerCAmelCase_			,    question=lowerCAmelCase_			,    words=lowerCAmelCase_			,    boxes=lowerCAmelCase_			,    top_k=2     )
												self.assertEqual(lowerCAmelCase_			,    []     )




						@slow
						@require_torch
						@require_detectrona
						@require_pytesseract
						def 						UpperCAmelCase_			(  self     ):
												'''simple docstring'''
												lowercase__							: Dict=			pipeline(
												    "document-question-answering"			,    model="tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"			,    revision="9977165"			,    )
												lowercase__							: Optional[Any]=			INVOICE_URL
												lowercase__							: Dict=			"What is the invoice number?"

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

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

												lowercase__							: Any=			dqa_pipeline(
												    [{"image": image, "question": question}, {"image": image, "question": question}]			,    top_k=2     )
												self.assertEqual(
												    nested_simplify(lowerCAmelCase_			,    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 						UpperCAmelCase_			(  self     ):
												'''simple docstring'''
												lowercase__							: Tuple=			pipeline(
												    "document-question-answering"			,    model="tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"			,    revision="9977165"			,    max_seq_len=50			,    )
												lowercase__							: Tuple=			INVOICE_URL
												lowercase__							: Any=			"What is the invoice number?"

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

												lowercase__							: Optional[int]=			dqa_pipeline({"image": image, "question": question}			,    top_k=2     )
												self.assertEqual(
												    nested_simplify(lowerCAmelCase_			,    decimals=4     )			,    [
												        {"score": 0.99_74, "answer": "1110212019", "start": 23, "end": 23},
												        {"score": 0.99_48, "answer": "us-001", "start": 16, "end": 16},
												    ]			,    )

												lowercase__							: str=			dqa_pipeline(
												    [{"image": image, "question": question}, {"image": image, "question": question}]			,    top_k=2     )
												self.assertEqual(
												    nested_simplify(lowerCAmelCase_			,    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 						UpperCAmelCase_			(  self     ):
												'''simple docstring'''
												lowercase__							: Any=			AutoTokenizer.from_pretrained(
												    "impira/layoutlm-document-qa"			,    revision="3dc6de3"			,    add_prefix_space=lowerCAmelCase_     )
												lowercase__							: str=			pipeline(
												    "document-question-answering"			,    model="impira/layoutlm-document-qa"			,    tokenizer=lowerCAmelCase_			,    revision="3dc6de3"			,    )
												lowercase__							: Any=			INVOICE_URL
												lowercase__							: List[str]=			"What is the invoice number?"

												lowercase__							: str=			dqa_pipeline(image=lowerCAmelCase_			,    question=lowerCAmelCase_			,    top_k=2     )
												self.assertEqual(
												    nested_simplify(lowerCAmelCase_			,    decimals=4     )			,    [
												        {"score": 0.42_51, "answer": "us-001", "start": 16, "end": 16},
												        {"score": 0.08_19, "answer": "1110212019", "start": 23, "end": 23},
												    ]			,    )

												lowercase__							: Dict=			dqa_pipeline({"image": image, "question": question}			,    top_k=2     )
												self.assertEqual(
												    nested_simplify(lowerCAmelCase_			,    decimals=4     )			,    [
												        {"score": 0.42_51, "answer": "us-001", "start": 16, "end": 16},
												        {"score": 0.08_19, "answer": "1110212019", "start": 23, "end": 23},
												    ]			,    )

												lowercase__							: Union[str, Any]=			dqa_pipeline(
												    [{"image": image, "question": question}, {"image": image, "question": question}]			,    top_k=2     )
												self.assertEqual(
												    nested_simplify(lowerCAmelCase_			,    decimals=4     )			,    [
												        [
												            {"score": 0.42_51, "answer": "us-001", "start": 16, "end": 16},
												            {"score": 0.08_19, "answer": "1110212019", "start": 23, "end": 23},
												        ]
												    ]
												    * 2			,    )

												lowercase__							: Dict=			list(zip(*apply_tesseract(load_image(lowerCAmelCase_     )			,    lowerCAmelCase_			,    ""     )     )     )

												# This model should also work if `image` is set to None
												lowercase__							: List[str]=			dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question}			,    top_k=2     )
												self.assertEqual(
												    nested_simplify(lowerCAmelCase_			,    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 						UpperCAmelCase_			(  self     ):
												'''simple docstring'''
												lowercase__							: List[Any]=			AutoTokenizer.from_pretrained(
												    "impira/layoutlm-document-qa"			,    revision="3dc6de3"			,    add_prefix_space=lowerCAmelCase_     )
												lowercase__							: str=			pipeline(
												    "document-question-answering"			,    model="impira/layoutlm-document-qa"			,    tokenizer=lowerCAmelCase_			,    revision="3dc6de3"			,    max_seq_len=50			,    )
												lowercase__							: List[Any]=			INVOICE_URL
												lowercase__							: Optional[int]=			"What is the invoice number?"

												lowercase__							: int=			dqa_pipeline(image=lowerCAmelCase_			,    question=lowerCAmelCase_			,    top_k=2     )
												self.assertEqual(
												    nested_simplify(lowerCAmelCase_			,    decimals=4     )			,    [
												        {"score": 0.99_99, "answer": "us-001", "start": 16, "end": 16},
												        {"score": 0.99_98, "answer": "us-001", "start": 16, "end": 16},
												    ]			,    )

												lowercase__							: Tuple=			dqa_pipeline(
												    [{"image": image, "question": question}, {"image": image, "question": question}]			,    top_k=2     )
												self.assertEqual(
												    nested_simplify(lowerCAmelCase_			,    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			,    )

												lowercase__							: Optional[int]=			list(zip(*apply_tesseract(load_image(lowerCAmelCase_     )			,    lowerCAmelCase_			,    ""     )     )     )

												# This model should also work if `image` is set to None
												lowercase__							: Dict=			dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question}			,    top_k=2     )
												self.assertEqual(
												    nested_simplify(lowerCAmelCase_			,    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 						UpperCAmelCase_			(  self     ):
												'''simple docstring'''
												lowercase__							: List[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"			,    )

												lowercase__							: Optional[int]=			INVOICE_URL
												lowercase__							: int=			"What is the invoice number?"
												lowercase__							: List[str]=			dqa_pipeline(image=lowerCAmelCase_			,    question=lowerCAmelCase_			,    top_k=2     )
												self.assertEqual(nested_simplify(lowerCAmelCase_			,    decimals=4     )			,    [{"answer": "us-001"}]     )




						@require_tf
						@unittest.skip("Document question answering not implemented in TF"     )
						def 						UpperCAmelCase_			(  self     ):
												'''simple docstring'''
												pass














"""simple docstring"""
import unittest

import numpy as np

from transformers import DistilBertConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
			import jax.numpy as jnp

			from transformers.models.distilbert.modeling_flax_distilbert import (
			    FlaxDistilBertForMaskedLM,
			    FlaxDistilBertForMultipleChoice,
			    FlaxDistilBertForQuestionAnswering,
			    FlaxDistilBertForSequenceClassification,
			    FlaxDistilBertForTokenClassification,
			    FlaxDistilBertModel,
			)







class      UpperCamelCase_			(unittest.TestCase	):





	def __init__( self   :					int      ,      lowerCAmelCase_   :					Any      ,      lowerCAmelCase_   :					Union[str, Any]=13      ,      lowerCAmelCase_   :					Union[str, Any]=7      ,      lowerCAmelCase_   :					Optional[Any]=True      ,      lowerCAmelCase_   :					List[Any]=True      ,      lowerCAmelCase_   :					Any=True      ,      lowerCAmelCase_   :					Tuple=True      ,      lowerCAmelCase_   :					Tuple=99      ,      lowerCAmelCase_   :					str=32      ,      lowerCAmelCase_   :					Optional[int]=5      ,      lowerCAmelCase_   :					List[Any]=4      ,      lowerCAmelCase_   :					List[Any]=37      ,      lowerCAmelCase_   :					str="gelu"      ,      lowerCAmelCase_   :					Optional[Any]=0.1      ,      lowerCAmelCase_   :					Union[str, Any]=0.1      ,      lowerCAmelCase_   :					List[Any]=512      ,      lowerCAmelCase_   :					Dict=16      ,      lowerCAmelCase_   :					int=2      ,      lowerCAmelCase_   :					Union[str, Any]=0.0_2      ,      lowerCAmelCase_   :					Optional[Any]=4      ,      )		->     Tuple:
				UpperCAmelCase_	:					Optional[Any]     	=   parent
				UpperCAmelCase_	:					Tuple     	=   batch_size
				UpperCAmelCase_	:					str     	=   seq_length
				UpperCAmelCase_	:					Optional[int]     	=   is_training
				UpperCAmelCase_	:					Dict     	=   use_attention_mask
				UpperCAmelCase_	:					int     	=   use_token_type_ids
				UpperCAmelCase_	:					Tuple     	=   use_labels
				UpperCAmelCase_	:					Tuple     	=   vocab_size
				UpperCAmelCase_	:					Union[str, Any]     	=   hidden_size
				UpperCAmelCase_	:					int     	=   num_hidden_layers
				UpperCAmelCase_	:					List[str]     	=   num_attention_heads
				UpperCAmelCase_	:					str     	=   intermediate_size
				UpperCAmelCase_	:					List[str]     	=   hidden_act
				UpperCAmelCase_	:					Tuple     	=   hidden_dropout_prob
				UpperCAmelCase_	:					Optional[Any]     	=   attention_probs_dropout_prob
				UpperCAmelCase_	:					Optional[int]     	=   max_position_embeddings
				UpperCAmelCase_	:					Optional[Any]     	=   type_vocab_size
				UpperCAmelCase_	:					Dict     	=   type_sequence_label_size
				UpperCAmelCase_	:					List[Any]     	=   initializer_range
				UpperCAmelCase_	:					Any     	=   num_choices





	def 	_SCREAMING_SNAKE_CASE ( self   :					List[str]		)		->     str:
				UpperCAmelCase_	:					List[str]     	=   ids_tensor([self.batch_size, self.seq_length]      ,      self.vocab_size		)

				UpperCAmelCase_	:					Tuple     	=   None
				if self.use_attention_mask:
							UpperCAmelCase_	:					Dict     	=   random_attention_mask([self.batch_size, self.seq_length]		)

				UpperCAmelCase_	:					Any     	=   DistilBertConfig(
				    vocab_size=self.vocab_size      ,      dim=self.hidden_size      ,      n_layers=self.num_hidden_layers      ,      n_heads=self.num_attention_heads      ,      hidden_dim=self.intermediate_size      ,      hidden_act=self.hidden_act      ,      dropout=self.hidden_dropout_prob      ,      attention_dropout=self.attention_probs_dropout_prob      ,      max_position_embeddings=self.max_position_embeddings      ,      initializer_range=self.initializer_range      ,      tie_weights_=lowerCAmelCase_      ,      )

				return config, input_ids, attention_mask





	def 	_SCREAMING_SNAKE_CASE ( self   :					List[Any]		)		->     Union[str, Any]:
				UpperCAmelCase_	:					Any     	=   self.prepare_config_and_inputs()
				UpperCAmelCase_	,      UpperCAmelCase_	,      UpperCAmelCase_	:					List[str]     	=   config_and_inputs
				UpperCAmelCase_	:					Optional[Any]     	=   {"input_ids": input_ids, "attention_mask": attention_mask}
				return config, inputs_dict







@require_flax
class      UpperCamelCase_			(__A							,		unittest.TestCase	):
	__magic_name__       =							(
	    (
	        FlaxDistilBertModel,
	        FlaxDistilBertForMaskedLM,
	        FlaxDistilBertForMultipleChoice,
	        FlaxDistilBertForQuestionAnswering,
	        FlaxDistilBertForSequenceClassification,
	        FlaxDistilBertForTokenClassification,
	        FlaxDistilBertForQuestionAnswering,
	    )
	    if is_flax_available()
	    else ()
	)





	def 	_SCREAMING_SNAKE_CASE ( self   :					List[str]		)		->     Optional[Any]:
				UpperCAmelCase_	:					List[Any]     	=   FlaxDistilBertModelTester(self		)





	@slow
	def 	_SCREAMING_SNAKE_CASE ( self   :					Optional[Any]		)		->     List[str]:
				for model_class_name in self.all_model_classes:
							UpperCAmelCase_	:					str     	=   model_class_name.from_pretrained("distilbert-base-uncased"		)
							UpperCAmelCase_	:					List[str]     	=   model(np.ones((1, 1)		)		)
							self.assertIsNotNone(lowerCAmelCase_		)







@require_flax
class      UpperCamelCase_			(unittest.TestCase	):





	@slow
	def 	_SCREAMING_SNAKE_CASE ( self   :					str		)		->     Optional[int]:
				UpperCAmelCase_	:					Any     	=   FlaxDistilBertModel.from_pretrained("distilbert-base-uncased"		)
				UpperCAmelCase_	:					int     	=   np.array([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]		)
				UpperCAmelCase_	:					List[str]     	=   np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]		)
				UpperCAmelCase_	:					Any     	=   model(lowerCAmelCase_      ,      attention_mask=lowerCAmelCase_		)[0]
				UpperCAmelCase_	:					Union[str, Any]     	=   (1, 11, 768)
				self.assertEqual(output.shape      ,      lowerCAmelCase_		)
				UpperCAmelCase_	:					Dict     	=   np.array([[[-0.1_6_3_9, 0.3_2_9_9, 0.1_6_4_8], [-0.1_7_4_6, 0.3_2_8_9, 0.1_7_1_0], [-0.1_8_8_4, 0.3_3_5_7, 0.1_8_1_0]]]		)

				self.assertTrue(jnp.allclose(output[:, 1:4, 1:4]      ,      lowerCAmelCase_      ,      atol=1e-4		)		)



