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




					def 				SCREAMING_SNAKE_CASE     (      __UpperCamelCase :						List[str]       ) ->       Optional[int]:

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







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







	'''simple docstring'''
	_lowerCAmelCase							       =				MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING





	@require_pytesseract
	@require_vision
	def   __snake_case				(			self				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__		):
					A__         =							pipeline(
					    '''document-question-answering'''				,       model=UpperCamelCase__				,       tokenizer=UpperCamelCase__				,       image_processor=UpperCamelCase__		)

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





	def   __snake_case				(			self				,       UpperCamelCase__				,       UpperCamelCase__		):
					A__         =							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   __snake_case				(			self		):
					A__         =							pipeline('''document-question-answering'''				,       model='''hf-internal-testing/tiny-random-layoutlmv2'''		)
					A__         =							INVOICE_URL
					A__         =							'''How many cats are there?'''

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

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

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





	@slow
	@require_torch
	@require_detectrona
	@require_pytesseract
	def   __snake_case				(			self		):
					A__         =							pipeline(
					    '''document-question-answering'''				,       model='''tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'''				,       revision='''9977165'''				,       )
					A__         =							INVOICE_URL
					A__         =							'''What is the invoice number?'''

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

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

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

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

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

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

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

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

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

					A__         =							list(zip(*apply_tesseract(load_image(UpperCamelCase__		)				,       UpperCamelCase__				,       ''''''		)		)		)

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

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

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

					A__         =							list(zip(*apply_tesseract(load_image(UpperCamelCase__		)				,       UpperCamelCase__				,       ''''''		)		)		)

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

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

from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tensorflow_text_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


_SCREAMING_SNAKE_CASE    :      int          =			{
    'configuration_bert': ['BERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'BertConfig', 'BertOnnxConfig'],
    'tokenization_bert': ['BasicTokenizer', 'BertTokenizer', 'WordpieceTokenizer'],
}

try:
					if not is_tokenizers_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					_SCREAMING_SNAKE_CASE    :      Optional[Any]          =			['BertTokenizerFast']

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					_SCREAMING_SNAKE_CASE    :      Union[str, Any]          =			[
					    'BERT_PRETRAINED_MODEL_ARCHIVE_LIST',
					    'BertForMaskedLM',
					    'BertForMultipleChoice',
					    'BertForNextSentencePrediction',
					    'BertForPreTraining',
					    'BertForQuestionAnswering',
					    'BertForSequenceClassification',
					    'BertForTokenClassification',
					    'BertLayer',
					    'BertLMHeadModel',
					    'BertModel',
					    'BertPreTrainedModel',
					    'load_tf_weights_in_bert',
					]

try:
					if not is_tf_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					_SCREAMING_SNAKE_CASE    :      Tuple          =			[
					    'TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST',
					    'TFBertEmbeddings',
					    'TFBertForMaskedLM',
					    'TFBertForMultipleChoice',
					    'TFBertForNextSentencePrediction',
					    'TFBertForPreTraining',
					    'TFBertForQuestionAnswering',
					    'TFBertForSequenceClassification',
					    'TFBertForTokenClassification',
					    'TFBertLMHeadModel',
					    'TFBertMainLayer',
					    'TFBertModel',
					    'TFBertPreTrainedModel',
					]
try:
					if not is_tensorflow_text_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					_SCREAMING_SNAKE_CASE    :      Dict          =			['TFBertTokenizer']

try:
					if not is_flax_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					_SCREAMING_SNAKE_CASE    :      Optional[int]          =			[
					    'FlaxBertForCausalLM',
					    'FlaxBertForMaskedLM',
					    'FlaxBertForMultipleChoice',
					    'FlaxBertForNextSentencePrediction',
					    'FlaxBertForPreTraining',
					    'FlaxBertForQuestionAnswering',
					    'FlaxBertForSequenceClassification',
					    'FlaxBertForTokenClassification',
					    'FlaxBertModel',
					    'FlaxBertPreTrainedModel',
					]

if TYPE_CHECKING:
					from .configuration_bert import BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, BertConfig, BertOnnxConfig
					from .tokenization_bert import BasicTokenizer, BertTokenizer, WordpieceTokenizer

					try:
										if not is_tokenizers_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .tokenization_bert_fast import BertTokenizerFast

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_bert import (
										    BERT_PRETRAINED_MODEL_ARCHIVE_LIST,
										    BertForMaskedLM,
										    BertForMultipleChoice,
										    BertForNextSentencePrediction,
										    BertForPreTraining,
										    BertForQuestionAnswering,
										    BertForSequenceClassification,
										    BertForTokenClassification,
										    BertLayer,
										    BertLMHeadModel,
										    BertModel,
										    BertPreTrainedModel,
										    load_tf_weights_in_bert,
										)

					try:
										if not is_tf_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_tf_bert import (
										    TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST,
										    TFBertEmbeddings,
										    TFBertForMaskedLM,
										    TFBertForMultipleChoice,
										    TFBertForNextSentencePrediction,
										    TFBertForPreTraining,
										    TFBertForQuestionAnswering,
										    TFBertForSequenceClassification,
										    TFBertForTokenClassification,
										    TFBertLMHeadModel,
										    TFBertMainLayer,
										    TFBertModel,
										    TFBertPreTrainedModel,
										)

					try:
										if not is_tensorflow_text_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .tokenization_bert_tf import TFBertTokenizer

					try:
										if not is_flax_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_flax_bert import (
										    FlaxBertForCausalLM,
										    FlaxBertForMaskedLM,
										    FlaxBertForMultipleChoice,
										    FlaxBertForNextSentencePrediction,
										    FlaxBertForPreTraining,
										    FlaxBertForQuestionAnswering,
										    FlaxBertForSequenceClassification,
										    FlaxBertForTokenClassification,
										    FlaxBertModel,
										    FlaxBertPreTrainedModel,
										)

else:
					import sys

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