






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





				@staticmethod
				def 	_SCREAMING_SNAKE_CASE ( *lowerCAmelCase_   :					Any      ,      **lowerCAmelCase_   :					Dict		)		->     str:
							pass





			def 							snake_case (  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_							      = (
    '''https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png'''
)







@is_pipeline_test
@require_torch
@require_vision
class      UpperCamelCase_			(unittest.TestCase	):
	__magic_name__       =							MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING





	@require_pytesseract
	@require_vision
	def 	_SCREAMING_SNAKE_CASE ( self   :					Tuple      ,      lowerCAmelCase_   :					Optional[int]      ,      lowerCAmelCase_   :					Any      ,      lowerCAmelCase_   :					Optional[int]		)		->     Union[str, Any]:
				UpperCAmelCase_	:					int     	=   pipeline(
				    "document-question-answering"      ,      model=lowerCAmelCase_      ,      tokenizer=lowerCAmelCase_      ,      image_processor=lowerCAmelCase_		)

				UpperCAmelCase_	:					int     	=   INVOICE_URL
				UpperCAmelCase_	:					Union[str, Any]     	=   list(zip(*apply_tesseract(load_image(lowerCAmelCase_		)      ,      lowerCAmelCase_      ,      ""		)		)		)
				UpperCAmelCase_	:					Optional[Any]     	=   "What is the placebo?"
				UpperCAmelCase_	:					Tuple     	=   [
				    {
				        "image": load_image(lowerCAmelCase_		),
				        "question": question,
				    },
				    {
				        "image": image,
				        "question": question,
				    },
				    {
				        "image": image,
				        "question": question,
				        "word_boxes": word_boxes,
				    },
				]
				return dqa_pipeline, examples





	def 	_SCREAMING_SNAKE_CASE ( self   :					int      ,      lowerCAmelCase_   :					Any      ,      lowerCAmelCase_   :					List[Any]		)		->     int:
				UpperCAmelCase_	:					Union[str, Any]     	=   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 	_SCREAMING_SNAKE_CASE ( self   :					Optional[int]		)		->     Any:
				UpperCAmelCase_	:					Tuple     	=   pipeline("document-question-answering"      ,      model="hf-internal-testing/tiny-random-layoutlmv2"		)
				UpperCAmelCase_	:					Dict     	=   INVOICE_URL
				UpperCAmelCase_	:					int     	=   "How many cats are there?"

				UpperCAmelCase_	:					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},
				]
				UpperCAmelCase_	:					List[str]     	=   dqa_pipeline(image=lowerCAmelCase_      ,      question=lowerCAmelCase_      ,      top_k=2		)
				self.assertEqual(nested_simplify(lowerCAmelCase_      ,      decimals=4		)      ,      lowerCAmelCase_		)

				UpperCAmelCase_	:					Dict     	=   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_	:					int     	=   "./tests/fixtures/tests_samples/COCO/000000039769.png"
				UpperCAmelCase_	:					Dict     	=   dqa_pipeline(image=lowerCAmelCase_      ,      question=lowerCAmelCase_      ,      top_k=2		)
				self.assertEqual(lowerCAmelCase_      ,      []		)

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





	@slow
	@require_torch
	@require_detectrona
	@require_pytesseract
	def 	_SCREAMING_SNAKE_CASE ( self   :					Any		)		->     int:
				UpperCAmelCase_	:					Dict     	=   pipeline(
				    "document-question-answering"      ,      model="tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"      ,      revision="9977165"      ,      )
				UpperCAmelCase_	:					Optional[Any]     	=   INVOICE_URL
				UpperCAmelCase_	:					Dict     	=   "What is the invoice number?"

				UpperCAmelCase_	:					int     	=   dqa_pipeline(image=lowerCAmelCase_      ,      question=lowerCAmelCase_      ,      top_k=2		)
				self.assertEqual(
				    nested_simplify(lowerCAmelCase_      ,      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},
				    ]      ,      )

				UpperCAmelCase_	:					int     	=   dqa_pipeline({"image": image, "question": question}      ,      top_k=2		)
				self.assertEqual(
				    nested_simplify(lowerCAmelCase_      ,      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},
				    ]      ,      )

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

				UpperCAmelCase_	:					str     	=   dqa_pipeline(image=lowerCAmelCase_      ,      question=lowerCAmelCase_      ,      top_k=2		)
				self.assertEqual(
				    nested_simplify(lowerCAmelCase_      ,      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},
				    ]      ,      )

				UpperCAmelCase_	:					Optional[int]     	=   dqa_pipeline({"image": image, "question": question}      ,      top_k=2		)
				self.assertEqual(
				    nested_simplify(lowerCAmelCase_      ,      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},
				    ]      ,      )

				UpperCAmelCase_	:					str     	=   dqa_pipeline(
				    [{"image": image, "question": question}, {"image": image, "question": question}]      ,      top_k=2		)
				self.assertEqual(
				    nested_simplify(lowerCAmelCase_      ,      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 	_SCREAMING_SNAKE_CASE ( self   :					Union[str, Any]		)		->     Union[str, Any]:
				UpperCAmelCase_	:					Any     	=   AutoTokenizer.from_pretrained(
				    "impira/layoutlm-document-qa"      ,      revision="3dc6de3"      ,      add_prefix_space=lowerCAmelCase_		)
				UpperCAmelCase_	:					str     	=   pipeline(
				    "document-question-answering"      ,      model="impira/layoutlm-document-qa"      ,      tokenizer=lowerCAmelCase_      ,      revision="3dc6de3"      ,      )
				UpperCAmelCase_	:					Any     	=   INVOICE_URL
				UpperCAmelCase_	:					List[str]     	=   "What is the invoice number?"

				UpperCAmelCase_	:					str     	=   dqa_pipeline(image=lowerCAmelCase_      ,      question=lowerCAmelCase_      ,      top_k=2		)
				self.assertEqual(
				    nested_simplify(lowerCAmelCase_      ,      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},
				    ]      ,      )

				UpperCAmelCase_	:					Dict     	=   dqa_pipeline({"image": image, "question": question}      ,      top_k=2		)
				self.assertEqual(
				    nested_simplify(lowerCAmelCase_      ,      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},
				    ]      ,      )

				UpperCAmelCase_	:					Union[str, Any]     	=   dqa_pipeline(
				    [{"image": image, "question": question}, {"image": image, "question": question}]      ,      top_k=2		)
				self.assertEqual(
				    nested_simplify(lowerCAmelCase_      ,      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      ,      )

				UpperCAmelCase_	:					Dict     	=   list(zip(*apply_tesseract(load_image(lowerCAmelCase_		)      ,      lowerCAmelCase_      ,      ""		)		)		)

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

				UpperCAmelCase_	:					int     	=   dqa_pipeline(image=lowerCAmelCase_      ,      question=lowerCAmelCase_      ,      top_k=2		)
				self.assertEqual(
				    nested_simplify(lowerCAmelCase_      ,      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},
				    ]      ,      )

				UpperCAmelCase_	:					Tuple     	=   dqa_pipeline(
				    [{"image": image, "question": question}, {"image": image, "question": question}]      ,      top_k=2		)
				self.assertEqual(
				    nested_simplify(lowerCAmelCase_      ,      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      ,      )

				UpperCAmelCase_	:					Optional[int]     	=   list(zip(*apply_tesseract(load_image(lowerCAmelCase_		)      ,      lowerCAmelCase_      ,      ""		)		)		)

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

				UpperCAmelCase_	:					Optional[int]     	=   INVOICE_URL
				UpperCAmelCase_	:					int     	=   "What is the invoice number?"
				UpperCAmelCase_	:					List[str]     	=   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 	_SCREAMING_SNAKE_CASE ( self   :					List[Any]		)		->     List[str]:
				pass










'''simple docstring'''




import warnings

from ...utils import logging
from .image_processing_perceiver import PerceiverImageProcessor


a__					:			int          =     logging.get_logger(__name__)






class 				__snake_case     (					__magic_name__							):





	def __init__(		self      ,       *UpperCamelCase_      ,       **UpperCamelCase_		)      ->		None:
		warnings.warn(
		    'The class PerceiverFeatureExtractor is deprecated and will be removed in version 5 of Transformers.'
		    ' Please use PerceiverImageProcessor instead.'      ,       UpperCamelCase_      ,       )
		super().__init__(*UpperCamelCase_      ,       **UpperCamelCase_		)



