



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




									'''simple docstring'''



									@staticmethod
									def 		lowerCAmelCase__				(*__lowerCamelCase		,**__lowerCamelCase			)					->      int:

													"""simple docstring"""






													pass



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




		'''simple docstring'''



		snake_case_     =MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING



		def 		lowerCAmelCase__				(self		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase			)					->      Any:

						"""simple docstring"""






						lowerCAmelCase__					:    Any												=					pipeline('''visual-question-answering'''		,model='''hf-internal-testing/tiny-vilt-random-vqa'''			)
						lowerCAmelCase__					:    Dict												=					[
						    {
						        '''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 		lowerCAmelCase__				(self		,__lowerCamelCase		,__lowerCamelCase			)					->      Union[str, Any]:

						"""simple docstring"""






						lowerCAmelCase__					:    Optional[int]												=					vqa_pipeline(__lowerCamelCase		,top_k=1			)
						self.assertEqual(
						    __lowerCamelCase		,[
						        [{'''score''': ANY(__lowerCamelCase			), '''answer''': ANY(__lowerCamelCase			)}],
						        [{'''score''': ANY(__lowerCamelCase			), '''answer''': ANY(__lowerCamelCase			)}],
						    ]		,)



		@require_torch
		def 		lowerCAmelCase__				(self			)					->      Optional[int]:

						"""simple docstring"""






						lowerCAmelCase__					:    Dict												=					pipeline('''visual-question-answering'''		,model='''hf-internal-testing/tiny-vilt-random-vqa'''			)
						lowerCAmelCase__					:    str												=					'''./tests/fixtures/tests_samples/COCO/000000039769.png'''
						lowerCAmelCase__					:    int												=					'''How many cats are there?'''

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

						lowerCAmelCase__					:    Union[str, Any]												=					vqa_pipeline({'''image''': image, '''question''': question}		,top_k=2			)
						self.assertEqual(
						    __lowerCamelCase		,[{'''score''': ANY(__lowerCamelCase			), '''answer''': ANY(__lowerCamelCase			)}, {'''score''': ANY(__lowerCamelCase			), '''answer''': ANY(__lowerCamelCase			)}]			)



		@slow
		@require_torch
		def 		lowerCAmelCase__				(self			)					->      Union[str, Any]:

						"""simple docstring"""






						lowerCAmelCase__					:    Dict												=					pipeline('''visual-question-answering'''		,model='''dandelin/vilt-b32-finetuned-vqa'''			)
						lowerCAmelCase__					:    Tuple												=					'''./tests/fixtures/tests_samples/COCO/000000039769.png'''
						lowerCAmelCase__					:    int												=					'''How many cats are there?'''

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

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

						lowerCAmelCase__					:    Optional[int]												=					vqa_pipeline(
						    [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]		,top_k=2			)
						self.assertEqual(
						    nested_simplify(__lowerCamelCase		,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 		lowerCAmelCase__				(self			)					->      Optional[Any]:

						"""simple docstring"""






						pass






from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_torch_available,
)


__snake_case    :		Optional[int]  ={
    'configuration_vision_text_dual_encoder': ['VisionTextDualEncoderConfig'],
    'processing_vision_text_dual_encoder': ['VisionTextDualEncoderProcessor'],
}


try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__snake_case    :		int  =['VisionTextDualEncoderModel']


try:
							if not is_flax_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__snake_case    :		List[str]  =['FlaxVisionTextDualEncoderModel']

try:
							if not is_tf_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__snake_case    :		Tuple  =['TFVisionTextDualEncoderModel']


if TYPE_CHECKING:
							from .configuration_vision_text_dual_encoder import VisionTextDualEncoderConfig
							from .processing_vision_text_dual_encoder import VisionTextDualEncoderProcessor

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_vision_text_dual_encoder import VisionTextDualEncoderModel

							try:
														if not is_flax_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_flax_vision_text_dual_encoder import FlaxVisionTextDualEncoderModel

							try:
														if not is_tf_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_tf_vision_text_dual_encoder import TFVisionTextDualEncoderModel


else:
							import sys

							__snake_case    :		str  =_LazyModule(__name__, globals()['__file__'], _import_structure)

