


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 					snake_case__					(				*snake_case   ,    **snake_case  ):

												'''simple docstring'''


												pass



						def 							UpperCamelCase_(					_A :Dict	)->		Union[str, Any]:
									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     		=							(
    'https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png'
)







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






			"""simple docstring"""






			_UpperCamelCase		:       Any 			=		MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING




			@require_pytesseract
			@require_vision
			def 					snake_case__					(				self   ,    snake_case   ,    snake_case   ,    snake_case  ):

						'''simple docstring'''


						UpperCamelCase__       =							pipeline(
						    "document-question-answering"   ,    model=snake_case   ,    tokenizer=snake_case   ,    image_processor=snake_case  )

						UpperCamelCase__       =							INVOICE_URL
						UpperCamelCase__       =							list(zip(*apply_tesseract(load_image(snake_case  )   ,    snake_case   ,    ""  )  )  )
						UpperCamelCase__       =							"What is the placebo?"
						UpperCamelCase__       =							[
						    {
						        "image": load_image(snake_case  ),
						        "question": question,
						    },
						    {
						        "image": image,
						        "question": question,
						    },
						    {
						        "image": image,
						        "question": question,
						        "word_boxes": word_boxes,
						    },
						]
						return dqa_pipeline, examples




			def 					snake_case__					(				self   ,    snake_case   ,    snake_case  ):

						'''simple docstring'''


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




			@require_torch
			@require_detectrona
			@require_pytesseract
			def 					snake_case__					(				self  ):

						'''simple docstring'''


						UpperCamelCase__       =							pipeline("document-question-answering"   ,    model="hf-internal-testing/tiny-random-layoutlmv2"  )
						UpperCamelCase__       =							INVOICE_URL
						UpperCamelCase__       =							"How many cats are there?"

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

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

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

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




			@slow
			@require_torch
			@require_detectrona
			@require_pytesseract
			def 					snake_case__					(				self  ):

						'''simple docstring'''


						UpperCamelCase__       =							pipeline(
						    "document-question-answering"   ,    model="tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"   ,    revision="9977165"   ,    )
						UpperCamelCase__       =							INVOICE_URL
						UpperCamelCase__       =							"What is the invoice number?"

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

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

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

						'''simple docstring'''


						UpperCamelCase__       =							pipeline(
						    "document-question-answering"   ,    model="tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"   ,    revision="9977165"   ,    max_seq_len=50   ,    )
						UpperCamelCase__       =							INVOICE_URL
						UpperCamelCase__       =							"What is the invoice number?"

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

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

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

						'''simple docstring'''


						UpperCamelCase__       =							AutoTokenizer.from_pretrained(
						    "impira/layoutlm-document-qa"   ,    revision="3dc6de3"   ,    add_prefix_space=snake_case  )
						UpperCamelCase__       =							pipeline(
						    "document-question-answering"   ,    model="impira/layoutlm-document-qa"   ,    tokenizer=snake_case   ,    revision="3dc6de3"   ,    )
						UpperCamelCase__       =							INVOICE_URL
						UpperCamelCase__       =							"What is the invoice number?"

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

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

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

						UpperCamelCase__       =							list(zip(*apply_tesseract(load_image(snake_case  )   ,    snake_case   ,    ""  )  )  )

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

						'''simple docstring'''


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

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

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

						UpperCamelCase__       =							list(zip(*apply_tesseract(load_image(snake_case  )   ,    snake_case   ,    ""  )  )  )

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

						'''simple docstring'''


						UpperCamelCase__       =							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__       =							INVOICE_URL
						UpperCamelCase__       =							"What is the invoice number?"
						UpperCamelCase__       =							dqa_pipeline(image=snake_case   ,    question=snake_case   ,    top_k=2  )
						self.assertEqual(nested_simplify(snake_case   ,    decimals=4  )   ,    [{"answer": "us-001"}]  )







			@require_tf
			@unittest.skip("Document question answering not implemented in TF"  )
			def 					snake_case__					(				self  ):

						'''simple docstring'''


						pass






from unittest.mock import Mock, patch

from file_transfer.send_file import send_file
@patch("socket.socket"	)
@patch("builtins.open"	)
def 							UpperCamelCase_(					_A :Tuple					,  _A :str	)->		int:
			# ===== initialization =====
			UpperCamelCase__       =							Mock()
			UpperCamelCase__       =							conn, Mock()
			UpperCamelCase__       =							iter([1, None]	)
			UpperCamelCase__       =							lambda _A	: next(_A	)

			# ===== invoke =====
			send_file(filename="mytext.txt"					,  testing=_A	)

			# ===== ensurance =====
			sock.assert_called_once()
			sock.return_value.bind.assert_called_once()
			sock.return_value.listen.assert_called_once()
			sock.return_value.accept.assert_called_once()
			conn.recv.assert_called_once()

			file.return_value.__enter__.assert_called_once()
			file.return_value.__enter__.return_value.read.assert_called()

			conn.send.assert_called_once()
			conn.close.assert_called_once()
			sock.return_value.shutdown.assert_called_once()
			sock.return_value.close.assert_called_once()


