



import unittest

from transformers import 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       UpperCAmelCase__:

          '''simple docstring'''




          @staticmethod
          def 			UpperCAmelCase		(						*lowerCAmelCase  :			Tuple			,				**lowerCAmelCase  :			Dict)	->				int:




                """simple docstring"""

                pass



@is_pipeline_test
@require_vision
class       UpperCAmelCase__(							unittest.TestCase	):

   '''simple docstring'''




   @require_torch
   def 			UpperCAmelCase		(						self  :			Dict)	->				Optional[Any]:




         """simple docstring"""

         lowercase__     =  pipeline(
             model='hf-internal-testing/tiny-random-clip-zero-shot-image-classification'			,				)
         lowercase__     =  Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png')
         lowercase__     =  image_classifier(lowerCAmelCase			,				candidate_labels=['a', 'b', 'c'])

         # The floating scores are so close, we enter floating error approximation and the order is not guaranteed across
         # python and torch versions.
         self.assertIn(
             nested_simplify(lowerCAmelCase)			,				[
                 [{'score': 0.3_33, 'label': 'a'}, {'score': 0.3_33, 'label': 'b'}, {'score': 0.3_33, 'label': 'c'}],
                 [{'score': 0.3_33, 'label': 'a'}, {'score': 0.3_33, 'label': 'c'}, {'score': 0.3_33, 'label': 'b'}],
             ]			,				)

         lowercase__     =  image_classifier([image] * 5			,				candidate_labels=['A', 'B', 'C']			,				batch_size=2)
         self.assertEqual(
             nested_simplify(lowerCAmelCase)			,				[
                 [
                     {'score': 0.3_33, 'label': ANY(lowerCAmelCase)},
                     {'score': 0.3_33, 'label': ANY(lowerCAmelCase)},
                     {'score': 0.3_33, 'label': ANY(lowerCAmelCase)},
                 ],
                 [
                     {'score': 0.3_33, 'label': ANY(lowerCAmelCase)},
                     {'score': 0.3_33, 'label': ANY(lowerCAmelCase)},
                     {'score': 0.3_33, 'label': ANY(lowerCAmelCase)},
                 ],
                 [
                     {'score': 0.3_33, 'label': ANY(lowerCAmelCase)},
                     {'score': 0.3_33, 'label': ANY(lowerCAmelCase)},
                     {'score': 0.3_33, 'label': ANY(lowerCAmelCase)},
                 ],
                 [
                     {'score': 0.3_33, 'label': ANY(lowerCAmelCase)},
                     {'score': 0.3_33, 'label': ANY(lowerCAmelCase)},
                     {'score': 0.3_33, 'label': ANY(lowerCAmelCase)},
                 ],
                 [
                     {'score': 0.3_33, 'label': ANY(lowerCAmelCase)},
                     {'score': 0.3_33, 'label': ANY(lowerCAmelCase)},
                     {'score': 0.3_33, 'label': ANY(lowerCAmelCase)},
                 ],
             ]			,				)


   @require_tf
   def 			UpperCAmelCase		(						self  :			Optional[Any])	->				Union[str, Any]:




         """simple docstring"""

         lowercase__     =  pipeline(
             model='hf-internal-testing/tiny-random-clip-zero-shot-image-classification'			,				framework='tf')
         lowercase__     =  Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png')
         lowercase__     =  image_classifier(lowerCAmelCase			,				candidate_labels=['a', 'b', 'c'])

         self.assertEqual(
             nested_simplify(lowerCAmelCase)			,				[{'score': 0.3_33, 'label': 'a'}, {'score': 0.3_33, 'label': 'b'}, {'score': 0.3_33, 'label': 'c'}]			,				)

         lowercase__     =  image_classifier([image] * 5			,				candidate_labels=['A', 'B', 'C']			,				batch_size=2)
         self.assertEqual(
             nested_simplify(lowerCAmelCase)			,				[
                 [
                     {'score': 0.3_33, 'label': ANY(lowerCAmelCase)},
                     {'score': 0.3_33, 'label': ANY(lowerCAmelCase)},
                     {'score': 0.3_33, 'label': ANY(lowerCAmelCase)},
                 ],
                 [
                     {'score': 0.3_33, 'label': ANY(lowerCAmelCase)},
                     {'score': 0.3_33, 'label': ANY(lowerCAmelCase)},
                     {'score': 0.3_33, 'label': ANY(lowerCAmelCase)},
                 ],
                 [
                     {'score': 0.3_33, 'label': ANY(lowerCAmelCase)},
                     {'score': 0.3_33, 'label': ANY(lowerCAmelCase)},
                     {'score': 0.3_33, 'label': ANY(lowerCAmelCase)},
                 ],
                 [
                     {'score': 0.3_33, 'label': ANY(lowerCAmelCase)},
                     {'score': 0.3_33, 'label': ANY(lowerCAmelCase)},
                     {'score': 0.3_33, 'label': ANY(lowerCAmelCase)},
                 ],
                 [
                     {'score': 0.3_33, 'label': ANY(lowerCAmelCase)},
                     {'score': 0.3_33, 'label': ANY(lowerCAmelCase)},
                     {'score': 0.3_33, 'label': ANY(lowerCAmelCase)},
                 ],
             ]			,				)


   @slow
   @require_torch
   def 			UpperCAmelCase		(						self  :			int)	->				int:




         """simple docstring"""

         lowercase__     =  pipeline(
             task='zero-shot-image-classification'			,				model='openai/clip-vit-base-patch32'			,				)
         # This is an image of 2 cats with remotes and no planes
         lowercase__     =  Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png')
         lowercase__     =  image_classifier(lowerCAmelCase			,				candidate_labels=['cat', 'plane', 'remote'])

         self.assertEqual(
             nested_simplify(lowerCAmelCase)			,				[
                 {'score': 0.5_11, 'label': 'remote'},
                 {'score': 0.4_85, 'label': 'cat'},
                 {'score': 0.0_04, 'label': 'plane'},
             ]			,				)

         lowercase__     =  image_classifier([image] * 5			,				candidate_labels=['cat', 'plane', 'remote']			,				batch_size=2)
         self.assertEqual(
             nested_simplify(lowerCAmelCase)			,				[
                 [
                     {'score': 0.5_11, 'label': 'remote'},
                     {'score': 0.4_85, 'label': 'cat'},
                     {'score': 0.0_04, 'label': 'plane'},
                 ],
             ]
             * 5			,				)



   @slow
   @require_tf
   def 			UpperCAmelCase		(						self  :			int)	->				Dict:




         """simple docstring"""

         lowercase__     =  pipeline(
             task='zero-shot-image-classification'			,				model='openai/clip-vit-base-patch32'			,				framework='tf')
         # This is an image of 2 cats with remotes and no planes
         lowercase__     =  Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png')
         lowercase__     =  image_classifier(lowerCAmelCase			,				candidate_labels=['cat', 'plane', 'remote'])
         self.assertEqual(
             nested_simplify(lowerCAmelCase)			,				[
                 {'score': 0.5_11, 'label': 'remote'},
                 {'score': 0.4_85, 'label': 'cat'},
                 {'score': 0.0_04, 'label': 'plane'},
             ]			,				)

         lowercase__     =  image_classifier([image] * 5			,				candidate_labels=['cat', 'plane', 'remote']			,				batch_size=2)
         self.assertEqual(
             nested_simplify(lowerCAmelCase)			,				[
                 [
                     {'score': 0.5_11, 'label': 'remote'},
                     {'score': 0.4_85, 'label': 'cat'},
                     {'score': 0.0_04, 'label': 'plane'},
                 ],
             ]
             * 5			,				)





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


a__      :				Dict   	=			logging.get_logger(__name__)

a__      :				List[Any]   	=			{
    "facebook/s2t-small-librispeech-asr": (
        "https://huggingface.co/facebook/s2t-small-librispeech-asr/resolve/main/config.json"
    ),
    # See all Speech2Text models at https://huggingface.co/models?filter=speech_to_text
}



class       UpperCAmelCase__(							lowerCamelCase	):

   '''simple docstring'''




   A    :  int       				=    "speech_to_text"
   A    :  Optional[Any]       				=    ["past_key_values"]
   A    :  Optional[int]       				=    {"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"}


   def __init__(						self  :			Optional[int]			,				lowerCAmelCase  :			Tuple=1_00_00			,				lowerCAmelCase  :			int=12			,				lowerCAmelCase  :			int=20_48			,				lowerCAmelCase  :			Union[str, Any]=4			,				lowerCAmelCase  :			str=6			,				lowerCAmelCase  :			Dict=20_48			,				lowerCAmelCase  :			Dict=4			,				lowerCAmelCase  :			Optional[int]=0.0			,				lowerCAmelCase  :			Union[str, Any]=0.0			,				lowerCAmelCase  :			int=True			,				lowerCAmelCase  :			Optional[Any]=True			,				lowerCAmelCase  :			Dict="relu"			,				lowerCAmelCase  :			Tuple=2_56			,				lowerCAmelCase  :			Dict=0.1			,				lowerCAmelCase  :			Optional[Any]=0.0			,				lowerCAmelCase  :			List[Any]=0.0			,				lowerCAmelCase  :			Any=0.02			,				lowerCAmelCase  :			List[Any]=2			,				lowerCAmelCase  :			Tuple=True			,				lowerCAmelCase  :			Tuple=1			,				lowerCAmelCase  :			List[str]=0			,				lowerCAmelCase  :			Union[str, Any]=2			,				lowerCAmelCase  :			Any=60_00			,				lowerCAmelCase  :			Optional[int]=10_24			,				lowerCAmelCase  :			Optional[Any]=2			,				lowerCAmelCase  :			Optional[Any]=(5, 5)			,				lowerCAmelCase  :			Union[str, Any]=10_24			,				lowerCAmelCase  :			List[Any]=80			,				lowerCAmelCase  :			List[str]=1			,				**lowerCAmelCase  :			List[str]			,				)	->				Dict:




         """simple docstring"""

         lowercase__     =  vocab_size
         lowercase__     =  d_model
         lowercase__     =  encoder_ffn_dim
         lowercase__     =  encoder_layers
         lowercase__     =  encoder_attention_heads
         lowercase__     =  decoder_ffn_dim
         lowercase__     =  decoder_layers
         lowercase__     =  decoder_attention_heads
         lowercase__     =  dropout
         lowercase__     =  attention_dropout
         lowercase__     =  activation_dropout
         lowercase__     =  activation_function
         lowercase__     =  init_std
         lowercase__     =  encoder_layerdrop
         lowercase__     =  decoder_layerdrop
         lowercase__     =  use_cache
         lowercase__     =  encoder_layers
         lowercase__     =  scale_embedding  # scale factor will be sqrt(d_model) if True
         lowercase__     =  max_source_positions
         lowercase__     =  max_target_positions
         lowercase__     =  num_conv_layers
         lowercase__     =  list(lowerCAmelCase)
         lowercase__     =  conv_channels
         lowercase__     =  input_feat_per_channel
         lowercase__     =  input_channels

         if len(self.conv_kernel_sizes) != self.num_conv_layers:
               raise ValueError(
                   'Configuration for convolutional module is incorrect. '
                   'It is required that `len(config.conv_kernel_sizes)` == `config.num_conv_layers` '
                   f'''but is `len(config.conv_kernel_sizes) = {len(self.conv_kernel_sizes)}`, '''
                   f'''`config.num_conv_layers = {self.num_conv_layers}`.''')

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