

"""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 UpperCamelCase__		( *lowerCamelCase_ ,**lowerCamelCase_    )    ->      Any:
            pass




  def   _A					(	_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.
UpperCAmelCase													=(
    "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 UpperCamelCase__		( self ,lowerCamelCase_ ,lowerCamelCase_ ,lowerCamelCase_    )    ->      str:
          A				  =		pipeline(
              """document-question-answering""" ,model=lowerCamelCase_ ,tokenizer=lowerCamelCase_ ,image_processor=lowerCamelCase_    )

          A				  =		INVOICE_URL
          A				  =		list(zip(*apply_tesseract(load_image(lowerCamelCase_    ) ,lowerCamelCase_ ,""""""    )    )    )
          A				  =		"""What is the placebo?"""
          A				  =		[
              {
                  """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 ,lowerCamelCase_ ,lowerCamelCase_    )    ->      List[Any]:
          A				  =		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    )    ->      Dict:
          A				  =		pipeline("""document-question-answering""" ,model="""hf-internal-testing/tiny-random-layoutlmv2"""    )
          A				  =		INVOICE_URL
          A				  =		"""How many cats are there?"""

          A				  =		[
              {"""score""": 0.00_01, """answer""": """oy 2312/2019""", """start""": 3_8, """end""": 3_9},
              {"""score""": 0.00_01, """answer""": """oy 2312/2019 DUE""", """start""": 3_8, """end""": 4_0},
          ]
          A				  =		dqa_pipeline(image=lowerCamelCase_ ,question=lowerCamelCase_ ,top_k=2    )
          self.assertEqual(nested_simplify(lowerCamelCase_ ,decimals=4    ) ,lowerCamelCase_    )

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

          # We can optionnally pass directly the words and bounding boxes
          A				  =		"""./tests/fixtures/tests_samples/COCO/000000039769.png"""
          A				  =		[]
          A				  =		[]
          A				  =		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    )    ->      Optional[Any]:
          A				  =		pipeline(
              """document-question-answering""" ,model="""tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa""" ,revision="""9977165""" ,)
          A				  =		INVOICE_URL
          A				  =		"""What is the invoice number?"""

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

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

          A				  =		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""": 1_6, """end""": 1_6},
                      {"""score""": 0.00_09, """answer""": """us-001""", """start""": 1_6, """end""": 1_6},
                  ],
              ]
              * 2 ,)




    @slow
    @require_torch
    @require_detectrona
    @require_pytesseract
    def UpperCamelCase__		( self    )    ->      Union[str, Any]:
          A				  =		pipeline(
              """document-question-answering""" ,model="""tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa""" ,revision="""9977165""" ,max_seq_len=5_0 ,)
          A				  =		INVOICE_URL
          A				  =		"""What is the invoice number?"""

          A				  =		dqa_pipeline(image=lowerCamelCase_ ,question=lowerCamelCase_ ,top_k=2    )
          self.assertEqual(
              nested_simplify(lowerCamelCase_ ,decimals=4    ) ,[
                  {"""score""": 0.99_74, """answer""": """1110212019""", """start""": 2_3, """end""": 2_3},
                  {"""score""": 0.99_48, """answer""": """us-001""", """start""": 1_6, """end""": 1_6},
              ] ,)

          A				  =		dqa_pipeline({"""image""": image, """question""": question} ,top_k=2    )
          self.assertEqual(
              nested_simplify(lowerCamelCase_ ,decimals=4    ) ,[
                  {"""score""": 0.99_74, """answer""": """1110212019""", """start""": 2_3, """end""": 2_3},
                  {"""score""": 0.99_48, """answer""": """us-001""", """start""": 1_6, """end""": 1_6},
              ] ,)

          A				  =		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""": 2_3, """end""": 2_3},
                      {"""score""": 0.99_48, """answer""": """us-001""", """start""": 1_6, """end""": 1_6},
                  ]
              ]
              * 2 ,)




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

          A				  =		dqa_pipeline(image=lowerCamelCase_ ,question=lowerCamelCase_ ,top_k=2    )
          self.assertEqual(
              nested_simplify(lowerCamelCase_ ,decimals=4    ) ,[
                  {"""score""": 0.42_51, """answer""": """us-001""", """start""": 1_6, """end""": 1_6},
                  {"""score""": 0.08_19, """answer""": """1110212019""", """start""": 2_3, """end""": 2_3},
              ] ,)

          A				  =		dqa_pipeline({"""image""": image, """question""": question} ,top_k=2    )
          self.assertEqual(
              nested_simplify(lowerCamelCase_ ,decimals=4    ) ,[
                  {"""score""": 0.42_51, """answer""": """us-001""", """start""": 1_6, """end""": 1_6},
                  {"""score""": 0.08_19, """answer""": """1110212019""", """start""": 2_3, """end""": 2_3},
              ] ,)

          A				  =		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""": 1_6, """end""": 1_6},
                      {"""score""": 0.08_19, """answer""": """1110212019""", """start""": 2_3, """end""": 2_3},
                  ]
              ]
              * 2 ,)

          A				  =		list(zip(*apply_tesseract(load_image(lowerCamelCase_    ) ,lowerCamelCase_ ,""""""    )    )    )

          # This model should also work if `image` is set to None
          A				  =		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""": 1_6, """end""": 1_6},
                  {"""score""": 0.08_19, """answer""": """1110212019""", """start""": 2_3, """end""": 2_3},
              ] ,)




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

          A				  =		dqa_pipeline(image=lowerCamelCase_ ,question=lowerCamelCase_ ,top_k=2    )
          self.assertEqual(
              nested_simplify(lowerCamelCase_ ,decimals=4    ) ,[
                  {"""score""": 0.99_99, """answer""": """us-001""", """start""": 1_6, """end""": 1_6},
                  {"""score""": 0.99_98, """answer""": """us-001""", """start""": 1_6, """end""": 1_6},
              ] ,)

          A				  =		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""": 1_6, """end""": 1_6},
                      {"""score""": 0.99_98, """answer""": """us-001""", """start""": 1_6, """end""": 1_6},
                  ]
              ]
              * 2 ,)

          A				  =		list(zip(*apply_tesseract(load_image(lowerCamelCase_    ) ,lowerCamelCase_ ,""""""    )    )    )

          # This model should also work if `image` is set to None
          A				  =		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""": 1_6, """end""": 1_6},
                  {"""score""": 0.99_98, """answer""": """us-001""", """start""": 1_6, """end""": 1_6},
              ] ,)




    @slow
    @require_torch
    def UpperCamelCase__		( self    )    ->      List[str]:
          A				  =		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""" ,)

          A				  =		INVOICE_URL
          A				  =		"""What is the invoice number?"""
          A				  =		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    )    ->      Tuple:
          pass







"""simple docstring"""


from dataclasses import dataclass, field
from typing import ClassVar, Dict

from ..features import Features, Value
from .base import TaskTemplate





@dataclass(frozen=SCREAMING_SNAKE_CASE    )
class lowerCamelCase__   (				SCREAMING_SNAKE_CASE    ):



    '''simple docstring'''




    _lowerCamelCase					=   field(default='''summarization'''     ,   metadata={'''include_in_asdict_even_if_is_default''': True}    )
    _lowerCamelCase					=   Features({'''text''': Value('''string'''    )}    )
    _lowerCamelCase					=   Features({'''summary''': Value('''string'''    )}    )
    _lowerCamelCase					=   "text"
    _lowerCamelCase					=   "summary"




    @property
    def UpperCamelCase__		( self    )    ->      Dict[str, str]:
          return {self.text_column: "text", self.summary_column: "summary"}




