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


        @staticmethod
        def   lowerCAmelCase_	(       *__lowerCAmelCase						:	List[str] ,					**__lowerCAmelCase						:	Union[str, Any]       ):
          pass






    def      __UpperCAmelCase       (      lowercase		):







      """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 							a					(		unittest.TestCase ):
    _snake_case  :	Dict  				=				MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING


    @require_pytesseract
    @require_vision
    def   lowerCAmelCase_	(       self						:	Any ,					__lowerCAmelCase						:	Any ,					__lowerCAmelCase						:	Tuple ,					__lowerCAmelCase						:	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						:	int ,					__lowerCAmelCase						:	Dict ,					__lowerCAmelCase						:	Tuple       ):
      _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						:	Union[str, 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.0_001, """answer""": """oy 2312/2019""", """start""": 38, """end""": 39},
          {"""score""": 0.0_001, """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						:	Any       ):
      _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.9_944, """answer""": """us-001""", """start""": 16, """end""": 16},
              {"""score""": 0.0_009, """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.9_944, """answer""": """us-001""", """start""": 16, """end""": 16},
              {"""score""": 0.0_009, """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.9_944, """answer""": """us-001""", """start""": 16, """end""": 16},
                  {"""score""": 0.0_009, """answer""": """us-001""", """start""": 16, """end""": 16},
              ],
          ]
          * 2 ,					)


    @slow
    @require_torch
    @require_detectrona
    @require_pytesseract
    def   lowerCAmelCase_	(       self						:	str       ):
      _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.9_974, """answer""": """1110212019""", """start""": 23, """end""": 23},
              {"""score""": 0.9_948, """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.9_974, """answer""": """1110212019""", """start""": 23, """end""": 23},
              {"""score""": 0.9_948, """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.9_974, """answer""": """1110212019""", """start""": 23, """end""": 23},
                  {"""score""": 0.9_948, """answer""": """us-001""", """start""": 16, """end""": 16},
              ]
          ]
          * 2 ,					)


    @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""" ,					)
      _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.4_251, """answer""": """us-001""", """start""": 16, """end""": 16},
              {"""score""": 0.0_819, """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.4_251, """answer""": """us-001""", """start""": 16, """end""": 16},
              {"""score""": 0.0_819, """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.4_251, """answer""": """us-001""", """start""": 16, """end""": 16},
                  {"""score""": 0.0_819, """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.4_251, """answer""": """us-001""", """start""": 16, """end""": 16},
              {"""score""": 0.0_819, """answer""": """1110212019""", """start""": 23, """end""": 23},
          ] ,					)


    @slow
    @require_torch
    @require_pytesseract
    @require_vision
    def   lowerCAmelCase_	(       self						:	Optional[int]       ):
      _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.9_999, """answer""": """us-001""", """start""": 16, """end""": 16},
              {"""score""": 0.9_998, """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.9_999, """answer""": """us-001""", """start""": 16, """end""": 16},
                  {"""score""": 0.9_998, """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.9_999, """answer""": """us-001""", """start""": 16, """end""": 16},
              {"""score""": 0.9_998, """answer""": """us-001""", """start""": 16, """end""": 16},
          ] ,					)


    @slow
    @require_torch
    def   lowerCAmelCase_	(       self						:	Optional[Any]       ):
      _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						:	int       ):
      pass

"""simple docstring"""


from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase__			=    logging.get_logger(__name__)


class 							a					(		lowerCAmelCase_ ):
    _snake_case  :	List[Any]  				=				'timm_backbone'


    def __init__(       self						:	List[Any] ,					__lowerCAmelCase						:	str=None ,					__lowerCAmelCase						:	Optional[int]=3 ,					__lowerCAmelCase						:	Any=True ,					__lowerCAmelCase						:	Optional[int]=True ,					__lowerCAmelCase						:	Optional[Any]=None ,					**__lowerCAmelCase						:	Optional[Any] ,					):
      super().__init__(**__lowerCAmelCase       )
      _UpperCAmelCase    			=   backbone
      _UpperCAmelCase    			=   num_channels
      _UpperCAmelCase    			=   features_only
      _UpperCAmelCase    			=   use_pretrained_backbone
      _UpperCAmelCase    			=   True
      _UpperCAmelCase    			=   out_indices if out_indices is not None else (-1,)
