
'''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 	__lowercase				:

								@staticmethod
								def        UpperCAmelCase__ (*A			,   **A      ):
													pass




					def lowercase_	(   _lowercase				)     ->							Tuple:
										'''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.
__lowercase							:   str					=     (
    '''https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png'''
)

@is_pipeline_test
@require_torch
@require_vision
class 	__lowercase				(  unittest.TestCase    ):
			lowerCamelCase			:		List[Any]	      =			MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING

			@require_pytesseract
			@require_vision
			def        UpperCAmelCase__ (self			,   A			,   A			,   A      ):
								lowerCamelCase_					:      Optional[Any]     =		pipeline(
								    '''document-question-answering'''			,   model=A			,   tokenizer=A			,   image_processor=A      )

								lowerCamelCase_					:      Optional[int]     =		INVOICE_URL
								lowerCamelCase_					:      Dict     =		list(zip(*apply_tesseract(load_image(A      )			,   A			,   ''''''      )      )      )
								lowerCamelCase_					:      Union[str, Any]     =		'''What is the placebo?'''
								lowerCamelCase_					:      List[str]     =		[
								    {
								        '''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			,   A			,   A      ):
								lowerCamelCase_					:      Optional[Any]     =		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      ):
								lowerCamelCase_					:      Union[str, Any]     =		pipeline('''document-question-answering'''			,   model='''hf-internal-testing/tiny-random-layoutlmv2'''      )
								lowerCamelCase_					:      str     =		INVOICE_URL
								lowerCamelCase_					:      Tuple     =		'''How many cats are there?'''

								lowerCamelCase_					:      Optional[int]     =		[
								    {'''score''': 0.00_01, '''answer''': '''oy 2312/2019''', '''start''': 3_8, '''end''': 3_9},
								    {'''score''': 0.00_01, '''answer''': '''oy 2312/2019 DUE''', '''start''': 3_8, '''end''': 4_0},
								]
								lowerCamelCase_					:      List[Any]     =		dqa_pipeline(image=A			,   question=A			,   top_k=2      )
								self.assertEqual(nested_simplify(A			,   decimals=4      )			,   A      )

								lowerCamelCase_					:      str     =		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
								lowerCamelCase_					:      Dict     =		'''./tests/fixtures/tests_samples/COCO/000000039769.png'''
								lowerCamelCase_					:      Optional[int]     =		dqa_pipeline(image=A			,   question=A			,   top_k=2      )
								self.assertEqual(A			,   []      )

								# We can optionnally pass directly the words and bounding boxes
								lowerCamelCase_					:      Union[str, Any]     =		'''./tests/fixtures/tests_samples/COCO/000000039769.png'''
								lowerCamelCase_					:      Tuple     =		[]
								lowerCamelCase_					:      Tuple     =		[]
								lowerCamelCase_					:      Union[str, Any]     =		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      ):
								lowerCamelCase_					:      Tuple     =		pipeline(
								    '''document-question-answering'''			,   model='''tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'''			,   revision='''9977165'''			,   )
								lowerCamelCase_					:      int     =		INVOICE_URL
								lowerCamelCase_					:      Tuple     =		'''What is the invoice number?'''

								lowerCamelCase_					:      str     =		dqa_pipeline(image=A			,   question=A			,   top_k=2      )
								self.assertEqual(
								    nested_simplify(A			,   decimals=4      )			,   [
								        {'''score''': 0.99_44, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
								        {'''score''': 0.00_09, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
								    ]			,   )

								lowerCamelCase_					:      Union[str, Any]     =		dqa_pipeline({'''image''': image, '''question''': question}			,   top_k=2      )
								self.assertEqual(
								    nested_simplify(A			,   decimals=4      )			,   [
								        {'''score''': 0.99_44, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
								        {'''score''': 0.00_09, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
								    ]			,   )

								lowerCamelCase_					:      Optional[int]     =		dqa_pipeline(
								    [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]			,   top_k=2      )
								self.assertEqual(
								    nested_simplify(A			,   decimals=4      )			,   [
								        [
								            {'''score''': 0.99_44, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
								            {'''score''': 0.00_09, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
								        ],
								    ]
								    * 2			,   )

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

								lowerCamelCase_					:      Optional[Any]     =		dqa_pipeline(image=A			,   question=A			,   top_k=2      )
								self.assertEqual(
								    nested_simplify(A			,   decimals=4      )			,   [
								        {'''score''': 0.99_74, '''answer''': '''1110212019''', '''start''': 2_3, '''end''': 2_3},
								        {'''score''': 0.99_48, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
								    ]			,   )

								lowerCamelCase_					:      str     =		dqa_pipeline({'''image''': image, '''question''': question}			,   top_k=2      )
								self.assertEqual(
								    nested_simplify(A			,   decimals=4      )			,   [
								        {'''score''': 0.99_74, '''answer''': '''1110212019''', '''start''': 2_3, '''end''': 2_3},
								        {'''score''': 0.99_48, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
								    ]			,   )

								lowerCamelCase_					:      Dict     =		dqa_pipeline(
								    [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]			,   top_k=2      )
								self.assertEqual(
								    nested_simplify(A			,   decimals=4      )			,   [
								        [
								            {'''score''': 0.99_74, '''answer''': '''1110212019''', '''start''': 2_3, '''end''': 2_3},
								            {'''score''': 0.99_48, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
								        ]
								    ]
								    * 2			,   )

			@slow
			@require_torch
			@require_pytesseract
			@require_vision
			def        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      Dict     =		AutoTokenizer.from_pretrained(
								    '''impira/layoutlm-document-qa'''			,   revision='''3dc6de3'''			,   add_prefix_space=A      )
								lowerCamelCase_					:      Union[str, Any]     =		pipeline(
								    '''document-question-answering'''			,   model='''impira/layoutlm-document-qa'''			,   tokenizer=A			,   revision='''3dc6de3'''			,   )
								lowerCamelCase_					:      List[Any]     =		INVOICE_URL
								lowerCamelCase_					:      Optional[int]     =		'''What is the invoice number?'''

								lowerCamelCase_					:      Tuple     =		dqa_pipeline(image=A			,   question=A			,   top_k=2      )
								self.assertEqual(
								    nested_simplify(A			,   decimals=4      )			,   [
								        {'''score''': 0.42_51, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
								        {'''score''': 0.08_19, '''answer''': '''1110212019''', '''start''': 2_3, '''end''': 2_3},
								    ]			,   )

								lowerCamelCase_					:      List[str]     =		dqa_pipeline({'''image''': image, '''question''': question}			,   top_k=2      )
								self.assertEqual(
								    nested_simplify(A			,   decimals=4      )			,   [
								        {'''score''': 0.42_51, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
								        {'''score''': 0.08_19, '''answer''': '''1110212019''', '''start''': 2_3, '''end''': 2_3},
								    ]			,   )

								lowerCamelCase_					:      int     =		dqa_pipeline(
								    [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]			,   top_k=2      )
								self.assertEqual(
								    nested_simplify(A			,   decimals=4      )			,   [
								        [
								            {'''score''': 0.42_51, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
								            {'''score''': 0.08_19, '''answer''': '''1110212019''', '''start''': 2_3, '''end''': 2_3},
								        ]
								    ]
								    * 2			,   )

								lowerCamelCase_					:      Optional[int]     =		list(zip(*apply_tesseract(load_image(A      )			,   A			,   ''''''      )      )      )

								# This model should also work if `image` is set to None
								lowerCamelCase_					:      Any     =		dqa_pipeline({'''image''': None, '''word_boxes''': word_boxes, '''question''': question}			,   top_k=2      )
								self.assertEqual(
								    nested_simplify(A			,   decimals=4      )			,   [
								        {'''score''': 0.42_51, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
								        {'''score''': 0.08_19, '''answer''': '''1110212019''', '''start''': 2_3, '''end''': 2_3},
								    ]			,   )

			@slow
			@require_torch
			@require_pytesseract
			@require_vision
			def        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      List[str]     =		AutoTokenizer.from_pretrained(
								    '''impira/layoutlm-document-qa'''			,   revision='''3dc6de3'''			,   add_prefix_space=A      )
								lowerCamelCase_					:      List[Any]     =		pipeline(
								    '''document-question-answering'''			,   model='''impira/layoutlm-document-qa'''			,   tokenizer=A			,   revision='''3dc6de3'''			,   max_seq_len=5_0			,   )
								lowerCamelCase_					:      List[Any]     =		INVOICE_URL
								lowerCamelCase_					:      List[str]     =		'''What is the invoice number?'''

								lowerCamelCase_					:      List[Any]     =		dqa_pipeline(image=A			,   question=A			,   top_k=2      )
								self.assertEqual(
								    nested_simplify(A			,   decimals=4      )			,   [
								        {'''score''': 0.99_99, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
								        {'''score''': 0.99_98, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
								    ]			,   )

								lowerCamelCase_					:      Dict     =		dqa_pipeline(
								    [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]			,   top_k=2      )
								self.assertEqual(
								    nested_simplify(A			,   decimals=4      )			,   [
								        [
								            {'''score''': 0.99_99, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
								            {'''score''': 0.99_98, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
								        ]
								    ]
								    * 2			,   )

								lowerCamelCase_					:      Tuple     =		list(zip(*apply_tesseract(load_image(A      )			,   A			,   ''''''      )      )      )

								# This model should also work if `image` is set to None
								lowerCamelCase_					:      List[Any]     =		dqa_pipeline({'''image''': None, '''word_boxes''': word_boxes, '''question''': question}			,   top_k=2      )
								self.assertEqual(
								    nested_simplify(A			,   decimals=4      )			,   [
								        {'''score''': 0.99_99, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
								        {'''score''': 0.99_98, '''answer''': '''us-001''', '''start''': 1_6, '''end''': 1_6},
								    ]			,   )

			@slow
			@require_torch
			def        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      int     =		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'''			,   )

								lowerCamelCase_					:      str     =		INVOICE_URL
								lowerCamelCase_					:      List[Any]     =		'''What is the invoice number?'''
								lowerCamelCase_					:      Optional[Any]     =		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      ):
								pass




'''simple docstring'''


from collections import namedtuple

__lowercase							:   List[Any]					=     namedtuple('''from_to''', '''from_ to''')

__lowercase							:   Optional[Any]					=     {
    '''cubicmeter''': from_to(1, 1),
    '''litre''': from_to(0.0_01, 1000),
    '''kilolitre''': from_to(1, 1),
    '''gallon''': from_to(0.0_04_54, 2_64.1_72),
    '''cubicyard''': from_to(0.7_64_55, 1.3_07_95),
    '''cubicfoot''': from_to(0.0_28, 35.31_47),
    '''cup''': from_to(0.0_00_23_65_88, 42_26.75),
}




def lowercase_	(   _lowercase			,       _lowercase			,       _lowercase				)     ->							float:
					'''simple docstring'''

					if from_type not in METRIC_CONVERSION:
										raise ValueError(
										    F"""Invalid 'from_type' value: {from_type!r}  Supported values are:\n"""
										    + ''', '''.join(_lowercase				)				)
					if to_type not in METRIC_CONVERSION:
										raise ValueError(
										    F"""Invalid 'to_type' value: {to_type!r}.  Supported values are:\n"""
										    + ''', '''.join(_lowercase				)				)
					return value * METRIC_CONVERSION[from_type].from_ * METRIC_CONVERSION[to_type].to


if __name__ == "__main__":
					import doctest

					doctest.testmod()


