





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 UpperCamelCase_ (      *UpperCamelCase      :					Union[str, Any]      ,				**UpperCamelCase      :					Optional[Any]							):


										'''simple docstring'''





										pass




						def     lowerCamelCase_	(      lowerCAmelCase:	List[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.
lowerCAmelCase_						   =    (
    """https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png"""
)



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


		'''simple docstring'''
		a_							:       List[Any]      					=MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING







		@require_pytesseract
		@require_vision
		def UpperCamelCase_ (      self      :					Union[str, Any]      ,				UpperCamelCase      :					List[Any]      ,				UpperCamelCase      :					int      ,				UpperCamelCase      :					Any							):


				'''simple docstring'''





				_snake_case   :				Tuple														=	pipeline(
				    'document-question-answering'      ,				model=lowerCamelCase_      ,				tokenizer=lowerCamelCase_      ,				image_processor=lowerCamelCase_							)

				_snake_case   :				List[str]														=	INVOICE_URL
				_snake_case   :				Tuple														=	list(zip(*apply_tesseract(load_image(lowerCamelCase_							)      ,				lowerCamelCase_      ,				''							)							)							)
				_snake_case   :				List[str]														=	"""What is the placebo?"""
				_snake_case   :				Dict														=	[
				    {
				        """image""": load_image(lowerCamelCase_							),
				        """question""": question,
				    },
				    {
				        """image""": image,
				        """question""": question,
				    },
				    {
				        """image""": image,
				        """question""": question,
				        """word_boxes""": word_boxes,
				    },
				]
				return dqa_pipeline, examples







		def UpperCamelCase_ (      self      :					Dict      ,				UpperCamelCase      :					Optional[int]      ,				UpperCamelCase      :					List[Any]							):


				'''simple docstring'''





				_snake_case   :				List[str]														=	dqa_pipeline(lowerCamelCase_      ,				top_k=2							)
				self.assertEqual(
				    lowerCamelCase_      ,				[
				        [
				            {'score': ANY(lowerCamelCase_							), 'answer': ANY(lowerCamelCase_							), 'start': ANY(lowerCamelCase_							), 'end': ANY(lowerCamelCase_							)},
				            {'score': ANY(lowerCamelCase_							), 'answer': ANY(lowerCamelCase_							), 'start': ANY(lowerCamelCase_							), 'end': ANY(lowerCamelCase_							)},
				        ]
				    ]
				    * 3      ,				)







		@require_torch
		@require_detectrona
		@require_pytesseract
		def UpperCamelCase_ (      self      :					Tuple							):


				'''simple docstring'''





				_snake_case   :				Union[str, Any]														=	pipeline('document-question-answering'      ,				model='hf-internal-testing/tiny-random-layoutlmv2'							)
				_snake_case   :				Optional[int]														=	INVOICE_URL
				_snake_case   :				List[Any]														=	"""How many cats are there?"""

				_snake_case   :				Union[str, Any]														=	[
				    {"""score""": 0.00_01, """answer""": """oy 2312/2019""", """start""": 38, """end""": 39},
				    {"""score""": 0.00_01, """answer""": """oy 2312/2019 DUE""", """start""": 38, """end""": 40},
				]
				_snake_case   :				Any														=	dqa_pipeline(image=lowerCamelCase_      ,				question=lowerCamelCase_      ,				top_k=2							)
				self.assertEqual(nested_simplify(lowerCamelCase_      ,				decimals=4							)      ,				lowerCamelCase_							)

				_snake_case   :				Optional[Any]														=	dqa_pipeline({'image': image, 'question': question}      ,				top_k=2							)
				self.assertEqual(nested_simplify(lowerCamelCase_      ,				decimals=4							)      ,				lowerCamelCase_							)

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

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







		@slow
		@require_torch
		@require_detectrona
		@require_pytesseract
		def UpperCamelCase_ (      self      :					Any							):


				'''simple docstring'''





				_snake_case   :				Optional[int]														=	pipeline(
				    'document-question-answering'      ,				model='tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'      ,				revision='9977165'      ,				)
				_snake_case   :				List[str]														=	INVOICE_URL
				_snake_case   :				Optional[Any]														=	"""What is the invoice number?"""

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

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

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







		@slow
		@require_torch
		@require_detectrona
		@require_pytesseract
		def UpperCamelCase_ (      self      :					int							):


				'''simple docstring'''





				_snake_case   :				Union[str, Any]														=	pipeline(
				    'document-question-answering'      ,				model='tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'      ,				revision='9977165'      ,				max_seq_len=50      ,				)
				_snake_case   :				List[Any]														=	INVOICE_URL
				_snake_case   :				Union[str, Any]														=	"""What is the invoice number?"""

				_snake_case   :				Optional[int]														=	dqa_pipeline(image=lowerCamelCase_      ,				question=lowerCamelCase_      ,				top_k=2							)
				self.assertEqual(
				    nested_simplify(lowerCamelCase_      ,				decimals=4							)      ,				[
				        {'score': 0.99_74, 'answer': '1110212019', 'start': 23, 'end': 23},
				        {'score': 0.99_48, 'answer': 'us-001', 'start': 16, 'end': 16},
				    ]      ,				)

				_snake_case   :				Union[str, Any]														=	dqa_pipeline({'image': image, 'question': question}      ,				top_k=2							)
				self.assertEqual(
				    nested_simplify(lowerCamelCase_      ,				decimals=4							)      ,				[
				        {'score': 0.99_74, 'answer': '1110212019', 'start': 23, 'end': 23},
				        {'score': 0.99_48, 'answer': 'us-001', 'start': 16, 'end': 16},
				    ]      ,				)

				_snake_case   :				Union[str, Any]														=	dqa_pipeline(
				    [{'image': image, 'question': question}, {'image': image, 'question': question}]      ,				top_k=2							)
				self.assertEqual(
				    nested_simplify(lowerCamelCase_      ,				decimals=4							)      ,				[
				        [
				            {'score': 0.99_74, 'answer': '1110212019', 'start': 23, 'end': 23},
				            {'score': 0.99_48, 'answer': 'us-001', 'start': 16, 'end': 16},
				        ]
				    ]
				    * 2      ,				)







		@slow
		@require_torch
		@require_pytesseract
		@require_vision
		def UpperCamelCase_ (      self      :					Tuple							):


				'''simple docstring'''





				_snake_case   :				int														=	AutoTokenizer.from_pretrained(
				    'impira/layoutlm-document-qa'      ,				revision='3dc6de3'      ,				add_prefix_space=lowerCamelCase_							)
				_snake_case   :				List[str]														=	pipeline(
				    'document-question-answering'      ,				model='impira/layoutlm-document-qa'      ,				tokenizer=lowerCamelCase_      ,				revision='3dc6de3'      ,				)
				_snake_case   :				Optional[Any]														=	INVOICE_URL
				_snake_case   :				int														=	"""What is the invoice number?"""

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

				_snake_case   :				Any														=	dqa_pipeline({'image': image, 'question': question}      ,				top_k=2							)
				self.assertEqual(
				    nested_simplify(lowerCamelCase_      ,				decimals=4							)      ,				[
				        {'score': 0.42_51, 'answer': 'us-001', 'start': 16, 'end': 16},
				        {'score': 0.08_19, 'answer': '1110212019', 'start': 23, 'end': 23},
				    ]      ,				)

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

				_snake_case   :				Dict														=	list(zip(*apply_tesseract(load_image(lowerCamelCase_							)      ,				lowerCamelCase_      ,				''							)							)							)

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







		@slow
		@require_torch
		@require_pytesseract
		@require_vision
		def UpperCamelCase_ (      self      :					Tuple							):


				'''simple docstring'''





				_snake_case   :				List[str]														=	AutoTokenizer.from_pretrained(
				    'impira/layoutlm-document-qa'      ,				revision='3dc6de3'      ,				add_prefix_space=lowerCamelCase_							)
				_snake_case   :				Optional[Any]														=	pipeline(
				    'document-question-answering'      ,				model='impira/layoutlm-document-qa'      ,				tokenizer=lowerCamelCase_      ,				revision='3dc6de3'      ,				max_seq_len=50      ,				)
				_snake_case   :				int														=	INVOICE_URL
				_snake_case   :				Tuple														=	"""What is the invoice number?"""

				_snake_case   :				Dict														=	dqa_pipeline(image=lowerCamelCase_      ,				question=lowerCamelCase_      ,				top_k=2							)
				self.assertEqual(
				    nested_simplify(lowerCamelCase_      ,				decimals=4							)      ,				[
				        {'score': 0.99_99, 'answer': 'us-001', 'start': 16, 'end': 16},
				        {'score': 0.99_98, '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(lowerCamelCase_      ,				decimals=4							)      ,				[
				        [
				            {'score': 0.99_99, 'answer': 'us-001', 'start': 16, 'end': 16},
				            {'score': 0.99_98, 'answer': 'us-001', 'start': 16, 'end': 16},
				        ]
				    ]
				    * 2      ,				)

				_snake_case   :				Dict														=	list(zip(*apply_tesseract(load_image(lowerCamelCase_							)      ,				lowerCamelCase_      ,				''							)							)							)

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







		@slow
		@require_torch
		def UpperCamelCase_ (      self      :					Optional[Any]							):


				'''simple docstring'''





				_snake_case   :				Any														=	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   :				List[str]														=	INVOICE_URL
				_snake_case   :				int														=	"""What is the invoice number?"""
				_snake_case   :				Optional[Any]														=	dqa_pipeline(image=lowerCamelCase_      ,				question=lowerCamelCase_      ,				top_k=2							)
				self.assertEqual(nested_simplify(lowerCamelCase_      ,				decimals=4							)      ,				[{'answer': 'us-001'}]							)







		@require_tf
		@unittest.skip('Document question answering not implemented in TF'							)
		def UpperCamelCase_ (      self      :					Optional[Any]							):


				'''simple docstring'''





				pass











# Function to print upper half of diamond (pyramid)


def     lowerCamelCase_	(      lowerCAmelCase:	Optional[Any]				)->		List[str]:
  for i in range(0    ,							lowerCAmelCase				):
    for _ in range(0    ,							n - i - 1				):  # printing spaces
      print(' '    ,							end=''				)
    for _ in range(0    ,							i + 1				):  # printing stars
      print('* '    ,							end=''				)
    print()


def     lowerCamelCase_	(      lowerCAmelCase:	Optional[int]				)->		List[Any]:
  for i in range(lowerCAmelCase    ,							0    ,							-1				):
    for _ in range(lowerCAmelCase    ,							0    ,							-1				):  # printing stars
      print('* '    ,							end=''				)
    print()
    for _ in range(n - i + 1    ,							0    ,							-1				):  # printing spaces
      print(' '    ,							end=''				)


def     lowerCamelCase_	(      lowerCAmelCase:	Tuple				)->		int:
  if n <= 0:
    print('       ...       ....        nothing printing :('				)
    return
  floyd(lowerCAmelCase				)  # upper half
  reverse_floyd(lowerCAmelCase				)  # lower half


if __name__ == "__main__":
      print(r"""| /\ | |- |  |-  |--| |\  /| |-""")
      print(r"""|/  \| |- |_ |_  |__| | \/ | |_""")
      lowerCAmelCase_						   =    1
      while K:
            lowerCAmelCase_						   =    int(input("""enter the number and , and see the magic : """))
            print()
            pretty_print(user_number)
            lowerCAmelCase_						   =    int(input("""press 0 to exit... and 1 to continue..."""))

      print("""Good Bye...""")




