

'''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 							_snake_case :
       @staticmethod
       def     __UpperCamelCase						( *SCREAMING_SNAKE_CASE__			:      List[str]     ,**SCREAMING_SNAKE_CASE__			:      Optional[int]							):
         pass





@is_pipeline_test
@require_torch
@require_vision
class 							_snake_case (       unittest.TestCase						):
   _A       :     Union[str, Any]       =					MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING
   def     __UpperCamelCase						( self			:      Union[str, Any]     ,SCREAMING_SNAKE_CASE__			:      Tuple     ,SCREAMING_SNAKE_CASE__			:      str     ,SCREAMING_SNAKE_CASE__			:      Any							):
     SCREAMING_SNAKE_CASE:int						      =					pipeline("visual-question-answering"     ,model="hf-internal-testing/tiny-vilt-random-vqa"							)
     SCREAMING_SNAKE_CASE:List[str]						      =					[
         {
             "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			:      Any     ,SCREAMING_SNAKE_CASE__			:      int     ,SCREAMING_SNAKE_CASE__			:      Any							):
     SCREAMING_SNAKE_CASE:List[Any]						      =					vqa_pipeline(SCREAMING_SNAKE_CASE__     ,top_k=1							)
     self.assertEqual(
         SCREAMING_SNAKE_CASE__     ,[
             [{"score": ANY(SCREAMING_SNAKE_CASE__							), "answer": ANY(SCREAMING_SNAKE_CASE__							)}],
             [{"score": ANY(SCREAMING_SNAKE_CASE__							), "answer": ANY(SCREAMING_SNAKE_CASE__							)}],
         ]     ,)
   @require_torch
   def     __UpperCamelCase						( self			:      Dict							):
     SCREAMING_SNAKE_CASE:Dict						      =					pipeline("visual-question-answering"     ,model="hf-internal-testing/tiny-vilt-random-vqa"							)
     SCREAMING_SNAKE_CASE:Optional[int]						      =					"./tests/fixtures/tests_samples/COCO/000000039769.png"
     SCREAMING_SNAKE_CASE:Optional[Any]						      =					"How many cats are there?"

     SCREAMING_SNAKE_CASE:List[str]						      =					vqa_pipeline(image=SCREAMING_SNAKE_CASE__     ,question="How many cats are there?"     ,top_k=2							)
     self.assertEqual(
         SCREAMING_SNAKE_CASE__     ,[{"score": ANY(SCREAMING_SNAKE_CASE__							), "answer": ANY(SCREAMING_SNAKE_CASE__							)}, {"score": ANY(SCREAMING_SNAKE_CASE__							), "answer": ANY(SCREAMING_SNAKE_CASE__							)}]							)

     SCREAMING_SNAKE_CASE:int						      =					vqa_pipeline({"image": image, "question": question}     ,top_k=2							)
     self.assertEqual(
         SCREAMING_SNAKE_CASE__     ,[{"score": ANY(SCREAMING_SNAKE_CASE__							), "answer": ANY(SCREAMING_SNAKE_CASE__							)}, {"score": ANY(SCREAMING_SNAKE_CASE__							), "answer": ANY(SCREAMING_SNAKE_CASE__							)}]							)
   @slow
   @require_torch
   def     __UpperCamelCase						( self			:      Union[str, Any]							):
     SCREAMING_SNAKE_CASE:List[str]						      =					pipeline("visual-question-answering"     ,model="dandelin/vilt-b32-finetuned-vqa"							)
     SCREAMING_SNAKE_CASE:Tuple						      =					"./tests/fixtures/tests_samples/COCO/000000039769.png"
     SCREAMING_SNAKE_CASE:Optional[Any]						      =					"How many cats are there?"

     SCREAMING_SNAKE_CASE:Any						      =					vqa_pipeline(image=SCREAMING_SNAKE_CASE__     ,question=SCREAMING_SNAKE_CASE__     ,top_k=2							)
     self.assertEqual(
         nested_simplify(SCREAMING_SNAKE_CASE__     ,decimals=4							)     ,[{"score": 0.8_799, "answer": "2"}, {"score": 0.296, "answer": "1"}]							)

     SCREAMING_SNAKE_CASE:int						      =					vqa_pipeline({"image": image, "question": question}     ,top_k=2							)
     self.assertEqual(
         nested_simplify(SCREAMING_SNAKE_CASE__     ,decimals=4							)     ,[{"score": 0.8_799, "answer": "2"}, {"score": 0.296, "answer": "1"}]							)

     SCREAMING_SNAKE_CASE:Optional[int]						      =					vqa_pipeline(
         [{"image": image, "question": question}, {"image": image, "question": question}]     ,top_k=2							)
     self.assertEqual(
         nested_simplify(SCREAMING_SNAKE_CASE__     ,decimals=4							)     ,[[{"score": 0.8_799, "answer": "2"}, {"score": 0.296, "answer": "1"}]] * 2     ,)







   @require_tf
   @unittest.skip("Visual question answering not implemented in TF"							)
   def     __UpperCamelCase						( self			:      List[str]							):
     pass









'''simple docstring'''

import inspect
import re
from hashlib import shaaaa
from typing import Dict, List

from .arrow import arrow
from .audiofolder import audiofolder
from .csv import csv
from .imagefolder import imagefolder
from .json import json
from .pandas import pandas
from .parquet import parquet
from .sql import sql  # noqa F401
from .text import text





def    A_  ( snake_case ):
  SCREAMING_SNAKE_CASE:List[Any]						      =					[]
  for line in lines:
    SCREAMING_SNAKE_CASE:Any						      =					re.sub(r"#.*"      ,       ""      ,       snake_case )  # remove comments
    if line:
      filtered_lines.append(snake_case )
  SCREAMING_SNAKE_CASE:int						      =					"\n".join(snake_case )

  # Make a hash from all this code
  SCREAMING_SNAKE_CASE:str						      =					full_str.encode("utf-8" )
  return shaaaa(snake_case ).hexdigest()


# get importable module names and hash for caching
A_       						=							{
    "csv": (csv.__name__, _hash_python_lines(inspect.getsource(csv).splitlines())),
    "json": (json.__name__, _hash_python_lines(inspect.getsource(json).splitlines())),
    "pandas": (pandas.__name__, _hash_python_lines(inspect.getsource(pandas).splitlines())),
    "parquet": (parquet.__name__, _hash_python_lines(inspect.getsource(parquet).splitlines())),
    "arrow": (arrow.__name__, _hash_python_lines(inspect.getsource(arrow).splitlines())),
    "text": (text.__name__, _hash_python_lines(inspect.getsource(text).splitlines())),
    "imagefolder": (imagefolder.__name__, _hash_python_lines(inspect.getsource(imagefolder).splitlines())),
    "audiofolder": (audiofolder.__name__, _hash_python_lines(inspect.getsource(audiofolder).splitlines())),
}

# Used to infer the module to use based on the data files extensions
A_       						=							{
    ".csv": ("csv", {}),
    ".tsv": ("csv", {"sep": "\t"}),
    ".json": ("json", {}),
    ".jsonl": ("json", {}),
    ".parquet": ("parquet", {}),
    ".arrow": ("arrow", {}),
    ".txt": ("text", {}),
}
_EXTENSION_TO_MODULE.update({ext: ("imagefolder", {}) for ext in imagefolder.ImageFolder.EXTENSIONS})
_EXTENSION_TO_MODULE.update({ext.upper(): ("imagefolder", {}) for ext in imagefolder.ImageFolder.EXTENSIONS})
_EXTENSION_TO_MODULE.update({ext: ("audiofolder", {}) for ext in audiofolder.AudioFolder.EXTENSIONS})
_EXTENSION_TO_MODULE.update({ext.upper(): ("audiofolder", {}) for ext in audiofolder.AudioFolder.EXTENSIONS})
A_       						=							{"imagefolder", "audiofolder"}

# Used to filter data files based on extensions given a module name
A_       						=							{}
for _ext, (_module, _) in _EXTENSION_TO_MODULE.items():
    _MODULE_TO_EXTENSIONS.setdefault(_module, []).append(_ext)

_MODULE_TO_EXTENSIONS["imagefolder"].append(".zip")


_MODULE_TO_EXTENSIONS["audiofolder"].append(".zip")






