"""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_					(						*_snake_case			:				Tuple   , **_snake_case			:				Optional[int]		)							->       Union[str, Any]:
											pass

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






					'''simple docstring'''


					a   	=		MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING



					def 			lowerCAmelCase_					(						self			:				Optional[int]   , _snake_case			:				Tuple   , _snake_case			:				int   , _snake_case			:				Optional[Any]		)							->       Any:
									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			:				int   , _snake_case			:				List[Any]   , _snake_case			:				int		)							->       Tuple:
									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			:				Union[str, Any]		)							->       Dict:
									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			:				Union[str, Any]		)							->       int:
									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.8799, "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.8799, "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.8799, "answer": "2"}, {"score": 0.296, "answer": "1"}]] * 2   , )



					@require_tf
					@unittest.skip("Visual question answering not implemented in TF"		)
					def 			lowerCAmelCase_					(						self			:				List[str]		)							->       Union[str, Any]:
									pass





"""simple docstring"""






from typing import Callable, Optional

from .. import Features
from ..packaged_modules.generator.generator import Generator
from .abc import AbstractDatasetInputStream

class lowerCamelCase		(_SCREAMING_SNAKE_CASE      ):






					'''simple docstring'''


					def __init__(						self			:				List[str]   , _snake_case			:				Callable   , _snake_case			:				Optional[Features] = None   , _snake_case			:				str = None   , _snake_case			:				bool = False   , _snake_case			:				bool = False   , _snake_case			:				Optional[dict] = None   , _snake_case			:				Optional[int] = None   , **_snake_case			:				Dict   , )							->       Any:
									super().__init__(
									    features=_snake_case   , cache_dir=_snake_case   , keep_in_memory=_snake_case   , streaming=_snake_case   , num_proc=_snake_case   , **_snake_case   , )
									SCREAMING_SNAKE_CASE__		  =				Generator(
									    cache_dir=_snake_case   , features=_snake_case   , generator=_snake_case   , gen_kwargs=_snake_case   , **_snake_case   , )



					def 			lowerCAmelCase_					(						self			:				List[str]		)							->       Optional[Any]:
									# Build iterable dataset
									if self.streaming:
													SCREAMING_SNAKE_CASE__		  =				self.builder.as_streaming_dataset(split="train"		)
									# Build regular (map-style) dataset
									else:
													SCREAMING_SNAKE_CASE__		  =				None
													SCREAMING_SNAKE_CASE__		  =				None
													SCREAMING_SNAKE_CASE__		  =				None
													SCREAMING_SNAKE_CASE__		  =				None

													self.builder.download_and_prepare(
													    download_config=_snake_case   , download_mode=_snake_case   , verification_mode=_snake_case   , base_path=_snake_case   , num_proc=self.num_proc   , )
													SCREAMING_SNAKE_CASE__		  =				self.builder.as_dataset(
													    split="train"   , verification_mode=_snake_case   , in_memory=self.keep_in_memory		)
									return dataset




