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






										'''simple docstring'''

										@staticmethod
										def  lowerCAmelCase__		(    *snake_case_	:    Any  ,		**snake_case_	:    str ):
														pass







							def     A__   (  lowerCamelCase  )					->		List[str]:
											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_  :	Union[str, Any] 				=					(
    """https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png"""
)

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






			'''simple docstring'''

			__UpperCamelCase :  int    =	MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING

			@require_pytesseract
			@require_vision
			def  lowerCAmelCase__		(    self	:    Dict  ,		snake_case_	:    Union[str, Any]  ,		snake_case_	:    Optional[Any]  ,		snake_case_	:    List[str] ):
							UpperCamelCase_:							List[Any]		=  pipeline(
							    """document-question-answering"""  ,		model=snake_case_  ,		tokenizer=snake_case_  ,		image_processor=snake_case_ )

							UpperCamelCase_:							Optional[int]		=  INVOICE_URL
							UpperCamelCase_:							Any		=  list(zip(*apply_tesseract(load_image(snake_case_ )  ,		snake_case_  ,		"""""" ) ) )
							UpperCamelCase_:							List[Any]		=  """What is the placebo?"""
							UpperCamelCase_:							Optional[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  lowerCAmelCase__		(    self	:    str  ,		snake_case_	:    List[str]  ,		snake_case_	:    Optional[int] ):
							UpperCamelCase_:							List[str]		=  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  lowerCAmelCase__		(    self	:    List[str] ):
							UpperCamelCase_:							Union[str, Any]		=  pipeline("""document-question-answering"""  ,		model="""hf-internal-testing/tiny-random-layoutlmv2""" )
							UpperCamelCase_:							List[str]		=  INVOICE_URL
							UpperCamelCase_:							Dict		=  """How many cats are there?"""

							UpperCamelCase_:							str		=  [
							    {"""score""": 0.0001, """answer""": """oy 2312/2019""", """start""": 38, """end""": 39},
							    {"""score""": 0.0001, """answer""": """oy 2312/2019 DUE""", """start""": 38, """end""": 40},
							]
							UpperCamelCase_:							List[Any]		=  dqa_pipeline(image=snake_case_  ,		question=snake_case_  ,		top_k=2 )
							self.assertEqual(nested_simplify(snake_case_  ,		decimals=4 )  ,		snake_case_ )

							UpperCamelCase_:							Optional[int]		=  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
							UpperCamelCase_:							Any		=  """./tests/fixtures/tests_samples/COCO/000000039769.png"""
							UpperCamelCase_:							Optional[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
							UpperCamelCase_:							Tuple		=  """./tests/fixtures/tests_samples/COCO/000000039769.png"""
							UpperCamelCase_:							List[str]		=  []
							UpperCamelCase_:							Optional[int]		=  []
							UpperCamelCase_:							Dict		=  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  lowerCAmelCase__		(    self	:    Union[str, Any] ):
							UpperCamelCase_:							int		=  pipeline(
							    """document-question-answering"""  ,		model="""tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"""  ,		revision="""9977165"""  ,		)
							UpperCamelCase_:							str		=  INVOICE_URL
							UpperCamelCase_:							Dict		=  """What is the invoice number?"""

							UpperCamelCase_:							Any		=  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},
							    ]  ,		)

							UpperCamelCase_:							List[str]		=  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},
							    ]  ,		)

							UpperCamelCase_:							Optional[int]		=  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  lowerCAmelCase__		(    self	:    Optional[int] ):
							UpperCamelCase_:							List[Any]		=  pipeline(
							    """document-question-answering"""  ,		model="""tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"""  ,		revision="""9977165"""  ,		max_seq_len=50  ,		)
							UpperCamelCase_:							int		=  INVOICE_URL
							UpperCamelCase_:							int		=  """What is the invoice number?"""

							UpperCamelCase_:							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},
							    ]  ,		)

							UpperCamelCase_:							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},
							    ]  ,		)

							UpperCamelCase_:							Dict		=  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  lowerCAmelCase__		(    self	:    str ):
							UpperCamelCase_:							Dict		=  AutoTokenizer.from_pretrained(
							    """impira/layoutlm-document-qa"""  ,		revision="""3dc6de3"""  ,		add_prefix_space=snake_case_ )
							UpperCamelCase_:							str		=  pipeline(
							    """document-question-answering"""  ,		model="""impira/layoutlm-document-qa"""  ,		tokenizer=snake_case_  ,		revision="""3dc6de3"""  ,		)
							UpperCamelCase_:							str		=  INVOICE_URL
							UpperCamelCase_:							str		=  """What is the invoice number?"""

							UpperCamelCase_:							List[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},
							    ]  ,		)

							UpperCamelCase_:							List[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},
							    ]  ,		)

							UpperCamelCase_:							Dict		=  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  ,		)

							UpperCamelCase_:							Union[str, Any]		=  list(zip(*apply_tesseract(load_image(snake_case_ )  ,		snake_case_  ,		"""""" ) ) )

							# 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(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  lowerCAmelCase__		(    self	:    Tuple ):
							UpperCamelCase_:							str		=  AutoTokenizer.from_pretrained(
							    """impira/layoutlm-document-qa"""  ,		revision="""3dc6de3"""  ,		add_prefix_space=snake_case_ )
							UpperCamelCase_:							Tuple		=  pipeline(
							    """document-question-answering"""  ,		model="""impira/layoutlm-document-qa"""  ,		tokenizer=snake_case_  ,		revision="""3dc6de3"""  ,		max_seq_len=50  ,		)
							UpperCamelCase_:							List[Any]		=  INVOICE_URL
							UpperCamelCase_:							Union[str, Any]		=  """What is the invoice number?"""

							UpperCamelCase_:							int		=  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},
							    ]  ,		)

							UpperCamelCase_:							Any		=  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  ,		)

							UpperCamelCase_:							Optional[int]		=  list(zip(*apply_tesseract(load_image(snake_case_ )  ,		snake_case_  ,		"""""" ) ) )

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








from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding

class     _UpperCamelCase			(   _A					):






			'''simple docstring'''

			__UpperCamelCase :  Union[str, Any]    =	["""image_processor""", """tokenizer"""]
			__UpperCamelCase :  List[Any]    =	"""AutoImageProcessor"""
			__UpperCamelCase :  Tuple    =	"""AutoTokenizer"""

			def __init__(    self	:    Any  ,		snake_case_	:    Optional[Any]  ,		snake_case_	:    Any ):
							super().__init__(snake_case_  ,		snake_case_ )
							UpperCamelCase_:							int		=  self.image_processor

			def __call__(    self	:    str  ,		snake_case_	:    Optional[int]=None  ,		snake_case_	:    int=None  ,		snake_case_	:    Dict=None  ,		**snake_case_	:    Optional[int] ):

							if text is None and images is None:
											raise ValueError("""You have to specify either text or images. Both cannot be none.""" )

							if text is not None:
											UpperCamelCase_:							List[Any]		=  self.tokenizer(snake_case_  ,		return_tensors=snake_case_  ,		**snake_case_ )

							if images is not None:
											UpperCamelCase_:							Any		=  self.image_processor(snake_case_  ,		return_tensors=snake_case_  ,		**snake_case_ )

							if text is not None and images is not None:
											UpperCamelCase_:							Tuple		=  image_features.pixel_values
											return encoding
							elif text is not None:
											return encoding
							else:
											return BatchEncoding(data=dict(**snake_case_ )  ,		tensor_type=snake_case_ )

			def  lowerCAmelCase__		(    self	:    List[str]  ,		*snake_case_	:    int  ,		**snake_case_	:    Optional[Any] ):
							return self.tokenizer.batch_decode(*snake_case_  ,		**snake_case_ )

			def  lowerCAmelCase__		(    self	:    int  ,		*snake_case_	:    Optional[Any]  ,		**snake_case_	:    str ):
							return self.tokenizer.decode(*snake_case_  ,		**snake_case_ )




			@property
			def  lowerCAmelCase__		(    self	:    Union[str, Any] ):
							return ["input_ids", "attention_mask", "pixel_values"]






