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







												@staticmethod
												def       UpperCAmelCase__					(						*snake_case__  :  Tuple      ,				**snake_case__  :  List[str] ):
													"""simple docstring"""







													pass

@is_pipeline_test
@require_torch
@require_vision
class       a   (   unittest.TestCase					):
							lowercase_		:						List[str]   							=  MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING







							def       UpperCAmelCase__					(						self  :  Optional[int]      ,				snake_case__  :  Union[str, Any]      ,				snake_case__  :  List[Any]      ,				snake_case__  :  int ):
								"""simple docstring"""







								__lowerCAmelCase         =							pipeline("visual-question-answering"      ,				model="hf-internal-testing/tiny-vilt-random-vqa" )
								__lowerCAmelCase         =							[
								    {
								        '''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  :  Optional[Any]      ,				snake_case__  :  Optional[Any]      ,				snake_case__  :  Any ):
								"""simple docstring"""







								__lowerCAmelCase         =							vqa_pipeline(UpperCamelCase__      ,				top_k=1 )
								self.assertEqual(
								    UpperCamelCase__      ,				[
								        [{"score": ANY(UpperCamelCase__ ), "answer": ANY(UpperCamelCase__ )}],
								        [{"score": ANY(UpperCamelCase__ ), "answer": ANY(UpperCamelCase__ )}],
								    ]      ,				)







							@require_torch
							def       UpperCAmelCase__					(						self  :  int ):
								"""simple docstring"""







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

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

								__lowerCAmelCase         =							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       UpperCAmelCase__					(						self  :  int ):
								"""simple docstring"""







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

								__lowerCAmelCase         =							vqa_pipeline(image=UpperCamelCase__      ,				question=UpperCamelCase__      ,				top_k=2 )
								self.assertEqual(
								    nested_simplify(UpperCamelCase__      ,				decimals=4 )      ,				[{"score": 0.8_7_9_9, "answer": "2"}, {"score": 0.2_9_6, "answer": "1"}] )

								__lowerCAmelCase         =							vqa_pipeline({"image": image, "question": question}      ,				top_k=2 )
								self.assertEqual(
								    nested_simplify(UpperCamelCase__      ,				decimals=4 )      ,				[{"score": 0.8_7_9_9, "answer": "2"}, {"score": 0.2_9_6, "answer": "1"}] )

								__lowerCAmelCase         =							vqa_pipeline(
								    [{"image": image, "question": question}, {"image": image, "question": question}]      ,				top_k=2 )
								self.assertEqual(
								    nested_simplify(UpperCamelCase__      ,				decimals=4 )      ,				[[{"score": 0.8_7_9_9, "answer": "2"}, {"score": 0.2_9_6, "answer": "1"}]] * 2      ,				)







							@require_tf
							@unittest.skip("Visual question answering not implemented in TF" )
							def       UpperCAmelCase__					(						self  :  Tuple ):
								"""simple docstring"""







								pass








import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
					import torch

					from transformers.generation import DisjunctiveConstraint

@require_torch
class       a		(   unittest.TestCase					):







							def       UpperCAmelCase__					(						self  :  Optional[int] ):
								"""simple docstring"""







								__lowerCAmelCase         =							[[1, 2, 4], [1, 2, 3, 4]]
								__lowerCAmelCase         =							DisjunctiveConstraint(snake_case__ )
								self.assertTrue(isinstance(dc.token_ids      ,				snake_case__ ) )

								with self.assertRaises(snake_case__ ):
									DisjunctiveConstraint(torch.LongTensor([[1, 2, 4], [1, 2, 3]] ) )

								with self.assertRaises(snake_case__ ):
									DisjunctiveConstraint([torch.LongTensor([1, 2, 4] ), torch.LongTensor([1, 2, 3, 4, 5] )] )







							def       UpperCAmelCase__					(						self  :  Tuple ):
								"""simple docstring"""







								__lowerCAmelCase         =							[[1, 2], [1, 2, 3, 4]]

								with self.assertRaises(snake_case__ ):
									DisjunctiveConstraint(snake_case__ )  # fails here







							def       UpperCAmelCase__					(						self  :  Union[str, Any] ):
								"""simple docstring"""







								__lowerCAmelCase         =							[[1, 2, 3], [1, 2, 4]]

								__lowerCAmelCase         =							DisjunctiveConstraint(snake_case__ )

								__lowerCAmelCase   ,						__lowerCAmelCase   ,						__lowerCAmelCase         =							dc.update(1 )
								__lowerCAmelCase         =							stepped is True and completed is False and reset is False
								self.assertTrue(snake_case__ )
								self.assertTrue(not dc.completed )
								self.assertTrue(dc.current_seq == [1] )

								__lowerCAmelCase   ,						__lowerCAmelCase   ,						__lowerCAmelCase         =							dc.update(2 )
								__lowerCAmelCase         =							stepped is True and completed is False and reset is False
								self.assertTrue(snake_case__ )
								self.assertTrue(not dc.completed )
								self.assertTrue(dc.current_seq == [1, 2] )

								__lowerCAmelCase   ,						__lowerCAmelCase   ,						__lowerCAmelCase         =							dc.update(3 )
								__lowerCAmelCase         =							stepped is True and completed is True and reset is False
								self.assertTrue(snake_case__ )
								self.assertTrue(dc.completed )  # Completed!
								self.assertTrue(dc.current_seq == [1, 2, 3] )







							def       UpperCAmelCase__					(						self  :  Optional[Any] ):
								"""simple docstring"""







								__lowerCAmelCase         =							[[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]]

								__lowerCAmelCase         =							DisjunctiveConstraint(snake_case__ )

								__lowerCAmelCase   ,						__lowerCAmelCase   ,						__lowerCAmelCase         =							dc.update(1 )
								self.assertTrue(not dc.completed )
								self.assertTrue(dc.current_seq == [1] )

								__lowerCAmelCase   ,						__lowerCAmelCase   ,						__lowerCAmelCase         =							dc.update(2 )
								self.assertTrue(not dc.completed )
								self.assertTrue(dc.current_seq == [1, 2] )

								__lowerCAmelCase   ,						__lowerCAmelCase   ,						__lowerCAmelCase         =							dc.update(4 )
								self.assertTrue(not dc.completed )
								self.assertTrue(dc.current_seq == [1, 2, 4] )

								__lowerCAmelCase   ,						__lowerCAmelCase   ,						__lowerCAmelCase         =							dc.update(5 )
								self.assertTrue(dc.completed )  # Completed!
								self.assertTrue(dc.current_seq == [1, 2, 4, 5] )

								dc.reset()

								__lowerCAmelCase   ,						__lowerCAmelCase   ,						__lowerCAmelCase         =							dc.update(1 )
								self.assertTrue(not dc.completed )
								self.assertTrue(dc.remaining() == 3 )
								self.assertTrue(dc.current_seq == [1] )

								__lowerCAmelCase   ,						__lowerCAmelCase   ,						__lowerCAmelCase         =							dc.update(2 )
								self.assertTrue(not dc.completed )
								self.assertTrue(dc.remaining() == 2 )
								self.assertTrue(dc.current_seq == [1, 2] )

								__lowerCAmelCase   ,						__lowerCAmelCase   ,						__lowerCAmelCase         =							dc.update(5 )
								self.assertTrue(dc.completed )  # Completed!
								self.assertTrue(dc.remaining() == 0 )
								self.assertTrue(dc.current_seq == [1, 2, 5] )






