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






										'''simple docstring'''

										@staticmethod
										def       lowerCAmelCase__	(       *snake_case_ :       Optional[int]						,	**snake_case_ :       int      ):
														pass







							def        A__					(						lowerCamelCase						)  ->	Optional[int]:

											'''simple docstring'''





											return None


# This is a pinned image from a specific revision of a document question answering space, hosted by HuggingFace,
# so we can expect it to be available.
lowerCamelCase_						: Dict         =  (
    'https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png'
)

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






			'''simple docstring'''

			__UpperCamelCase      :						int					      = MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING

			@require_pytesseract
			@require_vision
			def       lowerCAmelCase__	(       self :       List[Any]						,	snake_case_ :       int						,	snake_case_ :       List[str]						,	snake_case_ :       List[str]      ):
							UpperCamelCase_:							Dict	=						pipeline(
							    """document-question-answering"""						,	model=UpperCamelCase__						,	tokenizer=UpperCamelCase__						,	image_processor=UpperCamelCase__      )

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

			def       lowerCAmelCase__	(       self :       Optional[Any]						,	snake_case_ :       Any						,	snake_case_ :       str      ):
							UpperCamelCase_:							List[Any]	=						dqa_pipeline(UpperCamelCase__						,	top_k=2      )
							self.assertEqual(
							    UpperCamelCase__						,	[
							        [
							            {"""score""": ANY(UpperCamelCase__      ), """answer""": ANY(UpperCamelCase__      ), """start""": ANY(UpperCamelCase__      ), """end""": ANY(UpperCamelCase__      )},
							            {"""score""": ANY(UpperCamelCase__      ), """answer""": ANY(UpperCamelCase__      ), """start""": ANY(UpperCamelCase__      ), """end""": ANY(UpperCamelCase__      )},
							        ]
							    ]
							    * 3						,	)

			@require_torch
			@require_detectrona
			@require_pytesseract
			def       lowerCAmelCase__	(       self :       str      ):
							UpperCamelCase_:							Tuple	=						pipeline("""document-question-answering"""						,	model="""hf-internal-testing/tiny-random-layoutlmv2"""      )
							UpperCamelCase_:							str	=						INVOICE_URL
							UpperCamelCase_:							Union[str, Any]	=						'''How many cats are there?'''

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

							UpperCamelCase_:							List[str]	=						dqa_pipeline({"""image""": image, """question""": question}						,	top_k=2      )
							self.assertEqual(nested_simplify(UpperCamelCase__						,	decimals=4      )						,	UpperCamelCase__      )

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

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

			@slow
			@require_torch
			@require_detectrona
			@require_pytesseract
			def       lowerCAmelCase__	(       self :       int      ):
							UpperCamelCase_:							Any	=						pipeline(
							    """document-question-answering"""						,	model="""tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"""						,	revision="""9977165"""						,	)
							UpperCamelCase_:							Any	=						INVOICE_URL
							UpperCamelCase_:							Dict	=						'''What is the invoice number?'''

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

							UpperCamelCase_:							int	=						dqa_pipeline({"""image""": image, """question""": question}						,	top_k=2      )
							self.assertEqual(
							    nested_simplify(UpperCamelCase__						,	decimals=4      )						,	[
							        {"""score""": 0.9944, """answer""": """us-001""", """start""": 16, """end""": 16},
							        {"""score""": 0.0009, """answer""": """us-001""", """start""": 16, """end""": 16},
							    ]						,	)

							UpperCamelCase_:							List[str]	=						dqa_pipeline(
							    [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]						,	top_k=2      )
							self.assertEqual(
							    nested_simplify(UpperCamelCase__						,	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       lowerCAmelCase__	(       self :       Optional[Any]      ):
							UpperCamelCase_:							str	=						pipeline(
							    """document-question-answering"""						,	model="""tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"""						,	revision="""9977165"""						,	max_seq_len=50						,	)
							UpperCamelCase_:							Dict	=						INVOICE_URL
							UpperCamelCase_:							int	=						'''What is the invoice number?'''

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

							UpperCamelCase_:							Union[str, Any]	=						dqa_pipeline({"""image""": image, """question""": question}						,	top_k=2      )
							self.assertEqual(
							    nested_simplify(UpperCamelCase__						,	decimals=4      )						,	[
							        {"""score""": 0.9974, """answer""": """1110212019""", """start""": 23, """end""": 23},
							        {"""score""": 0.9948, """answer""": """us-001""", """start""": 16, """end""": 16},
							    ]						,	)

							UpperCamelCase_:							Optional[int]	=						dqa_pipeline(
							    [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]						,	top_k=2      )
							self.assertEqual(
							    nested_simplify(UpperCamelCase__						,	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       lowerCAmelCase__	(       self :       Optional[Any]      ):
							UpperCamelCase_:							Tuple	=						AutoTokenizer.from_pretrained(
							    """impira/layoutlm-document-qa"""						,	revision="""3dc6de3"""						,	add_prefix_space=UpperCamelCase__      )
							UpperCamelCase_:							Dict	=						pipeline(
							    """document-question-answering"""						,	model="""impira/layoutlm-document-qa"""						,	tokenizer=UpperCamelCase__						,	revision="""3dc6de3"""						,	)
							UpperCamelCase_:							Tuple	=						INVOICE_URL
							UpperCamelCase_:							Tuple	=						'''What is the invoice number?'''

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

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

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

							UpperCamelCase_:							Union[str, Any]	=						list(zip(*apply_tesseract(load_image(UpperCamelCase__      )						,	UpperCamelCase__						,	""""""      )      )      )

							# This model should also work if `image` is set to None
							UpperCamelCase_:							Tuple	=						dqa_pipeline({"""image""": None, """word_boxes""": word_boxes, """question""": question}						,	top_k=2      )
							self.assertEqual(
							    nested_simplify(UpperCamelCase__						,	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       lowerCAmelCase__	(       self :       Optional[Any]      ):
							UpperCamelCase_:							int	=						AutoTokenizer.from_pretrained(
							    """impira/layoutlm-document-qa"""						,	revision="""3dc6de3"""						,	add_prefix_space=UpperCamelCase__      )
							UpperCamelCase_:							Optional[int]	=						pipeline(
							    """document-question-answering"""						,	model="""impira/layoutlm-document-qa"""						,	tokenizer=UpperCamelCase__						,	revision="""3dc6de3"""						,	max_seq_len=50						,	)
							UpperCamelCase_:							List[Any]	=						INVOICE_URL
							UpperCamelCase_:							Union[str, Any]	=						'''What is the invoice number?'''

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

							UpperCamelCase_:							List[str]	=						dqa_pipeline(
							    [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]						,	top_k=2      )
							self.assertEqual(
							    nested_simplify(UpperCamelCase__						,	decimals=4      )						,	[
							        [
							            {"""score""": 0.9999, """answer""": """us-001""", """start""": 16, """end""": 16},
							            {"""score""": 0.9998, """answer""": """us-001""", """start""": 16, """end""": 16},
							        ]
							    ]
							    * 2						,	)

							UpperCamelCase_:							int	=						list(zip(*apply_tesseract(load_image(UpperCamelCase__      )						,	UpperCamelCase__						,	""""""      )      )      )

							# This model should also work if `image` is set to None
							UpperCamelCase_:							int	=						dqa_pipeline({"""image""": None, """word_boxes""": word_boxes, """question""": question}						,	top_k=2      )
							self.assertEqual(
							    nested_simplify(UpperCamelCase__						,	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       lowerCAmelCase__	(       self :       Any      ):
							UpperCamelCase_:							Any	=						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"""						,	)

							UpperCamelCase_:							Dict	=						INVOICE_URL
							UpperCamelCase_:							Optional[Any]	=						'''What is the invoice number?'''
							UpperCamelCase_:							Optional[int]	=						dqa_pipeline(image=UpperCamelCase__						,	question=UpperCamelCase__						,	top_k=2      )
							self.assertEqual(nested_simplify(UpperCamelCase__						,	decimals=4      )						,	[{"""answer""": """us-001"""}]      )




			@require_tf
			@unittest.skip("""Document question answering not implemented in TF"""      )
			def       lowerCAmelCase__	(       self :       Union[str, Any]      ):
							pass








import json
import logging
import os
import sys
from pathlib import Path

import finetune_rag

from transformers.file_utils import is_apex_available
from transformers.testing_utils import (
    TestCasePlus,
    execute_subprocess_async,
    require_ray,
    require_torch_gpu,
    require_torch_multi_gpu,
)


logging.basicConfig(level=logging.DEBUG)
lowerCamelCase_						: Union[str, Any]         =  logging.getLogger()

lowerCamelCase_						: List[str]         =  logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)

class        _UpperCamelCase				(					_A  ):






			'''simple docstring'''

			def       lowerCAmelCase__	(       self :       List[Any]						,	snake_case_ :       Dict      ):
							os.makedirs(snake_case_						,	exist_ok=snake_case_      )
							UpperCamelCase_:							int	=						{"""source""": """What is love ?""", """target""": """life"""}
							UpperCamelCase_:							Tuple	=						{"""train""": 12, """val""": 2, """test""": 2}
							for split in ["train", "test", "val"]:
											for field in ["source", "target"]:
															UpperCamelCase_:							Tuple	=						"""\n""".join([contents[field]] * n_lines[split]      )
															with open(os.path.join(snake_case_						,	f'''{split}.{field}'''      )						,	"""w"""      ) as f:
																			f.write(snake_case_      )

			def       lowerCAmelCase__	(       self :       Dict						,	snake_case_ :       int						,	snake_case_ :       str = "pytorch"      ):
							UpperCamelCase_:							Optional[Any]	=						self.get_auto_remove_tmp_dir()
							UpperCamelCase_:							Dict	=						os.path.join(snake_case_						,	"""output"""      )
							UpperCamelCase_:							Any	=						os.path.join(snake_case_						,	"""data"""      )
							self._create_dummy_data(data_dir=snake_case_      )

							UpperCamelCase_:							Union[str, Any]	=						f'''
                --data_dir {data_dir} \
                --output_dir {output_dir} \
                --model_name_or_path facebook/rag-sequence-base \
                --model_type rag_sequence \
                --do_train \
                --do_predict \
                --n_val -1 \
                --val_check_interval 1.0 \
                --train_batch_size 2 \
                --eval_batch_size 1 \
                --max_source_length 25 \
                --max_target_length 25 \
                --val_max_target_length 25 \
                --test_max_target_length 25 \
                --label_smoothing 0.1 \
                --dropout 0.1 \
                --attention_dropout 0.1 \
                --weight_decay 0.001 \
                --adam_epsilon 1e-08 \
                --max_grad_norm 0.1 \
                --lr_scheduler polynomial \
                --learning_rate 3e-04 \
                --num_train_epochs 1 \
                --warmup_steps 4 \
                --gradient_accumulation_steps 1 \
                --distributed-port 8787 \
                --use_dummy_dataset 1 \
                --distributed_retriever {distributed_retriever} \
            '''.split()

							if gpus > 0:
											testargs.append(f'''--gpus={gpus}'''      )
											if is_apex_available():
															testargs.append("""--fp16"""      )
							else:
											testargs.append("""--gpus=0"""      )
											testargs.append("""--distributed_backend=ddp_cpu"""      )
											testargs.append("""--num_processes=2"""      )

							UpperCamelCase_:							Optional[Any]	=						[sys.executable, str(Path(finetune_rag.__file__      ).resolve()      )] + testargs
							execute_subprocess_async(snake_case_						,	env=self.get_env()      )

							UpperCamelCase_:							Optional[int]	=						os.path.join(snake_case_						,	"""metrics.json"""      )
							with open(snake_case_      ) as f:
											UpperCamelCase_:							Any	=						json.load(snake_case_      )
							return result

			@require_torch_gpu
			def       lowerCAmelCase__	(       self :       Optional[Any]      ):
							UpperCamelCase_:							List[str]	=						self._run_finetune(gpus=1      )
							self.assertGreaterEqual(result["""test"""][0]["""test_avg_em"""]						,	0.2      )

			@require_torch_multi_gpu
			def       lowerCAmelCase__	(       self :       Optional[Any]      ):
							UpperCamelCase_:							Any	=						self._run_finetune(gpus=2      )
							self.assertGreaterEqual(result["""test"""][0]["""test_avg_em"""]						,	0.2      )

			@require_torch_gpu
			@require_ray
			def       lowerCAmelCase__	(       self :       Dict      ):
							UpperCamelCase_:							List[Any]	=						self._run_finetune(gpus=1						,	distributed_retriever="""ray"""      )
							self.assertGreaterEqual(result["""test"""][0]["""test_avg_em"""]						,	0.2      )




			@require_torch_multi_gpu
			@require_ray
			def       lowerCAmelCase__	(       self :       Dict      ):
							UpperCamelCase_:							List[Any]	=						self._run_finetune(gpus=1						,	distributed_retriever="""ray"""      )
							self.assertGreaterEqual(result["""test"""][0]["""test_avg_em"""]						,	0.2      )






