






'''simple docstring'''







import numpy as np
import torch
import torch.nn as nn
from transformers import CLIPConfig, CLIPVisionModelWithProjection, PreTrainedModel

from ...utils import logging


_A	:			Any         =logging.get_logger(__name__)

class _lowercase	(				_lowercase		):
							a				      =       CLIPConfig

							a				      =       ["""CLIPEncoderLayer"""]




							def __init__(							self:				Union[str, Any]			,							UpperCamelCase__:				CLIPConfig     ):
														super().__init__(UpperCamelCase__     )

														lowerCamelCase__					:	List[Any]         =       CLIPVisionModelWithProjection(config.vision_config     )

														lowerCamelCase__					:	str         =       nn.Linear(config.vision_config.projection_dim			,							1     )
														lowerCamelCase__					:	Any         =       nn.Linear(config.vision_config.projection_dim			,							1     )




							@torch.no_grad()
							def 		lowerCamelCase_				(							self:				Dict			,							UpperCamelCase__:				List[str]			,							UpperCamelCase__:				Optional[int]			,							UpperCamelCase__:				List[Any]=0.5			,							UpperCamelCase__:				Tuple=0.5     ):
														lowerCamelCase__					:	List[str]         =       self.vision_model(UpperCamelCase__     )[0]

														lowerCamelCase__					:	Optional[Any]         =       self.p_head(UpperCamelCase__     )
														lowerCamelCase__					:	Any         =       nsfw_detected.flatten()
														lowerCamelCase__					:	List[str]         =       nsfw_detected > p_threshold
														lowerCamelCase__					:	int         =       nsfw_detected.tolist()

														if any(UpperCamelCase__     ):
																					logger.warning(
																					    """Potential NSFW content was detected in one or more images. A black image will be returned instead."""
																					    """ Try again with a different prompt and/or seed."""     )

														for idx, nsfw_detected_ in enumerate(UpperCamelCase__     ):
																					if nsfw_detected_:
																												lowerCamelCase__					:	Optional[Any]         =       np.zeros(images[idx].shape     )

														lowerCamelCase__					:	List[Any]         =       self.w_head(UpperCamelCase__     )
														lowerCamelCase__					:	Any         =       watermark_detected.flatten()
														lowerCamelCase__					:	Optional[int]         =       watermark_detected > w_threshold
														lowerCamelCase__					:	int         =       watermark_detected.tolist()

														if any(UpperCamelCase__     ):
																					logger.warning(
																					    """Potential watermarked content was detected in one or more images. A black image will be returned instead."""
																					    """ Try again with a different prompt and/or seed."""     )

														for idx, watermark_detected_ in enumerate(UpperCamelCase__     ):
																					if watermark_detected_:
																												lowerCamelCase__					:	List[str]         =       np.zeros(images[idx].shape     )

														return images, nsfw_detected, watermark_detected









'''simple docstring'''







import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionImageVariationPipeline
from diffusers.utils.testing_utils import load_image, require_torch_gpu, slow, torch_device


_A	:			Union[str, Any]         =False

class _lowercase	(				unittest.TestCase		):
							pass



@slow
@require_torch_gpu
class _lowercase	(				unittest.TestCase		):




							def 		lowerCamelCase_				(							self:				Optional[int]     ):
														lowerCamelCase__					:	Tuple         =       VersatileDiffusionImageVariationPipeline.from_pretrained("""shi-labs/versatile-diffusion"""     )
														pipe.to(UpperCamelCase__     )
														pipe.set_progress_bar_config(disable=UpperCamelCase__     )

														lowerCamelCase__					:	Optional[int]         =       load_image(
														    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg"""     )
														lowerCamelCase__					:	List[Any]         =       torch.manual_seed(0     )
														lowerCamelCase__					:	List[Any]         =       pipe(
														    image=UpperCamelCase__			,							generator=UpperCamelCase__			,							guidance_scale=7.5			,							num_inference_steps=50			,							output_type="""numpy"""			,							).images

														lowerCamelCase__					:	List[str]         =       image[0, 253:256, 253:256, -1]

														assert image.shape == (1, 512, 512, 3)
														lowerCamelCase__					:	Tuple         =       np.array([0.0_441, 0.0_469, 0.0_507, 0.0_575, 0.0_632, 0.0_650, 0.0_865, 0.0_909, 0.0_945]     )

														assert np.abs(image_slice.flatten() - expected_slice     ).max() < 1e-2

