



'''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      lowercase_			( *__snake_case					:    Any		,     **__snake_case					:    Union[str, Any]      ):
           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.
lowerCAmelCase:   Any					=		(
    'https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png'
)

@is_pipeline_test
@require_torch
@require_vision
class 				a__(   unittest.TestCase      ):
    lowercase__					=       MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING







    @require_pytesseract
    @require_vision
    def      lowercase_			( self					:    Tuple		,     __snake_case					:    List[str]		,     __snake_case					:    Optional[int]		,     __snake_case					:    List[Any]      ):
      a		:   Dict	  =	pipeline(
          'document-question-answering'		,     model=__snake_case		,     tokenizer=__snake_case		,     image_processor=__snake_case      )

      a		:   List[Any]	  =	INVOICE_URL
      a		:   Union[str, Any]	  =	list(zip(*apply_tesseract(load_image(__snake_case      )		,     __snake_case		,     ''      )      )      )
      a		:   Optional[int]	  =	'What is the placebo?'
      a		:   Any	  =	[
          {
              'image': load_image(__snake_case      ),
              'question': question,
          },
          {
              'image': image,
              'question': question,
          },
          {
              'image': image,
              'question': question,
              'word_boxes': word_boxes,
          },
      ]
      return dqa_pipeline, examples







    def      lowercase_			( self					:    str		,     __snake_case					:    Optional[Any]		,     __snake_case					:    List[str]      ):
      a		:   Dict	  =	dqa_pipeline(__snake_case		,     top_k=2      )
      self.assertEqual(
          __snake_case		,     [
              [
                  {'score': ANY(__snake_case      ), 'answer': ANY(__snake_case      ), 'start': ANY(__snake_case      ), 'end': ANY(__snake_case      )},
                  {'score': ANY(__snake_case      ), 'answer': ANY(__snake_case      ), 'start': ANY(__snake_case      ), 'end': ANY(__snake_case      )},
              ]
          ]
          * 3		,     )







    @require_torch
    @require_detectrona
    @require_pytesseract
    def      lowercase_			( self					:    Dict      ):
      a		:   Tuple	  =	pipeline('document-question-answering'		,     model='hf-internal-testing/tiny-random-layoutlmv2'      )
      a		:   Tuple	  =	INVOICE_URL
      a		:   str	  =	'How many cats are there?'

      a		:   Optional[int]	  =	[
          {'score': 0.0001, 'answer': 'oy 2312/2019', 'start': 38, 'end': 39},
          {'score': 0.0001, 'answer': 'oy 2312/2019 DUE', 'start': 38, 'end': 40},
      ]
      a		:   Union[str, Any]	  =	dqa_pipeline(image=__snake_case		,     question=__snake_case		,     top_k=2      )
      self.assertEqual(nested_simplify(__snake_case		,     decimals=4      )		,     __snake_case      )

      a		:   Tuple	  =	dqa_pipeline({'image': image, 'question': question}		,     top_k=2      )
      self.assertEqual(nested_simplify(__snake_case		,     decimals=4      )		,     __snake_case      )

      # This image does not detect ANY text in it, meaning layoutlmv2 should fail.
      # Empty answer probably
      a		:   str	  =	'./tests/fixtures/tests_samples/COCO/000000039769.png'
      a		:   List[Any]	  =	dqa_pipeline(image=__snake_case		,     question=__snake_case		,     top_k=2      )
      self.assertEqual(__snake_case		,     []      )

      # We can optionnally pass directly the words and bounding boxes
      a		:   Any	  =	'./tests/fixtures/tests_samples/COCO/000000039769.png'
      a		:   Tuple	  =	[]
      a		:   List[str]	  =	[]
      a		:   int	  =	dqa_pipeline(image=__snake_case		,     question=__snake_case		,     words=__snake_case		,     boxes=__snake_case		,     top_k=2      )
      self.assertEqual(__snake_case		,     []      )







    @slow
    @require_torch
    @require_detectrona
    @require_pytesseract
    def      lowercase_			( self					:    Union[str, Any]      ):
      a		:   Optional[Any]	  =	pipeline(
          'document-question-answering'		,     model='tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'		,     revision='9977165'		,     )
      a		:   List[Any]	  =	INVOICE_URL
      a		:   Any	  =	'What is the invoice number?'

      a		:   Tuple	  =	dqa_pipeline(image=__snake_case		,     question=__snake_case		,     top_k=2      )
      self.assertEqual(
          nested_simplify(__snake_case		,     decimals=4      )		,     [
              {'score': 0.9944, 'answer': 'us-001', 'start': 16, 'end': 16},
              {'score': 0.0009, 'answer': 'us-001', 'start': 16, 'end': 16},
          ]		,     )

      a		:   Any	  =	dqa_pipeline({'image': image, 'question': question}		,     top_k=2      )
      self.assertEqual(
          nested_simplify(__snake_case		,     decimals=4      )		,     [
              {'score': 0.9944, 'answer': 'us-001', 'start': 16, 'end': 16},
              {'score': 0.0009, 'answer': 'us-001', 'start': 16, 'end': 16},
          ]		,     )

      a		:   Any	  =	dqa_pipeline(
          [{'image': image, 'question': question}, {'image': image, 'question': question}]		,     top_k=2      )
      self.assertEqual(
          nested_simplify(__snake_case		,     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      lowercase_			( self					:    Union[str, Any]      ):
      a		:   Optional[int]	  =	pipeline(
          'document-question-answering'		,     model='tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'		,     revision='9977165'		,     max_seq_len=50		,     )
      a		:   Any	  =	INVOICE_URL
      a		:   List[Any]	  =	'What is the invoice number?'

      a		:   Optional[int]	  =	dqa_pipeline(image=__snake_case		,     question=__snake_case		,     top_k=2      )
      self.assertEqual(
          nested_simplify(__snake_case		,     decimals=4      )		,     [
              {'score': 0.9974, 'answer': '1110212019', 'start': 23, 'end': 23},
              {'score': 0.9948, 'answer': 'us-001', 'start': 16, 'end': 16},
          ]		,     )

      a		:   Union[str, Any]	  =	dqa_pipeline({'image': image, 'question': question}		,     top_k=2      )
      self.assertEqual(
          nested_simplify(__snake_case		,     decimals=4      )		,     [
              {'score': 0.9974, 'answer': '1110212019', 'start': 23, 'end': 23},
              {'score': 0.9948, 'answer': 'us-001', 'start': 16, 'end': 16},
          ]		,     )

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

      a		:   Union[str, Any]	  =	dqa_pipeline(image=__snake_case		,     question=__snake_case		,     top_k=2      )
      self.assertEqual(
          nested_simplify(__snake_case		,     decimals=4      )		,     [
              {'score': 0.4251, 'answer': 'us-001', 'start': 16, 'end': 16},
              {'score': 0.0819, 'answer': '1110212019', 'start': 23, 'end': 23},
          ]		,     )

      a		:   Any	  =	dqa_pipeline({'image': image, 'question': question}		,     top_k=2      )
      self.assertEqual(
          nested_simplify(__snake_case		,     decimals=4      )		,     [
              {'score': 0.4251, 'answer': 'us-001', 'start': 16, 'end': 16},
              {'score': 0.0819, 'answer': '1110212019', 'start': 23, 'end': 23},
          ]		,     )

      a		:   List[str]	  =	dqa_pipeline(
          [{'image': image, 'question': question}, {'image': image, 'question': question}]		,     top_k=2      )
      self.assertEqual(
          nested_simplify(__snake_case		,     decimals=4      )		,     [
              [
                  {'score': 0.4251, 'answer': 'us-001', 'start': 16, 'end': 16},
                  {'score': 0.0819, 'answer': '1110212019', 'start': 23, 'end': 23},
              ]
          ]
          * 2		,     )

      a		:   str	  =	list(zip(*apply_tesseract(load_image(__snake_case      )		,     __snake_case		,     ''      )      )      )

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

      a		:   Any	  =	dqa_pipeline(image=__snake_case		,     question=__snake_case		,     top_k=2      )
      self.assertEqual(
          nested_simplify(__snake_case		,     decimals=4      )		,     [
              {'score': 0.9999, 'answer': 'us-001', 'start': 16, 'end': 16},
              {'score': 0.9998, 'answer': 'us-001', 'start': 16, 'end': 16},
          ]		,     )

      a		:   str	  =	dqa_pipeline(
          [{'image': image, 'question': question}, {'image': image, 'question': question}]		,     top_k=2      )
      self.assertEqual(
          nested_simplify(__snake_case		,     decimals=4      )		,     [
              [
                  {'score': 0.9999, 'answer': 'us-001', 'start': 16, 'end': 16},
                  {'score': 0.9998, 'answer': 'us-001', 'start': 16, 'end': 16},
              ]
          ]
          * 2		,     )

      a		:   Union[str, Any]	  =	list(zip(*apply_tesseract(load_image(__snake_case      )		,     __snake_case		,     ''      )      )      )

      # This model should also work if `image` is set to None
      a		:   int	  =	dqa_pipeline({'image': None, 'word_boxes': word_boxes, 'question': question}		,     top_k=2      )
      self.assertEqual(
          nested_simplify(__snake_case		,     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      lowercase_			( self					:    Union[str, Any]      ):
      a		:   int	  =	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		:   List[Any]	  =	INVOICE_URL
      a		:   int	  =	'What is the invoice number?'
      a		:   List[str]	  =	dqa_pipeline(image=__snake_case		,     question=__snake_case		,     top_k=2      )
      self.assertEqual(nested_simplify(__snake_case		,     decimals=4      )		,     [{'answer': 'us-001'}]      )







    @require_tf
    @unittest.skip('Document question answering not implemented in TF'      )
    def      lowercase_			( self					:    List[str]      ):
      pass




'''simple docstring'''






from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_tokenizers_available,
    is_torch_available,
)


lowerCAmelCase:   List[Any]					=		{'configuration_reformer': ['REFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ReformerConfig']}

try:
     if not is_sentencepiece_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     lowerCAmelCase:   Dict					=		['ReformerTokenizer']

try:
     if not is_tokenizers_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     lowerCAmelCase:   Tuple					=		['ReformerTokenizerFast']

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     lowerCAmelCase:   List[Any]					=		[
         'REFORMER_PRETRAINED_MODEL_ARCHIVE_LIST',
         'ReformerAttention',
         'ReformerForMaskedLM',
         'ReformerForQuestionAnswering',
         'ReformerForSequenceClassification',
         'ReformerLayer',
         'ReformerModel',
         'ReformerModelWithLMHead',
         'ReformerPreTrainedModel',
     ]


if TYPE_CHECKING:
     from .configuration_reformer import REFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, ReformerConfig

     try:
          if not is_sentencepiece_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .tokenization_reformer import ReformerTokenizer

     try:
          if not is_tokenizers_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .tokenization_reformer_fast import ReformerTokenizerFast

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_reformer import (
              REFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
              ReformerAttention,
              ReformerForMaskedLM,
              ReformerForQuestionAnswering,
              ReformerForSequenceClassification,
              ReformerLayer,
              ReformerModel,
              ReformerModelWithLMHead,
              ReformerPreTrainedModel,
          )

else:
     import sys

     lowerCAmelCase:   str					=		_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)