



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


									'''simple docstring'''

									@staticmethod
									def UpperCamelCase__					(							*lowerCAmelCase__     : List[str]							,     **lowerCAmelCase__     : List[str]						):





											"""simple docstring"""






											pass


						def 				lowerCAmelCase_   (	_lowerCamelCase:   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.
UpperCamelCase__       :	Optional[int]    = (
    '''https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png'''
)






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


			'''simple docstring'''

			_A      :		Dict			    =		MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING

			@require_pytesseract
			@require_vision
			def UpperCamelCase__					(							self     : Union[str, Any]							,     lowerCAmelCase__     : Any							,     lowerCAmelCase__     : Tuple							,     lowerCAmelCase__     : Union[str, Any]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		List[Any]           =		pipeline(
					    """document-question-answering"""							,     model=lowerCAmelCase__							,     tokenizer=lowerCAmelCase__							,     image_processor=lowerCAmelCase__						)

					__SCREAMING_SNAKE_CASE       :		List[Any]           =		INVOICE_URL
					__SCREAMING_SNAKE_CASE       :		Optional[Any]           =		list(zip(*apply_tesseract(load_image(lowerCAmelCase__						)							,     lowerCAmelCase__							,     """"""						)						)						)
					__SCREAMING_SNAKE_CASE       :		Any           =		"""What is the placebo?"""
					__SCREAMING_SNAKE_CASE       :		Optional[int]           =		[
					    {
					        """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     : str							,     lowerCAmelCase__     : Any							,     lowerCAmelCase__     : List[str]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Dict           =		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"""






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

					__SCREAMING_SNAKE_CASE       :		Dict           =		[
					    {"""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},
					]
					__SCREAMING_SNAKE_CASE       :		Tuple           =		dqa_pipeline(image=lowerCAmelCase__							,     question=lowerCAmelCase__							,     top_k=2						)
					self.assertEqual(nested_simplify(lowerCAmelCase__							,     decimals=4						)							,     lowerCAmelCase__						)

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

					# We can optionnally pass directly the words and bounding boxes
					__SCREAMING_SNAKE_CASE       :		int           =		"""./tests/fixtures/tests_samples/COCO/000000039769.png"""
					__SCREAMING_SNAKE_CASE       :		Any           =		[]
					__SCREAMING_SNAKE_CASE       :		str           =		[]
					__SCREAMING_SNAKE_CASE       :		List[str]           =		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"""






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

					__SCREAMING_SNAKE_CASE       :		Optional[Any]           =		dqa_pipeline(image=lowerCAmelCase__							,     question=lowerCAmelCase__							,     top_k=2						)
					self.assertEqual(
					    nested_simplify(lowerCAmelCase__							,     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},
					    ]							,     )

					__SCREAMING_SNAKE_CASE       :		Optional[int]           =		dqa_pipeline({"""image""": image, """question""": question}							,     top_k=2						)
					self.assertEqual(
					    nested_simplify(lowerCAmelCase__							,     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},
					    ]							,     )

					__SCREAMING_SNAKE_CASE       :		List[str]           =		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""": 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     : Optional[Any]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Optional[Any]           =		pipeline(
					    """document-question-answering"""							,     model="""tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"""							,     revision="""9977165"""							,     max_seq_len=5_0							,     )
					__SCREAMING_SNAKE_CASE       :		Optional[int]           =		INVOICE_URL
					__SCREAMING_SNAKE_CASE       :		Tuple           =		"""What is the invoice number?"""

					__SCREAMING_SNAKE_CASE       :		Dict           =		dqa_pipeline(image=lowerCAmelCase__							,     question=lowerCAmelCase__							,     top_k=2						)
					self.assertEqual(
					    nested_simplify(lowerCAmelCase__							,     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},
					    ]							,     )

					__SCREAMING_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""": 2_3, """end""": 2_3},
					        {"""score""": 0.99_48, """answer""": """us-001""", """start""": 1_6, """end""": 1_6},
					    ]							,     )

					__SCREAMING_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_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     : Optional[Any]						):





					"""simple docstring"""






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

					__SCREAMING_SNAKE_CASE       :		Optional[Any]           =		dqa_pipeline(image=lowerCAmelCase__							,     question=lowerCAmelCase__							,     top_k=2						)
					self.assertEqual(
					    nested_simplify(lowerCAmelCase__							,     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},
					    ]							,     )

					__SCREAMING_SNAKE_CASE       :		Tuple           =		dqa_pipeline({"""image""": image, """question""": question}							,     top_k=2						)
					self.assertEqual(
					    nested_simplify(lowerCAmelCase__							,     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},
					    ]							,     )

					__SCREAMING_SNAKE_CASE       :		Tuple           =		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""": 1_6, """end""": 1_6},
					            {"""score""": 0.08_19, """answer""": """1110212019""", """start""": 2_3, """end""": 2_3},
					        ]
					    ]
					    * 2							,     )

					__SCREAMING_SNAKE_CASE       :		Optional[int]           =		list(zip(*apply_tesseract(load_image(lowerCAmelCase__						)							,     lowerCAmelCase__							,     """"""						)						)						)

					# This model should also work if `image` is set to None
					__SCREAMING_SNAKE_CASE       :		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""": 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     : str						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		List[str]           =		AutoTokenizer.from_pretrained(
					    """impira/layoutlm-document-qa"""							,     revision="""3dc6de3"""							,     add_prefix_space=lowerCAmelCase__						)
					__SCREAMING_SNAKE_CASE       :		Dict           =		pipeline(
					    """document-question-answering"""							,     model="""impira/layoutlm-document-qa"""							,     tokenizer=lowerCAmelCase__							,     revision="""3dc6de3"""							,     max_seq_len=5_0							,     )
					__SCREAMING_SNAKE_CASE       :		Tuple           =		INVOICE_URL
					__SCREAMING_SNAKE_CASE       :		Any           =		"""What is the invoice number?"""

					__SCREAMING_SNAKE_CASE       :		Any           =		dqa_pipeline(image=lowerCAmelCase__							,     question=lowerCAmelCase__							,     top_k=2						)
					self.assertEqual(
					    nested_simplify(lowerCAmelCase__							,     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},
					    ]							,     )

					__SCREAMING_SNAKE_CASE       :		Tuple           =		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""": 1_6, """end""": 1_6},
					            {"""score""": 0.99_98, """answer""": """us-001""", """start""": 1_6, """end""": 1_6},
					        ]
					    ]
					    * 2							,     )

					__SCREAMING_SNAKE_CASE       :		List[Any]           =		list(zip(*apply_tesseract(load_image(lowerCAmelCase__						)							,     lowerCAmelCase__							,     """"""						)						)						)

					# This model should also work if `image` is set to None
					__SCREAMING_SNAKE_CASE       :		Optional[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""": 1_6, """end""": 1_6},
					        {"""score""": 0.99_98, """answer""": """us-001""", """start""": 1_6, """end""": 1_6},
					    ]							,     )

			@slow
			@require_torch
			def UpperCamelCase__					(							self     : int						):





					"""simple docstring"""






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





					"""simple docstring"""






					pass




'''simple docstring'''




def 				lowerCAmelCase_   (	_lowerCamelCase:   int					):
		__SCREAMING_SNAKE_CASE       :		str           =		int(_lowerCamelCase					)
		if n_element < 1:
				__SCREAMING_SNAKE_CASE       :		List[str]           =		ValueError("""a should be a positive number"""					)
				raise my_error

		__SCREAMING_SNAKE_CASE       :		List[Any]           =		[1]
		__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE       :		Optional[int]           =		(0, 0, 0)
		__SCREAMING_SNAKE_CASE       :		List[str]           =		1
		while index < n_element:
				while hamming_list[i] * 2 <= hamming_list[-1]:
						i += 1
				while hamming_list[j] * 3 <= hamming_list[-1]:
						j += 1
				while hamming_list[k] * 5 <= hamming_list[-1]:
						k += 1
				hamming_list.append(
				    min(hamming_list[i] * 2       ,      hamming_list[j] * 3       ,      hamming_list[k] * 5					)					)
				index += 1
		return hamming_list


if __name__ == "__main__":
						UpperCamelCase__       :	Optional[Any]    = input('''Enter the last number (nth term) of the Hamming Number Series: ''')
						print('''Formula of Hamming Number Series => 2^i * 3^j * 5^k''')
						UpperCamelCase__       :	List[str]    = hamming(int(n))
						print('''-----------------------------------------------------''')
						print(f"The list with nth numbers is: {hamming_numbers}")
						print('''-----------------------------------------------------''')