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




														@staticmethod
														def 	__snake_case(						*_UpperCamelCase   :					Tuple	,							**_UpperCamelCase   :					List[Any]      )					->				Any:



																			'''simple docstring'''

																			pass





							def 	__lowerCamelCase							(UpperCAmelCase__     :							Dict  ):
												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 						lowercase    (     unittest.TestCase     ):
							lowercase__						:       Union[str, Any]								=							MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING




							@require_pytesseract
							@require_vision
							def 	__snake_case(						self   :					Optional[Any]	,							_UpperCamelCase   :					List[Any]	,							_UpperCamelCase   :					List[Any]	,							_UpperCamelCase   :					Union[str, Any]      )					->				Optional[int]:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE    			=       pipeline(
												    "document-question-answering"	,							model=_UpperCamelCase	,							tokenizer=_UpperCamelCase	,							image_processor=_UpperCamelCase      )

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




							def 	__snake_case(						self   :					Optional[Any]	,							_UpperCamelCase   :					int	,							_UpperCamelCase   :					Dict      )					->				List[Any]:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE    			=       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 	__snake_case(						self   :					List[Any]      )					->				List[Any]:



												'''simple docstring'''

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

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

												SCREAMING_SNAKE_CASE    			=       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
												SCREAMING_SNAKE_CASE    			=       "./tests/fixtures/tests_samples/COCO/000000039769.png"
												SCREAMING_SNAKE_CASE    			=       dqa_pipeline(image=_UpperCamelCase	,							question=_UpperCamelCase	,							top_k=2      )
												self.assertEqual(_UpperCamelCase	,							[]      )

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




							@slow
							@require_torch
							@require_detectrona
							@require_pytesseract
							def 	__snake_case(						self   :					Union[str, Any]      )					->				Any:



												'''simple docstring'''

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

												SCREAMING_SNAKE_CASE    			=       dqa_pipeline(image=_UpperCamelCase	,							question=_UpperCamelCase	,							top_k=2      )
												self.assertEqual(
												    nested_simplify(_UpperCamelCase	,							decimals=4      )	,							[
												        {"score": 0.9_9_4_4, "answer": "us-001", "start": 16, "end": 16},
												        {"score": 0.0_0_0_9, "answer": "us-001", "start": 16, "end": 16},
												    ]	,							)

												SCREAMING_SNAKE_CASE    			=       dqa_pipeline({"image": image, "question": question}	,							top_k=2      )
												self.assertEqual(
												    nested_simplify(_UpperCamelCase	,							decimals=4      )	,							[
												        {"score": 0.9_9_4_4, "answer": "us-001", "start": 16, "end": 16},
												        {"score": 0.0_0_0_9, "answer": "us-001", "start": 16, "end": 16},
												    ]	,							)

												SCREAMING_SNAKE_CASE    			=       dqa_pipeline(
												    [{"image": image, "question": question}, {"image": image, "question": question}]	,							top_k=2      )
												self.assertEqual(
												    nested_simplify(_UpperCamelCase	,							decimals=4      )	,							[
												        [
												            {"score": 0.9_9_4_4, "answer": "us-001", "start": 16, "end": 16},
												            {"score": 0.0_0_0_9, "answer": "us-001", "start": 16, "end": 16},
												        ],
												    ]
												    * 2	,							)




							@slow
							@require_torch
							@require_detectrona
							@require_pytesseract
							def 	__snake_case(						self   :					int      )					->				Optional[int]:



												'''simple docstring'''

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

												SCREAMING_SNAKE_CASE    			=       dqa_pipeline(image=_UpperCamelCase	,							question=_UpperCamelCase	,							top_k=2      )
												self.assertEqual(
												    nested_simplify(_UpperCamelCase	,							decimals=4      )	,							[
												        {"score": 0.9_9_7_4, "answer": "1110212019", "start": 23, "end": 23},
												        {"score": 0.9_9_4_8, "answer": "us-001", "start": 16, "end": 16},
												    ]	,							)

												SCREAMING_SNAKE_CASE    			=       dqa_pipeline({"image": image, "question": question}	,							top_k=2      )
												self.assertEqual(
												    nested_simplify(_UpperCamelCase	,							decimals=4      )	,							[
												        {"score": 0.9_9_7_4, "answer": "1110212019", "start": 23, "end": 23},
												        {"score": 0.9_9_4_8, "answer": "us-001", "start": 16, "end": 16},
												    ]	,							)

												SCREAMING_SNAKE_CASE    			=       dqa_pipeline(
												    [{"image": image, "question": question}, {"image": image, "question": question}]	,							top_k=2      )
												self.assertEqual(
												    nested_simplify(_UpperCamelCase	,							decimals=4      )	,							[
												        [
												            {"score": 0.9_9_7_4, "answer": "1110212019", "start": 23, "end": 23},
												            {"score": 0.9_9_4_8, "answer": "us-001", "start": 16, "end": 16},
												        ]
												    ]
												    * 2	,							)




							@slow
							@require_torch
							@require_pytesseract
							@require_vision
							def 	__snake_case(						self   :					Union[str, Any]      )					->				Any:



												'''simple docstring'''

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

												SCREAMING_SNAKE_CASE    			=       dqa_pipeline(image=_UpperCamelCase	,							question=_UpperCamelCase	,							top_k=2      )
												self.assertEqual(
												    nested_simplify(_UpperCamelCase	,							decimals=4      )	,							[
												        {"score": 0.4_2_5_1, "answer": "us-001", "start": 16, "end": 16},
												        {"score": 0.0_8_1_9, "answer": "1110212019", "start": 23, "end": 23},
												    ]	,							)

												SCREAMING_SNAKE_CASE    			=       dqa_pipeline({"image": image, "question": question}	,							top_k=2      )
												self.assertEqual(
												    nested_simplify(_UpperCamelCase	,							decimals=4      )	,							[
												        {"score": 0.4_2_5_1, "answer": "us-001", "start": 16, "end": 16},
												        {"score": 0.0_8_1_9, "answer": "1110212019", "start": 23, "end": 23},
												    ]	,							)

												SCREAMING_SNAKE_CASE    			=       dqa_pipeline(
												    [{"image": image, "question": question}, {"image": image, "question": question}]	,							top_k=2      )
												self.assertEqual(
												    nested_simplify(_UpperCamelCase	,							decimals=4      )	,							[
												        [
												            {"score": 0.4_2_5_1, "answer": "us-001", "start": 16, "end": 16},
												            {"score": 0.0_8_1_9, "answer": "1110212019", "start": 23, "end": 23},
												        ]
												    ]
												    * 2	,							)

												SCREAMING_SNAKE_CASE    			=       list(zip(*apply_tesseract(load_image(_UpperCamelCase      )	,							_UpperCamelCase	,							""      )      )      )

												# This model should also work if `image` is set to None
												SCREAMING_SNAKE_CASE    			=       dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question}	,							top_k=2      )
												self.assertEqual(
												    nested_simplify(_UpperCamelCase	,							decimals=4      )	,							[
												        {"score": 0.4_2_5_1, "answer": "us-001", "start": 16, "end": 16},
												        {"score": 0.0_8_1_9, "answer": "1110212019", "start": 23, "end": 23},
												    ]	,							)




							@slow
							@require_torch
							@require_pytesseract
							@require_vision
							def 	__snake_case(						self   :					Any      )					->				str:



												'''simple docstring'''

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

												SCREAMING_SNAKE_CASE    			=       dqa_pipeline(image=_UpperCamelCase	,							question=_UpperCamelCase	,							top_k=2      )
												self.assertEqual(
												    nested_simplify(_UpperCamelCase	,							decimals=4      )	,							[
												        {"score": 0.9_9_9_9, "answer": "us-001", "start": 16, "end": 16},
												        {"score": 0.9_9_9_8, "answer": "us-001", "start": 16, "end": 16},
												    ]	,							)

												SCREAMING_SNAKE_CASE    			=       dqa_pipeline(
												    [{"image": image, "question": question}, {"image": image, "question": question}]	,							top_k=2      )
												self.assertEqual(
												    nested_simplify(_UpperCamelCase	,							decimals=4      )	,							[
												        [
												            {"score": 0.9_9_9_9, "answer": "us-001", "start": 16, "end": 16},
												            {"score": 0.9_9_9_8, "answer": "us-001", "start": 16, "end": 16},
												        ]
												    ]
												    * 2	,							)

												SCREAMING_SNAKE_CASE    			=       list(zip(*apply_tesseract(load_image(_UpperCamelCase      )	,							_UpperCamelCase	,							""      )      )      )

												# This model should also work if `image` is set to None
												SCREAMING_SNAKE_CASE    			=       dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question}	,							top_k=2      )
												self.assertEqual(
												    nested_simplify(_UpperCamelCase	,							decimals=4      )	,							[
												        {"score": 0.9_9_9_9, "answer": "us-001", "start": 16, "end": 16},
												        {"score": 0.9_9_9_8, "answer": "us-001", "start": 16, "end": 16},
												    ]	,							)




							@slow
							@require_torch
							def 	__snake_case(						self   :					Optional[Any]      )					->				Dict:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE    			=       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    			=       INVOICE_URL
												SCREAMING_SNAKE_CASE    			=       "What is the invoice number?"
												SCREAMING_SNAKE_CASE    			=       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 	__snake_case(						self   :					List[str]      )					->				Dict:



												'''simple docstring'''

												pass





# coding=utf-8
# Copyright 2023 The HuggingFace Inc. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# this script dumps information about the environment

import os
import platform
import sys


_lowerCamelCase					:   List[Any]							=		'''3'''

print('''Python version:''', sys.version)

print('''OS platform:''', platform.platform())
print('''OS architecture:''', platform.machine())

try:
							import torch

							print('''Torch version:''', torch.__version__)
							print('''Cuda available:''', torch.cuda.is_available())
							print('''Cuda version:''', torch.version.cuda)
							print('''CuDNN version:''', torch.backends.cudnn.version())
							print('''Number of GPUs available:''', torch.cuda.device_count())
except ImportError:
							print('''Torch version:''', None)

try:
							import transformers

							print('''transformers version:''', transformers.__version__)
except ImportError:
							print('''transformers version:''', None)




