




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:



  '''simple docstring'''


  @staticmethod
  def 							a__					(							*A_   :    Dict		,				**A_   :    Tuple    )  ->      List[Any]:

        """simple docstring"""






        pass







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



 '''simple docstring'''


 UpperCamelCase		  =		MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING

 def 							a__					(							self   :    Dict		,				A_   :    List[Any]		,				A_   :    Optional[int]		,				A_   :    Any    )  ->      str:

       """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   :    int		,				A_   :    Tuple		,				A_   :    Tuple    )  ->      Dict:

       """simple docstring"""






       lowerCamelCase_							=       vqa_pipeline(A_		,				top_k=1    )
       self.assertEqual(
           A_		,				[
               [{'score': ANY(A_    ), 'answer': ANY(A_    )}],
               [{'score': ANY(A_    ), 'answer': ANY(A_    )}],
           ]		,				)

 @require_torch
 def 							a__					(							self   :    Union[str, Any]    )  ->      Optional[Any]:

       """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=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_    )}]    )

       lowerCamelCase_							=       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 							a__					(							self   :    int    )  ->      Any:

       """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=A_		,				question=A_		,				top_k=2    )
       self.assertEqual(
           nested_simplify(A_		,				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(A_		,				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(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 							a__					(							self   :    Optional[int]    )  ->      List[Any]:

       """simple docstring"""






       pass










'''simple docstring'''






import doctest
import logging
import os
import unittest
from pathlib import Path
from typing import List, Union

import transformers
from transformers.testing_utils import require_tf, require_torch, slow


__UpperCamelCase					    =	logging.getLogger()




@unittest.skip('''Temporarily disable the doc tests.'''		)
@require_torch
@require_tf
@slow
class  _A (					unittest.TestCase		):

		def 	lowercase__     (   self					:   Optional[int]  ,      __magic_name__					:   Path  ,      __magic_name__					:   Union[str, None] = None  ,      __magic_name__					:   Union[List[str], None] = None  ,      __magic_name__					:   Union[str, List[str], None] = None  ,      __magic_name__					:   bool = True  ,      )					->    Optional[int]:


					"""simple docstring"""

					__snake_case					:      Union[str, Any]							      =     [file for file in os.listdir(__magic_name__			) if os.path.isfile(os.path.join(__magic_name__  ,      __magic_name__			)			)]

					if identifier is not None:
								__snake_case					:      List[Any]							      =     [file for file in files if identifier in file]

					if n_identifier is not None:
								if isinstance(__magic_name__  ,      __magic_name__			):
											for n_ in n_identifier:
														__snake_case					:      Optional[int]							      =     [file for file in files if n_ not in file]
								else:
											__snake_case					:      Tuple							      =     [file for file in files if n_identifier not in file]

					__snake_case					:      Dict							      =     ignore_files or []
					ignore_files.append("""__init__.py"""			)
					__snake_case					:      List[str]							      =     [file for file in files if file not in ignore_files]

					for file in files:
								# Open all files
								print("""Testing"""  ,      __magic_name__			)

								if only_modules:
											__snake_case					:      List[Any]							      =     file.split("""."""			)[0]
											try:
														__snake_case					:      List[Any]							      =     getattr(__magic_name__  ,      __magic_name__			)
														__snake_case					:      Union[str, Any]							      =     doctest.DocTestSuite(__magic_name__			)
														__snake_case					:      Dict							      =     unittest.TextTestRunner().run(__magic_name__			)
														self.assertIs(len(result.failures			)  ,      0			)
											except AttributeError:
														logger.info(f'''{module_identifier} is not a module.'''			)
								else:
											__snake_case					:      Tuple							      =     doctest.testfile(str("""..""" / directory / file			)  ,      optionflags=doctest.ELLIPSIS			)
											self.assertIs(result.failed  ,      0			)

		def 	lowercase__     (   self					:   Union[str, Any]			)					->    Any:


					"""simple docstring"""

					__snake_case					:      List[Any]							      =     Path("""src/transformers"""			)
					__snake_case					:      List[Any]							      =     """modeling"""
					__snake_case					:      Union[str, Any]							      =     [
					    """modeling_ctrl.py""",
					    """modeling_tf_ctrl.py""",
					]
					self.analyze_directory(__magic_name__  ,      identifier=__magic_name__  ,      ignore_files=__magic_name__			)

		def 	lowercase__     (   self					:   Union[str, Any]			)					->    int:


					"""simple docstring"""

					__snake_case					:      Union[str, Any]							      =     Path("""src/transformers"""			)
					__snake_case					:      Any							      =     """tokenization"""
					self.analyze_directory(__magic_name__  ,      identifier=__magic_name__			)

		def 	lowercase__     (   self					:   Union[str, Any]			)					->    int:


					"""simple docstring"""

					__snake_case					:      List[Any]							      =     Path("""src/transformers"""			)
					__snake_case					:      List[str]							      =     """configuration"""
					self.analyze_directory(__magic_name__  ,      identifier=__magic_name__			)

		def 	lowercase__     (   self					:   Dict			)					->    Dict:


					"""simple docstring"""

					__snake_case					:      Tuple							      =     Path("""src/transformers"""			)
					__snake_case					:      int							      =     ["""configuration""", """modeling""", """tokenization"""]
					self.analyze_directory(__magic_name__  ,      n_identifier=__magic_name__			)

		def 	lowercase__     (   self					:   Optional[int]			)					->    Union[str, Any]:


					"""simple docstring"""

					__snake_case					:      int							      =     Path("""docs/source"""			)
					__snake_case					:      Optional[int]							      =     ["""favicon.ico"""]
					self.analyze_directory(__magic_name__  ,      ignore_files=__magic_name__  ,      only_modules=__magic_name__			)


