


"""simple docstring"""






import copy
import os
from typing import Union

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


UpperCAmelCase    :     int   						=		logging.get_logger(__name__)

UpperCAmelCase    :     List[Any]   						=		{
    'BAAI/AltCLIP': 'https://huggingface.co/BAAI/AltCLIP/resolve/main/config.json',
    # See all AltCLIP models at https://huggingface.co/models?filter=altclip
}





class      lowerCamelCase__   (     A				):







					"""simple docstring"""


					__a	       =							"""altclip_text_model"""






					def __init__(       self    : List[str] ,       UpperCamelCase    : Dict=250_002 ,       UpperCamelCase    : Union[str, Any]=1_024 ,       UpperCamelCase    : Optional[int]=24 ,       UpperCamelCase    : Union[str, Any]=16 ,       UpperCamelCase    : Any=4_096 ,       UpperCamelCase    : Any="gelu" ,       UpperCamelCase    : Dict=0.1 ,       UpperCamelCase    : int=0.1 ,       UpperCamelCase    : List[str]=514 ,       UpperCamelCase    : Dict=1 ,       UpperCamelCase    : Tuple=0.02 ,       UpperCamelCase    : Any=0.02 ,       UpperCamelCase    : str=1e-0_5 ,       UpperCamelCase    : Tuple=1 ,       UpperCamelCase    : Any=0 ,       UpperCamelCase    : List[str]=2 ,       UpperCamelCase    : Optional[int]="absolute" ,       UpperCamelCase    : int=True ,       UpperCamelCase    : int=768 ,       **UpperCamelCase    : Optional[int] ,       ):
												'''simple docstring'''






												super().__init__(pad_token_id=UpperCamelCase ,       bos_token_id=UpperCamelCase ,       eos_token_id=UpperCamelCase ,       **UpperCamelCase  )

												__UpperCAmelCase   :						Optional[Any]      							=					vocab_size
												__UpperCAmelCase   :						str      							=					hidden_size
												__UpperCAmelCase   :						Optional[Any]      							=					num_hidden_layers
												__UpperCAmelCase   :						Union[str, Any]      							=					num_attention_heads
												__UpperCAmelCase   :						str      							=					hidden_act
												__UpperCAmelCase   :						List[str]      							=					intermediate_size
												__UpperCAmelCase   :						Optional[int]      							=					hidden_dropout_prob
												__UpperCAmelCase   :						List[str]      							=					attention_probs_dropout_prob
												__UpperCAmelCase   :						Any      							=					max_position_embeddings
												__UpperCAmelCase   :						str      							=					type_vocab_size
												__UpperCAmelCase   :						Any      							=					initializer_range
												__UpperCAmelCase   :						str      							=					initializer_factor
												__UpperCAmelCase   :						Union[str, Any]      							=					layer_norm_eps
												__UpperCAmelCase   :						str      							=					position_embedding_type
												__UpperCAmelCase   :						str      							=					use_cache
												__UpperCAmelCase   :						int      							=					project_dim






class      lowerCamelCase__   (     A				):







					"""simple docstring"""


					__a	       =							"""altclip_vision_model"""






					def __init__(       self    : int ,       UpperCamelCase    : Union[str, Any]=768 ,       UpperCamelCase    : int=3_072 ,       UpperCamelCase    : Dict=512 ,       UpperCamelCase    : str=12 ,       UpperCamelCase    : List[str]=12 ,       UpperCamelCase    : Dict=3 ,       UpperCamelCase    : Any=224 ,       UpperCamelCase    : Optional[Any]=32 ,       UpperCamelCase    : str="quick_gelu" ,       UpperCamelCase    : List[Any]=1e-5 ,       UpperCamelCase    : Optional[int]=0.0 ,       UpperCamelCase    : Tuple=0.02 ,       UpperCamelCase    : Union[str, Any]=1.0 ,       **UpperCamelCase    : Tuple ,       ):
												'''simple docstring'''






												super().__init__(**UpperCamelCase  )

												__UpperCAmelCase   :						Optional[Any]      							=					hidden_size
												__UpperCAmelCase   :						List[Any]      							=					intermediate_size
												__UpperCAmelCase   :						Dict      							=					projection_dim
												__UpperCAmelCase   :						Any      							=					num_hidden_layers
												__UpperCAmelCase   :						Optional[Any]      							=					num_attention_heads
												__UpperCAmelCase   :						Optional[Any]      							=					num_channels
												__UpperCAmelCase   :						List[Any]      							=					patch_size
												__UpperCAmelCase   :						Dict      							=					image_size
												__UpperCAmelCase   :						Any      							=					initializer_range
												__UpperCAmelCase   :						Optional[int]      							=					initializer_factor
												__UpperCAmelCase   :						Optional[int]      							=					attention_dropout
												__UpperCAmelCase   :						Optional[Any]      							=					layer_norm_eps
												__UpperCAmelCase   :						int      							=					hidden_act






					@classmethod
					def lowerCamelCase__							(       cls    : List[str] ,       UpperCamelCase    : Union[str, os.PathLike] ,       **UpperCamelCase    : Tuple  ):
												'''simple docstring'''






												cls._set_token_in_kwargs(UpperCamelCase  )

												__UpperCAmelCase							,__UpperCAmelCase   :						Optional[int]      							=					cls.get_config_dict(UpperCamelCase ,       **UpperCamelCase  )

												# get the vision config dict if we are loading from AltCLIPConfig
												if config_dict.get("""model_type"""  ) == "altclip":
																			__UpperCAmelCase   :						str      							=					config_dict["""vision_config"""]

												if "model_type" in config_dict and hasattr(cls ,       """model_type"""  ) and config_dict["model_type"] != cls.model_type:
																			logger.warning(
																			    f'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type '''
																			    f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''  )

												return cls.from_dict(UpperCamelCase ,       **UpperCamelCase  )






class      lowerCamelCase__   (     A				):







					"""simple docstring"""


					__a	       =							"""altclip"""
					__a	       =							True






					def __init__(       self    : List[Any] ,       UpperCamelCase    : Optional[Any]=None ,       UpperCamelCase    : Any=None ,       UpperCamelCase    : Tuple=768 ,       UpperCamelCase    : Optional[Any]=2.6592 ,       **UpperCamelCase    : Any  ):
												'''simple docstring'''






												__UpperCAmelCase   :						List[Any]      							=					kwargs.pop("""text_config_dict""" ,       UpperCamelCase  )
												__UpperCAmelCase   :						Any      							=					kwargs.pop("""vision_config_dict""" ,       UpperCamelCase  )

												super().__init__(**UpperCamelCase  )

												# Instead of simply assigning `[text|vision]_config_dict` to `[text|vision]_config`, we use the values in
												# `[text|vision]_config_dict` to update the values in `[text|vision]_config`. The values should be same in most
												# cases, but we don't want to break anything regarding `_config_dict` that existed before commit `8827e1b2`.
												if text_config_dict is not None:
																			if text_config is None:
																										__UpperCAmelCase   :						Any      							=					{}

																			# This is the complete result when using `text_config_dict`.
																			__UpperCAmelCase   :						Union[str, Any]      							=					AltCLIPTextConfig(**UpperCamelCase  ).to_dict()

																			# Give a warning if the values exist in both `_text_config_dict` and `text_config` but being different.
																			for key, value in _text_config_dict.items():
																										if key in text_config and value != text_config[key] and key not in ["transformers_version"]:
																																	# If specified in `text_config_dict`
																																	if key in text_config_dict:
																																								__UpperCAmelCase   :						int      							=					(
																																								    f'''`{key}` is found in both `text_config_dict` and `text_config` but with different values. '''
																																								    f'''The value `text_config_dict["{key}"]` will be used instead.'''
																																								)
																																	# If inferred from default argument values (just to be super careful)
																																	else:
																																								__UpperCAmelCase   :						Dict      							=					(
																																								    f'''`text_config_dict` is provided which will be used to initialize `AltCLIPTextConfig`. The '''
																																								    f'''value `text_config["{key}"]` will be overriden.'''
																																								)
																																	logger.warning(UpperCamelCase  )

            # Update all values in `text_config` with the ones in `_text_config_dict`.
																			text_config.update(_text_config_dict  )

												if vision_config_dict is not None:
																			if vision_config is None:
																										__UpperCAmelCase   :						List[Any]      							=					{}

																			# This is the complete result when using `vision_config_dict`.
																			__UpperCAmelCase   :						Union[str, Any]      							=					AltCLIPVisionConfig(**UpperCamelCase  ).to_dict()
																			# convert keys to string instead of integer
																			if "id2label" in _vision_config_dict:
																										__UpperCAmelCase   :						Optional[Any]      							=					{
																										    str(UpperCamelCase  ): value for key, value in _vision_config_dict["""id2label"""].items()
																										}

																			# Give a warning if the values exist in both `_vision_config_dict` and `vision_config` but being different.
																			for key, value in _vision_config_dict.items():
																										if key in vision_config and value != vision_config[key] and key not in ["transformers_version"]:
																																	# If specified in `vision_config_dict`
																																	if key in vision_config_dict:
																																								__UpperCAmelCase   :						Union[str, Any]      							=					(
																																								    f'''`{key}` is found in both `vision_config_dict` and `vision_config` but with different '''
																																								    f'''values. The value `vision_config_dict["{key}"]` will be used instead.'''
																																								)
																																	# If inferred from default argument values (just to be super careful)
																																	else:
																																								__UpperCAmelCase   :						Dict      							=					(
																																								    f'''`vision_config_dict` is provided which will be used to initialize `AltCLIPVisionConfig`. '''
																																								    f'''The value `vision_config["{key}"]` will be overriden.'''
																																								)
																																	logger.warning(UpperCamelCase  )

            # Update all values in `vision_config` with the ones in `_vision_config_dict`.
																			vision_config.update(_vision_config_dict  )

												if text_config is None:
																			__UpperCAmelCase   :						List[str]      							=					{}
																			logger.info("""`text_config` is `None`. Initializing the `AltCLIPTextConfig` with default values."""  )

												if vision_config is None:
																			__UpperCAmelCase   :						List[Any]      							=					{}
																			logger.info("""`vision_config` is `None`. initializing the `AltCLIPVisionConfig` with default values."""  )

												__UpperCAmelCase   :						List[Any]      							=					AltCLIPTextConfig(**UpperCamelCase  )
												__UpperCAmelCase   :						Optional[int]      							=					AltCLIPVisionConfig(**UpperCamelCase  )

												__UpperCAmelCase   :						int      							=					projection_dim
												__UpperCAmelCase   :						str      							=					logit_scale_init_value
												__UpperCAmelCase   :						Optional[Any]      							=					1.0






					@classmethod
					def lowerCamelCase__							(       cls    : Optional[Any] ,       UpperCamelCase    : AltCLIPTextConfig ,       UpperCamelCase    : AltCLIPVisionConfig ,       **UpperCamelCase    : Union[str, Any]  ):
												'''simple docstring'''






												return cls(text_config=text_config.to_dict() ,       vision_config=vision_config.to_dict() ,       **UpperCamelCase  )






					def lowerCamelCase__							(       self    : Any  ):
												'''simple docstring'''






												__UpperCAmelCase   :						List[str]      							=					copy.deepcopy(self.__dict__  )
												__UpperCAmelCase   :						Tuple      							=					self.text_config.to_dict()
												__UpperCAmelCase   :						str      							=					self.vision_config.to_dict()
												__UpperCAmelCase   :						Dict      							=					self.__class__.model_type
												return output




"""simple docstring"""






import unittest

from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, 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__							(       *UpperCamelCase    : List[str] ,       **UpperCamelCase    : Any  ):
																			'''simple docstring'''






																			pass





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







					"""simple docstring"""


					__a	       =							MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING






					def lowerCamelCase__							(       self    : List[str] ,       UpperCamelCase    : Tuple ,       UpperCamelCase    : Optional[int] ,       UpperCamelCase    : Tuple  ):
												'''simple docstring'''






												__UpperCAmelCase   :						Optional[int]      							=					pipeline(
												    """zero-shot-object-detection""" ,       model="""hf-internal-testing/tiny-random-owlvit-object-detection"""  )

												__UpperCAmelCase   :						Tuple      							=					[
												    {
												        """image""": """./tests/fixtures/tests_samples/COCO/000000039769.png""",
												        """candidate_labels""": ["""cat""", """remote""", """couch"""],
												    }
												]
												return object_detector, examples






					def lowerCamelCase__							(       self    : int ,       UpperCamelCase    : Optional[int] ,       UpperCamelCase    : List[Any]  ):
												'''simple docstring'''






												__UpperCAmelCase   :						Union[str, Any]      							=					object_detector(examples[0] ,       threshold=0.0  )

												__UpperCAmelCase   :						Tuple      							=					len(UpperCamelCase  )
												self.assertGreater(UpperCamelCase ,       0  )
												self.assertEqual(
												    UpperCamelCase ,       [
												        {
												            """score""": ANY(UpperCamelCase  ),
												            """label""": ANY(UpperCamelCase  ),
												            """box""": {"""xmin""": ANY(UpperCamelCase  ), """ymin""": ANY(UpperCamelCase  ), """xmax""": ANY(UpperCamelCase  ), """ymax""": ANY(UpperCamelCase  )},
												        }
												        for i in range(UpperCamelCase  )
												    ] ,       )






					@require_tf
					@unittest.skip("""Zero Shot Object Detection not implemented in TF"""  )
					def lowerCamelCase__							(       self    : Optional[int]  ):
												'''simple docstring'''






												pass






					@require_torch
					def lowerCamelCase__							(       self    : Union[str, Any]  ):
												'''simple docstring'''






												__UpperCAmelCase   :						Tuple      							=					pipeline(
												    """zero-shot-object-detection""" ,       model="""hf-internal-testing/tiny-random-owlvit-object-detection"""  )

												__UpperCAmelCase   :						Tuple      							=					object_detector(
												    """./tests/fixtures/tests_samples/COCO/000000039769.png""" ,       candidate_labels=["""cat""", """remote""", """couch"""] ,       threshold=0.64 ,       )

												self.assertEqual(
												    nested_simplify(UpperCamelCase ,       decimals=4  ) ,       [
												        {"""score""": 0.7235, """label""": """cat""", """box""": {"""xmin""": 204, """ymin""": 167, """xmax""": 232, """ymax""": 190}},
												        {"""score""": 0.7218, """label""": """remote""", """box""": {"""xmin""": 204, """ymin""": 167, """xmax""": 232, """ymax""": 190}},
												        {"""score""": 0.7184, """label""": """couch""", """box""": {"""xmin""": 204, """ymin""": 167, """xmax""": 232, """ymax""": 190}},
												        {"""score""": 0.6748, """label""": """remote""", """box""": {"""xmin""": 571, """ymin""": 83, """xmax""": 598, """ymax""": 103}},
												        {"""score""": 0.6656, """label""": """cat""", """box""": {"""xmin""": 571, """ymin""": 83, """xmax""": 598, """ymax""": 103}},
												        {"""score""": 0.6614, """label""": """couch""", """box""": {"""xmin""": 571, """ymin""": 83, """xmax""": 598, """ymax""": 103}},
												        {"""score""": 0.6456, """label""": """remote""", """box""": {"""xmin""": 494, """ymin""": 105, """xmax""": 521, """ymax""": 127}},
												        {"""score""": 0.642, """label""": """remote""", """box""": {"""xmin""": 67, """ymin""": 274, """xmax""": 93, """ymax""": 297}},
												        {"""score""": 0.6419, """label""": """cat""", """box""": {"""xmin""": 494, """ymin""": 105, """xmax""": 521, """ymax""": 127}},
												    ] ,       )

												__UpperCAmelCase   :						Any      							=					object_detector(
												    [
												        {
												            """image""": """./tests/fixtures/tests_samples/COCO/000000039769.png""",
												            """candidate_labels""": ["""cat""", """remote""", """couch"""],
												        }
												    ] ,       threshold=0.64 ,       )

												self.assertEqual(
												    nested_simplify(UpperCamelCase ,       decimals=4  ) ,       [
												        [
												            {"""score""": 0.7235, """label""": """cat""", """box""": {"""xmin""": 204, """ymin""": 167, """xmax""": 232, """ymax""": 190}},
												            {"""score""": 0.7218, """label""": """remote""", """box""": {"""xmin""": 204, """ymin""": 167, """xmax""": 232, """ymax""": 190}},
												            {"""score""": 0.7184, """label""": """couch""", """box""": {"""xmin""": 204, """ymin""": 167, """xmax""": 232, """ymax""": 190}},
												            {"""score""": 0.6748, """label""": """remote""", """box""": {"""xmin""": 571, """ymin""": 83, """xmax""": 598, """ymax""": 103}},
												            {"""score""": 0.6656, """label""": """cat""", """box""": {"""xmin""": 571, """ymin""": 83, """xmax""": 598, """ymax""": 103}},
												            {"""score""": 0.6614, """label""": """couch""", """box""": {"""xmin""": 571, """ymin""": 83, """xmax""": 598, """ymax""": 103}},
												            {"""score""": 0.6456, """label""": """remote""", """box""": {"""xmin""": 494, """ymin""": 105, """xmax""": 521, """ymax""": 127}},
												            {"""score""": 0.642, """label""": """remote""", """box""": {"""xmin""": 67, """ymin""": 274, """xmax""": 93, """ymax""": 297}},
												            {"""score""": 0.6419, """label""": """cat""", """box""": {"""xmin""": 494, """ymin""": 105, """xmax""": 521, """ymax""": 127}},
												        ]
												    ] ,       )






					@require_torch
					@slow
					def lowerCamelCase__							(       self    : Optional[int]  ):
												'''simple docstring'''






												__UpperCAmelCase   :						Any      							=					pipeline("""zero-shot-object-detection"""  )

												__UpperCAmelCase   :						int      							=					object_detector(
												    """http://images.cocodataset.org/val2017/000000039769.jpg""" ,       candidate_labels=["""cat""", """remote""", """couch"""] ,       )
												self.assertEqual(
												    nested_simplify(UpperCamelCase ,       decimals=4  ) ,       [
												        {"""score""": 0.2868, """label""": """cat""", """box""": {"""xmin""": 324, """ymin""": 20, """xmax""": 640, """ymax""": 373}},
												        {"""score""": 0.277, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 72, """xmax""": 177, """ymax""": 115}},
												        {"""score""": 0.2537, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 55, """xmax""": 315, """ymax""": 472}},
												        {"""score""": 0.1474, """label""": """remote""", """box""": {"""xmin""": 335, """ymin""": 74, """xmax""": 371, """ymax""": 187}},
												        {"""score""": 0.1208, """label""": """couch""", """box""": {"""xmin""": 4, """ymin""": 0, """xmax""": 642, """ymax""": 476}},
												    ] ,       )

												__UpperCAmelCase   :						List[str]      							=					object_detector(
												    [
												        {
												            """image""": """http://images.cocodataset.org/val2017/000000039769.jpg""",
												            """candidate_labels""": ["""cat""", """remote""", """couch"""],
												        },
												        {
												            """image""": """http://images.cocodataset.org/val2017/000000039769.jpg""",
												            """candidate_labels""": ["""cat""", """remote""", """couch"""],
												        },
												    ] ,       )
												self.assertEqual(
												    nested_simplify(UpperCamelCase ,       decimals=4  ) ,       [
												        [
												            {"""score""": 0.2868, """label""": """cat""", """box""": {"""xmin""": 324, """ymin""": 20, """xmax""": 640, """ymax""": 373}},
												            {"""score""": 0.277, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 72, """xmax""": 177, """ymax""": 115}},
												            {"""score""": 0.2537, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 55, """xmax""": 315, """ymax""": 472}},
												            {"""score""": 0.1474, """label""": """remote""", """box""": {"""xmin""": 335, """ymin""": 74, """xmax""": 371, """ymax""": 187}},
												            {"""score""": 0.1208, """label""": """couch""", """box""": {"""xmin""": 4, """ymin""": 0, """xmax""": 642, """ymax""": 476}},
												        ],
												        [
												            {"""score""": 0.2868, """label""": """cat""", """box""": {"""xmin""": 324, """ymin""": 20, """xmax""": 640, """ymax""": 373}},
												            {"""score""": 0.277, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 72, """xmax""": 177, """ymax""": 115}},
												            {"""score""": 0.2537, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 55, """xmax""": 315, """ymax""": 472}},
												            {"""score""": 0.1474, """label""": """remote""", """box""": {"""xmin""": 335, """ymin""": 74, """xmax""": 371, """ymax""": 187}},
												            {"""score""": 0.1208, """label""": """couch""", """box""": {"""xmin""": 4, """ymin""": 0, """xmax""": 642, """ymax""": 476}},
												        ],
												    ] ,       )






					@require_tf
					@unittest.skip("""Zero Shot Object Detection not implemented in TF"""  )
					def lowerCamelCase__							(       self    : Any  ):
												'''simple docstring'''






												pass






					@require_torch
					@slow
					def lowerCamelCase__							(       self    : str  ):
												'''simple docstring'''






												__UpperCAmelCase   :						List[str]      							=					0.2
												__UpperCAmelCase   :						List[Any]      							=					pipeline("""zero-shot-object-detection"""  )

												__UpperCAmelCase   :						Any      							=					object_detector(
												    """http://images.cocodataset.org/val2017/000000039769.jpg""" ,       candidate_labels=["""cat""", """remote""", """couch"""] ,       threshold=UpperCamelCase ,       )
												self.assertEqual(
												    nested_simplify(UpperCamelCase ,       decimals=4  ) ,       [
												        {"""score""": 0.2868, """label""": """cat""", """box""": {"""xmin""": 324, """ymin""": 20, """xmax""": 640, """ymax""": 373}},
												        {"""score""": 0.277, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 72, """xmax""": 177, """ymax""": 115}},
												        {"""score""": 0.2537, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 55, """xmax""": 315, """ymax""": 472}},
												    ] ,       )






					@require_torch
					@slow
					def lowerCamelCase__							(       self    : int  ):
												'''simple docstring'''






												__UpperCAmelCase   :						List[Any]      							=					2
												__UpperCAmelCase   :						Union[str, Any]      							=					pipeline("""zero-shot-object-detection"""  )

												__UpperCAmelCase   :						List[str]      							=					object_detector(
												    """http://images.cocodataset.org/val2017/000000039769.jpg""" ,       candidate_labels=["""cat""", """remote""", """couch"""] ,       top_k=UpperCamelCase ,       )
												self.assertEqual(
												    nested_simplify(UpperCamelCase ,       decimals=4  ) ,       [
												        {"""score""": 0.2868, """label""": """cat""", """box""": {"""xmin""": 324, """ymin""": 20, """xmax""": 640, """ymax""": 373}},
												        {"""score""": 0.277, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 72, """xmax""": 177, """ymax""": 115}},
												    ] ,       )
