





'''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    lowerCAmelCase_		:




        @staticmethod
        def 							_snake_case   (	*_lowerCAmelCase			,     **_lowerCAmelCase		)    ->							List[Any]:
            pass

@is_pipeline_test
@require_torch
@require_vision
class    lowerCAmelCase_		(		unittest.TestCase		):
 __lowerCamelCase						:							Optional[Any]         =					MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING




 def 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase		)    ->							Dict:
     _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 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase		)    ->							List[str]:
     _lowerCAmelCase				=      vqa_pipeline(_lowerCAmelCase			,     top_k=1		)
     self.assertEqual(
         _lowerCAmelCase			,     [
             [{"score": ANY(_lowerCAmelCase		), "answer": ANY(_lowerCAmelCase		)}],
             [{"score": ANY(_lowerCAmelCase		), "answer": ANY(_lowerCAmelCase		)}],
         ]			,     )




 @require_torch
 def 							_snake_case   (	self		)    ->							Any:
     _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=_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		)}]		)

     _lowerCAmelCase				=      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 							_snake_case   (	self		)    ->							Optional[Any]:
     _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=_lowerCAmelCase			,     question=_lowerCAmelCase			,     top_k=2		)
     self.assertEqual(
         nested_simplify(_lowerCAmelCase			,     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(_lowerCAmelCase			,     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(_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 							_snake_case   (	self		)    ->							Tuple:
     pass









'''simple docstring'''


from ...configuration_utils import PretrainedConfig
from ...utils import logging


_SCREAMING_SNAKE_CASE	 =							logging.get_logger(__name__)

_SCREAMING_SNAKE_CASE	 =							{
    "facebook/s2t-wav2vec2-large-en-de": (
        "https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/config.json"
    ),
    # See all Speech2Text models at https://huggingface.co/models?filter=speech2text2
}

class    lowerCAmelCase_		(		__magic_name__		):
 __lowerCamelCase						:							str         =					"speech_to_text_2"
 __lowerCamelCase						:							Union[str, Any]         =					["past_key_values"]
 __lowerCamelCase						:							Tuple         =					{"num_attention_heads": "decoder_attention_heads", "hidden_size": "d_model"}




 def __init__(	self			,     _lowerCAmelCase=10000			,     _lowerCAmelCase=6			,     _lowerCAmelCase=2048			,     _lowerCAmelCase=4			,     _lowerCAmelCase=0.0			,     _lowerCAmelCase=True			,     _lowerCAmelCase="relu"			,     _lowerCAmelCase=256			,     _lowerCAmelCase=0.1			,     _lowerCAmelCase=0.0			,     _lowerCAmelCase=0.0			,     _lowerCAmelCase=0.02			,     _lowerCAmelCase=2			,     _lowerCAmelCase=True			,     _lowerCAmelCase=1			,     _lowerCAmelCase=0			,     _lowerCAmelCase=2			,     _lowerCAmelCase=1024			,     **_lowerCAmelCase			,     )    ->							Dict:
     _lowerCAmelCase				=      vocab_size
     _lowerCAmelCase				=      d_model
     _lowerCAmelCase				=      decoder_ffn_dim
     _lowerCAmelCase				=      decoder_layers
     _lowerCAmelCase				=      decoder_attention_heads
     _lowerCAmelCase				=      dropout
     _lowerCAmelCase				=      attention_dropout
     _lowerCAmelCase				=      activation_dropout
     _lowerCAmelCase				=      activation_function
     _lowerCAmelCase				=      init_std
     _lowerCAmelCase				=      decoder_layerdrop
     _lowerCAmelCase				=      use_cache
     _lowerCAmelCase				=      decoder_layers
     _lowerCAmelCase				=      scale_embedding  # scale factor will be sqrt(d_model) if True
     _lowerCAmelCase				=      max_target_positions

     super().__init__(
         pad_token_id=_lowerCAmelCase			,     bos_token_id=_lowerCAmelCase			,     eos_token_id=_lowerCAmelCase			,     decoder_start_token_id=_lowerCAmelCase			,     **_lowerCAmelCase			,     )


