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

from ...utils import logging


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



class        lowercase_   (					__snake_case		):
							_lowerCamelCase						=		CLIPConfig

							_lowerCamelCase						=		['CLIPEncoderLayer']



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

													_snake_case						:					List[str]    =   CLIPVisionModelWithProjection(config.vision_config    )

													_snake_case						:					Optional[Any]    =   nn.Linear(config.vision_config.projection_dim				,    1    )
													_snake_case						:					str    =   nn.Linear(config.vision_config.projection_dim				,    1    )



							@torch.no_grad()
							def 	UpperCamelCase	(						self				,    lowercase_				,    lowercase_				,    lowercase_=0.5				,    lowercase_=0.5    ):
													_snake_case						:					Optional[Any]    =   self.vision_model(lowercase_    )[0]

													_snake_case						:					Optional[Any]    =   self.p_head(lowercase_    )
													_snake_case						:					Dict    =   nsfw_detected.flatten()
													_snake_case						:					int    =   nsfw_detected > p_threshold
													_snake_case						:					str    =   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_:
																									_snake_case						:					Optional[int]    =   np.zeros(images[idx].shape    )

													_snake_case						:					int    =   self.w_head(lowercase_    )
													_snake_case						:					List[str]    =   watermark_detected.flatten()
													_snake_case						:					Tuple    =   watermark_detected > w_threshold
													_snake_case						:					str    =   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_:
																									_snake_case						:					Optional[int]    =   np.zeros(images[idx].shape    )

													return images, nsfw_detected, watermark_detected
import torch
from transformers import PreTrainedModel, XLMRobertaConfig, XLMRobertaModel



class        lowercase_   (					__snake_case		):
							_lowerCamelCase						=		'M-CLIP'



							def __init__(						self				,    lowercase_=1_024				,    lowercase_=768				,    **lowercase_    ):
													_snake_case						:					str    =   transformerDimSize
													_snake_case						:					Union[str, Any]    =   imageDimSize
													super().__init__(**lowercase_    )




class        lowercase_   (					__snake_case		):
							_lowerCamelCase						=		MCLIPConfig



							def __init__(						self				,    lowercase_				,    *lowercase_				,    **lowercase_    ):
													super().__init__(lowercase_				,    *lowercase_				,    **lowercase_    )
													_snake_case						:					List[Any]    =   XLMRobertaModel(lowercase_    )
													_snake_case						:					int    =   torch.nn.Linear(
													    in_features=config.transformerDimensions				,    out_features=config.numDims    )



							def 	UpperCamelCase	(						self				,    lowercase_				,    lowercase_    ):
													_snake_case						:					Tuple    =   self.transformer(input_ids=lowercase_				,    attention_mask=lowercase_    )[0]
													_snake_case						:					Tuple    =   (embs * attention_mask.unsqueeze(2    )).sum(dim=1    ) / attention_mask.sum(dim=1    )[:, None]
													return self.LinearTransformation(lowercase_    ), embs