




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     _lowercase     (    *UpperCamelCase__     ,  **UpperCamelCase__     )     ->      List[Any]:
                  pass




       def      A      (      _SCREAMING_SNAKE_CASE    )	-> 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.
SCREAMING_SNAKE_CASE__       :      Dict  =   (
    'https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png'
)




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

    '''simple docstring'''



    lowerCamelCase_      :	Optional[Any]	     =      MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING







    @require_pytesseract
    @require_vision
    def     _lowercase     (    self     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     )     ->      str:
           lowerCamelCase   :      str			 =					pipeline(
               "document-question-answering"     ,  model=UpperCamelCase__     ,  tokenizer=UpperCamelCase__     ,  image_processor=UpperCamelCase__     )

           lowerCamelCase   :      Optional[Any]			 =					INVOICE_URL
           lowerCamelCase   :      Optional[int]			 =					list(zip(*apply_tesseract(load_image(UpperCamelCase__     )     ,  UpperCamelCase__     ,  ""     )     )     )
           lowerCamelCase   :      Optional[Any]			 =					"What is the placebo?"
           lowerCamelCase   :      int			 =					[
               {
                   "image": load_image(UpperCamelCase__     ),
                   "question": question,
               },
               {
                   "image": image,
                   "question": question,
               },
               {
                   "image": image,
                   "question": question,
                   "word_boxes": word_boxes,
               },
           ]
           return dqa_pipeline, examples







    def     _lowercase     (    self     ,  UpperCamelCase__     ,  UpperCamelCase__     )     ->      Union[str, Any]:
           lowerCamelCase   :      Dict			 =					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     _lowercase     (    self     )     ->      Union[str, Any]:
           lowerCamelCase   :      Any			 =					pipeline("document-question-answering"     ,  model="hf-internal-testing/tiny-random-layoutlmv2"     )
           lowerCamelCase   :      Union[str, Any]			 =					INVOICE_URL
           lowerCamelCase   :      Tuple			 =					"How many cats are there?"

           lowerCamelCase   :      Dict			 =					[
               {"score": 0.0001, "answer": "oy 2312/2019", "start": 38, "end": 39},
               {"score": 0.0001, "answer": "oy 2312/2019 DUE", "start": 38, "end": 40},
           ]
           lowerCamelCase   :      List[Any]			 =					dqa_pipeline(image=UpperCamelCase__     ,  question=UpperCamelCase__     ,  top_k=2     )
           self.assertEqual(nested_simplify(UpperCamelCase__     ,  decimals=4     )     ,  UpperCamelCase__     )

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

           # We can optionnally pass directly the words and bounding boxes
           lowerCamelCase   :      Dict			 =					"./tests/fixtures/tests_samples/COCO/000000039769.png"
           lowerCamelCase   :      Optional[int]			 =					[]
           lowerCamelCase   :      List[Any]			 =					[]
           lowerCamelCase   :      Optional[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     _lowercase     (    self     )     ->      Any:
           lowerCamelCase   :      Tuple			 =					pipeline(
               "document-question-answering"     ,  model="tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"     ,  revision="9977165"     ,  )
           lowerCamelCase   :      List[str]			 =					INVOICE_URL
           lowerCamelCase   :      str			 =					"What is the invoice number?"

           lowerCamelCase   :      int			 =					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},
               ]     ,  )

           lowerCamelCase   :      Tuple			 =					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},
               ]     ,  )

           lowerCamelCase   :      Tuple			 =					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     _lowercase     (    self     )     ->      Any:
           lowerCamelCase   :      Optional[int]			 =					pipeline(
               "document-question-answering"     ,  model="tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"     ,  revision="9977165"     ,  max_seq_len=50     ,  )
           lowerCamelCase   :      Dict			 =					INVOICE_URL
           lowerCamelCase   :      Optional[Any]			 =					"What is the invoice number?"

           lowerCamelCase   :      Tuple			 =					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},
               ]     ,  )

           lowerCamelCase   :      Optional[Any]			 =					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},
               ]     ,  )

           lowerCamelCase   :      int			 =					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     _lowercase     (    self     )     ->      Union[str, Any]:
           lowerCamelCase   :      str			 =					AutoTokenizer.from_pretrained(
               "impira/layoutlm-document-qa"     ,  revision="3dc6de3"     ,  add_prefix_space=UpperCamelCase__     )
           lowerCamelCase   :      Dict			 =					pipeline(
               "document-question-answering"     ,  model="impira/layoutlm-document-qa"     ,  tokenizer=UpperCamelCase__     ,  revision="3dc6de3"     ,  )
           lowerCamelCase   :      Dict			 =					INVOICE_URL
           lowerCamelCase   :      Any			 =					"What is the invoice number?"

           lowerCamelCase   :      Union[str, Any]			 =					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},
               ]     ,  )

           lowerCamelCase   :      Dict			 =					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},
               ]     ,  )

           lowerCamelCase   :      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     ,  )

           lowerCamelCase   :      int			 =					list(zip(*apply_tesseract(load_image(UpperCamelCase__     )     ,  UpperCamelCase__     ,  ""     )     )     )

           # This model should also work if `image` is set to None
           lowerCamelCase   :      List[str]			 =					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     _lowercase     (    self     )     ->      List[Any]:
           lowerCamelCase   :      Any			 =					AutoTokenizer.from_pretrained(
               "impira/layoutlm-document-qa"     ,  revision="3dc6de3"     ,  add_prefix_space=UpperCamelCase__     )
           lowerCamelCase   :      Dict			 =					pipeline(
               "document-question-answering"     ,  model="impira/layoutlm-document-qa"     ,  tokenizer=UpperCamelCase__     ,  revision="3dc6de3"     ,  max_seq_len=50     ,  )
           lowerCamelCase   :      List[str]			 =					INVOICE_URL
           lowerCamelCase   :      Any			 =					"What is the invoice number?"

           lowerCamelCase   :      str			 =					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},
               ]     ,  )

           lowerCamelCase   :      Dict			 =					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     ,  )

           lowerCamelCase   :      Tuple			 =					list(zip(*apply_tesseract(load_image(UpperCamelCase__     )     ,  UpperCamelCase__     ,  ""     )     )     )

           # This model should also work if `image` is set to None
           lowerCamelCase   :      Optional[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     _lowercase     (    self     )     ->      str:
           lowerCamelCase   :      Optional[int]			 =					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   :      str			 =					INVOICE_URL
           lowerCamelCase   :      Any			 =					"What is the invoice number?"
           lowerCamelCase   :      Tuple			 =					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     _lowercase     (    self     )     ->      Tuple:
           pass













import unittest

from huggingface_hub import hf_hub_download

from transformers import MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, VideoMAEFeatureExtractor
from transformers.pipelines import VideoClassificationPipeline, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_decord,
    require_tf,
    require_torch,
    require_torch_or_tf,
    require_vision,
)

from .test_pipelines_common import ANY




@is_pipeline_test
@require_torch_or_tf
@require_vision
@require_decord
class     SCREAMING_SNAKE_CASE__ (						unittest.TestCase      ):
 __SCREAMING_SNAKE_CASE							       =	MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING



 def 				UpperCamelCase			(							self,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase ):
        A__											=						hf_hub_download(
            repo_id='''nateraw/video-demo''',filename='''archery.mp4''',repo_type='''dataset''' )
        A__											=						VideoClassificationPipeline(model=__lowerCamelCase,image_processor=__lowerCamelCase,top_k=2 )
        A__											=						[
            example_video_filepath,
            '''https://huggingface.co/datasets/nateraw/video-demo/resolve/main/archery.mp4''',
        ]
        return video_classifier, examples



 def 				UpperCamelCase			(							self,__lowerCamelCase,__lowerCamelCase ):
        for example in examples:
               A__											=						video_classifier(__lowerCamelCase )

               self.assertEqual(
                   __lowerCamelCase,[
                       {'''score''': ANY(__lowerCamelCase ), '''label''': ANY(__lowerCamelCase )},
                       {'''score''': ANY(__lowerCamelCase ), '''label''': ANY(__lowerCamelCase )},
                   ],)



 @require_torch
 def 				UpperCamelCase			(							self ):
        A__											=						'''hf-internal-testing/tiny-random-VideoMAEForVideoClassification'''
        A__											=						VideoMAEFeatureExtractor(
            size={'''shortest_edge''': 10},crop_size={'''height''': 10, '''width''': 10} )
        A__											=						pipeline(
            '''video-classification''',model=__lowerCamelCase,feature_extractor=__lowerCamelCase,frame_sampling_rate=4 )

        A__											=						hf_hub_download(repo_id='''nateraw/video-demo''',filename='''archery.mp4''',repo_type='''dataset''' )
        A__											=						video_classifier(__lowerCamelCase,top_k=2 )
        self.assertEqual(
            nested_simplify(__lowerCamelCase,decimals=4 ),[{'''score''': 0.5199, '''label''': '''LABEL_0'''}, {'''score''': 0.4801, '''label''': '''LABEL_1'''}],)

        A__											=						video_classifier(
            [
                video_file_path,
                video_file_path,
            ],top_k=2,)
        self.assertEqual(
            nested_simplify(__lowerCamelCase,decimals=4 ),[
                [{'''score''': 0.5199, '''label''': '''LABEL_0'''}, {'''score''': 0.4801, '''label''': '''LABEL_1'''}],
                [{'''score''': 0.5199, '''label''': '''LABEL_0'''}, {'''score''': 0.4801, '''label''': '''LABEL_1'''}],
            ],)




 @require_tf
 def 				UpperCamelCase			(							self ):
        pass





