

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


_a		       =   logging.getLogger()






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


     def   UpperCAmelCase  (							self			:		Optional[int]			,  UpperCAmelCase			:		int			,  UpperCAmelCase			:		int = None			,  UpperCAmelCase			:		Dict = None			,  UpperCAmelCase			:		Dict = None			,  UpperCAmelCase			:		Optional[int] = True			,  )							->	str:
      __lowerCAmelCase:   Optional[int]   	=      [file for file in os.listdir(_lowerCamelCase					) if os.path.isfile(os.path.join(_lowerCamelCase			,  _lowerCamelCase					)					)]

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

      if n_identifier is not None:
       if isinstance(_lowerCamelCase			,  _lowerCamelCase					):
        for n_ in n_identifier:
         __lowerCAmelCase:   int   	=      [file for file in files if n_ not in file]
       else:
        __lowerCAmelCase:   Optional[Any]   	=      [file for file in files if n_identifier not in file]

      __lowerCAmelCase:   Dict   	=      ignore_files or []
      ignore_files.append('__init__.py'					)
      __lowerCAmelCase:   List[Any]   	=      [file for file in files if file not in ignore_files]

      for file in files:
       # Open all files
       print('Testing'			,  _lowerCamelCase					)

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


     def   UpperCAmelCase  (							self			:		Any					)							->	List[str]:
      __lowerCAmelCase:   str   	=      Path('src/transformers'					)
      __lowerCAmelCase:   str   	=      'modeling'
      __lowerCAmelCase:   int   	=      [
          'modeling_ctrl.py',
          'modeling_tf_ctrl.py',
      ]
      self.analyze_directory(_lowerCamelCase			,  identifier=_lowerCamelCase			,  ignore_files=_lowerCamelCase					)


     def   UpperCAmelCase  (							self			:		List[str]					)							->	List[str]:
      __lowerCAmelCase:   int   	=      Path('src/transformers'					)
      __lowerCAmelCase:   Union[str, Any]   	=      'tokenization'
      self.analyze_directory(_lowerCamelCase			,  identifier=_lowerCamelCase					)


     def   UpperCAmelCase  (							self			:		Dict					)							->	List[str]:
      __lowerCAmelCase:   List[Any]   	=      Path('src/transformers'					)
      __lowerCAmelCase:   List[str]   	=      'configuration'
      self.analyze_directory(_lowerCamelCase			,  identifier=_lowerCamelCase					)


     def   UpperCAmelCase  (							self			:		Any					)							->	Optional[int]:
      __lowerCAmelCase:   Optional[Any]   	=      Path('src/transformers'					)
      __lowerCAmelCase:   Dict   	=      ['configuration', 'modeling', 'tokenization']
      self.analyze_directory(_lowerCamelCase			,  n_identifier=_lowerCamelCase					)


     def   UpperCAmelCase  (							self			:		Union[str, Any]					)							->	Dict:
      __lowerCAmelCase:   Tuple   	=      Path('docs/source'					)
      __lowerCAmelCase:   Optional[int]   	=      ['favicon.ico']
      self.analyze_directory(_lowerCamelCase			,  ignore_files=_lowerCamelCase			,  only_modules=_lowerCamelCase					)













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







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

@is_pipeline_test
@require_torch
@require_vision
class        a     (   unittest.TestCase	):
    UpperCAmelCase_ :     Union[str, Any]	     =MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING







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







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

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

      lowercase							    =       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 			UpperCamelCase_      (			self       ):
      lowercase							    =       pipeline('visual-question-answering'							,		model='dandelin/vilt-b32-finetuned-vqa'       )
      lowercase							    =       './tests/fixtures/tests_samples/COCO/000000039769.png'
      lowercase							    =       'How many cats are there?'

      lowercase							    =       vqa_pipeline(image=_lowerCamelCase							,		question=_lowerCamelCase							,		top_k=2       )
      self.assertEqual(
          nested_simplify(_lowerCamelCase							,		decimals=4       )							,		[{'score': 0.8_7_9_9, 'answer': '2'}, {'score': 0.2_9_6, 'answer': '1'}]       )

      lowercase							    =       vqa_pipeline({'image': image, 'question': question}							,		top_k=2       )
      self.assertEqual(
          nested_simplify(_lowerCamelCase							,		decimals=4       )							,		[{'score': 0.8_7_9_9, 'answer': '2'}, {'score': 0.2_9_6, 'answer': '1'}]       )

      lowercase							    =       vqa_pipeline(
          [{'image': image, 'question': question}, {'image': image, 'question': question}]							,		top_k=2       )
      self.assertEqual(
          nested_simplify(_lowerCamelCase							,		decimals=4       )							,		[[{'score': 0.8_7_9_9, 'answer': '2'}, {'score': 0.2_9_6, 'answer': '1'}]] * 2							,		)







    @require_tf
    @unittest.skip('Visual question answering not implemented in TF'       )
    def 			UpperCamelCase_      (			self       ):
      pass


