
"""simple docstring"""




import argparse
import json
import os

import fairseq
import torch
from fairseq.data import Dictionary

from transformers import (
    UniSpeechConfig,
    UniSpeechForCTC,
    UniSpeechForPreTraining,
    WavaVecaFeatureExtractor,
    WavaVecaPhonemeCTCTokenizer,
    WavaVecaProcessor,
    logging,
)


logging.set_verbosity_info()
UpperCAmelCase__   :					Any												=				logging.get_logger(__name__)

UpperCAmelCase__   :					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': 'ctc_proj',
    'mask_emb': 'masked_spec_embed',
}
UpperCAmelCase__   :					int												=				[
    'ctc_proj',
    'quantizer.weight_proj',
    'quantizer.codevectors',
    'project_q',
    'project_hid',
]

def lowercase_						(     _snake_case     ,_snake_case     ,_snake_case     ,_snake_case     ,_snake_case     ,_snake_case		):
							for attribute in key.split("""."""		):
														if is_finetuned:
																					if attribute in ["quantizer", "project_q", "project_hid"]:
																												# those layers are only relevant for pretraining and should be dropped
																												return

																					if attribute == "ctc_proj":
																												# we should rename `ctc_proj` to `lm_head` for fine-tuned phoneme models
																												SCREAMING_SNAKE_CASE__		: Any     =     """lm_head"""

														SCREAMING_SNAKE_CASE__		: Optional[int]     =     getattr(_snake_case     ,_snake_case		)

							if weight_type is not None:
														SCREAMING_SNAKE_CASE__		: Any     =     getattr(_snake_case     ,_snake_case		).shape
							else:
														SCREAMING_SNAKE_CASE__		: List[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__		: Union[str, Any]     =     value
							elif weight_type == "weight_g":
														SCREAMING_SNAKE_CASE__		: Any     =     value
							elif weight_type == "weight_v":
														SCREAMING_SNAKE_CASE__		: int     =     value
							elif weight_type == "bias":
														SCREAMING_SNAKE_CASE__		: Any     =     value
							else:
														SCREAMING_SNAKE_CASE__		: Dict     =     value

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

def lowercase_						(     _snake_case     ,_snake_case     ,_snake_case		):
							SCREAMING_SNAKE_CASE__		: List[str]     =     []
							SCREAMING_SNAKE_CASE__		: Any     =     fairseq_model.state_dict()

							SCREAMING_SNAKE_CASE__		: int     =     hf_model.unispeech.feature_extractor

							for name, value in fairseq_dict.items():
														SCREAMING_SNAKE_CASE__		: str     =     False
														if "conv_layers" in name:
																					load_conv_layer(
																					    _snake_case     ,_snake_case     ,_snake_case     ,_snake_case     ,hf_model.config.feat_extract_norm == """group"""     ,)
																					SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     True
														else:
																					for key, mapped_key in MAPPING.items():
																												SCREAMING_SNAKE_CASE__		: Optional[int]     =     """unispeech.""" + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key
																												if key in name or key.split("""w2v_model."""		)[-1] == name.split("""."""		)[0]:
																																			SCREAMING_SNAKE_CASE__		: List[str]     =     True
																																			if "*" in mapped_key:
																																										SCREAMING_SNAKE_CASE__		: List[str]     =     name.split(_snake_case		)[0].split("""."""		)[-2]
																																										SCREAMING_SNAKE_CASE__		: str     =     mapped_key.replace("""*"""     ,_snake_case		)
																																			if "weight_g" in name:
																																										SCREAMING_SNAKE_CASE__		: List[str]     =     """weight_g"""
																																			elif "weight_v" in name:
																																										SCREAMING_SNAKE_CASE__		: Any     =     """weight_v"""
																																			elif "bias" in name:
																																										SCREAMING_SNAKE_CASE__		: str     =     """bias"""
																																			elif "weight" in name:
																																										# TODO: don't match quantizer.weight_proj
																																										SCREAMING_SNAKE_CASE__		: str     =     """weight"""
																																			else:
																																										SCREAMING_SNAKE_CASE__		: Optional[Any]     =     None
																																			set_recursively(_snake_case     ,_snake_case     ,_snake_case     ,_snake_case     ,_snake_case     ,_snake_case		)
																												continue
														if not is_used:
																					unused_weights.append(_snake_case		)

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

def lowercase_						(     _snake_case     ,_snake_case     ,_snake_case     ,_snake_case     ,_snake_case		):
							SCREAMING_SNAKE_CASE__		: List[str]     =     full_name.split("""conv_layers."""		)[-1]
							SCREAMING_SNAKE_CASE__		: Any     =     name.split("""."""		)
							SCREAMING_SNAKE_CASE__		: str     =     int(items[0]		)
							SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     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__		: str     =     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__		: List[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__		: Optional[Any]     =     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[str]     =     value
																					logger.info(f'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.'''		)
							else:
														unused_weights.append(_snake_case		)

@torch.no_grad()
def lowercase_						(     _snake_case     ,_snake_case     ,_snake_case=None     ,_snake_case=None     ,_snake_case=True		):
							if config_path is not None:
														SCREAMING_SNAKE_CASE__		: Dict     =     UniSpeechConfig.from_pretrained(_snake_case		)
							else:
														SCREAMING_SNAKE_CASE__		: Tuple     =     UniSpeechConfig()

							if is_finetuned:
														if dict_path:
																					SCREAMING_SNAKE_CASE__		: Tuple     =     Dictionary.load_from_json(_snake_case		)

																					# important change bos & pad token id since CTC symbol is <pad> and
																					# not <s> as in fairseq
																					SCREAMING_SNAKE_CASE__		: Optional[Any]     =     target_dict.pad_index
																					SCREAMING_SNAKE_CASE__		: Dict     =     target_dict.bos_index
																					SCREAMING_SNAKE_CASE__		: Dict     =     target_dict.eos_index
																					SCREAMING_SNAKE_CASE__		: Dict     =     len(target_dict.symbols		)
																					SCREAMING_SNAKE_CASE__		: List[str]     =     os.path.join(_snake_case     ,"""vocab.json"""		)
																					if not os.path.isdir(_snake_case		):
																												logger.error("""--pytorch_dump_folder_path ({}) should be a directory""".format(_snake_case		)		)
																												return
																					os.makedirs(_snake_case     ,exist_ok=_snake_case		)
																					SCREAMING_SNAKE_CASE__		: Any     =     target_dict.indices

																					# fairseq has the <pad> and <s> switched
																					SCREAMING_SNAKE_CASE__		: List[Any]     =     42
																					SCREAMING_SNAKE_CASE__		: Any     =     43
																					with open(_snake_case     ,"""w"""     ,encoding="""utf-8"""		) as vocab_handle:
																												json.dump(_snake_case     ,_snake_case		)
																					SCREAMING_SNAKE_CASE__		: List[Any]     =     WavaVecaPhonemeCTCTokenizer(
																					    _snake_case     ,unk_token=target_dict.unk_word     ,pad_token=target_dict.pad_word     ,bos_token=target_dict.bos_word     ,eos_token=target_dict.eos_word     ,word_delimiter_token="""|"""     ,do_lower_case=_snake_case     ,)
																					SCREAMING_SNAKE_CASE__		: Tuple     =     True if config.feat_extract_norm == """layer""" else False
																					SCREAMING_SNAKE_CASE__		: Tuple     =     WavaVecaFeatureExtractor(
																					    feature_size=1     ,sampling_rate=16_000     ,padding_value=0     ,do_normalize=_snake_case     ,return_attention_mask=_snake_case     ,)
																					SCREAMING_SNAKE_CASE__		: Optional[Any]     =     WavaVecaProcessor(feature_extractor=_snake_case     ,tokenizer=_snake_case		)
																					processor.save_pretrained(_snake_case		)

														SCREAMING_SNAKE_CASE__		: Tuple     =     UniSpeechForCTC(_snake_case		)
							else:
														SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     UniSpeechForPreTraining(_snake_case		)

							if is_finetuned:
														SCREAMING_SNAKE_CASE__		: Dict     =     fairseq.checkpoint_utils.load_model_ensemble_and_task(
														    [checkpoint_path]     ,arg_overrides={"""data""": """/""".join(dict_path.split("""/"""		)[:-1]		), """w2v_path""": checkpoint_path}		)
							else:
														SCREAMING_SNAKE_CASE__		: Optional[int]     =     fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path]		)

							SCREAMING_SNAKE_CASE__		: str     =     model[0].eval()

							recursively_load_weights(_snake_case     ,_snake_case     ,_snake_case		)

							hf_unispeech.save_pretrained(_snake_case		)


if __name__ == "__main__":
						UpperCAmelCase__   :					Union[str, Any]												=				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('--config_path', default=None, type=str, help='Path to hf config.json of model to convert')
						parser.add_argument(
						    '--not_finetuned', action='store_true', help='Whether the model to convert is a fine-tuned model or not'
						)
						UpperCAmelCase__   :					Tuple												=				parser.parse_args()
						convert_unispeech_checkpoint(
						    args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, not args.not_finetuned
						)






"""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 			lowerCAmelCase_		:


									"""simple docstring"""



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



																"""simple docstring"""




																pass






					def lowercase_						(     _snake_case		):
												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__   :					str												=				(
    'https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png'
)



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


				"""simple docstring"""



				__UpperCamelCase   :		List[Any]       =     MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING



				@require_pytesseract
				@require_vision
				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__					)      ->	Optional[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: List[str]     =     pipeline(
											    """document-question-answering"""     , model=SCREAMING_SNAKE_CASE__     , tokenizer=SCREAMING_SNAKE_CASE__     , image_processor=SCREAMING_SNAKE_CASE__					)

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



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__					)      ->	List[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Tuple     =     dqa_pipeline(SCREAMING_SNAKE_CASE__     , top_k=2					)
											self.assertEqual(
											    SCREAMING_SNAKE_CASE__     , [
											        [
											            {"""score""": ANY(SCREAMING_SNAKE_CASE__					), """answer""": ANY(SCREAMING_SNAKE_CASE__					), """start""": ANY(SCREAMING_SNAKE_CASE__					), """end""": ANY(SCREAMING_SNAKE_CASE__					)},
											            {"""score""": ANY(SCREAMING_SNAKE_CASE__					), """answer""": ANY(SCREAMING_SNAKE_CASE__					), """start""": ANY(SCREAMING_SNAKE_CASE__					), """end""": ANY(SCREAMING_SNAKE_CASE__					)},
											        ]
											    ]
											    * 3     , )



				@require_torch
				@require_detectrona
				@require_pytesseract
				def 			__magic_name__					(self					)      ->	List[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: List[Any]     =     pipeline("""document-question-answering"""     , model="""hf-internal-testing/tiny-random-layoutlmv2"""					)
											SCREAMING_SNAKE_CASE__		: str     =     INVOICE_URL
											SCREAMING_SNAKE_CASE__		: Any     =     """How many cats are there?"""

											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     [
											    {"""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__		: Any     =     dqa_pipeline(image=SCREAMING_SNAKE_CASE__     , question=SCREAMING_SNAKE_CASE__     , top_k=2					)
											self.assertEqual(nested_simplify(SCREAMING_SNAKE_CASE__     , decimals=4					)     , SCREAMING_SNAKE_CASE__					)

											SCREAMING_SNAKE_CASE__		: str     =     dqa_pipeline({"""image""": image, """question""": question}     , top_k=2					)
											self.assertEqual(nested_simplify(SCREAMING_SNAKE_CASE__     , decimals=4					)     , SCREAMING_SNAKE_CASE__					)

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

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



				@slow
				@require_torch
				@require_detectrona
				@require_pytesseract
				def 			__magic_name__					(self					)      ->	List[str]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: List[str]     =     pipeline(
											    """document-question-answering"""     , model="""tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"""     , revision="""9977165"""     , )
											SCREAMING_SNAKE_CASE__		: str     =     INVOICE_URL
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     """What is the invoice number?"""

											SCREAMING_SNAKE_CASE__		: List[str]     =     dqa_pipeline(image=SCREAMING_SNAKE_CASE__     , question=SCREAMING_SNAKE_CASE__     , top_k=2					)
											self.assertEqual(
											    nested_simplify(SCREAMING_SNAKE_CASE__     , 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(SCREAMING_SNAKE_CASE__     , 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__		: Optional[int]     =     dqa_pipeline(
											    [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]     , top_k=2					)
											self.assertEqual(
											    nested_simplify(SCREAMING_SNAKE_CASE__     , 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					)      ->	List[str]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: List[str]     =     pipeline(
											    """document-question-answering"""     , model="""tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"""     , revision="""9977165"""     , max_seq_len=50     , )
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     INVOICE_URL
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     """What is the invoice number?"""

											SCREAMING_SNAKE_CASE__		: int     =     dqa_pipeline(image=SCREAMING_SNAKE_CASE__     , question=SCREAMING_SNAKE_CASE__     , top_k=2					)
											self.assertEqual(
											    nested_simplify(SCREAMING_SNAKE_CASE__     , 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}     , top_k=2					)
											self.assertEqual(
											    nested_simplify(SCREAMING_SNAKE_CASE__     , decimals=4					)     , [
											        {"""score""": 0.9974, """answer""": """1110212019""", """start""": 23, """end""": 23},
											        {"""score""": 0.9948, """answer""": """us-001""", """start""": 16, """end""": 16},
											    ]     , )

											SCREAMING_SNAKE_CASE__		: List[str]     =     dqa_pipeline(
											    [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]     , top_k=2					)
											self.assertEqual(
											    nested_simplify(SCREAMING_SNAKE_CASE__     , 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					)      ->	Any:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Optional[int]     =     AutoTokenizer.from_pretrained(
											    """impira/layoutlm-document-qa"""     , revision="""3dc6de3"""     , add_prefix_space=SCREAMING_SNAKE_CASE__					)
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     pipeline(
											    """document-question-answering"""     , model="""impira/layoutlm-document-qa"""     , tokenizer=SCREAMING_SNAKE_CASE__     , revision="""3dc6de3"""     , )
											SCREAMING_SNAKE_CASE__		: List[str]     =     INVOICE_URL
											SCREAMING_SNAKE_CASE__		: Any     =     """What is the invoice number?"""

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

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

											SCREAMING_SNAKE_CASE__		: Dict     =     dqa_pipeline(
											    [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]     , top_k=2					)
											self.assertEqual(
											    nested_simplify(SCREAMING_SNAKE_CASE__     , 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[Any]     =     list(zip(*apply_tesseract(load_image(SCREAMING_SNAKE_CASE__					)     , SCREAMING_SNAKE_CASE__     , """"""					)					)					)

											# This model should also work if `image` is set to None
											SCREAMING_SNAKE_CASE__		: Dict     =     dqa_pipeline({"""image""": None, """word_boxes""": word_boxes, """question""": question}     , top_k=2					)
											self.assertEqual(
											    nested_simplify(SCREAMING_SNAKE_CASE__     , 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					)      ->	List[str]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Any     =     AutoTokenizer.from_pretrained(
											    """impira/layoutlm-document-qa"""     , revision="""3dc6de3"""     , add_prefix_space=SCREAMING_SNAKE_CASE__					)
											SCREAMING_SNAKE_CASE__		: Dict     =     pipeline(
											    """document-question-answering"""     , model="""impira/layoutlm-document-qa"""     , tokenizer=SCREAMING_SNAKE_CASE__     , revision="""3dc6de3"""     , max_seq_len=50     , )
											SCREAMING_SNAKE_CASE__		: str     =     INVOICE_URL
											SCREAMING_SNAKE_CASE__		: Dict     =     """What is the invoice number?"""

											SCREAMING_SNAKE_CASE__		: Optional[int]     =     dqa_pipeline(image=SCREAMING_SNAKE_CASE__     , question=SCREAMING_SNAKE_CASE__     , top_k=2					)
											self.assertEqual(
											    nested_simplify(SCREAMING_SNAKE_CASE__     , 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__		: Union[str, Any]     =     dqa_pipeline(
											    [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]     , top_k=2					)
											self.assertEqual(
											    nested_simplify(SCREAMING_SNAKE_CASE__     , 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__		: Any     =     list(zip(*apply_tesseract(load_image(SCREAMING_SNAKE_CASE__					)     , SCREAMING_SNAKE_CASE__     , """"""					)					)					)

											# 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(SCREAMING_SNAKE_CASE__     , 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					)      ->	Optional[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: 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"""     , )

											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     INVOICE_URL
											SCREAMING_SNAKE_CASE__		: List[Any]     =     """What is the invoice number?"""
											SCREAMING_SNAKE_CASE__		: int     =     dqa_pipeline(image=SCREAMING_SNAKE_CASE__     , question=SCREAMING_SNAKE_CASE__     , top_k=2					)
											self.assertEqual(nested_simplify(SCREAMING_SNAKE_CASE__     , decimals=4					)     , [{"""answer""": """us-001"""}]					)



				@require_tf
				@unittest.skip("""Document question answering not implemented in TF"""					)
				def 			__magic_name__					(self					)      ->	int:



											"""simple docstring"""




											pass




