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 		_lowerCamelCase		:
          @staticmethod
          def 						UpperCamelCase_				(  *lowerCAmelCase , **lowerCAmelCase   )       ->					Tuple:
           pass






@is_pipeline_test
@require_torch
@require_vision
class 		_lowerCamelCase		(   unittest.TestCase    ):
    __a  =      MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING
    def 						UpperCamelCase_				(  self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase   )       ->					List[str]:
     SCREAMING_SNAKE_CASE__:   str=      pipeline('''visual-question-answering''' , model='''hf-internal-testing/tiny-vilt-random-vqa'''   )
     SCREAMING_SNAKE_CASE__:   Any=      [
         {
             '''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   )       ->					int:
     SCREAMING_SNAKE_CASE__:   Tuple=      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   )       ->					List[Any]:
     SCREAMING_SNAKE_CASE__:   Union[str, Any]=      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__:   Any=      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   )}]   )

     SCREAMING_SNAKE_CASE__:   List[Any]=      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   )       ->					int:
     SCREAMING_SNAKE_CASE__:   int=      pipeline('''visual-question-answering''' , model='''dandelin/vilt-b32-finetuned-vqa'''   )
     SCREAMING_SNAKE_CASE__:   Optional[int]=      '''./tests/fixtures/tests_samples/COCO/000000039769.png'''
     SCREAMING_SNAKE_CASE__:   Tuple=      '''How many cats are there?'''

     SCREAMING_SNAKE_CASE__:   Optional[int]=      vqa_pipeline(image=lowerCAmelCase , question=lowerCAmelCase , top_k=2   )
     self.assertEqual(
         nested_simplify(lowerCAmelCase , decimals=4   ) , [{'''score''': 0.8799, '''answer''': '''2'''}, {'''score''': 0.296, '''answer''': '''1'''}]   )

     SCREAMING_SNAKE_CASE__:   List[str]=      vqa_pipeline({'''image''': image, '''question''': question} , top_k=2   )
     self.assertEqual(
         nested_simplify(lowerCAmelCase , decimals=4   ) , [{'''score''': 0.8799, '''answer''': '''2'''}, {'''score''': 0.296, '''answer''': '''1'''}]   )

     SCREAMING_SNAKE_CASE__:   Tuple=      vqa_pipeline(
         [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}] , top_k=2   )
     self.assertEqual(
         nested_simplify(lowerCAmelCase , 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 						UpperCamelCase_				(  self   )       ->					List[Any]:
     pass


from . import (
    albert,
    align,
    altclip,
    audio_spectrogram_transformer,
    auto,
    autoformer,
    bark,
    bart,
    barthez,
    bartpho,
    beit,
    bert,
    bert_generation,
    bert_japanese,
    bertweet,
    big_bird,
    bigbird_pegasus,
    biogpt,
    bit,
    blenderbot,
    blenderbot_small,
    blip,
    blip_a,
    bloom,
    bridgetower,
    byta,
    camembert,
    canine,
    chinese_clip,
    clap,
    clip,
    clipseg,
    codegen,
    conditional_detr,
    convbert,
    convnext,
    convnextva,
    cpm,
    cpmant,
    ctrl,
    cvt,
    dataavec,
    deberta,
    deberta_va,
    decision_transformer,
    deformable_detr,
    deit,
    deprecated,
    deta,
    detr,
    dialogpt,
    dinat,
    distilbert,
    dit,
    donut,
    dpr,
    dpt,
    efficientformer,
    efficientnet,
    electra,
    encodec,
    encoder_decoder,
    ernie,
    ernie_m,
    esm,
    falcon,
    flaubert,
    flava,
    fnet,
    focalnet,
    fsmt,
    funnel,
    git,
    glpn,
    gpta,
    gpt_bigcode,
    gpt_neo,
    gpt_neox,
    gpt_neox_japanese,
    gpt_swa,
    gptj,
    gptsan_japanese,
    graphormer,
    groupvit,
    herbert,
    hubert,
    ibert,
    imagegpt,
    informer,
    instructblip,
    jukebox,
    layoutlm,
    layoutlmva,
    layoutlmva,
    layoutxlm,
    led,
    levit,
    lilt,
    llama,
    longformer,
    longta,
    luke,
    lxmert,
    mam_aaa,
    marian,
    markuplm,
    maskaformer,
    maskformer,
    mbart,
    mbartaa,
    mega,
    megatron_bert,
    megatron_gpta,
    mgp_str,
    mluke,
    mobilebert,
    mobilenet_va,
    mobilenet_va,
    mobilevit,
    mobilevitva,
    mpnet,
    mra,
    mta,
    musicgen,
    mvp,
    nat,
    nezha,
    nllb,
    nllb_moe,
    nystromformer,
    oneformer,
    open_llama,
    openai,
    opt,
    owlvit,
    pegasus,
    pegasus_x,
    perceiver,
    phobert,
    pixastruct,
    plbart,
    poolformer,
    prophetnet,
    qdqbert,
    rag,
    realm,
    reformer,
    regnet,
    rembert,
    resnet,
    roberta,
    roberta_prelayernorm,
    roc_bert,
    roformer,
    rwkv,
    sam,
    segformer,
    sew,
    sew_d,
    speech_encoder_decoder,
    speech_to_text,
    speech_to_text_a,
    speechta,
    splinter,
    squeezebert,
    swiftformer,
    swin,
    swinasr,
    swinva,
    switch_transformers,
    ta,
    table_transformer,
    tapas,
    time_series_transformer,
    timesformer,
    timm_backbone,
    transfo_xl,
    trocr,
    tvlt,
    umta,
    unispeech,
    unispeech_sat,
    upernet,
    videomae,
    vilt,
    vision_encoder_decoder,
    vision_text_dual_encoder,
    visual_bert,
    vit,
    vit_hybrid,
    vit_mae,
    vit_msn,
    vivit,
    wavaveca,
    wavaveca_conformer,
    wavaveca_phoneme,
    wavaveca_with_lm,
    wavlm,
    whisper,
    x_clip,
    xglm,
    xlm,
    xlm_prophetnet,
    xlm_roberta,
    xlm_roberta_xl,
    xlnet,
    xmod,
    yolos,
    yoso,
)

