






import argparse

import torch
from datasets import load_dataset
from donut import DonutModel

from transformers import (
    DonutImageProcessor,
    DonutProcessor,
    DonutSwinConfig,
    DonutSwinModel,
    MBartConfig,
    MBartForCausalLM,
    VisionEncoderDecoderModel,
    XLMRobertaTokenizerFast,
)







def 	_UpperCamelCase   (    UpperCamelCase_       :					List[Any]		)  ->   Any:

						"""simple docstring"""



						lowerCAmelCase__    						=						model.config

						lowerCAmelCase__    						=						DonutSwinConfig(
						    image_size=original_config.input_size    ,       patch_size=4    ,       depths=original_config.encoder_layer    ,       num_heads=[4, 8, 16, 32]    ,       window_size=original_config.window_size    ,       embed_dim=128    ,       )
						lowerCAmelCase__    						=						MBartConfig(
						    is_decoder=UpperCamelCase_    ,       is_encoder_decoder=UpperCamelCase_    ,       add_cross_attention=UpperCamelCase_    ,       decoder_layers=original_config.decoder_layer    ,       max_position_embeddings=original_config.max_position_embeddings    ,       vocab_size=len(
						        model.decoder.tokenizer		)    ,       scale_embedding=UpperCamelCase_    ,       add_final_layer_norm=UpperCamelCase_    ,       )

						return encoder_config, decoder_config







def 	_UpperCamelCase   (    UpperCamelCase_       :					Any		)  ->   List[Any]:

						"""simple docstring"""



						if "encoder.model" in name:
												lowerCAmelCase__    						=						name.replace('encoder.model'    ,       'encoder'		)
						if "decoder.model" in name:
												lowerCAmelCase__    						=						name.replace('decoder.model'    ,       'decoder'		)
						if "patch_embed.proj" in name:
												lowerCAmelCase__    						=						name.replace('patch_embed.proj'    ,       'embeddings.patch_embeddings.projection'		)
						if "patch_embed.norm" in name:
												lowerCAmelCase__    						=						name.replace('patch_embed.norm'    ,       'embeddings.norm'		)
						if name.startswith('encoder'		):
												if "layers" in name:
																		lowerCAmelCase__    						=						'encoder.' + name
												if "attn.proj" in name:
																		lowerCAmelCase__    						=						name.replace('attn.proj'    ,       'attention.output.dense'		)
												if "attn" in name and "mask" not in name:
																		lowerCAmelCase__    						=						name.replace('attn'    ,       'attention.self'		)
												if "norm1" in name:
																		lowerCAmelCase__    						=						name.replace('norm1'    ,       'layernorm_before'		)
												if "norm2" in name:
																		lowerCAmelCase__    						=						name.replace('norm2'    ,       'layernorm_after'		)
												if "mlp.fc1" in name:
																		lowerCAmelCase__    						=						name.replace('mlp.fc1'    ,       'intermediate.dense'		)
												if "mlp.fc2" in name:
																		lowerCAmelCase__    						=						name.replace('mlp.fc2'    ,       'output.dense'		)

												if name == "encoder.norm.weight":
																		lowerCAmelCase__    						=						'encoder.layernorm.weight'
												if name == "encoder.norm.bias":
																		lowerCAmelCase__    						=						'encoder.layernorm.bias'

						return name







def 	_UpperCamelCase   (    UpperCamelCase_       :					Tuple    ,       UpperCamelCase_       :					Any		)  ->   int:

						"""simple docstring"""



						for key in orig_state_dict.copy().keys():
												lowerCAmelCase__    						=						orig_state_dict.pop(UpperCamelCase_		)

												if "qkv" in key:
																		lowerCAmelCase__    						=						key.split('.'		)
																		lowerCAmelCase__    						=						int(key_split[3]		)
																		lowerCAmelCase__    						=						int(key_split[5]		)
																		lowerCAmelCase__    						=						model.encoder.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size

																		if "weight" in key:
																								lowerCAmelCase__    						=						val[:dim, :]
																								lowerCAmelCase__    						=						val[dim : dim * 2, :]
																								lowerCAmelCase__    						=						val[-dim:, :]
																		else:
																								lowerCAmelCase__    						=						val[:dim]
																								lowerCAmelCase__    						=						val[dim : dim * 2]
																								lowerCAmelCase__    						=						val[-dim:]
												elif "attn_mask" in key or key in ["encoder.model.norm.weight", "encoder.model.norm.bias"]:
																		# HuggingFace implementation doesn't use attn_mask buffer
																		# and model doesn't use final LayerNorms for the encoder
																		pass
												else:
																		lowerCAmelCase__    						=						val

						return orig_state_dict







def 	_UpperCamelCase   (    UpperCamelCase_       :					Optional[Any]    ,       UpperCamelCase_       :					Tuple=None    ,       UpperCamelCase_       :					Tuple=False		)  ->   int:

						"""simple docstring"""



						lowerCAmelCase__    						=						DonutModel.from_pretrained(UpperCamelCase_		).eval()

						# load HuggingFace model
						lowerCAmelCase__						,       lowerCAmelCase__    						=						get_configs(UpperCamelCase_		)
						lowerCAmelCase__    						=						DonutSwinModel(UpperCamelCase_		)
						lowerCAmelCase__    						=						MBartForCausalLM(UpperCamelCase_		)
						lowerCAmelCase__    						=						VisionEncoderDecoderModel(encoder=UpperCamelCase_    ,       decoder=UpperCamelCase_		)
						model.eval()

						lowerCAmelCase__    						=						original_model.state_dict()
						lowerCAmelCase__    						=						convert_state_dict(UpperCamelCase_    ,       UpperCamelCase_		)
						model.load_state_dict(UpperCamelCase_		)

						# verify results on scanned document
						lowerCAmelCase__    						=						load_dataset('hf-internal-testing/example-documents'		)
						lowerCAmelCase__    						=						dataset['test'][0]['image'].convert('RGB'		)

						lowerCAmelCase__    						=						XLMRobertaTokenizerFast.from_pretrained(UpperCamelCase_    ,       from_slow=UpperCamelCase_		)
						lowerCAmelCase__    						=						DonutImageProcessor(
						    do_align_long_axis=original_model.config.align_long_axis    ,       size=original_model.config.input_size[::-1]		)
						lowerCAmelCase__    						=						DonutProcessor(UpperCamelCase_    ,       UpperCamelCase_		)
						lowerCAmelCase__    						=						processor(UpperCamelCase_    ,       return_tensors='pt'		).pixel_values

						if model_name == "naver-clova-ix/donut-base-finetuned-docvqa":
												lowerCAmelCase__    						=						'<s_docvqa><s_question>{user_input}</s_question><s_answer>'
												lowerCAmelCase__    						=						'When is the coffee break?'
												lowerCAmelCase__    						=						task_prompt.replace('{user_input}'    ,       UpperCamelCase_		)
						elif model_name == "naver-clova-ix/donut-base-finetuned-rvlcdip":
												lowerCAmelCase__    						=						'<s_rvlcdip>'
						elif model_name in [
						    "naver-clova-ix/donut-base-finetuned-cord-v1",
						    "naver-clova-ix/donut-base-finetuned-cord-v1-2560",
						]:
												lowerCAmelCase__    						=						'<s_cord>'
						elif model_name == "naver-clova-ix/donut-base-finetuned-cord-v2":
												lowerCAmelCase__    						=						's_cord-v2>'
						elif model_name == "naver-clova-ix/donut-base-finetuned-zhtrainticket":
												lowerCAmelCase__    						=						'<s_zhtrainticket>'
						elif model_name in ["naver-clova-ix/donut-proto", "naver-clova-ix/donut-base"]:
												# use a random prompt
												lowerCAmelCase__    						=						'hello world'
						else:
												raise ValueError('Model name not supported'		)
						lowerCAmelCase__    						=						original_model.decoder.tokenizer(UpperCamelCase_    ,       add_special_tokens=UpperCamelCase_    ,       return_tensors='pt'		)[
						    'input_ids'
						]

						lowerCAmelCase__    						=						original_model.encoder.model.patch_embed(UpperCamelCase_		)
						lowerCAmelCase__						,       lowerCAmelCase__    						=						model.encoder.embeddings(UpperCamelCase_		)
						assert torch.allclose(UpperCamelCase_    ,       UpperCamelCase_    ,       atol=1e-3		)

						# verify encoder hidden states
						lowerCAmelCase__    						=						original_model.encoder(UpperCamelCase_		)
						lowerCAmelCase__    						=						model.encoder(UpperCamelCase_		).last_hidden_state
						assert torch.allclose(UpperCamelCase_    ,       UpperCamelCase_    ,       atol=1e-2		)

						# verify decoder hidden states
						lowerCAmelCase__    						=						original_model(UpperCamelCase_    ,       UpperCamelCase_    ,       UpperCamelCase_		).logits
						lowerCAmelCase__    						=						model(UpperCamelCase_    ,       decoder_input_ids=UpperCamelCase_		).logits
						assert torch.allclose(UpperCamelCase_    ,       UpperCamelCase_    ,       atol=1e-3		)
						print('Looks ok!'		)

						if pytorch_dump_folder_path is not None:
												print(F"Saving model and processor to {pytorch_dump_folder_path}"		)
												model.save_pretrained(UpperCamelCase_		)
												processor.save_pretrained(UpperCamelCase_		)

						if push_to_hub:
												model.push_to_hub('nielsr/' + model_name.split('/'		)[-1]    ,       commit_message='Update model'		)
												processor.push_to_hub('nielsr/' + model_name.split('/'		)[-1]    ,       commit_message='Update model'		)


if __name__ == "__main__":
					__snake_case    :    str         =     argparse.ArgumentParser()
					# Required parameters
					parser.add_argument(
					    """--model_name""",
					    default="""naver-clova-ix/donut-base-finetuned-docvqa""",
					    required=False,
					    type=str,
					    help="""Name of the original model you'd like to convert.""",
					)
					parser.add_argument(
					    """--pytorch_dump_folder_path""",
					    default=None,
					    required=False,
					    type=str,
					    help="""Path to the output PyTorch model directory.""",
					)
					parser.add_argument(
					    """--push_to_hub""",
					    action="""store_true""",
					    help="""Whether or not to push the converted model and processor to the 🤗 hub.""",
					)

					__snake_case    :    str         =     parser.parse_args()
					convert_donut_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)













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





