





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





													'''simple docstring'''







													@staticmethod
													def 							UpperCAmelCase__			(  *A			:   str							,    **A			:   List[Any]		):
															pass







						def       A__							(   SCREAMING_SNAKE_CASE__)       ->   Optional[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	:       Tuple    	=							(
    "https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png"
)







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





							'''simple docstring'''







							lowerCAmelCase__						=					MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING




							@require_pytesseract
							@require_vision
							def 							UpperCAmelCase__			(  self			:   Optional[int]							,    A			:   Union[str, Any]							,    A			:   Tuple							,    A			:   str		):
									__snake_case:						str			  =						pipeline(
									    """document-question-answering"""							,    model=A							,    tokenizer=A							,    image_processor=A		)

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




							def 							UpperCAmelCase__			(  self			:   int							,    A			:   Optional[Any]							,    A			:   Tuple		):
									__snake_case:						Tuple			  =						dqa_pipeline(A							,    top_k=2		)
									self.assertEqual(
									    A							,    [
									        [
									            {"""score""": ANY(A		), """answer""": ANY(A		), """start""": ANY(A		), """end""": ANY(A		)},
									            {"""score""": ANY(A		), """answer""": ANY(A		), """start""": ANY(A		), """end""": ANY(A		)},
									        ]
									    ]
									    * 3							,    )




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

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

									__snake_case:						Tuple			  =						dqa_pipeline({"""image""": image, """question""": question}							,    top_k=2		)
									self.assertEqual(nested_simplify(A							,    decimals=4		)							,    A		)

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

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




							@slow
							@require_torch
							@require_detectrona
							@require_pytesseract
							def 							UpperCAmelCase__			(  self			:   List[Any]		):
									__snake_case:						int			  =						pipeline(
									    """document-question-answering"""							,    model="""tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"""							,    revision="""9977165"""							,    )
									__snake_case:						Tuple			  =						INVOICE_URL
									__snake_case:						List[str]			  =						"""What is the invoice number?"""

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

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

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

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

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

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

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

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

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

									__snake_case:						int			  =						list(zip(*apply_tesseract(load_image(A		)							,    A							,    """"""		)		)		)

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

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

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

									__snake_case:						Optional[Any]			  =						list(zip(*apply_tesseract(load_image(A		)							,    A							,    """"""		)		)		)

									# This model should also work if `image` is set to None
									__snake_case:						Optional[int]			  =						dqa_pipeline({"""image""": None, """word_boxes""": word_boxes, """question""": question}							,    top_k=2		)
									self.assertEqual(
									    nested_simplify(A							,    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 							UpperCAmelCase__			(  self			:   Tuple		):
									__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"""							,    )

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






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













import warnings

from diffusers import StableDiffusionImgaImgPipeline  # noqa F401


warnings.warn(
    "The `image_to_image.py` script is outdated. Please use directly `from diffusers import"
    " StableDiffusionImg2ImgPipeline` instead."
)






