




"""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   a__			:





										@staticmethod
										def       __UpperCamelCase     (							*a__      :	Dict     ,**a__      :	int)						->       Optional[Any]:




															"""simple docstring"""







															pass





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




@is_pipeline_test
@require_torch
@require_vision
class   a__			(       unittest.TestCase  ):
						snake_case__             = MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING





						@require_pytesseract
						@require_vision
						def       __UpperCamelCase     (							self      :	Any     ,a__      :	Any     ,a__      :	List[Any]     ,a__      :	List[str])						->       Dict:




											"""simple docstring"""







											_lowerCAmelCase:Optional[int]									=  pipeline(
											    '''document-question-answering'''     ,model=a__     ,tokenizer=a__     ,image_processor=a__)

											_lowerCAmelCase:Tuple									=  INVOICE_URL
											_lowerCAmelCase:str									=  list(zip(*apply_tesseract(load_image(a__)     ,a__     ,'''''')))
											_lowerCAmelCase:str									=  '''What is the placebo?'''
											_lowerCAmelCase:Tuple									=  [
											    {
											        '''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      :	Dict     ,a__      :	List[str]     ,a__      :	Optional[int])						->       Dict:




											"""simple docstring"""







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




											"""simple docstring"""







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

											_lowerCAmelCase:Optional[int]									=  [
											    {'''score''': 0.0001, '''answer''': '''oy 2312/2019''', '''start''': 38, '''end''': 39},
											    {'''score''': 0.0001, '''answer''': '''oy 2312/2019 DUE''', '''start''': 38, '''end''': 40},
											]
											_lowerCAmelCase:Any									=  dqa_pipeline(image=a__     ,question=a__     ,top_k=2)
											self.assertEqual(nested_simplify(a__     ,decimals=4)     ,a__)

											_lowerCAmelCase:List[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:Any									=  '''./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:Tuple									=  '''./tests/fixtures/tests_samples/COCO/000000039769.png'''
											_lowerCAmelCase:Union[str, Any]									=  []
											_lowerCAmelCase:Any									=  []
											_lowerCAmelCase:Optional[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      :	Optional[Any])						->       Union[str, Any]:




											"""simple docstring"""







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

											_lowerCAmelCase:Optional[int]									=  dqa_pipeline(image=a__     ,question=a__     ,top_k=2)
											self.assertEqual(
											    nested_simplify(a__     ,decimals=4)     ,[
											        {'''score''': 0.9944, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
											        {'''score''': 0.0009, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
											    ]     ,)

											_lowerCAmelCase:Any									=  dqa_pipeline({'''image''': image, '''question''': question}     ,top_k=2)
											self.assertEqual(
											    nested_simplify(a__     ,decimals=4)     ,[
											        {'''score''': 0.9944, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
											        {'''score''': 0.0009, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
											    ]     ,)

											_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.9944, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
											            {'''score''': 0.0009, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
											        ],
											    ]
											    * 2     ,)





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




											"""simple docstring"""







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

											_lowerCAmelCase:Optional[int]									=  dqa_pipeline(image=a__     ,question=a__     ,top_k=2)
											self.assertEqual(
											    nested_simplify(a__     ,decimals=4)     ,[
											        {'''score''': 0.9974, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
											        {'''score''': 0.9948, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
											    ]     ,)

											_lowerCAmelCase:Dict									=  dqa_pipeline({'''image''': image, '''question''': question}     ,top_k=2)
											self.assertEqual(
											    nested_simplify(a__     ,decimals=4)     ,[
											        {'''score''': 0.9974, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
											        {'''score''': 0.9948, '''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(a__     ,decimals=4)     ,[
											        [
											            {'''score''': 0.9974, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
											            {'''score''': 0.9948, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
											        ]
											    ]
											    * 2     ,)





						@slow
						@require_torch
						@require_pytesseract
						@require_vision
						def       __UpperCamelCase     (							self      :	Any)						->       Tuple:




											"""simple docstring"""







											_lowerCAmelCase:List[str]									=  AutoTokenizer.from_pretrained(
											    '''impira/layoutlm-document-qa'''     ,revision='''3dc6de3'''     ,add_prefix_space=a__)
											_lowerCAmelCase:Tuple									=  pipeline(
											    '''document-question-answering'''     ,model='''impira/layoutlm-document-qa'''     ,tokenizer=a__     ,revision='''3dc6de3'''     ,)
											_lowerCAmelCase:str									=  INVOICE_URL
											_lowerCAmelCase:Dict									=  '''What is the invoice number?'''

											_lowerCAmelCase:Optional[int]									=  dqa_pipeline(image=a__     ,question=a__     ,top_k=2)
											self.assertEqual(
											    nested_simplify(a__     ,decimals=4)     ,[
											        {'''score''': 0.4251, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
											        {'''score''': 0.0819, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
											    ]     ,)

											_lowerCAmelCase:Dict									=  dqa_pipeline({'''image''': image, '''question''': question}     ,top_k=2)
											self.assertEqual(
											    nested_simplify(a__     ,decimals=4)     ,[
											        {'''score''': 0.4251, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
											        {'''score''': 0.0819, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
											    ]     ,)

											_lowerCAmelCase:Any									=  dqa_pipeline(
											    [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]     ,top_k=2)
											self.assertEqual(
											    nested_simplify(a__     ,decimals=4)     ,[
											        [
											            {'''score''': 0.4251, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
											            {'''score''': 0.0819, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
											        ]
											    ]
											    * 2     ,)

											_lowerCAmelCase:List[str]									=  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.4251, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
											        {'''score''': 0.0819, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23},
											    ]     ,)





						@slow
						@require_torch
						@require_pytesseract
						@require_vision
						def       __UpperCamelCase     (							self      :	List[Any])						->       str:




											"""simple docstring"""







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

											_lowerCAmelCase:Optional[int]									=  dqa_pipeline(image=a__     ,question=a__     ,top_k=2)
											self.assertEqual(
											    nested_simplify(a__     ,decimals=4)     ,[
											        {'''score''': 0.9999, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
											        {'''score''': 0.9998, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
											    ]     ,)

											_lowerCAmelCase:Optional[Any]									=  dqa_pipeline(
											    [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]     ,top_k=2)
											self.assertEqual(
											    nested_simplify(a__     ,decimals=4)     ,[
											        [
											            {'''score''': 0.9999, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
											            {'''score''': 0.9998, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
											        ]
											    ]
											    * 2     ,)

											_lowerCAmelCase:str									=  list(zip(*apply_tesseract(load_image(a__)     ,a__     ,'''''')))

											# This model should also work if `image` is set to None
											_lowerCAmelCase:str									=  dqa_pipeline({'''image''': None, '''word_boxes''': word_boxes, '''question''': question}     ,top_k=2)
											self.assertEqual(
											    nested_simplify(a__     ,decimals=4)     ,[
											        {'''score''': 0.9999, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
											        {'''score''': 0.9998, '''answer''': '''us-001''', '''start''': 16, '''end''': 16},
											    ]     ,)





						@slow
						@require_torch
						def       __UpperCamelCase     (							self      :	Optional[Any])						->       Tuple:




											"""simple docstring"""







											_lowerCAmelCase:Optional[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'''     ,)

											_lowerCAmelCase:List[str]									=  INVOICE_URL
											_lowerCAmelCase:Union[str, Any]									=  '''What is the invoice number?'''
											_lowerCAmelCase:Dict									=  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      :	Any)						->       str:




											"""simple docstring"""







											pass












"""simple docstring"""






def 							UpperCAmelCase		(		snake_case			:					str      ,       snake_case			:					str    ):
					assert x is not None
					assert y is not None

					_lowerCAmelCase:Optional[Any]									=  len(snake_case    )
					_lowerCAmelCase:Tuple									=  len(snake_case    )

					# declaring the array for storing the dp values
					_lowerCAmelCase:Optional[Any]									=  [[0] * (n + 1) for _ in range(m + 1    )]  # noqa: E741

					for i in range(1      ,       m + 1    ):
										for j in range(1      ,       n + 1    ):
															_lowerCAmelCase:Optional[int]									=  1 if x[i - 1] == y[j - 1] else 0

															_lowerCAmelCase:int									=  max(l[i - 1][j]      ,       l[i][j - 1]      ,       l[i - 1][j - 1] + match    )

					_lowerCAmelCase:List[Any]									=  ''''''
					_lowerCAmelCase							,   _lowerCAmelCase:Dict									=  m, n
					while i > 0 and j > 0:
										_lowerCAmelCase:Any									=  1 if x[i - 1] == y[j - 1] else 0

										if l[i][j] == l[i - 1][j - 1] + match:
															if match == 1:
																				_lowerCAmelCase:Any									=  x[i - 1] + seq
															i -= 1
															j -= 1
										elif l[i][j] == l[i - 1][j]:
															i -= 1
										else:
															j -= 1

					return l[m][n], seq


if __name__ == "__main__":
				UpperCamelCase__						=			'''AGGTAB'''
				UpperCamelCase__						=			'''GXTXAYB'''
				UpperCamelCase__						=			4
				UpperCamelCase__						=			'''GTAB'''

				UpperCamelCase__						,							UpperCamelCase__						=			longest_common_subsequence(a, b)
				print('''len =''', ln, ''', sub-sequence =''', subseq)
				import doctest

				doctest.testmod()






