



"""simple docstring"""






from __future__ import annotations

from typing import Dict

from ...configuration_utils import PretrainedConfig


_lowercase			:			Dict										=						{
    "susnato/ernie-m-base_pytorch": "https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/config.json",
    "susnato/ernie-m-large_pytorch": "https://huggingface.co/susnato/ernie-m-large_pytorch/blob/main/config.json",
}

class 	__SCREAMING_SNAKE_CASE     (				lowerCAmelCase_       ):


					'''simple docstring'''





					_a             =							'ernie_m'
					_a             =							{"dropout": "classifier_dropout", "num_classes": "num_labels"}



					def __init__(	self							:  Tuple,  lowerCamelCase							:  int = 25_0002,  lowerCamelCase							:  int = 768,  lowerCamelCase							:  int = 12,  lowerCamelCase							:  int = 12,  lowerCamelCase							:  int = 3072,  lowerCamelCase							:  str = "gelu",  lowerCamelCase							:  float = 0.1,  lowerCamelCase							:  float = 0.1,  lowerCamelCase							:  int = 514,  lowerCamelCase							:  float = 0.02,  lowerCamelCase							:  int = 1,  lowerCamelCase							:  float = 1E-05,  lowerCamelCase							:  Optional[int]=None,  lowerCamelCase							:  Any=False,  lowerCamelCase							:  Dict=0.0,  **lowerCamelCase							:  Dict,  )-> Optional[int]:
								super().__init__(pad_token_id=lowerCamelCase,  **lowerCamelCase					)
								lowerCamelCase__		:		List[str]		     =vocab_size
								lowerCamelCase__		:		Optional[int]		     =hidden_size
								lowerCamelCase__		:		Optional[Any]		     =num_hidden_layers
								lowerCamelCase__		:		Dict		     =num_attention_heads
								lowerCamelCase__		:		Optional[Any]		     =intermediate_size
								lowerCamelCase__		:		Tuple		     =hidden_act
								lowerCamelCase__		:		Optional[Any]		     =hidden_dropout_prob
								lowerCamelCase__		:		int		     =attention_probs_dropout_prob
								lowerCamelCase__		:		Tuple		     =max_position_embeddings
								lowerCamelCase__		:		str		     =initializer_range
								lowerCamelCase__		:		List[Any]		     =layer_norm_eps
								lowerCamelCase__		:		Tuple		     =classifier_dropout
								lowerCamelCase__		:		Any		     =is_decoder
								lowerCamelCase__		:		str		     =act_dropout









"""simple docstring"""






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

from ...utils import logging


_lowercase			:			int										=						logging.get_logger(__name__)

class 	__SCREAMING_SNAKE_CASE     (				lowerCAmelCase_       ):


					'''simple docstring'''





					_a             =							CLIPConfig

					_a             =							['CLIPEncoderLayer']



					def __init__(	self							:  int,  lowerCamelCase							:  CLIPConfig					)-> List[Any]:
								super().__init__(lowerCamelCase					)

								lowerCamelCase__		:		Dict		     =CLIPVisionModelWithProjection(config.vision_config					)

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




					@torch.no_grad()
					def 		snake_case	(	self							:  Dict,  lowerCamelCase							:  Optional[Any],  lowerCamelCase							:  List[str],  lowerCamelCase							:  List[Any]=0.5,  lowerCamelCase							:  Optional[Any]=0.5					)-> Optional[int]:
								lowerCamelCase__		:		Dict		     =self.vision_model(lowerCamelCase					)[0]

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

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

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

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

								return images, nsfw_detected, watermark_detected




