
"""simple docstring"""




import argparse
import json
import os

import evaluate
import torch
from datasets import load_dataset
from torch.optim import AdamW
from torch.utils.data import DataLoader
from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed

from accelerate import Accelerator, DistributedType
from accelerate.utils.deepspeed import DummyOptim, DummyScheduler


UpperCAmelCase__   :					str												=				1_6
UpperCAmelCase__   :					Optional[int]												=				3_2

def lowercase_						(     _snake_case     ,_snake_case = 16     ,_snake_case = "bert-base-cased"		):
							SCREAMING_SNAKE_CASE__		: Any     =     AutoTokenizer.from_pretrained(_snake_case		)
							SCREAMING_SNAKE_CASE__		: List[Any]     =     load_dataset("""glue"""     ,"""mrpc"""		)

							def tokenize_function(_snake_case		):
														# max_length=None => use the model max length (it's actually the default)
														SCREAMING_SNAKE_CASE__		: Any     =     tokenizer(examples["""sentence1"""]     ,examples["""sentence2"""]     ,truncation=_snake_case     ,max_length=_snake_case		)
														return outputs

							# Apply the method we just defined to all the examples in all the splits of the dataset
							SCREAMING_SNAKE_CASE__		: Dict     =     datasets.map(
							    _snake_case     ,batched=_snake_case     ,remove_columns=["""idx""", """sentence1""", """sentence2"""]     ,load_from_cache_file=_snake_case		)

							# We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the
							# transformers library
							SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     tokenized_datasets.rename_column("""label"""     ,"""labels"""		)

							def collate_fn(_snake_case		):
														# On TPU it's best to pad everything to the same length or training will be very slow.
														if accelerator.distributed_type == DistributedType.TPU:
																					return tokenizer.pad(_snake_case     ,padding="""max_length"""     ,max_length=128     ,return_tensors="""pt"""		)
														return tokenizer.pad(_snake_case     ,padding="""longest"""     ,return_tensors="""pt"""		)

							# Instantiate dataloaders.
							SCREAMING_SNAKE_CASE__		: List[str]     =     DataLoader(
							    tokenized_datasets["""train"""]     ,shuffle=_snake_case     ,collate_fn=_snake_case     ,batch_size=_snake_case		)
							SCREAMING_SNAKE_CASE__		: Tuple     =     DataLoader(
							    tokenized_datasets["""validation"""]     ,shuffle=_snake_case     ,collate_fn=_snake_case     ,batch_size=_snake_case		)

							return train_dataloader, eval_dataloader

def lowercase_						(     _snake_case     ,_snake_case     ,_snake_case     ,_snake_case		):
							model.eval()
							SCREAMING_SNAKE_CASE__		: List[Any]     =     0
							for step, batch in enumerate(_snake_case		):
														# We could avoid this line since we set the accelerator with `device_placement=True`.
														batch.to(accelerator.device		)
														with torch.no_grad():
																					SCREAMING_SNAKE_CASE__		: List[Any]     =     model(**_snake_case		)
														SCREAMING_SNAKE_CASE__		: List[Any]     =     outputs.logits.argmax(dim=-1		)
														# It is slightly faster to call this once, than multiple times
														SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__		: Optional[Any]     =     accelerator.gather(
														    (predictions, batch["""labels"""])		)  # If we are in a multiprocess environment, the last batch has duplicates
														if accelerator.use_distributed:
																					if step == len(_snake_case		) - 1:
																												SCREAMING_SNAKE_CASE__		: Any     =     predictions[: len(eval_dataloader.dataset		) - samples_seen]
																												SCREAMING_SNAKE_CASE__		: List[str]     =     references[: len(eval_dataloader.dataset		) - samples_seen]
																					else:
																												samples_seen += references.shape[0]
														metric.add_batch(
														    predictions=_snake_case     ,references=_snake_case     ,)

							SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     metric.compute()
							return eval_metric["accuracy"]

def lowercase_						(     _snake_case     ,_snake_case		):
							# Initialize accelerator
							SCREAMING_SNAKE_CASE__		: int     =     Accelerator()

							# Sample hyper-parameters for learning rate, batch size, seed and a few other HPs
							SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     config["""lr"""]
							SCREAMING_SNAKE_CASE__		: int     =     int(config["""num_epochs"""]		)
							SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     int(config["""seed"""]		)
							SCREAMING_SNAKE_CASE__		: Tuple     =     int(config["""batch_size"""]		)
							SCREAMING_SNAKE_CASE__		: Optional[int]     =     args.model_name_or_path

							set_seed(_snake_case		)
							SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__		: Optional[int]     =     get_dataloaders(_snake_case     ,_snake_case     ,_snake_case		)

							# Instantiate the model (we build the model here so that the seed also control new weights initialization)
							SCREAMING_SNAKE_CASE__		: List[Any]     =     AutoModelForSequenceClassification.from_pretrained(_snake_case     ,return_dict=_snake_case		)

							# Instantiate optimizer
							SCREAMING_SNAKE_CASE__		: Tuple     =     (
							    AdamW
							    if accelerator.state.deepspeed_plugin is None
							    or """optimizer""" not in accelerator.state.deepspeed_plugin.deepspeed_config
							    else DummyOptim
							)
							SCREAMING_SNAKE_CASE__		: int     =     optimizer_cls(params=model.parameters()     ,lr=_snake_case		)

							if accelerator.state.deepspeed_plugin is not None:
														SCREAMING_SNAKE_CASE__		: List[str]     =     accelerator.state.deepspeed_plugin.deepspeed_config[
														    """gradient_accumulation_steps"""
														]
							else:
														SCREAMING_SNAKE_CASE__		: int     =     1
							SCREAMING_SNAKE_CASE__		: Dict     =     (len(_snake_case		) * num_epochs) // gradient_accumulation_steps

							# Instantiate scheduler
							if (
							    accelerator.state.deepspeed_plugin is None
							    or "scheduler" not in accelerator.state.deepspeed_plugin.deepspeed_config
							):
														SCREAMING_SNAKE_CASE__		: str     =     get_linear_schedule_with_warmup(
														    optimizer=_snake_case     ,num_warmup_steps=0     ,num_training_steps=_snake_case     ,)
							else:
														SCREAMING_SNAKE_CASE__		: Optional[int]     =     DummyScheduler(_snake_case     ,total_num_steps=_snake_case     ,warmup_num_steps=0		)

							# Prepare everything
							# There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the
							# prepare method.
							SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__		: Tuple     =     accelerator.prepare(
							    _snake_case     ,_snake_case     ,_snake_case     ,_snake_case     ,_snake_case		)

							# We need to keep track of how many total steps we have iterated over
							SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     0
							# We also need to keep track of the stating epoch so files are named properly
							SCREAMING_SNAKE_CASE__		: List[Any]     =     0
							SCREAMING_SNAKE_CASE__		: Tuple     =     evaluate.load("""glue"""     ,"""mrpc"""		)
							SCREAMING_SNAKE_CASE__		: Dict     =     num_epochs

							if args.partial_train_epoch is not None:
														SCREAMING_SNAKE_CASE__		: Dict     =     args.partial_train_epoch

							if args.resume_from_checkpoint:
														accelerator.load_state(args.resume_from_checkpoint		)
														SCREAMING_SNAKE_CASE__		: Tuple     =     args.resume_from_checkpoint.split("""epoch_"""		)[1]
														SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     """"""
														for char in epoch_string:
																					if char.isdigit():
																												state_epoch_num += char
																					else:
																												break
														SCREAMING_SNAKE_CASE__		: List[Any]     =     int(_snake_case		) + 1
														SCREAMING_SNAKE_CASE__		: List[Any]     =     evaluation_loop(_snake_case     ,_snake_case     ,_snake_case     ,_snake_case		)
														accelerator.print("""resumed checkpoint performance:"""     ,_snake_case		)
														accelerator.print("""resumed checkpoint's scheduler's lr:"""     ,lr_scheduler.get_lr()[0]		)
														accelerator.print("""resumed optimizers's lr:"""     ,optimizer.param_groups[0]["""lr"""]		)
														with open(os.path.join(args.output_dir     ,f'''state_{starting_epoch-1}.json'''		)     ,"""r"""		) as f:
																					SCREAMING_SNAKE_CASE__		: Any     =     json.load(_snake_case		)
																					assert resumed_state["accuracy"] == accuracy, "Accuracy mismatch, loading from checkpoint failed"
																					assert (
																					    resumed_state["lr"] == lr_scheduler.get_lr()[0]
																					), "Scheduler learning rate mismatch, loading from checkpoint failed"
																					assert (
																					    resumed_state["optimizer_lr"] == optimizer.param_groups[0]["lr"]
																					), "Optimizer learning rate mismatch, loading from checkpoint failed"
																					assert resumed_state["epoch"] == starting_epoch - 1, "Epoch mismatch, loading from checkpoint failed"
																					return

    # Now we train the model
							SCREAMING_SNAKE_CASE__		: Dict     =     {}
							for epoch in range(_snake_case     ,_snake_case		):
														model.train()
														for step, batch in enumerate(_snake_case		):
																					SCREAMING_SNAKE_CASE__		: int     =     model(**_snake_case		)
																					SCREAMING_SNAKE_CASE__		: Tuple     =     outputs.loss
																					SCREAMING_SNAKE_CASE__		: Optional[Any]     =     loss / gradient_accumulation_steps
																					accelerator.backward(_snake_case		)
																					if step % gradient_accumulation_steps == 0:
																												optimizer.step()
																												lr_scheduler.step()
																												optimizer.zero_grad()

																					overall_step += 1
														SCREAMING_SNAKE_CASE__		: Any     =     f'''epoch_{epoch}'''
														SCREAMING_SNAKE_CASE__		: Any     =     os.path.join(args.output_dir     ,_snake_case		)
														accelerator.save_state(_snake_case		)
														SCREAMING_SNAKE_CASE__		: Tuple     =     evaluation_loop(_snake_case     ,_snake_case     ,_snake_case     ,_snake_case		)
														SCREAMING_SNAKE_CASE__		: Tuple     =     accuracy
														SCREAMING_SNAKE_CASE__		: List[str]     =     lr_scheduler.get_lr()[0]
														SCREAMING_SNAKE_CASE__		: List[str]     =     optimizer.param_groups[0]["""lr"""]
														SCREAMING_SNAKE_CASE__		: Tuple     =     epoch
														SCREAMING_SNAKE_CASE__		: Tuple     =     overall_step
														accelerator.print(f'''epoch {epoch}:'''     ,_snake_case		)

														accelerator.wait_for_everyone()
														if accelerator.is_main_process:
																					with open(os.path.join(args.output_dir     ,f'''state_{epoch}.json'''		)     ,"""w"""		) as f:
																												json.dump(_snake_case     ,_snake_case		)

def lowercase_						(     ):
							SCREAMING_SNAKE_CASE__		: List[Any]     =     argparse.ArgumentParser(description="""Simple example of training script tracking peak GPU memory usage."""		)
							parser.add_argument(
							    """--model_name_or_path"""     ,type=_snake_case     ,default="""bert-base-cased"""     ,help="""Path to pretrained model or model identifier from huggingface.co/models."""     ,required=_snake_case     ,)
							parser.add_argument(
							    """--output_dir"""     ,type=_snake_case     ,default="""."""     ,help="""Optional save directory where all checkpoint folders will be stored. Default is the current working directory."""     ,)
							parser.add_argument(
							    """--resume_from_checkpoint"""     ,type=_snake_case     ,default=_snake_case     ,help="""If the training should continue from a checkpoint folder."""     ,)
							parser.add_argument(
							    """--partial_train_epoch"""     ,type=_snake_case     ,default=_snake_case     ,help="""If passed, the training will stop after this number of epochs."""     ,)
							parser.add_argument(
							    """--num_epochs"""     ,type=_snake_case     ,default=2     ,help="""Number of train epochs."""     ,)
							SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     parser.parse_args()
							SCREAMING_SNAKE_CASE__		: Optional[Any]     =     {"""lr""": 2E-5, """num_epochs""": args.num_epochs, """seed""": 42, """batch_size""": 16}

							training_function(_snake_case     ,_snake_case		)


if __name__ == "__main__":
					main()






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




