


"""simple docstring"""






from typing import Optional

import pyspark

from .. import Features, NamedSplit
from ..download import DownloadMode
from ..packaged_modules.spark.spark import Spark
from .abc import AbstractDatasetReader

class 	__SCREAMING_SNAKE_CASE     (				lowercase__       ):


					'''simple docstring'''





					def __init__(	self							:  int,  lowerCamelCase							:  pyspark.sql.DataFrame,  lowerCamelCase							:  Optional[NamedSplit] = None,  lowerCamelCase							:  Optional[Features] = None,  lowerCamelCase							:  bool = True,  lowerCamelCase							:  str = None,  lowerCamelCase							:  bool = False,  lowerCamelCase							:  str = None,  lowerCamelCase							:  bool = True,  lowerCamelCase							:  str = "arrow",  **lowerCamelCase							:  str,  )-> List[str]:
								super().__init__(
								    split=_UpperCamelCase,  features=_UpperCamelCase,  cache_dir=_UpperCamelCase,  keep_in_memory=_UpperCamelCase,  streaming=_UpperCamelCase,  **_UpperCamelCase,  )
								lowerCamelCase__		:		Optional[Any]		     =load_from_cache_file
								lowerCamelCase__		:		Dict		     =file_format
								lowerCamelCase__		:		Tuple		     =Spark(
								    df=_UpperCamelCase,  features=_UpperCamelCase,  cache_dir=_UpperCamelCase,  working_dir=_UpperCamelCase,  **_UpperCamelCase,  )




					def 		snake_case	(	self							:  str					)-> str:
								if self.streaming:
											return self.builder.as_streaming_dataset(split=self.split					)
								lowerCamelCase__		:		Union[str, Any]		     =None if self._load_from_cache_file else DownloadMode.FORCE_REDOWNLOAD
								self.builder.download_and_prepare(
								    download_mode=_UpperCamelCase,  file_format=self._file_format,  )
								return self.builder.as_dataset(split=self.split					)









"""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




