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

from ...utils import logging


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






class 		a__						(					A__					):
					A	       =							CLIPConfig

					A	       =							['CLIPEncoderLayer']

					def __init__(    self      :       Union[str, Any],_A      :       CLIPConfig    ):







							"""simple docstring"""






							super().__init__(_A    )

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

							SCREAMING_SNAKE_CASE_  :		int				      =    nn.Linear(config.vision_config.projection_dim,1    )
							SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    nn.Linear(config.vision_config.projection_dim,1    )






					@torch.no_grad()
					def 				__UpperCamelCase   (    self      :       Union[str, Any],_A      :       int,_A      :       Union[str, Any],_A      :       Optional[Any]=0.5,_A      :       Optional[Any]=0.5    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		List[str]				      =    self.vision_model(_A    )[0]

							SCREAMING_SNAKE_CASE_  :		Tuple				      =    self.p_head(_A    )
							SCREAMING_SNAKE_CASE_  :		List[str]				      =    nsfw_detected.flatten()
							SCREAMING_SNAKE_CASE_  :		List[Any]				      =    nsfw_detected > p_threshold
							SCREAMING_SNAKE_CASE_  :		Any				      =    nsfw_detected.tolist()

							if any(_A    ):
									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(_A    ):
									if nsfw_detected_:
											SCREAMING_SNAKE_CASE_  :		Optional[int]				      =    np.zeros(images[idx].shape    )

							SCREAMING_SNAKE_CASE_  :		List[str]				      =    self.w_head(_A    )
							SCREAMING_SNAKE_CASE_  :		int				      =    watermark_detected.flatten()
							SCREAMING_SNAKE_CASE_  :		List[str]				      =    watermark_detected > w_threshold
							SCREAMING_SNAKE_CASE_  :		Optional[int]				      =    watermark_detected.tolist()

							if any(_A    ):
									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(_A    ):
									if watermark_detected_:
											SCREAMING_SNAKE_CASE_  :		Optional[int]				      =    np.zeros(images[idx].shape    )

							return images, nsfw_detected, watermark_detected

from functools import lru_cache




@lru_cache
def       _snake_case	(   lowerCAmelCase							:   int	):
		"""simple docstring"""







		if num < 0:
				raise ValueError("Number should not be negative."	)

		return 1 if num in (0, 1) else num * factorial(num - 1	)


if __name__ == "__main__":
		import doctest

		doctest.testmod()
