





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


       @staticmethod
       def 				SCREAMING_SNAKE_CASE    (		*_UpperCAmelCase		,      **_UpperCAmelCase):

            '''simple docstring'''






            pass






  def 			_lowerCAmelCase     (    __snake_case    :					Tuple     )	->	Union[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.
lowercase__       : List[Any]         =       (
    '''https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png'''
)


@is_pipeline_test
@require_torch
@require_vision
class 					SCREAMING_SNAKE_CASE       (unittest.TestCase				):
     lowerCAmelCase						     =			MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING


     @require_pytesseract
     @require_vision
     def 				SCREAMING_SNAKE_CASE    (		self		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase):

          '''simple docstring'''






          __A		:					Dict						=			pipeline(
              'document-question-answering'		,      model=_UpperCAmelCase		,      tokenizer=_UpperCAmelCase		,      image_processor=_UpperCAmelCase)

          __A		:					Optional[int]						=			INVOICE_URL
          __A		:					Any						=			list(zip(*apply_tesseract(load_image(_UpperCAmelCase)		,      _UpperCAmelCase		,      '')))
          __A		:					Tuple						=			'What is the placebo?'
          __A		:					List[Any]						=			[
              {
                  'image': load_image(_UpperCAmelCase),
                  'question': question,
              },
              {
                  'image': image,
                  'question': question,
              },
              {
                  'image': image,
                  'question': question,
                  'word_boxes': word_boxes,
              },
          ]
          return dqa_pipeline, examples


     def 				SCREAMING_SNAKE_CASE    (		self		,      _UpperCAmelCase		,      _UpperCAmelCase):

          '''simple docstring'''






          __A		:					str						=			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 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






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

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

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

          # We can optionnally pass directly the words and bounding boxes
          __A		:					Tuple						=			'./tests/fixtures/tests_samples/COCO/000000039769.png'
          __A		:					str						=			[]
          __A		:					str						=			[]
          __A		:					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 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






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

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

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

          __A		:					Optional[Any]						=			dqa_pipeline(
              [{'image': image, 'question': question}, {'image': image, 'question': question}]		,      top_k=2)
          self.assertEqual(
              nested_simplify(_UpperCAmelCase		,      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 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






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

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

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

          __A		:					List[str]						=			dqa_pipeline(
              [{'image': image, 'question': question}, {'image': image, 'question': question}]		,      top_k=2)
          self.assertEqual(
              nested_simplify(_UpperCAmelCase		,      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 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






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

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

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

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

          __A		:					Tuple						=			list(zip(*apply_tesseract(load_image(_UpperCAmelCase)		,      _UpperCAmelCase		,      '')))

          # This model should also work if `image` is set to None
          __A		:					Union[str, Any]						=			dqa_pipeline({'image': None, 'word_boxes': word_boxes, 'question': question}		,      top_k=2)
          self.assertEqual(
              nested_simplify(_UpperCAmelCase		,      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 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






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

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

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

          __A		:					Tuple						=			list(zip(*apply_tesseract(load_image(_UpperCAmelCase)		,      _UpperCAmelCase		,      '')))

          # This model should also work if `image` is set to None
          __A		:					Any						=			dqa_pipeline({'image': None, 'word_boxes': word_boxes, 'question': question}		,      top_k=2)
          self.assertEqual(
              nested_simplify(_UpperCAmelCase		,      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 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					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'		,      )

          __A		:					int						=			INVOICE_URL
          __A		:					Union[str, Any]						=			'What is the invoice number?'
          __A		:					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 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          pass






'''simple docstring'''




def 			_lowerCAmelCase     (    __snake_case    :					Optional[Any]     )	->	Optional[Any]:  # noqa: E741
     __A		:					Tuple						=			len(__snake_case     )
     __A		:					Optional[int]						=			0
     __A		:					str						=			[0] * n
     __A		:					int						=			[False] * n
     __A		:					Tuple						=			[False] * n

     def dfs(__snake_case    :					List[str]					,				__snake_case    :					List[Any]					,				__snake_case    :					Optional[Any]					,				__snake_case    :					int     ):
          if parent == root:
               out_edge_count += 1
          __A		:					str						=			True
          __A		:					Tuple						=			at

          for to in l[at]:
               if to == parent:
                    pass
               elif not visited[to]:
                    __A		:					Optional[int]						=			dfs(__snake_case					,				__snake_case					,				__snake_case					,				__snake_case     )
                    __A		:					int						=			min(low[at]					,				low[to]     )

                    # AP found via bridge
                    if at < low[to]:
                         __A		:					Tuple						=			True
                    # AP found via cycle
                    if at == low[to]:
                         __A		:					Optional[Any]						=			True
               else:
                    __A		:					Any						=			min(low[at]					,				__snake_case     )
          return out_edge_count

     for i in range(__snake_case     ):
          if not visited[i]:
               __A		:					Tuple						=			0
               __A		:					List[Any]						=			dfs(__snake_case					,				__snake_case					,				-1					,				__snake_case     )
               __A		:					Union[str, Any]						=			out_edge_count > 1

     for x in range(len(__snake_case     )     ):
          if is_art[x] is True:
               print(__snake_case     )


# Adjacency list of graph
lowercase__       : Tuple         =       {
    0: [1, 2],
    1: [0, 2],
    2: [0, 1, 3, 5],
    3: [2, 4],
    4: [3],
    5: [2, 6, 8],
    6: [5, 7],
    7: [6, 8],
    8: [5, 7],
}







compute_ap(data)