




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     SCREAMING_SNAKE_CASE__		:
           @staticmethod
           def        A__      (						*__lowerCamelCase	:		Optional[Any]       ,						**__lowerCamelCase	:		str						):



             """simple docstring"""
             pass





@is_pipeline_test
@require_torch
@require_vision
class     SCREAMING_SNAKE_CASE__		(unittest.TestCase			):
     lowercase_ :      Optional[Any]				= MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING
     def        A__      (						self	:		int       ,						__lowerCamelCase	:		Dict       ,						__lowerCamelCase	:		List[Any]       ,						__lowerCamelCase	:		Optional[Any]						):



       """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        A__      (						self	:		Union[str, Any]       ,						__lowerCamelCase	:		Optional[int]       ,						__lowerCamelCase	:		Union[str, 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        A__      (						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        A__      (						self	:		Optional[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.8799, '''answer''': '''2'''}, {'''score''': 0.296, '''answer''': '''1'''}]						)

       lowerCAmelCase__										=      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'''}]						)

       lowerCAmelCase__										=      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        A__      (						self	:		Any						):



       """simple docstring"""
       pass


'''simple docstring'''






import pytest
import requests

from datasets.utils.file_utils import http_head

from .utils import OfflineSimulationMode, RequestWouldHangIndefinitelyError, offline





@pytest.mark.integration
def  _SCREAMING_SNAKE_CASE		(      ):
     with offline(OfflineSimulationMode.CONNECTION_TIMES_OUT				):
          with pytest.raises(__snake_case				):
               requests.request('GET'   ,		'https://huggingface.co'				)
          with pytest.raises(requests.exceptions.ConnectTimeout				):
               requests.request('GET'   ,		'https://huggingface.co'   ,		timeout=1.0				)





@pytest.mark.integration
def  _SCREAMING_SNAKE_CASE		(      ):
     with offline(OfflineSimulationMode.CONNECTION_FAILS				):
          with pytest.raises(requests.exceptions.ConnectionError				):
               requests.request('GET'   ,		'https://huggingface.co'				)





def  _SCREAMING_SNAKE_CASE		(      ):
     with offline(OfflineSimulationMode.HF_DATASETS_OFFLINE_SET_TO_1				):
          with pytest.raises(__snake_case				):
               http_head('https://huggingface.co'				)



