


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

from ...utils import logging


A      :    Union[str, Any]		=					logging.get_logger(__name__)







class 			_lowercase (							lowercase__):


       """simple docstring"""




       A__											=			CLIPConfig

       A__											=			["CLIPEncoderLayer"]





       def __init__(   self							:			Union[str, Any]					,      __lowerCamelCase							:			CLIPConfig       ):


             '''simple docstring'''



             super().__init__(__lowerCamelCase       )

             lowerCamelCase__		:				Union[str, Any]    =  CLIPVisionModelWithProjection(config.vision_config       )

             lowerCamelCase__		:				int    =  nn.Linear(config.vision_config.projection_dim					,      1       )
             lowerCamelCase__		:				Optional[Any]    =  nn.Linear(config.vision_config.projection_dim					,      1       )





       @torch.no_grad()
       def 						lowerCAmelCase     (   self							:			Union[str, Any]					,      __lowerCamelCase							:			Dict					,      __lowerCamelCase							:			Optional[int]					,      __lowerCamelCase							:			int=0.5					,      __lowerCamelCase							:			Tuple=0.5       ):


             '''simple docstring'''



             lowerCamelCase__		:				int    =  self.vision_model(__lowerCamelCase       )[0]

             lowerCamelCase__		:				List[str]    =  self.p_head(__lowerCamelCase       )
             lowerCamelCase__		:				Optional[Any]    =  nsfw_detected.flatten()
             lowerCamelCase__		:				int    =  nsfw_detected > p_threshold
             lowerCamelCase__		:				Union[str, 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__		:				List[Any]    =  self.w_head(__lowerCamelCase       )
             lowerCamelCase__		:				Union[str, Any]    =  watermark_detected.flatten()
             lowerCamelCase__		:				str    =  watermark_detected > w_threshold
             lowerCamelCase__		:				Any    =  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__		:				Dict    =  np.zeros(images[idx].shape       )

             return images, nsfw_detected, watermark_detected






class 			_lowercase :  # Public class to implement a graph


       """simple docstring"""




       def __init__(   self							:			Dict					,      __lowerCamelCase							:			int					,      __lowerCamelCase							:			int					,      __lowerCamelCase							:			list[list[bool]]       ):


             '''simple docstring'''



             lowerCamelCase__		:				int    =  row
             lowerCamelCase__		:				Optional[Any]    =  col
             lowerCamelCase__		:				Union[str, Any]    =  graph





       def 						lowerCAmelCase     (   self							:			Optional[int]					,      __lowerCamelCase							:			int					,      __lowerCamelCase							:			int					,      __lowerCamelCase							:			list[list[bool]]       ):


             '''simple docstring'''



             return (
                 0 <= i < self.ROW
                 and 0 <= j < self.COL
                 and not visited[i][j]
                 and self.graph[i][j]
             )





       def 						lowerCAmelCase     (   self							:			Dict					,      __lowerCamelCase							:			int					,      __lowerCamelCase							:			int					,      __lowerCamelCase							:			list[list[bool]]       ):


             '''simple docstring'''



             lowerCamelCase__		:				str    =  [-1, -1, -1, 0, 0, 1, 1, 1]  # Coordinate order
             lowerCamelCase__		:				Tuple    =  [-1, 0, 1, -1, 1, -1, 0, 1]
             lowerCamelCase__		:				List[Any]    =  True  # Make those cells visited
             for k in range(8       ):
                   if self.is_safe(i + row_nbr[k]					,      j + col_nbr[k]					,      __lowerCamelCase       ):
                         self.diffs(i + row_nbr[k]					,      j + col_nbr[k]					,      __lowerCamelCase       )





       def 						lowerCAmelCase     (   self							:			List[Any]       ):  # And finally, count all islands.


             '''simple docstring'''



             lowerCamelCase__		:				List[Any]    =  [[False for j in range(self.COL       )] for i in range(self.ROW       )]
             lowerCamelCase__		:				Optional[Any]    =  0
             for i in range(self.ROW       ):
                   for j in range(self.COL       ):
                         if visited[i][j] is False and self.graph[i][j] == 1:
                               self.diffs(__lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase       )
                               count += 1
             return count


