






from typing import Optional

from torch import nn

from .transformer_ad import TransformeraDModel, TransformeraDModelOutput


class     __SCREAMING_SNAKE_CASE					(   nn.Module):






						def __init__(	self     ,						_UpperCamelCase = 16     ,						_UpperCamelCase = 88     ,						_UpperCamelCase = None     ,						_UpperCamelCase = 1     ,						_UpperCamelCase = 0.0     ,						_UpperCamelCase = 32     ,						_UpperCamelCase = None     ,						_UpperCamelCase = False     ,						_UpperCamelCase = None     ,						_UpperCamelCase = None     ,						_UpperCamelCase = "geglu"     ,						_UpperCamelCase = None     ,						):






												"""simple docstring"""
												super().__init__()
												lowerCAmelCase__    						=						nn.ModuleList(
												    [
												        TransformeraDModel(
												            num_attention_heads=_UpperCamelCase     ,						attention_head_dim=_UpperCamelCase     ,						in_channels=_UpperCamelCase     ,						num_layers=_UpperCamelCase     ,						dropout=_UpperCamelCase     ,						norm_num_groups=_UpperCamelCase     ,						cross_attention_dim=_UpperCamelCase     ,						attention_bias=_UpperCamelCase     ,						sample_size=_UpperCamelCase     ,						num_vector_embeds=_UpperCamelCase     ,						activation_fn=_UpperCamelCase     ,						num_embeds_ada_norm=_UpperCamelCase     ,						)
												        for _ in range(2   )
												    ]   )

												# Variables that can be set by a pipeline:

												# The ratio of transformer1 to transformer2's output states to be combined during inference
												lowerCAmelCase__    						=						0.5

												# The shape of `encoder_hidden_states` is expected to be
												# `(batch_size, condition_lengths[0]+condition_lengths[1], num_features)`
												lowerCAmelCase__    						=						[77, 2_57]

												# Which transformer to use to encode which condition.
												# E.g. `(1, 0)` means that we'll use `transformers[1](conditions[0])` and `transformers[0](conditions[1])`
												lowerCAmelCase__    						=						[1, 0]







						def       UpperCamelCase__      (	self     ,						_UpperCamelCase     ,						_UpperCamelCase     ,						_UpperCamelCase=None     ,						_UpperCamelCase=None     ,						_UpperCamelCase=None     ,						_UpperCamelCase = True     ,						):






												"""simple docstring"""
												lowerCAmelCase__    						=						hidden_states

												lowerCAmelCase__    						=						[]
												lowerCAmelCase__    						=						0
												# attention_mask is not used yet
												for i in range(2   ):
																		# for each of the two transformers, pass the corresponding condition tokens
																		lowerCAmelCase__    						=						encoder_hidden_states[:, tokens_start : tokens_start + self.condition_lengths[i]]
																		lowerCAmelCase__    						=						self.transformer_index_for_condition[i]
																		lowerCAmelCase__    						=						self.transformers[transformer_index](
																		    _UpperCamelCase     ,						encoder_hidden_states=_UpperCamelCase     ,						timestep=_UpperCamelCase     ,						cross_attention_kwargs=_UpperCamelCase     ,						return_dict=_UpperCamelCase     ,						)[0]
																		encoded_states.append(encoded_state - input_states   )
																		tokens_start += self.condition_lengths[i]

												lowerCAmelCase__    						=						encoded_states[0] * self.mix_ratio + encoded_states[1] * (1 - self.mix_ratio)
												lowerCAmelCase__    						=						output_states + input_states

												if not return_dict:
																		return (output_states,)

												return TransformeraDModelOutput(sample=_UpperCamelCase   )













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       UpperCamelCase__      (	*_UpperCamelCase     ,						**_UpperCamelCase   ):






																	"""simple docstring"""
																	pass







					def 	_UpperCamelCase   (    UpperCamelCase_       :					Tuple		)  ->   Any:

											"""simple docstring"""



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


@is_pipeline_test
@require_torch
@require_vision
class     __SCREAMING_SNAKE_CASE					(   unittest.TestCase):
						_SCREAMING_SNAKE_CASE	:       Dict   =					MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING






						@require_pytesseract
						@require_vision
						def       UpperCamelCase__      (	self     ,						_UpperCamelCase     ,						_UpperCamelCase     ,						_UpperCamelCase   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						pipeline(
												    'document-question-answering'     ,						model=_UpperCamelCase     ,						tokenizer=_UpperCamelCase     ,						image_processor=_UpperCamelCase   )

												lowerCAmelCase__    						=						INVOICE_URL
												lowerCAmelCase__    						=						list(zip(*apply_tesseract(load_image(_UpperCamelCase   )     ,						_UpperCamelCase     ,						''   )   )   )
												lowerCAmelCase__    						=						'What is the placebo?'
												lowerCAmelCase__    						=						[
												    {
												        'image': load_image(_UpperCamelCase   ),
												        'question': question,
												    },
												    {
												        'image': image,
												        'question': question,
												    },
												    {
												        'image': image,
												        'question': question,
												        'word_boxes': word_boxes,
												    },
												]
												return dqa_pipeline, examples






						def       UpperCamelCase__      (	self     ,						_UpperCamelCase     ,						_UpperCamelCase   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						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       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						pipeline('document-question-answering'     ,						model='hf-internal-testing/tiny-random-layoutlmv2'   )
												lowerCAmelCase__    						=						INVOICE_URL
												lowerCAmelCase__    						=						'How many cats are there?'

												lowerCAmelCase__    						=						[
												    {'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},
												]
												lowerCAmelCase__    						=						dqa_pipeline(image=_UpperCamelCase     ,						question=_UpperCamelCase     ,						top_k=2   )
												self.assertEqual(nested_simplify(_UpperCamelCase     ,						decimals=4   )     ,						_UpperCamelCase   )

												lowerCAmelCase__    						=						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
												lowerCAmelCase__    						=						'./tests/fixtures/tests_samples/COCO/000000039769.png'
												lowerCAmelCase__    						=						dqa_pipeline(image=_UpperCamelCase     ,						question=_UpperCamelCase     ,						top_k=2   )
												self.assertEqual(_UpperCamelCase     ,						[]   )

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






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






												"""simple docstring"""
												lowerCAmelCase__    						=						pipeline(
												    'document-question-answering'     ,						model='tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'     ,						revision='9977165'     ,						)
												lowerCAmelCase__    						=						INVOICE_URL
												lowerCAmelCase__    						=						'What is the invoice number?'

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

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

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

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

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

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

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

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

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

												lowerCAmelCase__    						=						list(zip(*apply_tesseract(load_image(_UpperCamelCase   )     ,						_UpperCamelCase     ,						''   )   )   )

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

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

												lowerCAmelCase__    						=						dqa_pipeline(
												    [{'image': image, 'question': question}, {'image': image, 'question': question}]     ,						top_k=2   )
												self.assertEqual(
												    nested_simplify(_UpperCamelCase     ,						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     ,						)

												lowerCAmelCase__    						=						list(zip(*apply_tesseract(load_image(_UpperCamelCase   )     ,						_UpperCamelCase     ,						''   )   )   )

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

												lowerCAmelCase__    						=						INVOICE_URL
												lowerCAmelCase__    						=						'What is the invoice number?'
												lowerCAmelCase__    						=						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       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												pass





