


"""simple docstring"""






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__		(						*UpperCAmelCase__       ,		**UpperCAmelCase__     ):
									pass


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





				"""simple docstring"""







				_lowerCAmelCase      :      Any      							=					MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING



				def   lowerCAmelCase__		(						self       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__     ):
								SCREAMING_SNAKE_CASE__     							=				pipeline("visual-question-answering"       ,		model="hf-internal-testing/tiny-vilt-random-vqa"     )
								SCREAMING_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       ,		UpperCAmelCase__       ,		UpperCAmelCase__     ):
								SCREAMING_SNAKE_CASE__     							=				vqa_pipeline(UpperCamelCase__       ,		top_k=1     )
								self.assertEqual(
								    UpperCamelCase__       ,		[
								        [{"score": ANY(UpperCamelCase__     ), "answer": ANY(UpperCamelCase__     )}],
								        [{"score": ANY(UpperCamelCase__     ), "answer": ANY(UpperCamelCase__     )}],
								    ]       ,		)



				@require_torch
				def   lowerCAmelCase__		(						self     ):
								SCREAMING_SNAKE_CASE__     							=				pipeline("visual-question-answering"       ,		model="hf-internal-testing/tiny-vilt-random-vqa"     )
								SCREAMING_SNAKE_CASE__     							=				'''./tests/fixtures/tests_samples/COCO/000000039769.png'''
								SCREAMING_SNAKE_CASE__     							=				'''How many cats are there?'''

								SCREAMING_SNAKE_CASE__     							=				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__     )}]     )

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

								SCREAMING_SNAKE_CASE__     							=				vqa_pipeline(image=UpperCamelCase__       ,		question=UpperCamelCase__       ,		top_k=2     )
								self.assertEqual(
								    nested_simplify(UpperCamelCase__       ,		decimals=4     )       ,		[{"score": 0.8_799, "answer": "2"}, {"score": 0.296, "answer": "1"}]     )

								SCREAMING_SNAKE_CASE__     							=				vqa_pipeline({"image": image, "question": question}       ,		top_k=2     )
								self.assertEqual(
								    nested_simplify(UpperCamelCase__       ,		decimals=4     )       ,		[{"score": 0.8_799, "answer": "2"}, {"score": 0.296, "answer": "1"}]     )

								SCREAMING_SNAKE_CASE__     							=				vqa_pipeline(
								    [{"image": image, "question": question}, {"image": image, "question": question}]       ,		top_k=2     )
								self.assertEqual(
								    nested_simplify(UpperCamelCase__       ,		decimals=4     )       ,		[[{"score": 0.8_799, "answer": "2"}, {"score": 0.296, "answer": "1"}]] * 2       ,		)






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




"""simple docstring"""






# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from argparse import ArgumentParser

from accelerate.commands.config import get_config_parser
from accelerate.commands.env import env_command_parser
from accelerate.commands.launch import launch_command_parser
from accelerate.commands.test import test_command_parser
from accelerate.commands.tpu import tpu_command_parser


def __lowercase     (      ):
				SCREAMING_SNAKE_CASE__     							=				ArgumentParser("Accelerate CLI tool"						,					usage="accelerate <command> [<args>]"						,					allow_abbrev=lowerCamelCase_  )
				SCREAMING_SNAKE_CASE__     							=				parser.add_subparsers(help="accelerate command helpers"  )

				# Register commands
				get_config_parser(subparsers=lowerCamelCase_  )
				env_command_parser(subparsers=lowerCamelCase_  )
				launch_command_parser(subparsers=lowerCamelCase_  )
				tpu_command_parser(subparsers=lowerCamelCase_  )
				test_command_parser(subparsers=lowerCamelCase_  )

				# Let's go
				SCREAMING_SNAKE_CASE__     							=				parser.parse_args()

				if not hasattr(lowerCamelCase_						,					"func"  ):
								parser.print_help()
								exit(1  )

				# Run
				args.func(lowerCamelCase_  )


if __name__ == "__main__":
	main()
