





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




										@staticmethod
										def 	lowerCAmelCase			(		*lowerCAmelCase_				,						**lowerCAmelCase_  )		->							Optional[Any]:
															pass
@is_pipeline_test
@require_torch
@require_vision
class 			UpperCamelCase_		(  unittest.TestCase					):
					lowerCAmelCase_							=	MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING




					def 	lowerCAmelCase			(		self				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_  )		->							Tuple:
										_snake_case									=				pipeline('visual-question-answering'				,						model='hf-internal-testing/tiny-vilt-random-vqa'  )
										_snake_case									=				[
										    {
										        '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_  )		->							str:
										_snake_case									=				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  )		->							str:
										_snake_case									=				pipeline('visual-question-answering'				,						model='hf-internal-testing/tiny-vilt-random-vqa'  )
										_snake_case									=				'./tests/fixtures/tests_samples/COCO/000000039769.png'
										_snake_case									=				'How many cats are there?'

										_snake_case									=				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									=				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  )		->							str:
										_snake_case									=				pipeline('visual-question-answering'				,						model='dandelin/vilt-b32-finetuned-vqa'  )
										_snake_case									=				'./tests/fixtures/tests_samples/COCO/000000039769.png'
										_snake_case									=				'How many cats are there?'

										_snake_case									=				vqa_pipeline(image=lowerCAmelCase_				,						question=lowerCAmelCase_				,						top_k=2  )
										self.assertEqual(
										    nested_simplify(lowerCAmelCase_				,						decimals=4  )				,						[{'score': 0.87_99, 'answer': '2'}, {'score': 0.2_96, 'answer': '1'}]  )

										_snake_case									=				vqa_pipeline({'image': image, 'question': question}				,						top_k=2  )
										self.assertEqual(
										    nested_simplify(lowerCAmelCase_				,						decimals=4  )				,						[{'score': 0.87_99, 'answer': '2'}, {'score': 0.2_96, 'answer': '1'}]  )

										_snake_case									=				vqa_pipeline(
										    [{'image': image, 'question': question}, {'image': image, 'question': question}]				,						top_k=2  )
										self.assertEqual(
										    nested_simplify(lowerCAmelCase_				,						decimals=4  )				,						[[{'score': 0.87_99, 'answer': '2'}, {'score': 0.2_96, 'answer': '1'}]] * 2				,						)






					@require_tf
					@unittest.skip('Visual question answering not implemented in TF'  )
					def 	lowerCAmelCase			(		self  )		->							List[Any]:
										pass










import unittest

from accelerate import debug_launcher
from accelerate.test_utils import require_cpu, test_ops, test_script
@require_cpu
class 			UpperCamelCase_		(  unittest.TestCase					):




					def 	lowerCAmelCase			(		self  )		->							List[Any]:
										debug_launcher(test_script.main  )






					def 	lowerCAmelCase			(		self  )		->							Optional[Any]:
										debug_launcher(test_ops.main  )



