



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		(    *lowerCAmelCase	:      Optional[Any]      ,					**lowerCAmelCase	:      Optional[int])						->					Optional[int]:




                """simple docstring"""

                pass

       def 	_lowerCAmelCase (    A__						):
             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'''




   A     :	Any												=     MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING


   @require_pytesseract
   @require_vision
   def       UpperCAmelCase		(    self	:      Tuple      ,					lowerCAmelCase	:      str      ,					lowerCAmelCase	:      Tuple      ,					lowerCAmelCase	:      Optional[int])						->					Union[str, Any]:




         """simple docstring"""

         lowercase__  =	pipeline(
             'document-question-answering'      ,					model=lowerCAmelCase      ,					tokenizer=lowerCAmelCase      ,					image_processor=lowerCAmelCase)

         lowercase__  =	INVOICE_URL
         lowercase__  =	list(zip(*apply_tesseract(load_image(lowerCAmelCase)      ,					lowerCAmelCase      ,					'')))
         lowercase__  =	'What is the placebo?'
         lowercase__  =	[
             {
                 '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	:      List[Any]      ,					lowerCAmelCase	:      Any      ,					lowerCAmelCase	:      Any)						->					Tuple:




         """simple docstring"""

         lowercase__  =	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	:      Any)						->					Any:




         """simple docstring"""

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

         lowercase__  =	[
             {'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__  =	dqa_pipeline(image=lowerCAmelCase      ,					question=lowerCAmelCase      ,					top_k=2)
         self.assertEqual(nested_simplify(lowerCAmelCase      ,					decimals=4)      ,					lowerCAmelCase)

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

         # We can optionnally pass directly the words and bounding boxes
         lowercase__  =	'./tests/fixtures/tests_samples/COCO/000000039769.png'
         lowercase__  =	[]
         lowercase__  =	[]
         lowercase__  =	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	:      List[str])						->					Tuple:




         """simple docstring"""

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

         lowercase__  =	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__  =	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__  =	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	:      List[Any])						->					List[Any]:




         """simple docstring"""

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

         lowercase__  =	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__  =	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__  =	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	:      List[str])						->					Union[str, Any]:




         """simple docstring"""

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

         lowercase__  =	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__  =	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__  =	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__  =	list(zip(*apply_tesseract(load_image(lowerCAmelCase)      ,					lowerCAmelCase      ,					'')))

         # This model should also work if `image` is set to None
         lowercase__  =	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	:      Union[str, Any])						->					Any:




         """simple docstring"""

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

         lowercase__  =	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__  =	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__  =	list(zip(*apply_tesseract(load_image(lowerCAmelCase)      ,					lowerCAmelCase      ,					'')))

         # This model should also work if `image` is set to None
         lowercase__  =	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	:      Tuple)						->					Tuple:




         """simple docstring"""

         lowercase__  =	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__  =	INVOICE_URL
         lowercase__  =	'What is the invoice number?'
         lowercase__  =	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	:      Optional[int])						->					str:




         """simple docstring"""

         pass





from sklearn.metrics import matthews_corrcoef

import datasets


a__   :					Any 						=      "\nCompute the Matthews correlation coefficient (MCC)\n\nThe Matthews correlation coefficient is used in machine learning as a\nmeasure of the quality of binary and multiclass classifications. It takes\ninto account true and false positives and negatives and is generally\nregarded as a balanced measure which can be used even if the classes are of\nvery different sizes. The MCC is in essence a correlation coefficient value\nbetween -1 and +1. A coefficient of +1 represents a perfect prediction, 0\nan average random prediction and -1 an inverse prediction.  The statistic\nis also known as the phi coefficient. [source: Wikipedia]\n"

a__   :					Any 						=      "\nArgs:\n    predictions (list of int): Predicted labels, as returned by a model.\n    references (list of int): Ground truth labels.\n    sample_weight (list of int, float, or bool): Sample weights. Defaults to `None`.\nReturns:\n    matthews_correlation (dict containing float): Matthews correlation.\nExamples:\n    Example 1, a basic example with only predictions and references as inputs:\n        >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n        >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n        ...                                     predictions=[1, 2, 2, 0, 3, 3])\n        >>> print(round(results['matthews_correlation'], 2))\n        0.54\n\n    Example 2, the same example as above, but also including sample weights:\n        >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n        >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n        ...                                     predictions=[1, 2, 2, 0, 3, 3],\n        ...                                     sample_weight=[0.5, 3, 1, 1, 1, 2])\n        >>> print(round(results['matthews_correlation'], 2))\n        0.1\n\n    Example 3, the same example as above, but with sample weights that cause a negative correlation:\n        >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n        >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n        ...                                     predictions=[1, 2, 2, 0, 3, 3],\n        ...                                     sample_weight=[0.5, 1, 0, 0, 0, 1])\n        >>> print(round(results['matthews_correlation'], 2))\n        -0.25\n"

a__   :					str 						=      "\\n@article{scikit-learn,\n  title={Scikit-learn: Machine Learning in {P}ython},\n  author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n         and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n         and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n         Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n  journal={Journal of Machine Learning Research},\n  volume={12},\n  pages={2825--2830},\n  year={2011}\n}\n"



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

   '''simple docstring'''




   def       UpperCAmelCase		(    self	:      List[Any])						->					int:




         """simple docstring"""

         return datasets.MetricInfo(
             description=_DESCRIPTION      ,					citation=_CITATION      ,					inputs_description=_KWARGS_DESCRIPTION      ,					features=datasets.Features(
                 {
                     'predictions': datasets.Value('int32'),
                     'references': datasets.Value('int32'),
                 })      ,					reference_urls=[
                 'https://scikit-learn.org/stable/modules/generated/sklearn.metrics.matthews_corrcoef.html'
             ]      ,					)



   def       UpperCAmelCase		(    self	:      Dict      ,					lowerCAmelCase	:      Dict      ,					lowerCAmelCase	:      Optional[int]      ,					lowerCAmelCase	:      Union[str, Any]=None)						->					Any:




         """simple docstring"""

         return {
             "matthews_correlation": float(matthews_corrcoef(lowerCAmelCase      ,					lowerCAmelCase      ,					sample_weight=lowerCAmelCase)),
         }
