


"""simple docstring"""







from typing import TYPE_CHECKING

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


a__    :							Dict            =					{
    '''configuration_transfo_xl''': ['''TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TransfoXLConfig'''],
    '''tokenization_transfo_xl''': ['''TransfoXLCorpus''', '''TransfoXLTokenizer'''],
}

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		a__    :							List[str]            =					[
		    '''TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''AdaptiveEmbedding''',
		    '''TransfoXLForSequenceClassification''',
		    '''TransfoXLLMHeadModel''',
		    '''TransfoXLModel''',
		    '''TransfoXLPreTrainedModel''',
		    '''load_tf_weights_in_transfo_xl''',
		]

try:
		if not is_tf_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		a__    :							List[str]            =					[
		    '''TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''TFAdaptiveEmbedding''',
		    '''TFTransfoXLForSequenceClassification''',
		    '''TFTransfoXLLMHeadModel''',
		    '''TFTransfoXLMainLayer''',
		    '''TFTransfoXLModel''',
		    '''TFTransfoXLPreTrainedModel''',
		]


if TYPE_CHECKING:
		from .configuration_transfo_xl import TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP, TransfoXLConfig
		from .tokenization_transfo_xl import TransfoXLCorpus, TransfoXLTokenizer

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_transfo_xl import (
				    TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST,
				    AdaptiveEmbedding,
				    TransfoXLForSequenceClassification,
				    TransfoXLLMHeadModel,
				    TransfoXLModel,
				    TransfoXLPreTrainedModel,
				    load_tf_weights_in_transfo_xl,
				)

		try:
				if not is_tf_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_tf_transfo_xl import (
				    TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TFAdaptiveEmbedding,
				    TFTransfoXLForSequenceClassification,
				    TFTransfoXLLMHeadModel,
				    TFTransfoXLMainLayer,
				    TFTransfoXLModel,
				    TFTransfoXLPreTrainedModel,
				)

else:
		import sys

		a__    :							List[str]            =					_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)




"""simple docstring"""







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

from ...utils import logging


a__    :							Optional[Any]            =					logging.get_logger(__name__)




class 			UpperCamelCase_     (			UpperCamelCase):

	"""simple docstring"""





	snake_case__    :  int    =      CLIPConfig

	snake_case__    :  str    =      ["CLIPEncoderLayer"]




	def __init__(			self      :    Optional[int]      ,      UpperCAmelCase__      :    CLIPConfig       )	->     Dict:
						super().__init__(UpperCAmelCase__       )

						__SCREAMING_SNAKE_CASE						       =    CLIPVisionModelWithProjection(config.vision_config       )

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




	@torch.no_grad()
	def   UpperCAmelCase_				(			self      :    str      ,      UpperCAmelCase__      :    List[str]      ,      UpperCAmelCase__      :    str      ,      UpperCAmelCase__      :    int=0.5      ,      UpperCAmelCase__      :    Optional[int]=0.5       )	->     Union[str, Any]:
						__SCREAMING_SNAKE_CASE						       =    self.vision_model(UpperCAmelCase__       )[0]

						__SCREAMING_SNAKE_CASE						       =    self.p_head(UpperCAmelCase__       )
						__SCREAMING_SNAKE_CASE						       =    nsfw_detected.flatten()
						__SCREAMING_SNAKE_CASE						       =    nsfw_detected > p_threshold
						__SCREAMING_SNAKE_CASE						       =    nsfw_detected.tolist()

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

						__SCREAMING_SNAKE_CASE						       =    self.w_head(UpperCAmelCase__       )
						__SCREAMING_SNAKE_CASE						       =    watermark_detected.flatten()
						__SCREAMING_SNAKE_CASE						       =    watermark_detected > w_threshold
						__SCREAMING_SNAKE_CASE						       =    watermark_detected.tolist()

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

						return images, nsfw_detected, watermark_detected
