






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

from ...utils import logging


_UpperCAmelCase			:		Dict   				=    logging.get_logger(__name__)
class  lowerCAmelCase_       (				snake_case__					):
							UpperCamelCase_		:Optional[int]		      =    CLIPConfig

							UpperCamelCase_		:Tuple		      =    ['CLIPEncoderLayer']




							def __init__(   self     :   Any ,							SCREAMING_SNAKE_CASE_     :   Optional[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 						__snake_case					(   self     :   Dict ,							SCREAMING_SNAKE_CASE_     :   Optional[int] ,							SCREAMING_SNAKE_CASE_     :   int ,							SCREAMING_SNAKE_CASE_     :   Dict=0.5 ,							SCREAMING_SNAKE_CASE_     :   List[Any]=0.5	):
											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













from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tensorflow_text_available, is_torch_available


_UpperCAmelCase			:		Optional[Any]   				=    {
    "configuration_ernie": ["ERNIE_PRETRAINED_CONFIG_ARCHIVE_MAP", "ErnieConfig", "ErnieOnnxConfig"],
}

try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			_UpperCAmelCase			:		Any   				=    [
			    "ERNIE_PRETRAINED_MODEL_ARCHIVE_LIST",
			    "ErnieForCausalLM",
			    "ErnieForMaskedLM",
			    "ErnieForMultipleChoice",
			    "ErnieForNextSentencePrediction",
			    "ErnieForPreTraining",
			    "ErnieForQuestionAnswering",
			    "ErnieForSequenceClassification",
			    "ErnieForTokenClassification",
			    "ErnieModel",
			    "ErniePreTrainedModel",
			]

if TYPE_CHECKING:
			from .configuration_ernie import ERNIE_PRETRAINED_CONFIG_ARCHIVE_MAP, ErnieConfig, ErnieOnnxConfig

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_ernie import (
						    ERNIE_PRETRAINED_MODEL_ARCHIVE_LIST,
						    ErnieForCausalLM,
						    ErnieForMaskedLM,
						    ErnieForMultipleChoice,
						    ErnieForNextSentencePrediction,
						    ErnieForPreTraining,
						    ErnieForQuestionAnswering,
						    ErnieForSequenceClassification,
						    ErnieForTokenClassification,
						    ErnieModel,
						    ErniePreTrainedModel,
						)

else:
			import sys

			_UpperCAmelCase			:		Dict   				=    _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)





