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







										"""simple docstring"""






										@staticmethod
										def 		_lowerCAmelCase		(      *_A							:							Tuple      ,    **_A							:							Any):



																	"""simple docstring"""







																	pass




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







				"""simple docstring"""






				a			=						MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING





				def 		_lowerCAmelCase		(      self							:							List[Any]      ,    _A							:							str      ,    _A							:							Dict      ,    _A							:							Optional[Any]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Any			  =    pipeline("""visual-question-answering"""      ,    model="""hf-internal-testing/tiny-vilt-random-vqa""")
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    [
											    {
											        """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							:							str      ,    _A							:							str      ,    _A							:							Union[str, Any]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						List[str]			  =    vqa_pipeline(_a      ,    top_k=1)
											self.assertEqual(
											    _a      ,    [
											        [{"""score""": ANY(_a), """answer""": ANY(_a)}],
											        [{"""score""": ANY(_a), """answer""": ANY(_a)}],
											    ]      ,    )





				@require_torch
				def 		_lowerCAmelCase		(      self							:							str):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    pipeline("""visual-question-answering"""      ,    model="""hf-internal-testing/tiny-vilt-random-vqa""")
											_SCREAMING_SNAKE_CASE		:						Dict			  =    """./tests/fixtures/tests_samples/COCO/000000039769.png"""
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    """How many cats are there?"""

											_SCREAMING_SNAKE_CASE		:						List[str]			  =    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)}])

											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    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 		_lowerCAmelCase		(      self							:							Tuple):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    pipeline("""visual-question-answering"""      ,    model="""dandelin/vilt-b32-finetuned-vqa""")
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    """./tests/fixtures/tests_samples/COCO/000000039769.png"""
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    """How many cats are there?"""

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

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

											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    vqa_pipeline(
											    [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]      ,    top_k=2)
											self.assertEqual(
											    nested_simplify(_a      ,    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							:							Tuple):



											"""simple docstring"""







											pass



"""simple docstring"""





import torch
from transformers import PreTrainedModel, XLMRobertaConfig, XLMRobertaModel




class  _snake_case		(    __snake_case ):







				"""simple docstring"""






				a			=						"M-CLIP"





				def __init__(      self							:							Optional[Any]      ,    _A							:							List[str]=1_0_2_4      ,    _A							:							Union[str, Any]=7_6_8      ,    **_A							:							Optional[Any]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Tuple			  =    transformerDimSize
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    imageDimSize
											super().__init__(**_A)




class  _snake_case		(    __snake_case ):







				"""simple docstring"""






				a			=						MCLIPConfig





				def __init__(      self							:							Dict      ,    _A							:							Optional[Any]      ,    *_A							:							Any      ,    **_A							:							Dict):



											"""simple docstring"""







											super().__init__(_A      ,    *_A      ,    **_A)
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    XLMRobertaModel(_A)
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    torch.nn.Linear(
											    in_features=config.transformerDimensions      ,    out_features=config.numDims)





				def 		_lowerCAmelCase		(      self							:							Union[str, Any]      ,    _A							:							str      ,    _A							:							int):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						str			  =    self.transformer(input_ids=_A      ,    attention_mask=_A)[0]
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    (embs * attention_mask.unsqueeze(2)).sum(dim=1) / attention_mask.sum(dim=1)[:, None]
											return self.LinearTransformation(_A), embs


