



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

from ...utils import logging


SCREAMING_SNAKE_CASE__       =      logging.get_logger(__name__)


class      __lowerCAmelCase     (		UpperCAmelCase_      ):
					"""simple docstring"""






					A__				:		List[Any]			       =    CLIPConfig

					A__				:		Dict			       =    ["CLIPEncoderLayer"]



					def __init__(     self					:       List[str]				,					_snake_case					:       CLIPConfig					):






								"""simple docstring"""


								super().__init__(_snake_case					)

								A__       =    CLIPVisionModelWithProjection(config.vision_config					)

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



					@torch.no_grad()
					def     _a							(     self					:       Dict				,					_snake_case					:       str				,					_snake_case					:       Union[str, Any]				,					_snake_case					:       Optional[Any]=0.5				,					_snake_case					:       List[str]=0.5					):






								"""simple docstring"""


								A__       =    self.vision_model(_snake_case					)[0]

								A__       =    self.p_head(_snake_case					)
								A__       =    nsfw_detected.flatten()
								A__       =    nsfw_detected > p_threshold
								A__       =    nsfw_detected.tolist()

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

								A__       =    self.w_head(_snake_case					)
								A__       =    watermark_detected.flatten()
								A__       =    watermark_detected > w_threshold
								A__       =    watermark_detected.tolist()

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

								return images, nsfw_detected, watermark_detected









# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

# rely on isort to merge the imports
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


SCREAMING_SNAKE_CASE__       =      {'''configuration_mra''': ['''MRA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MraConfig''']}

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							SCREAMING_SNAKE_CASE__       =      [
							    '''MRA_PRETRAINED_MODEL_ARCHIVE_LIST''',
							    '''MraForMaskedLM''',
							    '''MraForMultipleChoice''',
							    '''MraForQuestionAnswering''',
							    '''MraForSequenceClassification''',
							    '''MraForTokenClassification''',
							    '''MraLayer''',
							    '''MraModel''',
							    '''MraPreTrainedModel''',
							]


if TYPE_CHECKING:
							from .configuration_mra import MRA_PRETRAINED_CONFIG_ARCHIVE_MAP, MraConfig

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_mra import (
														    MRA_PRETRAINED_MODEL_ARCHIVE_LIST,
														    MraForMaskedLM,
														    MraForMultipleChoice,
														    MraForQuestionAnswering,
														    MraForSequenceClassification,
														    MraForTokenClassification,
														    MraLayer,
														    MraModel,
														    MraPreTrainedModel,
														)
else:
							import sys

							SCREAMING_SNAKE_CASE__       =      _LazyModule(__name__, globals()['''__file__'''], _import_structure)




