



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 		lowercase							:

       @staticmethod
       def       a__			(						*_a	,  **_a       )		->							Any:
             pass







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







@is_pipeline_test
@require_torch
@require_vision
class 		lowercase							(     unittest.TestCase	):
     _a											=		MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING

     @require_pytesseract
     @require_vision
     def       a__			(						self	,  _a	,  _a	,  _a       )		->							Union[str, Any]:
           _A							:  str    =		pipeline(
               """document-question-answering"""	,  model=lowercase_	,  tokenizer=lowercase_	,  image_processor=lowercase_       )

           _A							:  Tuple    =		INVOICE_URL
           _A							:  List[Any]    =		list(zip(*apply_tesseract(load_image(lowercase_       )	,  lowercase_	,  """"""       )       )       )
           _A							:  Union[str, Any]    =		"What is the placebo?"
           _A							:  int    =		[
               {
                   "image": load_image(lowercase_       ),
                   "question": question,
               },
               {
                   "image": image,
                   "question": question,
               },
               {
                   "image": image,
                   "question": question,
                   "word_boxes": word_boxes,
               },
           ]
           return dqa_pipeline, examples

     def       a__			(						self	,  _a	,  _a       )		->							Optional[int]:
           _A							:  Any    =		dqa_pipeline(lowercase_	,  top_k=2       )
           self.assertEqual(
               lowercase_	,  [
                   [
                       {"""score""": ANY(lowercase_       ), """answer""": ANY(lowercase_       ), """start""": ANY(lowercase_       ), """end""": ANY(lowercase_       )},
                       {"""score""": ANY(lowercase_       ), """answer""": ANY(lowercase_       ), """start""": ANY(lowercase_       ), """end""": ANY(lowercase_       )},
                   ]
               ]
               * 3	,  )

     @require_torch
     @require_detectrona
     @require_pytesseract
     def       a__			(						self       )		->							Optional[int]:
           _A							:  Tuple    =		pipeline("""document-question-answering"""	,  model="""hf-internal-testing/tiny-random-layoutlmv2"""       )
           _A							:  Optional[int]    =		INVOICE_URL
           _A							:  Union[str, Any]    =		"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							:  Union[str, Any]    =		dqa_pipeline(image=lowercase_	,  question=lowercase_	,  top_k=2       )
           self.assertEqual(nested_simplify(lowercase_	,  decimals=4       )	,  lowercase_       )

           _A							:  Tuple    =		dqa_pipeline({"""image""": image, """question""": question}	,  top_k=2       )
           self.assertEqual(nested_simplify(lowercase_	,  decimals=4       )	,  lowercase_       )

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

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

     @slow
     @require_torch
     @require_detectrona
     @require_pytesseract
     def       a__			(						self       )		->							Optional[Any]:
           _A							:  str    =		pipeline(
               """document-question-answering"""	,  model="""tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"""	,  revision="""9977165"""	,  )
           _A							:  str    =		INVOICE_URL
           _A							:  List[str]    =		"What is the invoice number?"

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

           _A							:  Optional[int]    =		dqa_pipeline({"""image""": image, """question""": question}	,  top_k=2       )
           self.assertEqual(
               nested_simplify(lowercase_	,  decimals=4       )	,  [
                   {"""score""": 0.9944, """answer""": """us-001""", """start""": 16, """end""": 16},
                   {"""score""": 0.0009, """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(lowercase_	,  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       a__			(						self       )		->							Dict:
           _A							:  Tuple    =		pipeline(
               """document-question-answering"""	,  model="""tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"""	,  revision="""9977165"""	,  max_seq_len=50	,  )
           _A							:  Optional[Any]    =		INVOICE_URL
           _A							:  str    =		"What is the invoice number?"

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

           _A							:  Dict    =		dqa_pipeline(
               [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]	,  top_k=2       )
           self.assertEqual(
               nested_simplify(lowercase_	,  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       a__			(						self       )		->							str:
           _A							:  Optional[int]    =		AutoTokenizer.from_pretrained(
               """impira/layoutlm-document-qa"""	,  revision="""3dc6de3"""	,  add_prefix_space=lowercase_       )
           _A							:  Optional[int]    =		pipeline(
               """document-question-answering"""	,  model="""impira/layoutlm-document-qa"""	,  tokenizer=lowercase_	,  revision="""3dc6de3"""	,  )
           _A							:  Optional[Any]    =		INVOICE_URL
           _A							:  Tuple    =		"What is the invoice number?"

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

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

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

           _A							:  int    =		list(zip(*apply_tesseract(load_image(lowercase_       )	,  lowercase_	,  """"""       )       )       )

           # This model should also work if `image` is set to None
           _A							:  Optional[int]    =		dqa_pipeline({"""image""": None, """word_boxes""": word_boxes, """question""": question}	,  top_k=2       )
           self.assertEqual(
               nested_simplify(lowercase_	,  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       a__			(						self       )		->							List[str]:
           _A							:  Dict    =		AutoTokenizer.from_pretrained(
               """impira/layoutlm-document-qa"""	,  revision="""3dc6de3"""	,  add_prefix_space=lowercase_       )
           _A							:  str    =		pipeline(
               """document-question-answering"""	,  model="""impira/layoutlm-document-qa"""	,  tokenizer=lowercase_	,  revision="""3dc6de3"""	,  max_seq_len=50	,  )
           _A							:  Union[str, Any]    =		INVOICE_URL
           _A							:  Optional[int]    =		"What is the invoice number?"

           _A							:  str    =		dqa_pipeline(image=lowercase_	,  question=lowercase_	,  top_k=2       )
           self.assertEqual(
               nested_simplify(lowercase_	,  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(lowercase_	,  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(lowercase_       )	,  lowercase_	,  """"""       )       )       )

           # 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(lowercase_	,  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       a__			(						self       )		->							str:
           _A							:  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"""	,  )

           _A							:  Tuple    =		INVOICE_URL
           _A							:  Any    =		"What is the invoice number?"
           _A							:  int    =		dqa_pipeline(image=lowercase_	,  question=lowercase_	,  top_k=2       )
           self.assertEqual(nested_simplify(lowercase_	,  decimals=4       )	,  [{"""answer""": """us-001"""}]       )



     @require_tf
     @unittest.skip("""Document question answering not implemented in TF"""       )
     def       a__			(						self       )		->							List[Any]:
           pass







from datetime import datetime as dt
import os

from github import Github


_snake_case            = [
    "good first issue",
    "good second issue",
    "good difficult issue",
    "feature request",
    "new model",
    "wip",
]







def        lowerCAmelCase_			(  ):
      _A							:  int    =		Github(os.environ["""GITHUB_TOKEN"""]  )
      _A							:  Tuple    =		g.get_repo("""huggingface/transformers"""  )
      _A							:  Dict    =		repo.get_issues(state="""open"""  )

      for issue in open_issues:
            _A							:  Optional[Any]    =		sorted([comment for comment in issue.get_comments()],key=lambda snake_case_  : i.created_at,reverse=snake_case_  )
            _A							:  Dict    =		comments[0] if len(snake_case_  ) > 0 else None
            if (
                last_comment is not None
                and last_comment.user.login == "github-actions[bot]"
                and (dt.utcnow() - issue.updated_at).days > 7
                and (dt.utcnow() - issue.created_at).days >= 30
                and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels()  )
            ):
                  # print(f"Would close issue {issue.number} since it has been 7 days of inactivity since bot mention.")
                  issue.edit(state="""closed"""  )
            elif (
                (dt.utcnow() - issue.updated_at).days > 23
                and (dt.utcnow() - issue.created_at).days >= 30
                and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels()  )
            ):
                  # print(f"Would add stale comment to {issue.number}")
                  issue.create_comment(
                      """This issue has been automatically marked as stale because it has not had """
                      """recent activity. If you think this still needs to be addressed """
                      """please comment on this thread.\n\nPlease note that issues that do not follow the """
                      """[contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) """
                      """are likely to be ignored."""  )


if __name__ == "__main__":
  main()

