
"""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     _UpperCAmelCase:



						@staticmethod
						def 						UpperCAmelCase							(					*__a				,		**__a)       ->       Union[str, Any]:






											'''simple docstring'''
											pass






@is_pipeline_test
@require_torch
@require_vision
class     _UpperCAmelCase(    unittest.TestCase	):
		lowercase__						=      MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING



		def 						UpperCAmelCase							(					self				,		__a				,		__a				,		__a)       ->       Optional[Any]:






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






							'''simple docstring'''
							_UpperCamelCase							 =  vqa_pipeline(__a				,		top_k=1)
							self.assertEqual(
							    __a				,		[
							        [{'''score''': ANY(__a), '''answer''': ANY(__a)}],
							        [{'''score''': ANY(__a), '''answer''': ANY(__a)}],
							    ]				,		)



		@require_torch
		def 						UpperCAmelCase							(					self)       ->       Dict:






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

							_UpperCamelCase							 =  vqa_pipeline(image=__a				,		question='''How many cats are there?'''				,		top_k=2)
							self.assertEqual(
							    __a				,		[{'''score''': ANY(__a), '''answer''': ANY(__a)}, {'''score''': ANY(__a), '''answer''': ANY(__a)}])

							_UpperCamelCase							 =  vqa_pipeline({'''image''': image, '''question''': question}				,		top_k=2)
							self.assertEqual(
							    __a				,		[{'''score''': ANY(__a), '''answer''': ANY(__a)}, {'''score''': ANY(__a), '''answer''': ANY(__a)}])



		@slow
		@require_torch
		def 						UpperCAmelCase							(					self)       ->       List[Any]:






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

							_UpperCamelCase							 =  vqa_pipeline(image=__a				,		question=__a				,		top_k=2)
							self.assertEqual(
							    nested_simplify(__a				,		decimals=4)				,		[{'''score''': 0.8799, '''answer''': '''2'''}, {'''score''': 0.296, '''answer''': '''1'''}])

							_UpperCamelCase							 =  vqa_pipeline({'''image''': image, '''question''': question}				,		top_k=2)
							self.assertEqual(
							    nested_simplify(__a				,		decimals=4)				,		[{'''score''': 0.8799, '''answer''': '''2'''}, {'''score''': 0.296, '''answer''': '''1'''}])

							_UpperCamelCase							 =  vqa_pipeline(
							    [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]				,		top_k=2)
							self.assertEqual(
							    nested_simplify(__a				,		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)       ->       Dict:






							'''simple docstring'''
							pass











"""simple docstring"""
import random
import unittest

import torch

from diffusers import IFImgaImgSuperResolutionPipeline
from diffusers.utils import floats_tensor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import skip_mps, torch_device

from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin
from . import IFPipelineTesterMixin
@skip_mps
class 							_UpperCAmelCase							( a		,a		,unittest.TestCase      ):





     '''simple docstring'''



     a__									=IFImgaImgSuperResolutionPipeline
     a__									=TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'''width''', '''height'''}
     a__									=TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({'''original_image'''}      )
     a__									=PipelineTesterMixin.required_optional_params - {'''latents'''}





     def  __lowerCAmelCase	(					self		)							->					List[str]:
         return self._get_superresolution_dummy_components()





     def  __lowerCAmelCase	(					self       ,	A       ,	A=0		)							->					Union[str, Any]:
         if str(A		).startswith('''mps'''		):
             _UpperCAmelCase							:  Any											=    torch.manual_seed(A		)
         else:
             _UpperCAmelCase							:  int											=    torch.Generator(device=A		).manual_seed(A		)

         _UpperCAmelCase							:  str											=    floats_tensor((1, 3, 3_2, 3_2)       ,	rng=random.Random(A		)		).to(A		)
         _UpperCAmelCase							:  Dict											=    floats_tensor((1, 3, 1_6, 1_6)       ,	rng=random.Random(A		)		).to(A		)

         _UpperCAmelCase							:  List[Any]											=    {
             '''prompt''': '''A painting of a squirrel eating a burger''',
             '''image''': image,
             '''original_image''': original_image,
             '''generator''': generator,
             '''num_inference_steps''': 2,
             '''output_type''': '''numpy''',
         }

         return inputs





     @unittest.skipIf(
         torch_device != '''cuda''' or not is_xformers_available()       ,	reason='''XFormers attention is only available with CUDA and `xformers` installed'''       ,	)
     def  __lowerCAmelCase	(					self		)							->					List[Any]:
         self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3		)





     def  __lowerCAmelCase	(					self		)							->					List[str]:
         self._test_save_load_optional_components()





     @unittest.skipIf(torch_device != '''cuda'''       ,	reason='''float16 requires CUDA'''		)
     def  __lowerCAmelCase	(					self		)							->					Optional[Any]:
         # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder
         super().test_save_load_floataa(expected_max_diff=1E-1		)





     def  __lowerCAmelCase	(					self		)							->					int:
         self._test_attention_slicing_forward_pass(expected_max_diff=1E-2		)





     def  __lowerCAmelCase	(					self		)							->					Union[str, Any]:
         self._test_save_load_local()





     def  __lowerCAmelCase	(					self		)							->					Union[str, Any]:
         self._test_inference_batch_single_identical(
             expected_max_diff=1E-2       ,	)


