



from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    convert_to_rgb,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    OPENAI_CLIP_MEAN,
    OPENAI_CLIP_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


_lowercase     							=						logging.get_logger(__name__)


if is_vision_available():
				import PIL
class 				__A					( A_      ):
			UpperCamelCase					:Optional[int]     =			['''pixel_values''']





			def __init__(self							,    __magic_name__ = True							,    __magic_name__ = None							,    __magic_name__ = PILImageResampling.BICUBIC							,    __magic_name__ = True							,    __magic_name__ = None							,    __magic_name__ = True							,    __magic_name__ = 1 / 255							,    __magic_name__ = True							,    __magic_name__ = None							,    __magic_name__ = None							,    __magic_name__ = True							,    **__magic_name__							,    ):
						super().__init__(**__magic_name__				)
						lowerCamelCase__ :      int								=		size if size is not None else {"""shortest_edge""": 224}
						lowerCamelCase__ :      Tuple								=		get_size_dict(__magic_name__							,    default_to_square=__magic_name__				)
						lowerCamelCase__ :      Tuple								=		crop_size if crop_size is not None else {"""height""": 224, """width""": 224}
						lowerCamelCase__ :      List[str]								=		get_size_dict(__magic_name__							,    default_to_square=__magic_name__							,    param_name="""crop_size"""				)

						lowerCamelCase__ :      Optional[int]								=		do_resize
						lowerCamelCase__ :      Optional[int]								=		size
						lowerCamelCase__ :      int								=		resample
						lowerCamelCase__ :      int								=		do_center_crop
						lowerCamelCase__ :      Union[str, Any]								=		crop_size
						lowerCamelCase__ :      Dict								=		do_rescale
						lowerCamelCase__ :      Dict								=		rescale_factor
						lowerCamelCase__ :      Optional[int]								=		do_normalize
						lowerCamelCase__ :      Union[str, Any]								=		image_mean if image_mean is not None else OPENAI_CLIP_MEAN
						lowerCamelCase__ :      str								=		image_std if image_std is not None else OPENAI_CLIP_STD
						lowerCamelCase__ :      Optional[int]								=		do_convert_rgb





			def        _snake_case    (self							,    __magic_name__							,    __magic_name__							,    __magic_name__ = PILImageResampling.BICUBIC							,    __magic_name__ = None							,    **__magic_name__							,    ):
						lowerCamelCase__ :      List[str]								=		get_size_dict(__magic_name__							,    default_to_square=__magic_name__				)
						if "shortest_edge" not in size:
									raise ValueError(f"The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}"				)
						lowerCamelCase__ :      Optional[Any]								=		get_resize_output_image_size(__magic_name__							,    size=size["""shortest_edge"""]							,    default_to_square=__magic_name__				)
						return resize(__magic_name__							,    size=__magic_name__							,    resample=__magic_name__							,    data_format=__magic_name__							,    **__magic_name__				)





			def        _snake_case    (self							,    __magic_name__							,    __magic_name__							,    __magic_name__ = None							,    **__magic_name__							,    ):
						lowerCamelCase__ :      Dict								=		get_size_dict(__magic_name__				)
						if "height" not in size or "width" not in size:
									raise ValueError(f"The `size` parameter must contain the keys (height, width). Got {size.keys()}"				)
						return center_crop(__magic_name__							,    size=(size["""height"""], size["""width"""])							,    data_format=__magic_name__							,    **__magic_name__				)





			def        _snake_case    (self							,    __magic_name__							,    __magic_name__							,    __magic_name__ = None							,    **__magic_name__							,    ):
						return rescale(__magic_name__							,    scale=__magic_name__							,    data_format=__magic_name__							,    **__magic_name__				)





			def        _snake_case    (self							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__ = None							,    **__magic_name__							,    ):
						return normalize(__magic_name__							,    mean=__magic_name__							,    std=__magic_name__							,    data_format=__magic_name__							,    **__magic_name__				)





			def        _snake_case    (self							,    __magic_name__							,    __magic_name__ = None							,    __magic_name__ = None							,    __magic_name__ = None							,    __magic_name__ = None							,    __magic_name__ = None							,    __magic_name__ = None							,    __magic_name__ = None							,    __magic_name__ = None							,    __magic_name__ = None							,    __magic_name__ = None							,    __magic_name__ = None							,    __magic_name__ = None							,    __magic_name__ = ChannelDimension.FIRST							,    **__magic_name__							,    ):
						lowerCamelCase__ :      str								=		do_resize if do_resize is not None else self.do_resize
						lowerCamelCase__ :      Optional[int]								=		size if size is not None else self.size
						lowerCamelCase__ :      Tuple								=		get_size_dict(__magic_name__							,    param_name="""size"""							,    default_to_square=__magic_name__				)
						lowerCamelCase__ :      Optional[int]								=		resample if resample is not None else self.resample
						lowerCamelCase__ :      List[Any]								=		do_center_crop if do_center_crop is not None else self.do_center_crop
						lowerCamelCase__ :      Union[str, Any]								=		crop_size if crop_size is not None else self.crop_size
						lowerCamelCase__ :      Optional[int]								=		get_size_dict(__magic_name__							,    param_name="""crop_size"""							,    default_to_square=__magic_name__				)
						lowerCamelCase__ :      Optional[int]								=		do_rescale if do_rescale is not None else self.do_rescale
						lowerCamelCase__ :      List[str]								=		rescale_factor if rescale_factor is not None else self.rescale_factor
						lowerCamelCase__ :      List[Any]								=		do_normalize if do_normalize is not None else self.do_normalize
						lowerCamelCase__ :      Any								=		image_mean if image_mean is not None else self.image_mean
						lowerCamelCase__ :      Tuple								=		image_std if image_std is not None else self.image_std
						lowerCamelCase__ :      Union[str, Any]								=		do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

						lowerCamelCase__ :      str								=		make_list_of_images(__magic_name__				)

						if not valid_images(__magic_name__				):
									raise ValueError(
									    """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """
									    """torch.Tensor, tf.Tensor or jax.ndarray."""				)

						if do_resize and size is None:
									raise ValueError("""Size must be specified if do_resize is True."""				)

						if do_center_crop and crop_size is None:
									raise ValueError("""Crop size must be specified if do_center_crop is True."""				)

						if do_rescale and rescale_factor is None:
									raise ValueError("""Rescale factor must be specified if do_rescale is True."""				)

						if do_normalize and (image_mean is None or image_std is None):
									raise ValueError("""Image mean and std must be specified if do_normalize is True."""				)

						# PIL RGBA images are converted to RGB
						if do_convert_rgb:
									lowerCamelCase__ :      Any								=		[convert_to_rgb(__magic_name__				) for image in images]

						# All transformations expect numpy arrays.
						lowerCamelCase__ :      int								=		[to_numpy_array(__magic_name__				) for image in images]

						if do_resize:
									lowerCamelCase__ :      Any								=		[self.resize(image=__magic_name__							,    size=__magic_name__							,    resample=__magic_name__				) for image in images]

						if do_center_crop:
									lowerCamelCase__ :      int								=		[self.center_crop(image=__magic_name__							,    size=__magic_name__				) for image in images]

						if do_rescale:
									lowerCamelCase__ :      Dict								=		[self.rescale(image=__magic_name__							,    scale=__magic_name__				) for image in images]

						if do_normalize:
									lowerCamelCase__ :      Any								=		[self.normalize(image=__magic_name__							,    mean=__magic_name__							,    std=__magic_name__				) for image in images]

						lowerCamelCase__ :      List[Any]								=		[to_channel_dimension_format(__magic_name__							,    __magic_name__				) for image in images]

						lowerCamelCase__ :      Optional[int]								=		{"""pixel_values""": images}
						return BatchFeature(data=__magic_name__							,    tensor_type=__magic_name__				)








import torch
import torch.nn as nn
from transformers import CLIPConfig, CLIPVisionModel, PreTrainedModel

from ...utils import logging


_lowercase     							=						logging.get_logger(__name__)






def      _A				(UpperCamelCase		:      Dict      ,			UpperCamelCase		:      Optional[int] )  ->Tuple:



			'''simple docstring'''







			lowerCamelCase__ :      List[str]								=		nn.functional.normalize(UpperCamelCase )
			lowerCamelCase__ :      Tuple								=		nn.functional.normalize(UpperCamelCase )
			return torch.mm(UpperCamelCase      ,			normalized_text_embeds.t() )
class 				__A					( A_      ):
			UpperCamelCase					:Optional[Any]     =			CLIPConfig

			UpperCamelCase					:Optional[Any]     =			['''CLIPEncoderLayer''']





			def __init__(self							,    __magic_name__				):
						super().__init__(__magic_name__				)

						lowerCamelCase__ :      Union[str, Any]								=		CLIPVisionModel(config.vision_config				)
						lowerCamelCase__ :      Any								=		nn.Linear(config.vision_config.hidden_size							,    config.projection_dim							,    bias=__magic_name__				)

						lowerCamelCase__ :      Any								=		nn.Parameter(torch.ones(17							,    config.projection_dim				)							,    requires_grad=__magic_name__				)
						lowerCamelCase__ :      Union[str, Any]								=		nn.Parameter(torch.ones(3							,    config.projection_dim				)							,    requires_grad=__magic_name__				)

						lowerCamelCase__ :      Any								=		nn.Parameter(torch.ones(17				)							,    requires_grad=__magic_name__				)
						lowerCamelCase__ :      Optional[Any]								=		nn.Parameter(torch.ones(3				)							,    requires_grad=__magic_name__				)





			@torch.no_grad()
			def        _snake_case    (self							,    __magic_name__							,    __magic_name__				):
						lowerCamelCase__ :      Any								=		self.vision_model(__magic_name__				)[1]  # pooled_output
						lowerCamelCase__ :      Tuple								=		self.visual_projection(__magic_name__				)

						# we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
						lowerCamelCase__ :      Any								=		cosine_distance(__magic_name__							,    self.special_care_embeds				).cpu().float().numpy()
						lowerCamelCase__ :      str								=		cosine_distance(__magic_name__							,    self.concept_embeds				).cpu().float().numpy()

						lowerCamelCase__ :      int								=		[]
						lowerCamelCase__ :      str								=		image_embeds.shape[0]
						for i in range(__magic_name__				):
									lowerCamelCase__ :      str								=		{"""special_scores""": {}, """special_care""": [], """concept_scores""": {}, """bad_concepts""": []}

									# increase this value to create a stronger `nfsw` filter
									# at the cost of increasing the possibility of filtering benign images
									lowerCamelCase__ :      Optional[Any]								=		0.0

									for concept_idx in range(len(special_cos_dist[0]				)				):
												lowerCamelCase__ :      List[str]								=		special_cos_dist[i][concept_idx]
												lowerCamelCase__ :      Tuple								=		self.special_care_embeds_weights[concept_idx].item()
												lowerCamelCase__ :      str								=		round(concept_cos - concept_threshold + adjustment							,    3				)
												if result_img["special_scores"][concept_idx] > 0:
															result_img["special_care"].append({concept_idx, result_img["""special_scores"""][concept_idx]}				)
															lowerCamelCase__ :      List[str]								=		0.01

									for concept_idx in range(len(cos_dist[0]				)				):
												lowerCamelCase__ :      str								=		cos_dist[i][concept_idx]
												lowerCamelCase__ :      Dict								=		self.concept_embeds_weights[concept_idx].item()
												lowerCamelCase__ :      int								=		round(concept_cos - concept_threshold + adjustment							,    3				)
												if result_img["concept_scores"][concept_idx] > 0:
															result_img["bad_concepts"].append(__magic_name__				)

									result.append(__magic_name__				)

						lowerCamelCase__ :      Tuple								=		[len(res["""bad_concepts"""]				) > 0 for res in result]

						return images, has_nsfw_concepts





			@torch.no_grad()
			def        _snake_case    (self							,    __magic_name__							,    __magic_name__				):
						lowerCamelCase__ :      Union[str, Any]								=		self.vision_model(__magic_name__				)[1]  # pooled_output
						lowerCamelCase__ :      str								=		self.visual_projection(__magic_name__				)

						lowerCamelCase__ :      Optional[Any]								=		cosine_distance(__magic_name__							,    self.special_care_embeds				)
						lowerCamelCase__ :      Dict								=		cosine_distance(__magic_name__							,    self.concept_embeds				)

						# increase this value to create a stronger `nsfw` filter
						# at the cost of increasing the possibility of filtering benign images
						lowerCamelCase__ :      Any								=		0.0

						lowerCamelCase__ :      Dict								=		special_cos_dist - self.special_care_embeds_weights + adjustment
						# special_scores = special_scores.round(decimals=3)
						lowerCamelCase__ :      Union[str, Any]								=		torch.any(special_scores > 0							,    dim=1				)
						lowerCamelCase__ :      Optional[Any]								=		special_care * 0.01
						lowerCamelCase__ :      Dict								=		special_adjustment.unsqueeze(1				).expand(-1							,    cos_dist.shape[1]				)

						lowerCamelCase__ :      Tuple								=		(cos_dist - self.concept_embeds_weights) + special_adjustment
						# concept_scores = concept_scores.round(decimals=3)
						lowerCamelCase__ :      Any								=		torch.any(concept_scores > 0							,    dim=1				)

						return images, has_nsfw_concepts



