





import PIL.Image
import PIL.ImageOps
from packaging import version
from PIL import Image


if version.parse(version.parse(PIL.__version__).base_version) >= version.parse("9.1.0"):
					UpperCamelCase__           =			{
					    "linear": PIL.Image.Resampling.BILINEAR,
					    "bilinear": PIL.Image.Resampling.BILINEAR,
					    "bicubic": PIL.Image.Resampling.BICUBIC,
					    "lanczos": PIL.Image.Resampling.LANCZOS,
					    "nearest": PIL.Image.Resampling.NEAREST,
					}
else:
					UpperCamelCase__           =			{
					    "linear": PIL.Image.LINEAR,
					    "bilinear": PIL.Image.BILINEAR,
					    "bicubic": PIL.Image.BICUBIC,
					    "lanczos": PIL.Image.LANCZOS,
					    "nearest": PIL.Image.NEAREST,
					}





def      _UpperCamelCase  (a__ :List[Any]							):






							"""simple docstring"""





							UpperCamelCase__					  =      (images / 2 + 0.5).clamp(0 ,      1							)
							UpperCamelCase__					  =      images.cpu().permute(0 ,      2 ,      3 ,      1							).float().numpy()
							UpperCamelCase__					  =      numpy_to_pil(lowerCamelCase__							)
							return images





def      _UpperCamelCase  (a__ :List[Any]							):






							"""simple docstring"""





							if images.ndim == 3:
														UpperCamelCase__					  =      images[None, ...]
							UpperCamelCase__					  =      (images * 255).round().astype("""uint8"""							)
							if images.shape[-1] == 1:
														# special case for grayscale (single channel) images
														UpperCamelCase__					  =      [Image.fromarray(image.squeeze() ,      mode="""L"""							) for image in images]
							else:
														UpperCamelCase__					  =      [Image.fromarray(lowerCamelCase__							) for image in images]

							return pil_images






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

from ...utils import logging


__A							=logging.get_logger(__name__)




class        _SCREAMING_SNAKE_CASE	( snake_case_		):
	lowerCAmelCase__   =					CLIPConfig

	lowerCAmelCase__   =					['CLIPEncoderLayer']



	def __init__(       self		,    lowercase					)							->			int:
					super().__init__(lowercase					)

					lowerCamelCase_         =     CLIPVisionModelWithProjection(config.vision_config					)

					lowerCamelCase_         =     nn.Linear(config.vision_config.projection_dim		,    1					)
					lowerCamelCase_         =     nn.Linear(config.vision_config.projection_dim		,    1					)



	@torch.no_grad()
	def        SCREAMING_SNAKE_CASE_(       self		,    lowercase		,    lowercase		,    lowercase=0.5		,    lowercase=0.5					)							->			int:
					lowerCamelCase_         =     self.vision_model(lowercase					)[0]

					lowerCamelCase_         =     self.p_head(lowercase					)
					lowerCamelCase_         =     nsfw_detected.flatten()
					lowerCamelCase_         =     nsfw_detected > p_threshold
					lowerCamelCase_         =     nsfw_detected.tolist()

					if any(lowercase					):
									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(lowercase					):
									if nsfw_detected_:
													lowerCamelCase_         =     np.zeros(images[idx].shape					)

					lowerCamelCase_         =     self.w_head(lowercase					)
					lowerCamelCase_         =     watermark_detected.flatten()
					lowerCamelCase_         =     watermark_detected > w_threshold
					lowerCamelCase_         =     watermark_detected.tolist()

					if any(lowercase					):
									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(lowercase					):
									if watermark_detected_:
													lowerCamelCase_         =     np.zeros(images[idx].shape					)

					return images, nsfw_detected, watermark_detected


