





'''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 					SCREAMING_SNAKE_CASE       :


       @staticmethod
       def 				SCREAMING_SNAKE_CASE    (		*_UpperCAmelCase		,      **_UpperCAmelCase):

            '''simple docstring'''






            pass






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


@is_pipeline_test
@require_torch
@require_vision
class 					SCREAMING_SNAKE_CASE       (unittest.TestCase				):
     lowerCAmelCase						     =			MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING


     @require_pytesseract
     @require_vision
     def 				SCREAMING_SNAKE_CASE    (		self		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase):

          '''simple docstring'''






          __A		:					Dict						=			pipeline(
              'document-question-answering'		,      model=_UpperCAmelCase		,      tokenizer=_UpperCAmelCase		,      image_processor=_UpperCAmelCase)

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


     def 				SCREAMING_SNAKE_CASE    (		self		,      _UpperCAmelCase		,      _UpperCAmelCase):

          '''simple docstring'''






          __A		:					str						=			dqa_pipeline(_UpperCAmelCase		,      top_k=2)
          self.assertEqual(
              _UpperCAmelCase		,      [
                  [
                      {'score': ANY(_UpperCAmelCase), 'answer': ANY(_UpperCAmelCase), 'start': ANY(_UpperCAmelCase), 'end': ANY(_UpperCAmelCase)},
                      {'score': ANY(_UpperCAmelCase), 'answer': ANY(_UpperCAmelCase), 'start': ANY(_UpperCAmelCase), 'end': ANY(_UpperCAmelCase)},
                  ]
              ]
              * 3		,      )


     @require_torch
     @require_detectrona
     @require_pytesseract
     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					List[str]						=			pipeline('document-question-answering'		,      model='hf-internal-testing/tiny-random-layoutlmv2')
          __A		:					Any						=			INVOICE_URL
          __A		:					List[str]						=			'How many cats are there?'

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

          __A		:					Union[str, Any]						=			dqa_pipeline({'image': image, 'question': question}		,      top_k=2)
          self.assertEqual(nested_simplify(_UpperCAmelCase		,      decimals=4)		,      _UpperCAmelCase)

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

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


     @slow
     @require_torch
     @require_detectrona
     @require_pytesseract
     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					List[str]						=			pipeline(
              'document-question-answering'		,      model='tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'		,      revision='9977165'		,      )
          __A		:					Optional[Any]						=			INVOICE_URL
          __A		:					int						=			'What is the invoice number?'

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

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

          __A		:					Optional[Any]						=			dqa_pipeline(
              [{'image': image, 'question': question}, {'image': image, 'question': question}]		,      top_k=2)
          self.assertEqual(
              nested_simplify(_UpperCAmelCase		,      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 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					str						=			pipeline(
              'document-question-answering'		,      model='tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'		,      revision='9977165'		,      max_seq_len=50		,      )
          __A		:					Optional[int]						=			INVOICE_URL
          __A		:					List[str]						=			'What is the invoice number?'

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

          __A		:					Tuple						=			dqa_pipeline({'image': image, 'question': question}		,      top_k=2)
          self.assertEqual(
              nested_simplify(_UpperCAmelCase		,      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(_UpperCAmelCase		,      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 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					Tuple						=			AutoTokenizer.from_pretrained(
              'impira/layoutlm-document-qa'		,      revision='3dc6de3'		,      add_prefix_space=_UpperCAmelCase)
          __A		:					List[Any]						=			pipeline(
              'document-question-answering'		,      model='impira/layoutlm-document-qa'		,      tokenizer=_UpperCAmelCase		,      revision='3dc6de3'		,      )
          __A		:					Tuple						=			INVOICE_URL
          __A		:					List[Any]						=			'What is the invoice number?'

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

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

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

          __A		:					Tuple						=			list(zip(*apply_tesseract(load_image(_UpperCAmelCase)		,      _UpperCAmelCase		,      '')))

          # This model should also work if `image` is set to None
          __A		:					Union[str, Any]						=			dqa_pipeline({'image': None, 'word_boxes': word_boxes, 'question': question}		,      top_k=2)
          self.assertEqual(
              nested_simplify(_UpperCAmelCase		,      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 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					int						=			AutoTokenizer.from_pretrained(
              'impira/layoutlm-document-qa'		,      revision='3dc6de3'		,      add_prefix_space=_UpperCAmelCase)
          __A		:					Dict						=			pipeline(
              'document-question-answering'		,      model='impira/layoutlm-document-qa'		,      tokenizer=_UpperCAmelCase		,      revision='3dc6de3'		,      max_seq_len=50		,      )
          __A		:					str						=			INVOICE_URL
          __A		:					List[Any]						=			'What is the invoice number?'

          __A		:					Tuple						=			dqa_pipeline(image=_UpperCAmelCase		,      question=_UpperCAmelCase		,      top_k=2)
          self.assertEqual(
              nested_simplify(_UpperCAmelCase		,      decimals=4)		,      [
                  {'score': 0.9999, 'answer': 'us-001', 'start': 16, 'end': 16},
                  {'score': 0.9998, '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(_UpperCAmelCase		,      decimals=4)		,      [
                  [
                      {'score': 0.9999, 'answer': 'us-001', 'start': 16, 'end': 16},
                      {'score': 0.9998, 'answer': 'us-001', 'start': 16, 'end': 16},
                  ]
              ]
              * 2		,      )

          __A		:					Tuple						=			list(zip(*apply_tesseract(load_image(_UpperCAmelCase)		,      _UpperCAmelCase		,      '')))

          # This model should also work if `image` is set to None
          __A		:					Any						=			dqa_pipeline({'image': None, 'word_boxes': word_boxes, 'question': question}		,      top_k=2)
          self.assertEqual(
              nested_simplify(_UpperCAmelCase		,      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 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					Optional[Any]						=			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		:					int						=			INVOICE_URL
          __A		:					Union[str, Any]						=			'What is the invoice number?'
          __A		:					List[Any]						=			dqa_pipeline(image=_UpperCAmelCase		,      question=_UpperCAmelCase		,      top_k=2)
          self.assertEqual(nested_simplify(_UpperCAmelCase		,      decimals=4)		,      [{'answer': 'us-001'}])


     @require_tf
     @unittest.skip('Document question answering not implemented in TF')
     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          pass



from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available


snake_case							:       Any										=	{
    '''configuration_gpt_neo''': ['''GPT_NEO_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''GPTNeoConfig''', '''GPTNeoOnnxConfig'''],
}

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     snake_case							:       List[Any]										=	[
         '''GPT_NEO_PRETRAINED_MODEL_ARCHIVE_LIST''',
         '''GPTNeoForCausalLM''',
         '''GPTNeoForQuestionAnswering''',
         '''GPTNeoForSequenceClassification''',
         '''GPTNeoForTokenClassification''',
         '''GPTNeoModel''',
         '''GPTNeoPreTrainedModel''',
         '''load_tf_weights_in_gpt_neo''',
     ]

try:
     if not is_flax_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     snake_case							:       Any										=	[
         '''FlaxGPTNeoForCausalLM''',
         '''FlaxGPTNeoModel''',
         '''FlaxGPTNeoPreTrainedModel''',
     ]


if TYPE_CHECKING:
     from .configuration_gpt_neo import GPT_NEO_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTNeoConfig, GPTNeoOnnxConfig

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_gpt_neo import (
              GPT_NEO_PRETRAINED_MODEL_ARCHIVE_LIST,
              GPTNeoForCausalLM,
              GPTNeoForQuestionAnswering,
              GPTNeoForSequenceClassification,
              GPTNeoForTokenClassification,
              GPTNeoModel,
              GPTNeoPreTrainedModel,
              load_tf_weights_in_gpt_neo,
          )

     try:
          if not is_flax_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_flax_gpt_neo import FlaxGPTNeoForCausalLM, FlaxGPTNeoModel, FlaxGPTNeoPreTrainedModel


else:
     import sys

     snake_case							:       Optional[int]										=	_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)





