



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







												@staticmethod
												def 							lowerCamelCase_ (	*snake_case						,							**snake_case    )      ->				int:
														pass







					def 						UpperCAmelCase  (		A						:    Tuple							):

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




@is_pipeline_test
@require_torch
@require_vision
class       lowercase__      (   unittest.TestCase     ):
							'''simple docstring'''







							_UpperCAmelCase   					=       MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING






							@require_pytesseract
							@require_vision
							def 							lowerCamelCase_ (	self						,							snake_case						,							snake_case						,							snake_case    )      ->				Optional[Any]:
									_UpperCAmelCase							=		pipeline(
									    'document-question-answering'						,							model=lowerCAmelCase__						,							tokenizer=lowerCAmelCase__						,							image_processor=lowerCAmelCase__    )

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






							def 							lowerCamelCase_ (	self						,							snake_case						,							snake_case    )      ->				Optional[Any]:
									_UpperCAmelCase							=		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 							lowerCamelCase_ (	self    )      ->				Optional[Any]:
									_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=lowerCAmelCase__						,							question=lowerCAmelCase__						,							top_k=2    )
									self.assertEqual(nested_simplify(lowerCAmelCase__						,							decimals=4    )						,							lowerCAmelCase__    )

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

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






							@slow
							@require_torch
							@require_detectrona
							@require_pytesseract
							def 							lowerCamelCase_ (	self    )      ->				Tuple:
									_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=lowerCAmelCase__						,							question=lowerCAmelCase__						,							top_k=2    )
									self.assertEqual(
									    nested_simplify(lowerCAmelCase__						,							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(lowerCAmelCase__						,							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(lowerCAmelCase__						,							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 							lowerCamelCase_ (	self    )      ->				Tuple:
									_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=lowerCAmelCase__						,							question=lowerCAmelCase__						,							top_k=2    )
									self.assertEqual(
									    nested_simplify(lowerCAmelCase__						,							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(lowerCAmelCase__						,							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(lowerCAmelCase__						,							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 							lowerCamelCase_ (	self    )      ->				Dict:
									_UpperCAmelCase							=		AutoTokenizer.from_pretrained(
									    'impira/layoutlm-document-qa'						,							revision='3dc6de3'						,							add_prefix_space=lowerCAmelCase__    )
									_UpperCAmelCase							=		pipeline(
									    'document-question-answering'						,							model='impira/layoutlm-document-qa'						,							tokenizer=lowerCAmelCase__						,							revision='3dc6de3'						,							)
									_UpperCAmelCase							=		INVOICE_URL
									_UpperCAmelCase							=		'What is the invoice number?'

									_UpperCAmelCase							=		dqa_pipeline(image=lowerCAmelCase__						,							question=lowerCAmelCase__						,							top_k=2    )
									self.assertEqual(
									    nested_simplify(lowerCAmelCase__						,							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(lowerCAmelCase__						,							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(lowerCAmelCase__						,							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(lowerCAmelCase__    )						,							lowerCAmelCase__						,							''    )    )    )

									# 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(lowerCAmelCase__						,							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 							lowerCamelCase_ (	self    )      ->				Optional[Any]:
									_UpperCAmelCase							=		AutoTokenizer.from_pretrained(
									    'impira/layoutlm-document-qa'						,							revision='3dc6de3'						,							add_prefix_space=lowerCAmelCase__    )
									_UpperCAmelCase							=		pipeline(
									    'document-question-answering'						,							model='impira/layoutlm-document-qa'						,							tokenizer=lowerCAmelCase__						,							revision='3dc6de3'						,							max_seq_len=50						,							)
									_UpperCAmelCase							=		INVOICE_URL
									_UpperCAmelCase							=		'What is the invoice number?'

									_UpperCAmelCase							=		dqa_pipeline(image=lowerCAmelCase__						,							question=lowerCAmelCase__						,							top_k=2    )
									self.assertEqual(
									    nested_simplify(lowerCAmelCase__						,							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(lowerCAmelCase__						,							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(lowerCAmelCase__    )						,							lowerCAmelCase__						,							''    )    )    )

									# 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(lowerCAmelCase__						,							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 							lowerCamelCase_ (	self    )      ->				Dict:
									_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=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 							lowerCamelCase_ (	self    )      ->				Any:
									pass



import datasets


__a    :	Optional[Any]         =    """\
@InProceedings{conneau2018xnli,
  author = \"Conneau, Alexis
                 and Rinott, Ruty
                 and Lample, Guillaume
                 and Williams, Adina
                 and Bowman, Samuel R.
                 and Schwenk, Holger
                 and Stoyanov, Veselin\",
  title = \"XNLI: Evaluating Cross-lingual Sentence Representations\",
  booktitle = \"Proceedings of the 2018 Conference on Empirical Methods
               in Natural Language Processing\",
  year = \"2018\",
  publisher = \"Association for Computational Linguistics\",
  location = \"Brussels, Belgium\",
}
"""

__a    :	Optional[Any]         =    """\
XNLI is a subset of a few thousand examples from MNLI which has been translated
into a 14 different languages (some low-ish resource). As with MNLI, the goal is
to predict textual entailment (does sentence A imply/contradict/neither sentence
B) and is a classification task (given two sentences, predict one of three
labels).
"""

__a    :	str         =    """
Computes XNLI score which is just simple accuracy.
Args:
    predictions: Predicted labels.
    references: Ground truth labels.
Returns:
    'accuracy': accuracy
Examples:

    >>> predictions = [0, 1]
    >>> references = [0, 1]
    >>> xnli_metric = datasets.load_metric(\"xnli\")
    >>> results = xnli_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'accuracy': 1.0}
"""





def 							UpperCAmelCase		(       lowercase				,     lowercase				):







				"""simple docstring"""



				return (preds == labels).mean()




@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION		,_KWARGS_DESCRIPTION					)
class   _UpperCamelCase (		datasets.Metric					):


			"""simple docstring"""






			def 			_SCREAMING_SNAKE_CASE     (  self		)	->   str:

							'''simple docstring'''


							return datasets.MetricInfo(
							    description=_DESCRIPTION ,  citation=_CITATION ,  inputs_description=_KWARGS_DESCRIPTION ,  features=datasets.Features(
							        {
							            '''predictions''': datasets.Value('''int64''' if self.config_name != '''sts-b''' else '''float32'''		),
							            '''references''': datasets.Value('''int64''' if self.config_name != '''sts-b''' else '''float32'''		),
							        }		) ,  codebase_urls=[] ,  reference_urls=[] ,  format='''numpy''' ,  )





			def 			_SCREAMING_SNAKE_CASE     (  self ,  lowerCAmelCase__ ,  lowerCAmelCase__		)	->   Dict:

							'''simple docstring'''


							return {"accuracy": simple_accuracy(lowerCAmelCase__ ,  lowerCAmelCase__		)}