





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



								"""simple docstring"""

								@staticmethod
								def 		__UpperCAmelCase       (    *lowerCamelCase_			:		List[str] ,				**lowerCamelCase_			:		Dict					):





												'''simple docstring'''







												pass


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



						"""simple docstring"""

						_UpperCamelCase  :	Any								=	MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING







						def 		__UpperCAmelCase       (    self			:		Any ,				lowerCamelCase_			:		Optional[int] ,				lowerCamelCase_			:		Tuple ,				lowerCamelCase_			:		str					):





										'''simple docstring'''







										_snake_case :			List[str]  						=   pipeline('visual-question-answering' ,				model='hf-internal-testing/tiny-vilt-random-vqa'					)
										_snake_case :			Optional[int]  						=   [
										    {
										        '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			:		Dict ,				lowerCamelCase_			:		List[Any] ,				lowerCamelCase_			:		int					):





										'''simple docstring'''







										_snake_case :			Union[str, Any]  						=   vqa_pipeline(lowerCamelCase_ ,				top_k=1					)
										self.assertEqual(
										    lowerCamelCase_ ,				[
										        [{'score': ANY(lowerCamelCase_					), 'answer': ANY(lowerCamelCase_					)}],
										        [{'score': ANY(lowerCamelCase_					), 'answer': ANY(lowerCamelCase_					)}],
										    ] ,				)







						@require_torch
						def 		__UpperCAmelCase       (    self			:		Optional[int]					):





										'''simple docstring'''







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

										_snake_case :			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_					)}]					)

										_snake_case :			Tuple  						=   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 		__UpperCAmelCase       (    self			:		Tuple					):





										'''simple docstring'''







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

										_snake_case :			List[str]  						=   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'}]					)

										_snake_case :			Union[str, 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'}]					)

										_snake_case :			Any  						=   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 		__UpperCAmelCase       (    self			:		Optional[int]					):





										'''simple docstring'''







										pass













lowercase_		:				Tuple				=						'''
# Installazione di Transformers
! pip install transformers datasets
# Per installare dalla fonte invece dell\'ultima versione rilasciata, commenta il comando sopra e
# rimuovi la modalità commento al comando seguente.
# ! pip install git+https://github.com/huggingface/transformers.git
'''

lowercase_		:				Optional[int]				=						[{'''type''': '''code''', '''content''': INSTALL_CONTENT}]
lowercase_		:				str				=						{
    '''{processor_class}''': '''FakeProcessorClass''',
    '''{model_class}''': '''FakeModelClass''',
    '''{object_class}''': '''FakeObjectClass''',
}






