
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  lowerCamelCase__							:




			'''simple docstring'''




			@staticmethod
			def    _lowerCamelCase				(					*a		:int     ,   **a		:List[str]						)		-> Dict:
										pass




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




	'''simple docstring'''




	@require_torch
	def    _lowerCamelCase				(					self		:Any						)		-> Optional[int]:
								__UpperCamelCase	:   Optional[Any]			   =					pipeline(
								    model="hf-internal-testing/tiny-random-clip-zero-shot-image-classification"     ,   )
								__UpperCamelCase	:   int			   =					Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"						)
								__UpperCamelCase	:   Union[str, Any]			   =					image_classifier(a     ,   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(a						)     ,   [
								        [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "b"}, {"score": 0.333, "label": "c"}],
								        [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "c"}, {"score": 0.333, "label": "b"}],
								    ]     ,   )

								__UpperCamelCase	:   str			   =					image_classifier([image] * 5     ,   candidate_labels=["A", "B", "C"]     ,   batch_size=2						)
								self.assertEqual(
								    nested_simplify(a						)     ,   [
								        [
								            {"score": 0.333, "label": ANY(a						)},
								            {"score": 0.333, "label": ANY(a						)},
								            {"score": 0.333, "label": ANY(a						)},
								        ],
								        [
								            {"score": 0.333, "label": ANY(a						)},
								            {"score": 0.333, "label": ANY(a						)},
								            {"score": 0.333, "label": ANY(a						)},
								        ],
								        [
								            {"score": 0.333, "label": ANY(a						)},
								            {"score": 0.333, "label": ANY(a						)},
								            {"score": 0.333, "label": ANY(a						)},
								        ],
								        [
								            {"score": 0.333, "label": ANY(a						)},
								            {"score": 0.333, "label": ANY(a						)},
								            {"score": 0.333, "label": ANY(a						)},
								        ],
								        [
								            {"score": 0.333, "label": ANY(a						)},
								            {"score": 0.333, "label": ANY(a						)},
								            {"score": 0.333, "label": ANY(a						)},
								        ],
								    ]     ,   )







	@require_tf
	def    _lowerCamelCase				(					self		:Optional[Any]						)		-> Tuple:
								__UpperCamelCase	:   int			   =					pipeline(
								    model="hf-internal-testing/tiny-random-clip-zero-shot-image-classification"     ,   framework="tf"						)
								__UpperCamelCase	:   Dict			   =					Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"						)
								__UpperCamelCase	:   Optional[Any]			   =					image_classifier(a     ,   candidate_labels=["a", "b", "c"]						)

								self.assertEqual(
								    nested_simplify(a						)     ,   [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "b"}, {"score": 0.333, "label": "c"}]     ,   )

								__UpperCamelCase	:   str			   =					image_classifier([image] * 5     ,   candidate_labels=["A", "B", "C"]     ,   batch_size=2						)
								self.assertEqual(
								    nested_simplify(a						)     ,   [
								        [
								            {"score": 0.333, "label": ANY(a						)},
								            {"score": 0.333, "label": ANY(a						)},
								            {"score": 0.333, "label": ANY(a						)},
								        ],
								        [
								            {"score": 0.333, "label": ANY(a						)},
								            {"score": 0.333, "label": ANY(a						)},
								            {"score": 0.333, "label": ANY(a						)},
								        ],
								        [
								            {"score": 0.333, "label": ANY(a						)},
								            {"score": 0.333, "label": ANY(a						)},
								            {"score": 0.333, "label": ANY(a						)},
								        ],
								        [
								            {"score": 0.333, "label": ANY(a						)},
								            {"score": 0.333, "label": ANY(a						)},
								            {"score": 0.333, "label": ANY(a						)},
								        ],
								        [
								            {"score": 0.333, "label": ANY(a						)},
								            {"score": 0.333, "label": ANY(a						)},
								            {"score": 0.333, "label": ANY(a						)},
								        ],
								    ]     ,   )







	@slow
	@require_torch
	def    _lowerCamelCase				(					self		:Union[str, Any]						)		-> Dict:
								__UpperCamelCase	:   List[Any]			   =					pipeline(
								    task="zero-shot-image-classification"     ,   model="openai/clip-vit-base-patch32"     ,   )
								# This is an image of 2 cats with remotes and no planes
								__UpperCamelCase	:   Any			   =					Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"						)
								__UpperCamelCase	:   List[str]			   =					image_classifier(a     ,   candidate_labels=["cat", "plane", "remote"]						)

								self.assertEqual(
								    nested_simplify(a						)     ,   [
								        {"score": 0.511, "label": "remote"},
								        {"score": 0.485, "label": "cat"},
								        {"score": 0.004, "label": "plane"},
								    ]     ,   )

								__UpperCamelCase	:   Optional[int]			   =					image_classifier([image] * 5     ,   candidate_labels=["cat", "plane", "remote"]     ,   batch_size=2						)
								self.assertEqual(
								    nested_simplify(a						)     ,   [
								        [
								            {"score": 0.511, "label": "remote"},
								            {"score": 0.485, "label": "cat"},
								            {"score": 0.004, "label": "plane"},
								        ],
								    ]
								    * 5     ,   )







	@slow
	@require_tf
	def    _lowerCamelCase				(					self		:Optional[int]						)		-> int:
								__UpperCamelCase	:   Optional[int]			   =					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
								__UpperCamelCase	:   Dict			   =					Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"						)
								__UpperCamelCase	:   Tuple			   =					image_classifier(a     ,   candidate_labels=["cat", "plane", "remote"]						)
								self.assertEqual(
								    nested_simplify(a						)     ,   [
								        {"score": 0.511, "label": "remote"},
								        {"score": 0.485, "label": "cat"},
								        {"score": 0.004, "label": "plane"},
								    ]     ,   )

								__UpperCamelCase	:   List[Any]			   =					image_classifier([image] * 5     ,   candidate_labels=["cat", "plane", "remote"]     ,   batch_size=2						)
								self.assertEqual(
								    nested_simplify(a						)     ,   [
								        [
								            {"score": 0.511, "label": "remote"},
								            {"score": 0.485, "label": "cat"},
								            {"score": 0.004, "label": "plane"},
								        ],
								    ]
								    * 5     ,   )

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


lowercase  :					List[Any]													=       logging.get_logger(__name__)

lowercase  :					Any													=       {
    'google/pegasus-large': 'https://huggingface.co/google/pegasus-large/resolve/main/config.json',
    # See all PEGASUS models at https://huggingface.co/models?filter=pegasus
}




class  lowerCamelCase__							(				__lowercase):




	'''simple docstring'''




	_A								=     'pegasus'
	_A								=     ['past_key_values']
	_A								=     {'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}







	def __init__(					self		:Dict     ,   a		:Dict=5_0_2_6_5     ,   a		:Dict=1_0_2_4     ,   a		:Union[str, Any]=1_2     ,   a		:Any=4_0_9_6     ,   a		:str=1_6     ,   a		:str=1_2     ,   a		:Optional[Any]=4_0_9_6     ,   a		:int=1_6     ,   a		:Optional[int]=0.0     ,   a		:Optional[int]=0.0     ,   a		:List[Any]=True     ,   a		:Union[str, Any]=True     ,   a		:int="gelu"     ,   a		:Dict=1_0_2_4     ,   a		:List[Any]=0.1     ,   a		:List[str]=0.0     ,   a		:List[Any]=0.0     ,   a		:str=0.02     ,   a		:int=0     ,   a		:Any=False     ,   a		:Dict=0     ,   a		:int=1     ,   a		:Optional[Any]=1     ,   **a		:Optional[int]     ,   )		-> str:
								__UpperCamelCase	:   List[Any]			   =					vocab_size
								__UpperCamelCase	:   Union[str, Any]			   =					max_position_embeddings
								__UpperCamelCase	:   str			   =					d_model
								__UpperCamelCase	:   Dict			   =					encoder_ffn_dim
								__UpperCamelCase	:   int			   =					encoder_layers
								__UpperCamelCase	:   int			   =					encoder_attention_heads
								__UpperCamelCase	:   List[Any]			   =					decoder_ffn_dim
								__UpperCamelCase	:   List[Any]			   =					decoder_layers
								__UpperCamelCase	:   List[str]			   =					decoder_attention_heads
								__UpperCamelCase	:   str			   =					dropout
								__UpperCamelCase	:   Union[str, Any]			   =					attention_dropout
								__UpperCamelCase	:   List[str]			   =					activation_dropout
								__UpperCamelCase	:   Optional[Any]			   =					activation_function
								__UpperCamelCase	:   Tuple			   =					init_std
								__UpperCamelCase	:   Optional[int]			   =					encoder_layerdrop
								__UpperCamelCase	:   Union[str, Any]			   =					decoder_layerdrop
								__UpperCamelCase	:   Optional[Any]			   =					use_cache
								__UpperCamelCase	:   Union[str, Any]			   =					encoder_layers
								__UpperCamelCase	:   int			   =					scale_embedding  # scale factor will be sqrt(d_model) if True
								super().__init__(
								    pad_token_id=a     ,   eos_token_id=a     ,   is_encoder_decoder=a     ,   decoder_start_token_id=a     ,   forced_eos_token_id=a     ,   **a     ,   )







	@property
	def    _lowerCamelCase				(					self		:Dict						)		-> int:
								return self.encoder_attention_heads







	@property
	def    _lowerCamelCase				(					self		:Optional[Any]						)		-> int:
								return self.d_model