


import unittest

from transformers import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING, is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
   from PIL import Image
else:






   class      UpperCamelCase					:


      '''simple docstring'''







      @staticmethod
      def 		UpperCamelCase  (     *UpperCamelCase_   ,							**UpperCamelCase_		):
           pass






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


   '''simple docstring'''







   lowercase					:	Union[str, Any]									=MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING
   def 		UpperCamelCase  (     self   ,							UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_		):
        lowercase_       :int     =       pipeline('''visual-question-answering'''   ,							model='''hf-internal-testing/tiny-vilt-random-vqa'''		)
        lowercase_       :Optional[Any]     =       [
            {
                '''image''': Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		),
                '''question''': '''How many cats are there?''',
            },
            {
                '''image''': '''./tests/fixtures/tests_samples/COCO/000000039769.png''',
                '''question''': '''How many cats are there?''',
            },
        ]
        return vqa_pipeline, examples
   def 		UpperCamelCase  (     self   ,							UpperCamelCase_   ,							UpperCamelCase_		):
        lowercase_       :Tuple     =       vqa_pipeline(UpperCamelCase_   ,							top_k=1		)
        self.assertEqual(
            UpperCamelCase_   ,							[
                [{'''score''': ANY(UpperCamelCase_		), '''answer''': ANY(UpperCamelCase_		)}],
                [{'''score''': ANY(UpperCamelCase_		), '''answer''': ANY(UpperCamelCase_		)}],
            ]   ,							)
   @require_torch
   def 		UpperCamelCase  (     self		):
        lowercase_       :Optional[Any]     =       pipeline('''visual-question-answering'''   ,							model='''hf-internal-testing/tiny-vilt-random-vqa'''		)
        lowercase_       :Optional[Any]     =       '''./tests/fixtures/tests_samples/COCO/000000039769.png'''
        lowercase_       :Union[str, Any]     =       '''How many cats are there?'''

        lowercase_       :Optional[int]     =       vqa_pipeline(image=UpperCamelCase_   ,							question='''How many cats are there?'''   ,							top_k=2		)
        self.assertEqual(
            UpperCamelCase_   ,							[{'''score''': ANY(UpperCamelCase_		), '''answer''': ANY(UpperCamelCase_		)}, {'''score''': ANY(UpperCamelCase_		), '''answer''': ANY(UpperCamelCase_		)}]		)

        lowercase_       :List[Any]     =       vqa_pipeline({'''image''': image, '''question''': question}   ,							top_k=2		)
        self.assertEqual(
            UpperCamelCase_   ,							[{'''score''': ANY(UpperCamelCase_		), '''answer''': ANY(UpperCamelCase_		)}, {'''score''': ANY(UpperCamelCase_		), '''answer''': ANY(UpperCamelCase_		)}]		)
   @slow
   @require_torch
   def 		UpperCamelCase  (     self		):
        lowercase_       :Any     =       pipeline('''visual-question-answering'''   ,							model='''dandelin/vilt-b32-finetuned-vqa'''		)
        lowercase_       :List[str]     =       '''./tests/fixtures/tests_samples/COCO/000000039769.png'''
        lowercase_       :List[str]     =       '''How many cats are there?'''

        lowercase_       :Any     =       vqa_pipeline(image=UpperCamelCase_   ,							question=UpperCamelCase_   ,							top_k=2		)
        self.assertEqual(
            nested_simplify(UpperCamelCase_   ,							decimals=4		)   ,							[{'''score''': 0.8799, '''answer''': '''2'''}, {'''score''': 0.296, '''answer''': '''1'''}]		)

        lowercase_       :Any     =       vqa_pipeline({'''image''': image, '''question''': question}   ,							top_k=2		)
        self.assertEqual(
            nested_simplify(UpperCamelCase_   ,							decimals=4		)   ,							[{'''score''': 0.8799, '''answer''': '''2'''}, {'''score''': 0.296, '''answer''': '''1'''}]		)

        lowercase_       :Dict     =       vqa_pipeline(
            [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]   ,							top_k=2		)
        self.assertEqual(
            nested_simplify(UpperCamelCase_   ,							decimals=4		)   ,							[[{'''score''': 0.8799, '''answer''': '''2'''}, {'''score''': 0.296, '''answer''': '''1'''}]] * 2   ,							)




   @require_tf
   @unittest.skip('''Visual question answering not implemented in TF'''		)
   def 		UpperCamelCase  (     self		):
        pass





from __future__ import annotations




def     UpperCamelCase    (      _a       ,		_a = None       ,		_a = None		)					->							None:
     '''simple docstring'''





     if start is None:
          lowercase_       :str     =       0

     if end is None:
          lowercase_       :str     =       len(_a		) - 1

     if start >= end:
          return

     lowercase_       :Dict     =       (start + end) // 2

     slowsort(_a       ,		_a       ,		_a		)
     slowsort(_a       ,		mid + 1       ,		_a		)

     if sequence[end] < sequence[mid]:
          lowercase_    ,      lowercase_       :List[Any]     =       sequence[mid], sequence[end]

     slowsort(_a       ,		_a       ,		end - 1		)


if __name__ == "__main__":
   from doctest import testmod

   testmod()

