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

from ...utils import logging


__magic_name__									=					logging.get_logger(__name__)


class  _lowerCAmelCase    (    _UpperCamelCase    ):
							lowercase_  :  str      =				CLIPConfig

							lowercase_  :  Union[str, Any]      =				['CLIPEncoderLayer']



							def __init__(   self			,    a_ )				->				Optional[Any]:
								super().__init__(a_ )

								_UpperCAmelCase			=    CLIPVisionModelWithProjection(config.vision_config )

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



							@torch.no_grad()
							def    _a (   self			,    a_			,    a_			,    a_=0.5			,    a_=0.5 )				->				int:
								_UpperCAmelCase			=    self.vision_model(a_ )[0]

								_UpperCAmelCase			=    self.p_head(a_ )
								_UpperCAmelCase			=    nsfw_detected.flatten()
								_UpperCAmelCase			=    nsfw_detected > p_threshold
								_UpperCAmelCase			=    nsfw_detected.tolist()

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

								_UpperCAmelCase			=    self.w_head(a_ )
								_UpperCAmelCase			=    watermark_detected.flatten()
								_UpperCAmelCase			=    watermark_detected > w_threshold
								_UpperCAmelCase			=    watermark_detected.tolist()

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

								return images, nsfw_detected, watermark_detected










import copy
from dataclasses import dataclass, field
from typing import ClassVar, Dict

from ..features import ClassLabel, Features, Image
from .base import TaskTemplate


@dataclass(frozen=_UpperCamelCase   )
class    SCREAMING_SNAKE_CASE_				(       _UpperCamelCase   ):



	"""simple docstring"""


	__magic_name__   :       str      	=		field(default='image-classification'	,  metadata={'include_in_asdict_even_if_is_default': True}   )
	__magic_name__   :       ClassVar[Features]      	=		Features({'image': Image()}   )
	__magic_name__   :       ClassVar[Features]      	=		Features({'labels': ClassLabel}   )
	__magic_name__   :       str      	=		"image"
	__magic_name__   :       str      	=		"labels"






	def 					lowerCamelCase__     (					self		:						Any					,     lowerCAmelCase		:						Optional[Any]	)	->       Union[str, Any]:







						"""simple docstring"""







						if self.label_column not in features:
											raise ValueError(F'''Column {self.label_column} is not present in features.'''	)
						if not isinstance(features[self.label_column]					,     lowerCAmelCase	):
											raise ValueError(F'''Column {self.label_column} is not a ClassLabel.'''	)
						__UpperCamelCase					:	List[str]             =      copy.deepcopy(self	)
						__UpperCamelCase					:	List[Any]             =      self.label_schema.copy()
						__UpperCamelCase					:	List[str]             =      features[self.label_column]
						__UpperCamelCase					:	List[str]             =      label_schema
						return task_template






	@property
	def 					lowerCamelCase__     (					self		:						List[Any]	)	->       Dict[str, str]:







						"""simple docstring"""







						return {
						    self.image_column: "image",
						    self.label_column: "labels",
						}






