


'''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 			snake_case__ (   *__lowercase   ,   **__lowercase    ):

   """simple docstring"""







   pass


 def       _lowercase							(					UpperCamelCase__							:       Tuple      ):
  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_			:  Optional[Any]										=   (
    'https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png'
)

@is_pipeline_test
@require_torch
@require_vision
class     _lowerCamelCase       (     unittest.TestCase					):




 '''simple docstring'''
 __lowercase					:		Optional[int]     				=    MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING


 @require_pytesseract
 @require_vision
 def 			snake_case__ (   self   ,   __lowercase   ,   __lowercase   ,   __lowercase    ):

  """simple docstring"""







  __A	:		List[str]	  =      pipeline(
      'document-question-answering'   ,   model=__lowercase   ,   tokenizer=__lowercase   ,   image_processor=__lowercase    )

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


 def 			snake_case__ (   self   ,   __lowercase   ,   __lowercase    ):

  """simple docstring"""







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


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

  """simple docstring"""







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

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

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

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

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


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

  """simple docstring"""







  __A	:		Dict	  =      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	:		Any	  =      dqa_pipeline(image=__lowercase   ,   question=__lowercase   ,   top_k=2    )
  self.assertEqual(
      nested_simplify(__lowercase   ,   decimals=4    )   ,   [
          {'score': 0.9_9_4_4, 'answer': 'us-001', 'start': 16, 'end': 16},
          {'score': 0.0_0_0_9, 'answer': 'us-001', 'start': 16, 'end': 16},
      ]   ,   )

  __A	:		Tuple	  =      dqa_pipeline({'image': image, 'question': question}   ,   top_k=2    )
  self.assertEqual(
      nested_simplify(__lowercase   ,   decimals=4    )   ,   [
          {'score': 0.9_9_4_4, 'answer': 'us-001', 'start': 16, 'end': 16},
          {'score': 0.0_0_0_9, 'answer': 'us-001', 'start': 16, 'end': 16},
      ]   ,   )

  __A	:		Dict	  =      dqa_pipeline(
      [{'image': image, 'question': question}, {'image': image, 'question': question}]   ,   top_k=2    )
  self.assertEqual(
      nested_simplify(__lowercase   ,   decimals=4    )   ,   [
          [
              {'score': 0.9_9_4_4, 'answer': 'us-001', 'start': 16, 'end': 16},
              {'score': 0.0_0_0_9, 'answer': 'us-001', 'start': 16, 'end': 16},
          ],
      ]
      * 2   ,   )


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

  """simple docstring"""







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

  __A	:		Union[str, Any]	  =      dqa_pipeline(image=__lowercase   ,   question=__lowercase   ,   top_k=2    )
  self.assertEqual(
      nested_simplify(__lowercase   ,   decimals=4    )   ,   [
          {'score': 0.9_9_7_4, 'answer': '1110212019', 'start': 23, 'end': 23},
          {'score': 0.9_9_4_8, 'answer': 'us-001', 'start': 16, 'end': 16},
      ]   ,   )

  __A	:		Union[str, Any]	  =      dqa_pipeline({'image': image, 'question': question}   ,   top_k=2    )
  self.assertEqual(
      nested_simplify(__lowercase   ,   decimals=4    )   ,   [
          {'score': 0.9_9_7_4, 'answer': '1110212019', 'start': 23, 'end': 23},
          {'score': 0.9_9_4_8, 'answer': 'us-001', 'start': 16, 'end': 16},
      ]   ,   )

  __A	:		Union[str, Any]	  =      dqa_pipeline(
      [{'image': image, 'question': question}, {'image': image, 'question': question}]   ,   top_k=2    )
  self.assertEqual(
      nested_simplify(__lowercase   ,   decimals=4    )   ,   [
          [
              {'score': 0.9_9_7_4, 'answer': '1110212019', 'start': 23, 'end': 23},
              {'score': 0.9_9_4_8, 'answer': 'us-001', 'start': 16, 'end': 16},
          ]
      ]
      * 2   ,   )


 @slow
 @require_torch
 @require_pytesseract
 @require_vision
 def 			snake_case__ (   self    ):

  """simple docstring"""







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

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

  __A	:		Optional[Any]	  =      dqa_pipeline({'image': image, 'question': question}   ,   top_k=2    )
  self.assertEqual(
      nested_simplify(__lowercase   ,   decimals=4    )   ,   [
          {'score': 0.4_2_5_1, 'answer': 'us-001', 'start': 16, 'end': 16},
          {'score': 0.0_8_1_9, 'answer': '1110212019', 'start': 23, 'end': 23},
      ]   ,   )

  __A	:		List[Any]	  =      dqa_pipeline(
      [{'image': image, 'question': question}, {'image': image, 'question': question}]   ,   top_k=2    )
  self.assertEqual(
      nested_simplify(__lowercase   ,   decimals=4    )   ,   [
          [
              {'score': 0.4_2_5_1, 'answer': 'us-001', 'start': 16, 'end': 16},
              {'score': 0.0_8_1_9, 'answer': '1110212019', 'start': 23, 'end': 23},
          ]
      ]
      * 2   ,   )

  __A	:		int	  =      list(zip(*apply_tesseract(load_image(__lowercase    )   ,   __lowercase   ,   ''    )    )    )

  # This model should also work if `image` is set to None
  __A	:		Optional[int]	  =      dqa_pipeline({'image': None, 'word_boxes': word_boxes, 'question': question}   ,   top_k=2    )
  self.assertEqual(
      nested_simplify(__lowercase   ,   decimals=4    )   ,   [
          {'score': 0.4_2_5_1, 'answer': 'us-001', 'start': 16, 'end': 16},
          {'score': 0.0_8_1_9, 'answer': '1110212019', 'start': 23, 'end': 23},
      ]   ,   )


 @slow
 @require_torch
 @require_pytesseract
 @require_vision
 def 			snake_case__ (   self    ):

  """simple docstring"""







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

  __A	:		List[str]	  =      dqa_pipeline(image=__lowercase   ,   question=__lowercase   ,   top_k=2    )
  self.assertEqual(
      nested_simplify(__lowercase   ,   decimals=4    )   ,   [
          {'score': 0.9_9_9_9, 'answer': 'us-001', 'start': 16, 'end': 16},
          {'score': 0.9_9_9_8, 'answer': 'us-001', 'start': 16, 'end': 16},
      ]   ,   )

  __A	:		Optional[int]	  =      dqa_pipeline(
      [{'image': image, 'question': question}, {'image': image, 'question': question}]   ,   top_k=2    )
  self.assertEqual(
      nested_simplify(__lowercase   ,   decimals=4    )   ,   [
          [
              {'score': 0.9_9_9_9, 'answer': 'us-001', 'start': 16, 'end': 16},
              {'score': 0.9_9_9_8, 'answer': 'us-001', 'start': 16, 'end': 16},
          ]
      ]
      * 2   ,   )

  __A	:		Dict	  =      list(zip(*apply_tesseract(load_image(__lowercase    )   ,   __lowercase   ,   ''    )    )    )

  # 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(__lowercase   ,   decimals=4    )   ,   [
          {'score': 0.9_9_9_9, 'answer': 'us-001', 'start': 16, 'end': 16},
          {'score': 0.9_9_9_8, 'answer': 'us-001', 'start': 16, 'end': 16},
      ]   ,   )


 @slow
 @require_torch
 def 			snake_case__ (   self    ):

  """simple docstring"""







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







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

  """simple docstring"""







  pass










'''simple docstring'''
from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase_			:  Optional[int]										=   logging.get_logger(__name__)

UpperCAmelCase_			:  List[str]										=   {
    'google/fnet-base': 'https://huggingface.co/google/fnet-base/resolve/main/config.json',
    'google/fnet-large': 'https://huggingface.co/google/fnet-large/resolve/main/config.json'
    # See all FNet models at https://huggingface.co/models?filter=fnet
}

class     _lowerCamelCase       (     snake_case_					):




 '''simple docstring'''
 __lowercase					:		List[Any]     				=    '''fnet'''


 def __init__(   self   ,   __lowercase=32_000   ,   __lowercase=768   ,   __lowercase=12   ,   __lowercase=3_072   ,   __lowercase="gelu_new"   ,   __lowercase=0.1   ,   __lowercase=512   ,   __lowercase=4   ,   __lowercase=0.0_2   ,   __lowercase=1E-12   ,   __lowercase=False   ,   __lowercase=512   ,   __lowercase=3   ,   __lowercase=1   ,   __lowercase=2   ,   **__lowercase   ,   ):

  """simple docstring"""







  super().__init__(pad_token_id=__lowercase   ,   bos_token_id=__lowercase   ,   eos_token_id=__lowercase   ,   **__lowercase    )

  __A	:		Union[str, Any]	  =      vocab_size
  __A	:		Dict	  =      max_position_embeddings
  __A	:		List[str]	  =      hidden_size
  __A	:		Tuple	  =      num_hidden_layers
  __A	:		Optional[int]	  =      intermediate_size
  __A	:		Dict	  =      hidden_act
  __A	:		List[str]	  =      hidden_dropout_prob
  __A	:		str	  =      initializer_range
  __A	:		Dict	  =      type_vocab_size
  __A	:		int	  =      layer_norm_eps
  __A	:		Tuple	  =      use_tpu_fourier_optimizations
  __A	:		Optional[Any]	  =      tpu_short_seq_length






