


import argparse
import json
import os

import fairseq
import torch
from torch import nn

from transformers import (
    SpeechaTextaConfig,
    SpeechaTextaForCausalLM,
    SpeechaTextaTokenizer,
    SpeechEncoderDecoderConfig,
    SpeechEncoderDecoderModel,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
    WavaVecaModel,
    logging,
)


logging.set_verbosity_info()
__lowerCAmelCase	: List[str]  				=logging.get_logger(__name__)

__lowerCAmelCase	: Dict  				={
    'post_extract_proj': 'feature_projection.projection',
    'encoder.pos_conv.0': 'encoder.pos_conv_embed.conv',
    'self_attn.k_proj': 'encoder.layers.*.attention.k_proj',
    'self_attn.v_proj': 'encoder.layers.*.attention.v_proj',
    'self_attn.q_proj': 'encoder.layers.*.attention.q_proj',
    'self_attn.out_proj': 'encoder.layers.*.attention.out_proj',
    'self_attn_layer_norm': 'encoder.layers.*.layer_norm',
    'fc1': 'encoder.layers.*.feed_forward.intermediate_dense',
    'fc2': 'encoder.layers.*.feed_forward.output_dense',
    'final_layer_norm': 'encoder.layers.*.final_layer_norm',
    'encoder.layer_norm': 'encoder.layer_norm',
    'w2v_model.layer_norm': 'feature_projection.layer_norm',
    'quantizer.weight_proj': 'quantizer.weight_proj',
    'quantizer.vars': 'quantizer.codevectors',
    'project_q': 'project_q',
    'final_proj': 'project_hid',
    'w2v_encoder.proj': 'lm_head',
    'mask_emb': 'masked_spec_embed',
}
__lowerCAmelCase	: Union[str, Any]  				=[
    'lm_head',
    'quantizer.weight_proj',
    'quantizer.codevectors',
    'project_q',
    'project_hid',
]


def        _UpperCamelCase							(      lowercase__    ,       lowercase__    ,       lowercase__    ,       lowercase__    ,       lowercase__      ):
						for attribute in key.split('''.'''      ):
												__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		getattr(lowercase__    ,       lowercase__      )

						if weight_type is not None:
												__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		getattr(lowercase__    ,       lowercase__      ).shape
						else:
												__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		hf_pointer.shape

						assert hf_shape == value.shape, (
						    F'''Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be'''
						    F''' {value.shape} for {full_name}'''
						)

						if weight_type == "weight":
												__SCREAMING_SNAKE_CASE						:     List[str]					=		value
						elif weight_type == "weight_g":
												__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		value
						elif weight_type == "weight_v":
												__SCREAMING_SNAKE_CASE						:     Any					=		value
						elif weight_type == "bias":
												__SCREAMING_SNAKE_CASE						:     Dict					=		value
						else:
												__SCREAMING_SNAKE_CASE						:     Optional[int]					=		value

						logger.info(F'''{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}.'''      )


def        _UpperCamelCase							(      lowercase__    ,       lowercase__      ):
						__SCREAMING_SNAKE_CASE						:     Tuple					=		[]
						__SCREAMING_SNAKE_CASE						:     Any					=		fairseq_model.state_dict()

						__SCREAMING_SNAKE_CASE						:     Tuple					=		hf_model.feature_extractor

						# if encoder has different dim to decoder -> use proj_weight
						__SCREAMING_SNAKE_CASE						:     List[str]					=		None

						for name, value in fairseq_dict.items():
												__SCREAMING_SNAKE_CASE						:     str					=		False
												if "conv_layers" in name:
																		load_conv_layer(
																		    lowercase__    ,       lowercase__    ,       lowercase__    ,       lowercase__    ,       hf_model.config.feat_extract_norm == '''group'''    ,       )
																		__SCREAMING_SNAKE_CASE						:     Dict					=		True
												elif name.split('''.'''      )[0] == "proj":
																		__SCREAMING_SNAKE_CASE						:     List[Any]					=		fairseq_model.proj
																		__SCREAMING_SNAKE_CASE						:     List[Any]					=		True
												else:
																		for key, mapped_key in MAPPING.items():
																								if key in name or key.split('''w2v_model.'''      )[-1] == name.split('''.'''      )[0]:
																														__SCREAMING_SNAKE_CASE						:     Dict					=		True
																														if "*" in mapped_key:
																																				__SCREAMING_SNAKE_CASE						:     Optional[int]					=		name.split(lowercase__      )[0].split('''.'''      )[-2]
																																				__SCREAMING_SNAKE_CASE						:     List[str]					=		mapped_key.replace('''*'''    ,       lowercase__      )
																														if "weight_g" in name:
																																				__SCREAMING_SNAKE_CASE						:     int					=		'''weight_g'''
																														elif "weight_v" in name:
																																				__SCREAMING_SNAKE_CASE						:     str					=		'''weight_v'''
																														elif "bias" in name:
																																				__SCREAMING_SNAKE_CASE						:     Tuple					=		'''bias'''
																														elif "weight" in name:
																																				__SCREAMING_SNAKE_CASE						:     Tuple					=		'''weight'''
																														else:
																																				__SCREAMING_SNAKE_CASE						:     int					=		None
																														set_recursively(lowercase__    ,       lowercase__    ,       lowercase__    ,       lowercase__    ,       lowercase__      )
																								continue
												if not is_used:
																		unused_weights.append(lowercase__      )

						logger.warning(F'''Unused weights: {unused_weights}'''      )

						return proj_weight


def        _UpperCamelCase							(      lowercase__    ,       lowercase__    ,       lowercase__    ,       lowercase__    ,       lowercase__      ):
						__SCREAMING_SNAKE_CASE						:     List[Any]					=		full_name.split('''conv_layers.'''      )[-1]
						__SCREAMING_SNAKE_CASE						:     Optional[int]					=		name.split('''.'''      )
						__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		int(items[0]      )
						__SCREAMING_SNAKE_CASE						:     int					=		int(items[1]      )

						if type_id == 0:
												if "bias" in name:
																		assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, (
																		    F'''{full_name} has size {value.shape}, but'''
																		    F''' {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.'''
																		)
																		__SCREAMING_SNAKE_CASE						:     Dict					=		value
																		logger.info(F'''Feat extract conv layer {layer_id} was initialized from {full_name}.'''      )
												elif "weight" in name:
																		assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, (
																		    F'''{full_name} has size {value.shape}, but'''
																		    F''' {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.'''
																		)
																		__SCREAMING_SNAKE_CASE						:     str					=		value
																		logger.info(F'''Feat extract conv layer {layer_id} was initialized from {full_name}.'''      )
						elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm):
												if "bias" in name:
																		assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, (
																		    F'''{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was'''
																		    " found."
																		)
																		__SCREAMING_SNAKE_CASE						:     List[str]					=		value
																		logger.info(F'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.'''      )
												elif "weight" in name:
																		assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, (
																		    F'''{full_name} has size {value.shape}, but'''
																		    F''' {feature_extractor[layer_id].layer_norm.weight.data.shape} was found.'''
																		)
																		__SCREAMING_SNAKE_CASE						:     List[Any]					=		value
																		logger.info(F'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.'''      )
						else:
												unused_weights.append(lowercase__      )


def        _UpperCamelCase							(      lowercase__      ):
						__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     str					=		emb.weight.shape
						__SCREAMING_SNAKE_CASE						:     List[str]					=		nn.Linear(lowercase__    ,       lowercase__    ,       bias=lowercase__      )
						__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		emb.weight.data
						return lin_layer


def        _UpperCamelCase							(      lowercase__      ):
						with open(lowercase__    ,       '''r'''    ,       encoding='''utf-8'''      ) as f:
												__SCREAMING_SNAKE_CASE						:     Any					=		f.readlines()
												__SCREAMING_SNAKE_CASE						:     Dict					=		[line.split(''' '''      )[0] for line in lines]

						__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		len(lowercase__      )

						__SCREAMING_SNAKE_CASE						:     int					=		{
						    '''<s>''': 0,
						    '''<pad>''': 1,
						    '''</s>''': 2,
						    '''<unk>''': 3,
						}

						vocab_dict.update(dict(zip(lowercase__    ,       range(4    ,       num_words + 4      )      )      )      )
						return vocab_dict


@torch.no_grad()
def        _UpperCamelCase							(      lowercase__    ,       lowercase__    ,       lowercase__    ,       lowercase__    ,       lowercase__    ,       lowercase__    ,       lowercase__    ,       ):
						__SCREAMING_SNAKE_CASE						:     int					=		WavaVecaConfig.from_pretrained(lowercase__      )
						__SCREAMING_SNAKE_CASE						:     str					=		SpeechaTextaConfig.from_pretrained(
						    lowercase__    ,       vocab_size=lowercase__    ,       decoder_layers=lowercase__    ,       do_stable_layer_norm=lowercase__      )

						__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		WavaVecaFeatureExtractor(
						    feature_size=1    ,       sampling_rate=16000    ,       padding_value=0    ,       do_normalize=lowercase__    ,       return_attention_mask=lowercase__    ,       )

						__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     Tuple					=		fairseq.checkpoint_utils.load_model_ensemble_and_task(
						    [checkpoint_path]    ,       arg_overrides={'''data''': '''/'''.join(dict_path.split('''/'''      )[:-1]      )}      )
						__SCREAMING_SNAKE_CASE						:     int					=		model[0].eval()

						# set weights for wav2vec2 encoder
						__SCREAMING_SNAKE_CASE						:     List[str]					=		WavaVecaModel(lowercase__      )
						__SCREAMING_SNAKE_CASE						:     Tuple					=		recursively_load_weights_wavaveca(model.encoder    ,       lowercase__      )

						__SCREAMING_SNAKE_CASE						:     int					=		SpeechaTextaForCausalLM(lowercase__      )
						__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     Optional[int]					=		hf_decoder.model.decoder.load_state_dict(model.decoder.state_dict()    ,       strict=lowercase__      )

						# set output linear layer
						unexpected_keys.remove('''embed_out'''      )
						__SCREAMING_SNAKE_CASE						:     Dict					=		nn.Parameter(model.decoder.embed_out.detach()      )

						# layer norm is init to identity matrix so leaving it is fine
						logger.warning(F'''The following keys are missing when loading the decoder weights: {missing_keys}'''      )
						logger.warning(F'''The following keys are unexpected when loading the decoder weights: {unexpected_keys}'''      )

						__SCREAMING_SNAKE_CASE						:     Any					=		SpeechEncoderDecoderModel(encoder=lowercase__    ,       decoder=lowercase__      )
						__SCREAMING_SNAKE_CASE						:     List[str]					=		False

						# add projection layer
						__SCREAMING_SNAKE_CASE						:     Optional[int]					=		nn.Parameter(projection_layer.weight      )
						__SCREAMING_SNAKE_CASE						:     Any					=		nn.Parameter(projection_layer.bias      )

						__SCREAMING_SNAKE_CASE						:     Tuple					=		create_vocab_dict(lowercase__      )

						with open(os.path.join(lowercase__    ,       '''vocab.json'''      )    ,       '''w'''      ) as fp:
												json.dump(lowercase__    ,       lowercase__      )

						__SCREAMING_SNAKE_CASE						:     List[str]					=		SpeechaTextaTokenizer(os.path.join(lowercase__    ,       '''vocab.json'''      )      )
						tokenizer.save_pretrained(lowercase__      )

						__SCREAMING_SNAKE_CASE						:     Dict					=		hf_wavavec.config.to_dict()
						__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		tokenizer.pad_token_id
						__SCREAMING_SNAKE_CASE						:     Optional[int]					=		tokenizer.bos_token_id
						__SCREAMING_SNAKE_CASE						:     Any					=		tokenizer.eos_token_id
						__SCREAMING_SNAKE_CASE						:     Dict					=		'''speech_to_text_2'''
						__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		'''wav2vec2'''

						__SCREAMING_SNAKE_CASE						:     Any					=		SpeechEncoderDecoderConfig.from_dict(lowercase__      )

						hf_wavavec.save_pretrained(lowercase__      )
						feature_extractor.save_pretrained(lowercase__      )


if __name__ == "__main__":
						__lowerCAmelCase	: Dict  				=argparse.ArgumentParser()
						parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.')
						parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to fairseq checkpoint')
						parser.add_argument('--dict_path', default=None, type=str, help='Path to dict of fine-tuned model')
						parser.add_argument(
						    '--encoder_config_path',
						    default='facebook/wav2vec2-large-lv60',
						    type=str,
						    help='Path to hf encoder wav2vec2 checkpoint config',
						)
						parser.add_argument(
						    '--decoder_config_path',
						    default='facebook/s2t-small-mustc-en-fr-st',
						    type=str,
						    help='Path to hf decoder s2t checkpoint config',
						)
						parser.add_argument('--vocab_size', default=1_0_2_2_4, type=int, help='Vocab size of decoder')
						parser.add_argument('--num_decoder_layers', default=7, type=int, help='Number of decoder layers')

						__lowerCAmelCase	: Any  				=parser.parse_args()
						convert_wavaveca_checkpoint(
						    args.checkpoint_path,
						    args.pytorch_dump_folder_path,
						    args.dict_path,
						    encoder_config_path=args.encoder_config_path,
						    decoder_config_path=args.decoder_config_path,
						    vocab_size=args.vocab_size,
						    num_decoder_layers=args.num_decoder_layers,
						)




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        _lowercase      :






										'''simple docstring'''



										@staticmethod
										def 		__magic_name__(       *lowerCAmelCase__			:Union[str, Any]			,			**lowerCAmelCase__			:str	)		->			Union[str, Any]:
																pass




						def        _UpperCamelCase							(      lowercase__      ):
												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	: str  				=(
    'https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png'
)
@is_pipeline_test
@require_torch
@require_vision
class        _lowercase      (			unittest.TestCase       ):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE__				:			Tuple						=		MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING


				@require_pytesseract
				@require_vision
				def 		__magic_name__(       self			:Any			,			lowerCAmelCase__			:List[str]			,			lowerCAmelCase__			:Optional[Any]			,			lowerCAmelCase__			:Any	)		->			Any:
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		pipeline(
										    '''document-question-answering'''			,			model=lowerCAmelCase__			,			tokenizer=lowerCAmelCase__			,			image_processor=lowerCAmelCase__	)

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


				def 		__magic_name__(       self			:Optional[Any]			,			lowerCAmelCase__			:Tuple			,			lowerCAmelCase__			:Tuple	)		->			str:
										__SCREAMING_SNAKE_CASE						:     Any					=		dqa_pipeline(lowerCAmelCase__			,			top_k=2	)
										self.assertEqual(
										    lowerCAmelCase__			,			[
										        [
										            {'''score''': ANY(lowerCAmelCase__	), '''answer''': ANY(lowerCAmelCase__	), '''start''': ANY(lowerCAmelCase__	), '''end''': ANY(lowerCAmelCase__	)},
										            {'''score''': ANY(lowerCAmelCase__	), '''answer''': ANY(lowerCAmelCase__	), '''start''': ANY(lowerCAmelCase__	), '''end''': ANY(lowerCAmelCase__	)},
										        ]
										    ]
										    * 3			,			)


				@require_torch
				@require_detectrona
				@require_pytesseract
				def 		__magic_name__(       self			:Dict	)		->			List[str]:
										__SCREAMING_SNAKE_CASE						:     Tuple					=		pipeline('''document-question-answering'''			,			model='''hf-internal-testing/tiny-random-layoutlmv2'''	)
										__SCREAMING_SNAKE_CASE						:     Dict					=		INVOICE_URL
										__SCREAMING_SNAKE_CASE						:     int					=		'''How many cats are there?'''

										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		[
										    {'''score''': 0.0001, '''answer''': '''oy 2312/2019''', '''start''': 38, '''end''': 39},
										    {'''score''': 0.0001, '''answer''': '''oy 2312/2019 DUE''', '''start''': 38, '''end''': 40},
										]
										__SCREAMING_SNAKE_CASE						:     Tuple					=		dqa_pipeline(image=lowerCAmelCase__			,			question=lowerCAmelCase__			,			top_k=2	)
										self.assertEqual(nested_simplify(lowerCAmelCase__			,			decimals=4	)			,			lowerCAmelCase__	)

										__SCREAMING_SNAKE_CASE						:     Dict					=		dqa_pipeline({'''image''': image, '''question''': question}			,			top_k=2	)
										self.assertEqual(nested_simplify(lowerCAmelCase__			,			decimals=4	)			,			lowerCAmelCase__	)

										# This image does not detect ANY text in it, meaning layoutlmv2 should fail.
										# Empty answer probably
										__SCREAMING_SNAKE_CASE						:     Any					=		'''./tests/fixtures/tests_samples/COCO/000000039769.png'''
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		dqa_pipeline(image=lowerCAmelCase__			,			question=lowerCAmelCase__			,			top_k=2	)
										self.assertEqual(lowerCAmelCase__			,			[]	)

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


				@slow
				@require_torch
				@require_detectrona
				@require_pytesseract
				def 		__magic_name__(       self			:int	)		->			Optional[Any]:
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		pipeline(
										    '''document-question-answering'''			,			model='''tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'''			,			revision='''9977165'''			,			)
										__SCREAMING_SNAKE_CASE						:     Dict					=		INVOICE_URL
										__SCREAMING_SNAKE_CASE						:     Any					=		'''What is the invoice number?'''

										__SCREAMING_SNAKE_CASE						:     Any					=		dqa_pipeline(image=lowerCAmelCase__			,			question=lowerCAmelCase__			,			top_k=2	)
										self.assertEqual(
										    nested_simplify(lowerCAmelCase__			,			decimals=4	)			,			[
										        {'''score''': 0.9944, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										        {'''score''': 0.0009, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										    ]			,			)

										__SCREAMING_SNAKE_CASE						:     Tuple					=		dqa_pipeline({'''image''': image, '''question''': question}			,			top_k=2	)
										self.assertEqual(
										    nested_simplify(lowerCAmelCase__			,			decimals=4	)			,			[
										        {'''score''': 0.9944, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										        {'''score''': 0.0009, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										    ]			,			)

										__SCREAMING_SNAKE_CASE						:     List[Any]					=		dqa_pipeline(
										    [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]			,			top_k=2	)
										self.assertEqual(
										    nested_simplify(lowerCAmelCase__			,			decimals=4	)			,			[
										        [
										            {'''score''': 0.9944, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										            {'''score''': 0.0009, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										        ],
										    ]
										    * 2			,			)


				@slow
				@require_torch
				@require_detectrona
				@require_pytesseract
				def 		__magic_name__(       self			:Optional[Any]	)		->			Any:
										__SCREAMING_SNAKE_CASE						:     int					=		pipeline(
										    '''document-question-answering'''			,			model='''tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'''			,			revision='''9977165'''			,			max_seq_len=50			,			)
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		INVOICE_URL
										__SCREAMING_SNAKE_CASE						:     Tuple					=		'''What is the invoice number?'''

										__SCREAMING_SNAKE_CASE						:     List[str]					=		dqa_pipeline(image=lowerCAmelCase__			,			question=lowerCAmelCase__			,			top_k=2	)
										self.assertEqual(
										    nested_simplify(lowerCAmelCase__			,			decimals=4	)			,			[
										        {'''score''': 0.9974, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
										        {'''score''': 0.9948, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										    ]			,			)

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

										__SCREAMING_SNAKE_CASE						:     str					=		dqa_pipeline(
										    [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]			,			top_k=2	)
										self.assertEqual(
										    nested_simplify(lowerCAmelCase__			,			decimals=4	)			,			[
										        [
										            {'''score''': 0.9974, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
										            {'''score''': 0.9948, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										        ]
										    ]
										    * 2			,			)


				@slow
				@require_torch
				@require_pytesseract
				@require_vision
				def 		__magic_name__(       self			:int	)		->			List[Any]:
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		AutoTokenizer.from_pretrained(
										    '''impira/layoutlm-document-qa'''			,			revision='''3dc6de3'''			,			add_prefix_space=lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     Dict					=		pipeline(
										    '''document-question-answering'''			,			model='''impira/layoutlm-document-qa'''			,			tokenizer=lowerCAmelCase__			,			revision='''3dc6de3'''			,			)
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		INVOICE_URL
										__SCREAMING_SNAKE_CASE						:     str					=		'''What is the invoice number?'''

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

										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		dqa_pipeline({'''image''': image, '''question''': question}			,			top_k=2	)
										self.assertEqual(
										    nested_simplify(lowerCAmelCase__			,			decimals=4	)			,			[
										        {'''score''': 0.4251, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										        {'''score''': 0.0819, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
										    ]			,			)

										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		dqa_pipeline(
										    [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]			,			top_k=2	)
										self.assertEqual(
										    nested_simplify(lowerCAmelCase__			,			decimals=4	)			,			[
										        [
										            {'''score''': 0.4251, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										            {'''score''': 0.0819, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
										        ]
										    ]
										    * 2			,			)

										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		list(zip(*apply_tesseract(load_image(lowerCAmelCase__	)			,			lowerCAmelCase__			,			''''''	)	)	)

										# This model should also work if `image` is set to None
										__SCREAMING_SNAKE_CASE						:     str					=		dqa_pipeline({'''image''': None, '''word_boxes''': word_boxes, '''question''': question}			,			top_k=2	)
										self.assertEqual(
										    nested_simplify(lowerCAmelCase__			,			decimals=4	)			,			[
										        {'''score''': 0.4251, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										        {'''score''': 0.0819, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
										    ]			,			)


				@slow
				@require_torch
				@require_pytesseract
				@require_vision
				def 		__magic_name__(       self			:str	)		->			Dict:
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		AutoTokenizer.from_pretrained(
										    '''impira/layoutlm-document-qa'''			,			revision='''3dc6de3'''			,			add_prefix_space=lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     Tuple					=		pipeline(
										    '''document-question-answering'''			,			model='''impira/layoutlm-document-qa'''			,			tokenizer=lowerCAmelCase__			,			revision='''3dc6de3'''			,			max_seq_len=50			,			)
										__SCREAMING_SNAKE_CASE						:     List[str]					=		INVOICE_URL
										__SCREAMING_SNAKE_CASE						:     Dict					=		'''What is the invoice number?'''

										__SCREAMING_SNAKE_CASE						:     List[Any]					=		dqa_pipeline(image=lowerCAmelCase__			,			question=lowerCAmelCase__			,			top_k=2	)
										self.assertEqual(
										    nested_simplify(lowerCAmelCase__			,			decimals=4	)			,			[
										        {'''score''': 0.9999, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										        {'''score''': 0.9998, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										    ]			,			)

										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		dqa_pipeline(
										    [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]			,			top_k=2	)
										self.assertEqual(
										    nested_simplify(lowerCAmelCase__			,			decimals=4	)			,			[
										        [
										            {'''score''': 0.9999, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										            {'''score''': 0.9998, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										        ]
										    ]
										    * 2			,			)

										__SCREAMING_SNAKE_CASE						:     List[str]					=		list(zip(*apply_tesseract(load_image(lowerCAmelCase__	)			,			lowerCAmelCase__			,			''''''	)	)	)

										# This model should also work if `image` is set to None
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		dqa_pipeline({'''image''': None, '''word_boxes''': word_boxes, '''question''': question}			,			top_k=2	)
										self.assertEqual(
										    nested_simplify(lowerCAmelCase__			,			decimals=4	)			,			[
										        {'''score''': 0.9999, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										        {'''score''': 0.9998, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
										    ]			,			)


				@slow
				@require_torch
				def 		__magic_name__(       self			:Union[str, Any]	)		->			Tuple:
										__SCREAMING_SNAKE_CASE						:     str					=		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						:     Union[str, Any]					=		INVOICE_URL
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		'''What is the invoice number?'''
										__SCREAMING_SNAKE_CASE						:     Tuple					=		dqa_pipeline(image=lowerCAmelCase__			,			question=lowerCAmelCase__			,			top_k=2	)
										self.assertEqual(nested_simplify(lowerCAmelCase__			,			decimals=4	)			,			[{'''answer''': '''us-001'''}]	)



				@require_tf
				@unittest.skip('''Document question answering not implemented in TF'''	)
				def 		__magic_name__(       self			:Union[str, Any]	)		->			Tuple:
										pass
