





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							:
     @staticmethod
     def      A_	(      *UpperCAmelCase  :      Any			,     **UpperCAmelCase  :      int	)			->	str:
         pass


    def        SCREAMING_SNAKE_CASE      (       _UpperCAmelCase )			->     List[str]:
        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				:       List[Any]	   =				(
    'https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png'
)

@is_pipeline_test
@require_torch
@require_vision
class    lowerCAmelCase							(					unittest.TestCase			):
 UpperCAmelCase__													=		MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING
 @require_pytesseract
 @require_vision
 def      A_	(      self  :      int			,     UpperCAmelCase  :      Optional[int]			,     UpperCAmelCase  :      List[str]			,     UpperCAmelCase  :      Union[str, Any]	)			->	str:
     lowerCamelCase__    :      List[Any]					 =		pipeline(
         'document-question-answering'			,     model=UpperCAmelCase			,     tokenizer=UpperCAmelCase			,     image_processor=UpperCAmelCase	)

     lowerCamelCase__    :      List[Any]					 =		INVOICE_URL
     lowerCamelCase__    :      Any					 =		list(zip(*apply_tesseract(load_image(UpperCAmelCase	)			,     UpperCAmelCase			,     ''	)	)	)
     lowerCamelCase__    :      Tuple					 =		'What is the placebo?'
     lowerCamelCase__    :      Optional[Any]					 =		[
         {
             'image': load_image(UpperCAmelCase	),
             'question': question,
         },
         {
             'image': image,
             'question': question,
         },
         {
             'image': image,
             'question': question,
             'word_boxes': word_boxes,
         },
     ]
     return dqa_pipeline, examples
 def      A_	(      self  :      Optional[int]			,     UpperCAmelCase  :      Any			,     UpperCAmelCase  :      List[str]	)			->	Optional[int]:
     lowerCamelCase__    :      Union[str, 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      A_	(      self  :      str	)			->	int:
     lowerCamelCase__    :      Any					 =		pipeline('document-question-answering'			,     model='hf-internal-testing/tiny-random-layoutlmv2'	)
     lowerCamelCase__    :      Tuple					 =		INVOICE_URL
     lowerCamelCase__    :      Any					 =		'How many cats are there?'

     lowerCamelCase__    :      Dict					 =		[
         {'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},
     ]
     lowerCamelCase__    :      List[Any]					 =		dqa_pipeline(image=UpperCAmelCase			,     question=UpperCAmelCase			,     top_k=2	)
     self.assertEqual(nested_simplify(UpperCAmelCase			,     decimals=4	)			,     UpperCAmelCase	)

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

     # We can optionnally pass directly the words and bounding boxes
     lowerCamelCase__    :      Tuple					 =		'./tests/fixtures/tests_samples/COCO/000000039769.png'
     lowerCamelCase__    :      Optional[int]					 =		[]
     lowerCamelCase__    :      Union[str, Any]					 =		[]
     lowerCamelCase__    :      List[Any]					 =		dqa_pipeline(image=UpperCAmelCase			,     question=UpperCAmelCase			,     words=UpperCAmelCase			,     boxes=UpperCAmelCase			,     top_k=2	)
     self.assertEqual(UpperCAmelCase			,     []	)
 @slow
 @require_torch
 @require_detectrona
 @require_pytesseract
 def      A_	(      self  :      int	)			->	Optional[int]:
     lowerCamelCase__    :      Optional[int]					 =		pipeline(
         'document-question-answering'			,     model='tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'			,     revision='9977165'			,     )
     lowerCamelCase__    :      List[Any]					 =		INVOICE_URL
     lowerCamelCase__    :      Dict					 =		'What is the invoice number?'

     lowerCamelCase__    :      Tuple					 =		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},
         ]			,     )

     lowerCamelCase__    :      Union[str, Any]					 =		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},
         ]			,     )

     lowerCamelCase__    :      Dict					 =		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      A_	(      self  :      Tuple	)			->	Optional[int]:
     lowerCamelCase__    :      str					 =		pipeline(
         'document-question-answering'			,     model='tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'			,     revision='9977165'			,     max_seq_len=50			,     )
     lowerCamelCase__    :      int					 =		INVOICE_URL
     lowerCamelCase__    :      str					 =		'What is the invoice number?'

     lowerCamelCase__    :      List[Any]					 =		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},
         ]			,     )

     lowerCamelCase__    :      Union[str, Any]					 =		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},
         ]			,     )

     lowerCamelCase__    :      str					 =		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      A_	(      self  :      Optional[int]	)			->	int:
     lowerCamelCase__    :      List[Any]					 =		AutoTokenizer.from_pretrained(
         'impira/layoutlm-document-qa'			,     revision='3dc6de3'			,     add_prefix_space=UpperCAmelCase	)
     lowerCamelCase__    :      Dict					 =		pipeline(
         'document-question-answering'			,     model='impira/layoutlm-document-qa'			,     tokenizer=UpperCAmelCase			,     revision='3dc6de3'			,     )
     lowerCamelCase__    :      Tuple					 =		INVOICE_URL
     lowerCamelCase__    :      int					 =		'What is the invoice number?'

     lowerCamelCase__    :      Dict					 =		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},
         ]			,     )

     lowerCamelCase__    :      List[Any]					 =		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},
         ]			,     )

     lowerCamelCase__    :      List[Any]					 =		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			,     )

     lowerCamelCase__    :      Optional[int]					 =		list(zip(*apply_tesseract(load_image(UpperCAmelCase	)			,     UpperCAmelCase			,     ''	)	)	)

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

     lowerCamelCase__    :      Any					 =		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},
         ]			,     )

     lowerCamelCase__    :      int					 =		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			,     )

     lowerCamelCase__    :      Optional[int]					 =		list(zip(*apply_tesseract(load_image(UpperCAmelCase	)			,     UpperCAmelCase			,     ''	)	)	)

     # This model should also work if `image` is set to None
     lowerCamelCase__    :      List[str]					 =		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      A_	(      self  :      Union[str, Any]	)			->	Optional[Any]:
     lowerCamelCase__    :      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'			,     )

     lowerCamelCase__    :      Union[str, Any]					 =		INVOICE_URL
     lowerCamelCase__    :      Dict					 =		'What is the invoice number?'
     lowerCamelCase__    :      List[Any]					 =		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      A_	(      self  :      Optional[int]	)			->	Any:
     pass







def 						UpperCAmelCase			(	a_						)		->	list:







				"""simple docstring"""



				if len(a_						) <= 1:
								return [tuple(a_						)]

				__A   			= []

				def generate(a_		,   a_						):
								if k == 1:
												res.append(tuple(arr[:]						)						)
												return

								generate(k - 1		,   a_						)

								for i in range(k - 1						):
												if k % 2 == 0:  # k is even
																__A			,							__A   			= arr[k - 1], arr[i]
												else:  # k is odd
																__A			,							__A   			= arr[k - 1], arr[0]
												generate(k - 1		,   a_						)

				generate(len(a_						)		,   a_						)
				return res


if __name__ == "__main__":
					SCREAMING_SNAKE_CASE							:int							 =				input('Enter numbers separated by a comma:\n').strip()
					SCREAMING_SNAKE_CASE							:Dict							 =				[int(item) for item in user_input.split(',')]
					print(heaps(arr))
