

'''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 		__SCREAMING_SNAKE_CASE							:







											@staticmethod
											def  lowerCamelCase_	(       *UpperCAmelCase__     :		List[Any]		,					**UpperCAmelCase__     :		Optional[int]   ):
																	'''simple docstring'''
																	pass






							def 				_lowerCAmelCase			(   __magic_name__						:     str						)				->  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.
UpperCamelCase_    						=       (
    """https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png"""
)





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







				@require_pytesseract
				@require_vision
				def  lowerCamelCase_	(       self     :		str		,					UpperCAmelCase__     :		List[Any]		,					UpperCAmelCase__     :		List[Any]		,					UpperCAmelCase__     :		str   ):
										'''simple docstring'''
										lowercase :							List[Any]    		=pipeline(
										    '''document-question-answering'''		,					model=UpperCAmelCase__		,					tokenizer=UpperCAmelCase__		,					image_processor=UpperCAmelCase__   )

										lowercase :							str    		=INVOICE_URL
										lowercase :							Dict    		=list(zip(*apply_tesseract(load_image(UpperCAmelCase__   )		,					UpperCAmelCase__		,					''''''   )   )   )
										lowercase :							Optional[int]    		='''What is the placebo?'''
										lowercase :							Optional[int]    		=[
										    {
										        '''image''': load_image(UpperCAmelCase__   ),
										        '''question''': question,
										    },
										    {
										        '''image''': image,
										        '''question''': question,
										    },
										    {
										        '''image''': image,
										        '''question''': question,
										        '''word_boxes''': word_boxes,
										    },
										]
										return dqa_pipeline, examples







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

										lowercase :							str    		=[
										    {'''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},
										]
										lowercase :							Union[str, Any]    		=dqa_pipeline(image=UpperCAmelCase__		,					question=UpperCAmelCase__		,					top_k=2   )
										self.assertEqual(nested_simplify(UpperCAmelCase__		,					decimals=4   )		,					UpperCAmelCase__   )

										lowercase :							Union[str, Any]    		=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
										lowercase :							Optional[Any]    		='''./tests/fixtures/tests_samples/COCO/000000039769.png'''
										lowercase :							Any    		=dqa_pipeline(image=UpperCAmelCase__		,					question=UpperCAmelCase__		,					top_k=2   )
										self.assertEqual(UpperCAmelCase__		,					[]   )

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







				@slow
				@require_torch
				@require_detectrona
				@require_pytesseract
				def  lowerCamelCase_	(       self     :		Any   ):
										'''simple docstring'''
										lowercase :							List[Any]    		=pipeline(
										    '''document-question-answering'''		,					model='''tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'''		,					revision='''9977165'''		,					)
										lowercase :							Union[str, Any]    		=INVOICE_URL
										lowercase :							Any    		='''What is the invoice number?'''

										lowercase :							Dict    		=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},
										    ]		,					)

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

										lowercase :							Any    		=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  lowerCamelCase_	(       self     :		str   ):
										'''simple docstring'''
										lowercase :							Any    		=pipeline(
										    '''document-question-answering'''		,					model='''tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'''		,					revision='''9977165'''		,					max_seq_len=50		,					)
										lowercase :							Optional[int]    		=INVOICE_URL
										lowercase :							Dict    		='''What is the invoice number?'''

										lowercase :							Optional[Any]    		=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},
										    ]		,					)

										lowercase :							Optional[Any]    		=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},
										    ]		,					)

										lowercase :							Optional[Any]    		=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  lowerCamelCase_	(       self     :		str   ):
										'''simple docstring'''
										lowercase :							Optional[int]    		=AutoTokenizer.from_pretrained(
										    '''impira/layoutlm-document-qa'''		,					revision='''3dc6de3'''		,					add_prefix_space=UpperCAmelCase__   )
										lowercase :							Optional[Any]    		=pipeline(
										    '''document-question-answering'''		,					model='''impira/layoutlm-document-qa'''		,					tokenizer=UpperCAmelCase__		,					revision='''3dc6de3'''		,					)
										lowercase :							Dict    		=INVOICE_URL
										lowercase :							Union[str, Any]    		='''What is the invoice number?'''

										lowercase :							Union[str, Any]    		=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},
										    ]		,					)

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

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

										lowercase :							Dict    		=list(zip(*apply_tesseract(load_image(UpperCAmelCase__   )		,					UpperCAmelCase__		,					''''''   )   )   )

										# This model should also work if `image` is set to None
										lowercase :							int    		=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  lowerCamelCase_	(       self     :		Any   ):
										'''simple docstring'''
										lowercase :							List[str]    		=AutoTokenizer.from_pretrained(
										    '''impira/layoutlm-document-qa'''		,					revision='''3dc6de3'''		,					add_prefix_space=UpperCAmelCase__   )
										lowercase :							Any    		=pipeline(
										    '''document-question-answering'''		,					model='''impira/layoutlm-document-qa'''		,					tokenizer=UpperCAmelCase__		,					revision='''3dc6de3'''		,					max_seq_len=50		,					)
										lowercase :							Union[str, Any]    		=INVOICE_URL
										lowercase :							List[str]    		='''What is the invoice number?'''

										lowercase :							Dict    		=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},
										    ]		,					)

										lowercase :							Tuple    		=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		,					)

										lowercase :							List[Any]    		=list(zip(*apply_tesseract(load_image(UpperCAmelCase__   )		,					UpperCAmelCase__		,					''''''   )   )   )

										# This model should also work if `image` is set to None
										lowercase :							str    		=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  lowerCamelCase_	(       self     :		Any   ):
										'''simple docstring'''
										lowercase :							Tuple    		=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'''		,					)

										lowercase :							Tuple    		=INVOICE_URL
										lowercase :							str    		='''What is the invoice number?'''
										lowercase :							str    		=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  lowerCamelCase_	(       self     :		Any   ):
										'''simple docstring'''
										pass








'''simple docstring'''





import re

import jax.numpy as jnp
from flax.traverse_util import flatten_dict, unflatten_dict
from jax.random import PRNGKey

from ..utils import logging


UpperCamelCase_    						=       logging.get_logger(__name__)






def 				_lowerCAmelCase			(   __magic_name__						:     List[str]						)				->  Dict:
						lowercase :							List[str]    		=R'''\w+[.]\d+'''
						lowercase :							List[str]    		=re.findall(__magic_name__   , __magic_name__						)
						for pat in pats:
												lowercase :							Optional[int]    		=key.replace(__magic_name__   , '''_'''.join(pat.split('''.'''						)						)						)
						return key






def 				_lowerCAmelCase			(   __magic_name__						:     List[str]   , __magic_name__						:     Union[str, Any]   , __magic_name__						:     Dict						)				->  str:
						lowercase :							Dict    		=pt_tuple_key[:-1] + ('''scale''',)
						if (
						    any('''norm''' in str_ for str_ in pt_tuple_key						)
						    and (pt_tuple_key[-1] == "bias")
						    and (pt_tuple_key[:-1] + ("bias",) not in random_flax_state_dict)
						    and (pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict)
						):
												lowercase :							str    		=pt_tuple_key[:-1] + ('''scale''',)
												return renamed_pt_tuple_key, pt_tensor
						elif pt_tuple_key[-1] in ["weight", "gamma"] and pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict:
												lowercase :							str    		=pt_tuple_key[:-1] + ('''scale''',)
												return renamed_pt_tuple_key, pt_tensor

						# embedding
						if pt_tuple_key[-1] == "weight" and pt_tuple_key[:-1] + ("embedding",) in random_flax_state_dict:
												lowercase :							Dict    		=pt_tuple_key[:-1] + ('''embedding''',)
												return renamed_pt_tuple_key, pt_tensor

						# conv layer
						lowercase :							Tuple    		=pt_tuple_key[:-1] + ('''kernel''',)
						if pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4:
												lowercase :							Tuple    		=pt_tensor.transpose(2   , 3   , 1   , 0						)
												return renamed_pt_tuple_key, pt_tensor

						# linear layer
						lowercase :							str    		=pt_tuple_key[:-1] + ('''kernel''',)
						if pt_tuple_key[-1] == "weight":
												lowercase :							Optional[Any]    		=pt_tensor.T
												return renamed_pt_tuple_key, pt_tensor

						# old PyTorch layer norm weight
						lowercase :							Dict    		=pt_tuple_key[:-1] + ('''weight''',)
						if pt_tuple_key[-1] == "gamma":
												return renamed_pt_tuple_key, pt_tensor

						# old PyTorch layer norm bias
						lowercase :							Union[str, Any]    		=pt_tuple_key[:-1] + ('''bias''',)
						if pt_tuple_key[-1] == "beta":
												return renamed_pt_tuple_key, pt_tensor

						return pt_tuple_key, pt_tensor







def 				_lowerCAmelCase			(   __magic_name__						:     int   , __magic_name__						:     Optional[Any]   , __magic_name__						:     Union[str, Any]=42						)				->  List[str]:
						# Step 1: Convert pytorch tensor to numpy
						lowercase :							Optional[Any]    		={k: v.numpy() for k, v in pt_state_dict.items()}

						# Step 2: Since the model is stateless, get random Flax params
						lowercase :							str    		=flax_model.init_weights(PRNGKey(__magic_name__						)						)

						lowercase :							Dict    		=flatten_dict(__magic_name__						)
						lowercase :							Dict    		={}

						# Need to change some parameters name to match Flax names
						for pt_key, pt_tensor in pt_state_dict.items():
												lowercase :							Dict    		=rename_key(__magic_name__						)
												lowercase :							Optional[int]    		=tuple(renamed_pt_key.split('''.'''						)						)

												# Correctly rename weight parameters
												lowercase		,	lowercase :							Any    		=rename_key_and_reshape_tensor(__magic_name__   , __magic_name__   , __magic_name__						)

												if flax_key in random_flax_state_dict:
																		if flax_tensor.shape != random_flax_state_dict[flax_key].shape:
																								raise ValueError(
																								    f'''PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape '''
																								    f'''{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}.'''						)

        # also add unexpected weight so that warning is thrown
												lowercase :							Tuple    		=jnp.asarray(__magic_name__						)

						return unflatten_dict(__magic_name__						)





