


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  :




      @staticmethod
      def        SCREAMING_SNAKE_CASE__				(		*_lowerCamelCase					,  **_lowerCamelCase    ):
            pass







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




 def        SCREAMING_SNAKE_CASE__				(		self					,  _lowerCamelCase					,  _lowerCamelCase					,  _lowerCamelCase    ):
       a						:Optional[int] 							=       pipeline('''visual-question-answering'''					,  model='''hf-internal-testing/tiny-vilt-random-vqa'''    )
       a						:Any 							=       [
           {
               '''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        SCREAMING_SNAKE_CASE__				(		self					,  _lowerCamelCase					,  _lowerCamelCase    ):
       a						:List[str] 							=       vqa_pipeline(_lowerCamelCase					,  top_k=1    )
       self.assertEqual(
           _lowerCamelCase					,  [
               [{'''score''': ANY(_lowerCamelCase    ), '''answer''': ANY(_lowerCamelCase    )}],
               [{'''score''': ANY(_lowerCamelCase    ), '''answer''': ANY(_lowerCamelCase    )}],
           ]					,  )




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

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

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




 @slow
 @require_torch
 def        SCREAMING_SNAKE_CASE__				(		self    ):
       a						:Optional[Any] 							=       pipeline('''visual-question-answering'''					,  model='''dandelin/vilt-b32-finetuned-vqa'''    )
       a						:Dict 							=       '''./tests/fixtures/tests_samples/COCO/000000039769.png'''
       a						:int 							=       '''How many cats are there?'''

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

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

       a						:Optional[Any] 							=       vqa_pipeline(
           [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]					,  top_k=2    )
       self.assertEqual(
           nested_simplify(_lowerCamelCase					,  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        SCREAMING_SNAKE_CASE__				(		self    ):
       pass












from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


__lowerCamelCase       :     Any   =     {
    """configuration_electra""": ["""ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP""", """ElectraConfig""", """ElectraOnnxConfig"""],
    """tokenization_electra""": ["""ElectraTokenizer"""],
}

try:
    if not is_tokenizers_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    __lowerCamelCase       :     Dict   =     ["""ElectraTokenizerFast"""]

try:
    if not is_torch_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    __lowerCamelCase       :     Tuple   =     [
        """ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST""",
        """ElectraForCausalLM""",
        """ElectraForMaskedLM""",
        """ElectraForMultipleChoice""",
        """ElectraForPreTraining""",
        """ElectraForQuestionAnswering""",
        """ElectraForSequenceClassification""",
        """ElectraForTokenClassification""",
        """ElectraModel""",
        """ElectraPreTrainedModel""",
        """load_tf_weights_in_electra""",
    ]

try:
    if not is_tf_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    __lowerCamelCase       :     List[Any]   =     [
        """TF_ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST""",
        """TFElectraForMaskedLM""",
        """TFElectraForMultipleChoice""",
        """TFElectraForPreTraining""",
        """TFElectraForQuestionAnswering""",
        """TFElectraForSequenceClassification""",
        """TFElectraForTokenClassification""",
        """TFElectraModel""",
        """TFElectraPreTrainedModel""",
    ]

try:
    if not is_flax_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    __lowerCamelCase       :     List[str]   =     [
        """FlaxElectraForCausalLM""",
        """FlaxElectraForMaskedLM""",
        """FlaxElectraForMultipleChoice""",
        """FlaxElectraForPreTraining""",
        """FlaxElectraForQuestionAnswering""",
        """FlaxElectraForSequenceClassification""",
        """FlaxElectraForTokenClassification""",
        """FlaxElectraModel""",
        """FlaxElectraPreTrainedModel""",
    ]


if TYPE_CHECKING:
    from .configuration_electra import ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP, ElectraConfig, ElectraOnnxConfig
    from .tokenization_electra import ElectraTokenizer

    try:
        if not is_tokenizers_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .tokenization_electra_fast import ElectraTokenizerFast

    try:
        if not is_torch_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_electra import (
            ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST,
            ElectraForCausalLM,
            ElectraForMaskedLM,
            ElectraForMultipleChoice,
            ElectraForPreTraining,
            ElectraForQuestionAnswering,
            ElectraForSequenceClassification,
            ElectraForTokenClassification,
            ElectraModel,
            ElectraPreTrainedModel,
            load_tf_weights_in_electra,
        )

    try:
        if not is_tf_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_tf_electra import (
            TF_ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST,
            TFElectraForMaskedLM,
            TFElectraForMultipleChoice,
            TFElectraForPreTraining,
            TFElectraForQuestionAnswering,
            TFElectraForSequenceClassification,
            TFElectraForTokenClassification,
            TFElectraModel,
            TFElectraPreTrainedModel,
        )

    try:
        if not is_flax_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_flax_electra import (
            FlaxElectraForCausalLM,
            FlaxElectraForMaskedLM,
            FlaxElectraForMultipleChoice,
            FlaxElectraForPreTraining,
            FlaxElectraForQuestionAnswering,
            FlaxElectraForSequenceClassification,
            FlaxElectraForTokenClassification,
            FlaxElectraModel,
            FlaxElectraPreTrainedModel,
        )

else:
    import sys

    __lowerCamelCase       :     Any   =     _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
