


"""simple docstring"""




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

from ...utils import logging


lowercase_			     =  logging.get_logger(__name__)
class 					__lowerCAmelCase					(   lowercase_       ):


		'''simple docstring'''



		__UpperCAmelCase		:			Tuple										=				CLIPConfig

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

		def __init__(  self							, _a      ):
				super().__init__(__UpperCamelCase      )

				__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 			__UpperCAmelCase       (  self							, _a							, _a							, _a=0.5							, _a=0.5      ):
				__a           =			self.vision_model(__UpperCamelCase      )[0]

				__a           =			self.p_head(__UpperCamelCase      )
				__a           =			nsfw_detected.flatten()
				__a           =			nsfw_detected > p_threshold
				__a           =			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_:
								__a           =			np.zeros(images[idx].shape      )

				__a           =			self.w_head(__UpperCamelCase      )
				__a           =			watermark_detected.flatten()
				__a           =			watermark_detected > w_threshold
				__a           =			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_:
								__a           =			np.zeros(images[idx].shape      )

				return images, nsfw_detected, watermark_detected







"""simple docstring"""







from argparse import ArgumentParser

from datasets.commands.convert import ConvertCommand
from datasets.commands.dummy_data import DummyDataCommand
from datasets.commands.env import EnvironmentCommand
from datasets.commands.run_beam import RunBeamCommand
from datasets.commands.test import TestCommand
from datasets.utils.logging import set_verbosity_info



def 			lowercase__(       __SCREAMING_SNAKE_CASE							:     Optional[int]      ):
     return {key.lstrip('-'      ): value for key, value in zip(unknown_args[::2]     ,			unknown_args[1::2]      )}



def 			lowercase__(       ):
     lowercase_	:				Any													=	ArgumentParser(
         'HuggingFace Datasets CLI tool'     ,			usage='datasets-cli <command> [<args>]'     ,			allow_abbrev=__SCREAMING_SNAKE_CASE      )
     lowercase_	:				Tuple													=	parser.add_subparsers(help='datasets-cli command helpers'      )
     set_verbosity_info()

     # Register commands
     ConvertCommand.register_subcommand(__SCREAMING_SNAKE_CASE      )
     EnvironmentCommand.register_subcommand(__SCREAMING_SNAKE_CASE      )
     TestCommand.register_subcommand(__SCREAMING_SNAKE_CASE      )
     RunBeamCommand.register_subcommand(__SCREAMING_SNAKE_CASE      )
     DummyDataCommand.register_subcommand(__SCREAMING_SNAKE_CASE      )

     # Parse args
     lowercase_							,  lowercase_	:				Dict													=	parser.parse_known_args()
     if not hasattr(__SCREAMING_SNAKE_CASE     ,			'func'      ):
          parser.print_help()
          exit(1      )
     lowercase_	:				int													=	parse_unknown_args(__SCREAMING_SNAKE_CASE      )

     # Run
     lowercase_	:				List[Any]													=	args.func(__SCREAMING_SNAKE_CASE     ,			**__SCREAMING_SNAKE_CASE      )
     service.run()


if __name__ == "__main__":
       main()

