





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  __UpperCamelCase(				*A_ ,							**A_     ):
           '''simple docstring'''




           pass



    def 	A_       (  _lowerCAmelCase  )		->				Optional[int]:
     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 :  List[str]										=  (
    """https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png"""
)



@is_pipeline_test
@require_torch
@require_vision
class       A__     ( unittest.TestCase    ):
      _UpperCAmelCase			:Optional[int]							=     MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING





      @require_pytesseract
      @require_vision
      def  __UpperCamelCase(				self ,							A_ ,							A_ ,							A_     ):
       '''simple docstring'''




       UpperCamelCase			:      Optional[int] 			=     pipeline(
           "document-question-answering" ,							model=A_ ,							tokenizer=A_ ,							image_processor=A_     )

       UpperCamelCase			:      Union[str, Any] 			=     INVOICE_URL
       UpperCamelCase			:      List[Any] 			=     list(zip(*apply_tesseract(load_image(A_     ) ,							A_ ,							""     )     )     )
       UpperCamelCase			:      Dict 			=     "What is the placebo?"
       UpperCamelCase			:      Any 			=     [
           {
               "image": load_image(A_     ),
               "question": question,
           },
           {
               "image": image,
               "question": question,
           },
           {
               "image": image,
               "question": question,
               "word_boxes": word_boxes,
           },
       ]
       return dqa_pipeline, examples





      def  __UpperCamelCase(				self ,							A_ ,							A_     ):
       '''simple docstring'''




       UpperCamelCase			:      Any 			=     dqa_pipeline(A_ ,							top_k=2     )
       self.assertEqual(
           A_ ,							[
               [
                   {"score": ANY(A_     ), "answer": ANY(A_     ), "start": ANY(A_     ), "end": ANY(A_     )},
                   {"score": ANY(A_     ), "answer": ANY(A_     ), "start": ANY(A_     ), "end": ANY(A_     )},
               ]
           ]
           * 3 ,							)





      @require_torch
      @require_detectrona
      @require_pytesseract
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




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

       UpperCamelCase			:      int 			=     [
           {"score": 0.00_01, "answer": "oy 2312/2019", "start": 38, "end": 39},
           {"score": 0.00_01, "answer": "oy 2312/2019 DUE", "start": 38, "end": 40},
       ]
       UpperCamelCase			:      int 			=     dqa_pipeline(image=A_ ,							question=A_ ,							top_k=2     )
       self.assertEqual(nested_simplify(A_ ,							decimals=4     ) ,							A_     )

       UpperCamelCase			:      Dict 			=     dqa_pipeline({"image": image, "question": question} ,							top_k=2     )
       self.assertEqual(nested_simplify(A_ ,							decimals=4     ) ,							A_     )

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

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





      @slow
      @require_torch
      @require_detectrona
      @require_pytesseract
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      Dict 			=     pipeline(
           "document-question-answering" ,							model="tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa" ,							revision="9977165" ,							)
       UpperCamelCase			:      Dict 			=     INVOICE_URL
       UpperCamelCase			:      int 			=     "What is the invoice number?"

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

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

       UpperCamelCase			:      Union[str, Any] 			=     dqa_pipeline(
           [{"image": image, "question": question}, {"image": image, "question": question}] ,							top_k=2     )
       self.assertEqual(
           nested_simplify(A_ ,							decimals=4     ) ,							[
               [
                   {"score": 0.99_44, "answer": "us-001", "start": 16, "end": 16},
                   {"score": 0.00_09, "answer": "us-001", "start": 16, "end": 16},
               ],
           ]
           * 2 ,							)





      @slow
      @require_torch
      @require_detectrona
      @require_pytesseract
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




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

       UpperCamelCase			:      Dict 			=     dqa_pipeline(image=A_ ,							question=A_ ,							top_k=2     )
       self.assertEqual(
           nested_simplify(A_ ,							decimals=4     ) ,							[
               {"score": 0.99_74, "answer": "1110212019", "start": 23, "end": 23},
               {"score": 0.99_48, "answer": "us-001", "start": 16, "end": 16},
           ] ,							)

       UpperCamelCase			:      int 			=     dqa_pipeline({"image": image, "question": question} ,							top_k=2     )
       self.assertEqual(
           nested_simplify(A_ ,							decimals=4     ) ,							[
               {"score": 0.99_74, "answer": "1110212019", "start": 23, "end": 23},
               {"score": 0.99_48, "answer": "us-001", "start": 16, "end": 16},
           ] ,							)

       UpperCamelCase			:      Dict 			=     dqa_pipeline(
           [{"image": image, "question": question}, {"image": image, "question": question}] ,							top_k=2     )
       self.assertEqual(
           nested_simplify(A_ ,							decimals=4     ) ,							[
               [
                   {"score": 0.99_74, "answer": "1110212019", "start": 23, "end": 23},
                   {"score": 0.99_48, "answer": "us-001", "start": 16, "end": 16},
               ]
           ]
           * 2 ,							)





      @slow
      @require_torch
      @require_pytesseract
      @require_vision
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




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

       UpperCamelCase			:      Optional[Any] 			=     dqa_pipeline(image=A_ ,							question=A_ ,							top_k=2     )
       self.assertEqual(
           nested_simplify(A_ ,							decimals=4     ) ,							[
               {"score": 0.42_51, "answer": "us-001", "start": 16, "end": 16},
               {"score": 0.08_19, "answer": "1110212019", "start": 23, "end": 23},
           ] ,							)

       UpperCamelCase			:      Any 			=     dqa_pipeline({"image": image, "question": question} ,							top_k=2     )
       self.assertEqual(
           nested_simplify(A_ ,							decimals=4     ) ,							[
               {"score": 0.42_51, "answer": "us-001", "start": 16, "end": 16},
               {"score": 0.08_19, "answer": "1110212019", "start": 23, "end": 23},
           ] ,							)

       UpperCamelCase			:      Optional[Any] 			=     dqa_pipeline(
           [{"image": image, "question": question}, {"image": image, "question": question}] ,							top_k=2     )
       self.assertEqual(
           nested_simplify(A_ ,							decimals=4     ) ,							[
               [
                   {"score": 0.42_51, "answer": "us-001", "start": 16, "end": 16},
                   {"score": 0.08_19, "answer": "1110212019", "start": 23, "end": 23},
               ]
           ]
           * 2 ,							)

       UpperCamelCase			:      List[str] 			=     list(zip(*apply_tesseract(load_image(A_     ) ,							A_ ,							""     )     )     )

       # This model should also work if `image` is set to None
       UpperCamelCase			:      Any 			=     dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question} ,							top_k=2     )
       self.assertEqual(
           nested_simplify(A_ ,							decimals=4     ) ,							[
               {"score": 0.42_51, "answer": "us-001", "start": 16, "end": 16},
               {"score": 0.08_19, "answer": "1110212019", "start": 23, "end": 23},
           ] ,							)





      @slow
      @require_torch
      @require_pytesseract
      @require_vision
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




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

       UpperCamelCase			:      Dict 			=     dqa_pipeline(image=A_ ,							question=A_ ,							top_k=2     )
       self.assertEqual(
           nested_simplify(A_ ,							decimals=4     ) ,							[
               {"score": 0.99_99, "answer": "us-001", "start": 16, "end": 16},
               {"score": 0.99_98, "answer": "us-001", "start": 16, "end": 16},
           ] ,							)

       UpperCamelCase			:      str 			=     dqa_pipeline(
           [{"image": image, "question": question}, {"image": image, "question": question}] ,							top_k=2     )
       self.assertEqual(
           nested_simplify(A_ ,							decimals=4     ) ,							[
               [
                   {"score": 0.99_99, "answer": "us-001", "start": 16, "end": 16},
                   {"score": 0.99_98, "answer": "us-001", "start": 16, "end": 16},
               ]
           ]
           * 2 ,							)

       UpperCamelCase			:      str 			=     list(zip(*apply_tesseract(load_image(A_     ) ,							A_ ,							""     )     )     )

       # This model should also work if `image` is set to None
       UpperCamelCase			:      int 			=     dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question} ,							top_k=2     )
       self.assertEqual(
           nested_simplify(A_ ,							decimals=4     ) ,							[
               {"score": 0.99_99, "answer": "us-001", "start": 16, "end": 16},
               {"score": 0.99_98, "answer": "us-001", "start": 16, "end": 16},
           ] ,							)





      @slow
      @require_torch
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      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" ,							)

       UpperCamelCase			:      Tuple 			=     INVOICE_URL
       UpperCamelCase			:      Tuple 			=     "What is the invoice number?"
       UpperCamelCase			:      Optional[Any] 			=     dqa_pipeline(image=A_ ,							question=A_ ,							top_k=2     )
       self.assertEqual(nested_simplify(A_ ,							decimals=4     ) ,							[{"answer": "us-001"}]     )





      @require_tf
      @unittest.skip("Document question answering not implemented in TF"     )
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       pass







import math

import tensorflow as tf
from packaging import version



def 	A_       (  _lowerCAmelCase  )		->				Any:
 UpperCamelCase			:      List[Any] 			=     tf.convert_to_tensor(_lowerCAmelCase  )
 UpperCamelCase			:      Any 			=     0.5 * (1.0 + tf.math.erf(x / tf.cast(tf.sqrt(2.0  )							,     x.dtype  )  ))

 return x * cdf



def 	A_       (  _lowerCAmelCase  )		->				Dict:
 UpperCamelCase			:      Union[str, Any] 			=     tf.convert_to_tensor(_lowerCAmelCase  )
 UpperCamelCase			:      List[Any] 			=     tf.cast(math.pi							,     x.dtype  )
 UpperCamelCase			:      Optional[Any] 			=     tf.cast(0.044_715							,     x.dtype  )
 UpperCamelCase			:      int 			=     0.5 * (1.0 + tf.tanh(tf.sqrt(2.0 / pi  ) * (x + coeff * tf.pow(_lowerCAmelCase							,     3  ))  ))

 return x * cdf



def 	A_       (  _lowerCAmelCase  )		->				List[Any]:
 UpperCamelCase			:      str 			=     tf.convert_to_tensor(_lowerCAmelCase  )

 return x * tf.tanh(tf.math.softplus(_lowerCAmelCase  )  )



def 	A_       (  _lowerCAmelCase  )		->				List[Any]:
 UpperCamelCase			:      Tuple 			=     tf.convert_to_tensor(_lowerCAmelCase  )
 UpperCamelCase			:      List[Any] 			=     tf.cast(0.044_715							,     x.dtype  )
 UpperCamelCase			:      Optional[Any] 			=     tf.cast(0.7_978_845_608							,     x.dtype  )

 return 0.5 * x * (1.0 + tf.tanh(x * coeffa * (1.0 + coeffa * x * x)  ))



def 	A_       (  _lowerCAmelCase  )		->				Optional[Any]:
 UpperCamelCase			:      Any 			=     tf.convert_to_tensor(_lowerCAmelCase  )
 UpperCamelCase			:      List[Any] 			=     tf.cast(1.702							,     x.dtype  )
 return x * tf.math.sigmoid(coeff * x  )



def 	A_       (  _lowerCAmelCase  )		->				List[Any]:
 return tf.clip_by_value(_gelu(_lowerCAmelCase  )							,     -10							,     10  )



def 	A_       (  _lowerCAmelCase							,     _lowerCAmelCase=-1  )		->				str:
 UpperCamelCase			,					UpperCamelCase			:      List[Any] 			=     tf.split(_lowerCAmelCase							,     2							,     axis=_lowerCAmelCase  )
 return a * tf.math.sigmoid(_lowerCAmelCase  )


if version.parse(tf.version.VERSION) >= version.parse("""2.4"""):



    def 	A_       (  _lowerCAmelCase  )		->				Any:
     return tf.keras.activations.gelu(_lowerCAmelCase							,     approximate=_lowerCAmelCase  )

    __lowerCamelCase :  Optional[int]										=  tf.keras.activations.gelu


    __lowerCamelCase :  int										=  approximate_gelu_wrap
else:
    __lowerCamelCase :  List[Any]										=  _gelu
    __lowerCamelCase :  Optional[Any]										=  _gelu_new


__lowerCamelCase :  Any										=  {
    """gelu""": gelu,
    """gelu_10""": gelu_aa,
    """gelu_fast""": gelu_fast,
    """gelu_new""": gelu_new,
    """glu""": glu,
    """mish""": mish,
    """quick_gelu""": quick_gelu,
    """relu""": tf.keras.activations.relu,
    """sigmoid""": tf.keras.activations.sigmoid,
    """silu""": tf.keras.activations.swish,
    """swish""": tf.keras.activations.swish,
    """tanh""": tf.keras.activations.tanh,
}



def 	A_       (  _lowerCAmelCase  )		->				Optional[Any]:
 if activation_string in ACTaFN:
  return ACTaFN[activation_string]
 else:
  raise KeyError(F"""function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys()  )}"""  )
