



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







																'''simple docstring'''

																pass







			def      __lowercase       (			a__       )    ->			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.
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__      :			Optional[Any]      =  MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING






						@require_pytesseract
						@require_vision
						def 	_A     (   self				,							_A				,							_A				,							_A		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= pipeline(
													    'document-question-answering'				,							model=_A				,							tokenizer=_A				,							image_processor=_A		)

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






						def 	_A     (   self				,							_A				,							_A		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= 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 	_A     (   self		):







													'''simple docstring'''

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

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

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

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






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







													'''simple docstring'''

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

													__SCREAMING_SNAKE_CASE									= dqa_pipeline(image=_A				,							question=_A				,							top_k=2		)
													self.assertEqual(
													    nested_simplify(_A				,							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},
													    ]				,							)

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

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







													'''simple docstring'''

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

													__SCREAMING_SNAKE_CASE									= dqa_pipeline(image=_A				,							question=_A				,							top_k=2		)
													self.assertEqual(
													    nested_simplify(_A				,							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},
													    ]				,							)

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

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







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= AutoTokenizer.from_pretrained(
													    'impira/layoutlm-document-qa'				,							revision='3dc6de3'				,							add_prefix_space=_A		)
													__SCREAMING_SNAKE_CASE									= pipeline(
													    'document-question-answering'				,							model='impira/layoutlm-document-qa'				,							tokenizer=_A				,							revision='3dc6de3'				,							)
													__SCREAMING_SNAKE_CASE									= INVOICE_URL
													__SCREAMING_SNAKE_CASE									= 'What is the invoice number?'

													__SCREAMING_SNAKE_CASE									= dqa_pipeline(image=_A				,							question=_A				,							top_k=2		)
													self.assertEqual(
													    nested_simplify(_A				,							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},
													    ]				,							)

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

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

													__SCREAMING_SNAKE_CASE									= list(zip(*apply_tesseract(load_image(_A		)				,							_A				,							''		)		)		)

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







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= AutoTokenizer.from_pretrained(
													    'impira/layoutlm-document-qa'				,							revision='3dc6de3'				,							add_prefix_space=_A		)
													__SCREAMING_SNAKE_CASE									= pipeline(
													    'document-question-answering'				,							model='impira/layoutlm-document-qa'				,							tokenizer=_A				,							revision='3dc6de3'				,							max_seq_len=50				,							)
													__SCREAMING_SNAKE_CASE									= INVOICE_URL
													__SCREAMING_SNAKE_CASE									= 'What is the invoice number?'

													__SCREAMING_SNAKE_CASE									= dqa_pipeline(image=_A				,							question=_A				,							top_k=2		)
													self.assertEqual(
													    nested_simplify(_A				,							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},
													    ]				,							)

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

													__SCREAMING_SNAKE_CASE									= list(zip(*apply_tesseract(load_image(_A		)				,							_A				,							''		)		)		)

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







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= 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'				,							)

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







													'''simple docstring'''

													pass







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


lowerCAmelCase__		:					List[str]							={
    '''configuration_timesformer''': ['''TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TimesformerConfig'''],
}

try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			lowerCAmelCase__		:					List[str]							=[
			    '''TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''',
			    '''TimesformerModel''',
			    '''TimesformerForVideoClassification''',
			    '''TimesformerPreTrainedModel''',
			]

if TYPE_CHECKING:
			from .configuration_timesformer import TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimesformerConfig

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_timesformer import (
						    TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
						    TimesformerForVideoClassification,
						    TimesformerModel,
						    TimesformerPreTrainedModel,
						)

else:
			import sys

			lowerCAmelCase__		:					Union[str, Any]							=_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)






