




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        __SCREAMING_SNAKE_CASE(     *_A	,    **_A    ):




																"""simple docstring"""



																pass
@is_pipeline_test
@require_torch
@require_vision
class     a__					(      unittest.TestCase   ):
						_a							:					Optional[int]      =					MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING






						def        __SCREAMING_SNAKE_CASE(     self	,    _A	,    _A	,    _A    ):




											"""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        __SCREAMING_SNAKE_CASE(     self	,    _A	,    _A    ):




											"""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        __SCREAMING_SNAKE_CASE(     self    ):




											"""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        __SCREAMING_SNAKE_CASE(     self    ):




											"""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.87_99, "answer": "2"}, {"score": 0.2_96, "answer": "1"}]    )

											__lowerCAmelCase            = vqa_pipeline({"image": image, "question": question}	,    top_k=2    )
											self.assertEqual(
											    nested_simplify(UpperCamelCase__	,    decimals=4    )	,    [{"score": 0.87_99, "answer": "2"}, {"score": 0.2_96, "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.87_99, "answer": "2"}, {"score": 0.2_96, "answer": "1"}]] * 2	,    )






						@require_tf
						@unittest.skip("Visual question answering not implemented in TF"    )
						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											pass










import os


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_doctest_list.py
UpperCamelCase__					    =    """."""


if __name__ == "__main__":
					UpperCamelCase__					    =    os.path.join(REPO_PATH, """utils/documentation_tests.txt""")
					UpperCamelCase__					    =    []
					UpperCamelCase__					    =    []
					with open(doctest_file_path) as fp:
										for line in fp:
															UpperCamelCase__					    =    line.strip()
															UpperCamelCase__					    =    os.path.join(REPO_PATH, line)
															if not (os.path.isfile(path) or os.path.isdir(path)):
																				non_existent_paths.append(line)
															all_paths.append(path)
					if len(non_existent_paths) > 0:
										UpperCamelCase__					    =    """\n""".join(non_existent_paths)
										raise ValueError(f'''`utils/documentation_tests.txt` contains non-existent paths:\n{non_existent_paths}''')
					if all_paths != sorted(all_paths):
										raise ValueError("""Files in `utils/documentation_tests.txt` are not in alphabetical order.""")




