

"""simple docstring"""






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

from ...utils import logging


__lowerCamelCase										=		logging.get_logger(__name__)
class        _lowercase					(     __UpperCAmelCase     ):
     _lowerCamelCase     =       CLIPConfig

     _lowerCamelCase     =       ['''CLIPEncoderLayer''']







     def __init__(      self ,						UpperCamelCase_       ):
      super().__init__(UpperCamelCase_       )

      __magic_name__     =      CLIPVisionModelWithProjection(config.vision_config       )

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







     @torch.no_grad()
     def 	lowerCAmelCase__    (      self ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_=0.5 ,						UpperCamelCase_=0.5       ):
      __magic_name__     =      self.vision_model(UpperCamelCase_       )[0]

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

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

      return images, nsfw_detected, watermark_detected









"""simple docstring"""






from typing import List, Optional, Union

import numpy as np
import PIL.Image

from ...image_processing_utils import BaseImageProcessor, BatchFeature
from ...image_transforms import rescale, resize, to_channel_dimension_format
from ...image_utils import (
    ChannelDimension,
    PILImageResampling,
    get_image_size,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


__lowerCamelCase										=		logging.get_logger(__name__)
class        _lowercase					(     __UpperCAmelCase     ):
     _lowerCamelCase     =       ['''pixel_values''']







     def __init__(      self ,						UpperCamelCase_ = True ,						UpperCamelCase_ = 32 ,						UpperCamelCase_=PILImageResampling.BILINEAR ,						UpperCamelCase_ = True ,						**UpperCamelCase_ ,						):
      __magic_name__     =      do_resize
      __magic_name__     =      do_rescale
      __magic_name__     =      size_divisor
      __magic_name__     =      resample
      super().__init__(**UpperCamelCase_       )







     def 	lowerCAmelCase__    (      self ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ = None ,						**UpperCamelCase_       ):
      __magic_name__   ,						__magic_name__     =      get_image_size(UpperCamelCase_       )
      # Rounds the height and width down to the closest multiple of size_divisor
      __magic_name__     =      height // size_divisor * size_divisor
      __magic_name__     =      width // size_divisor * size_divisor
      __magic_name__     =      resize(UpperCamelCase_ ,						(new_h, new_w) ,						resample=UpperCamelCase_ ,						data_format=UpperCamelCase_ ,						**UpperCamelCase_       )
      return image







     def 	lowerCAmelCase__    (      self ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ = None ,						**UpperCamelCase_       ):
      return rescale(image=UpperCamelCase_ ,						scale=UpperCamelCase_ ,						data_format=UpperCamelCase_ ,						**UpperCamelCase_       )







     def 	lowerCAmelCase__    (      self ,						UpperCamelCase_ ,						UpperCamelCase_ = None ,						UpperCamelCase_ = None ,						UpperCamelCase_=None ,						UpperCamelCase_ = None ,						UpperCamelCase_ = None ,						UpperCamelCase_ = ChannelDimension.FIRST ,						**UpperCamelCase_ ,						):
      __magic_name__     =      do_resize if do_resize is not None else self.do_resize
      __magic_name__     =      do_rescale if do_rescale is not None else self.do_rescale
      __magic_name__     =      size_divisor if size_divisor is not None else self.size_divisor
      __magic_name__     =      resample if resample is not None else self.resample

      if do_resize and size_divisor is None:
       raise ValueError('''size_divisor is required for resizing'''       )

      __magic_name__     =      make_list_of_images(UpperCamelCase_       )

      if not valid_images(UpperCamelCase_       ):
       raise ValueError('''Invalid image(s)'''       )

      # All transformations expect numpy arrays.
      __magic_name__     =      [to_numpy_array(UpperCamelCase_       ) for img in images]

      if do_resize:
       __magic_name__     =      [self.resize(UpperCamelCase_ ,						size_divisor=UpperCamelCase_ ,						resample=UpperCamelCase_       ) for image in images]

      if do_rescale:
       __magic_name__     =      [self.rescale(UpperCamelCase_ ,						scale=1 / 255       ) for image in images]

      __magic_name__     =      [to_channel_dimension_format(UpperCamelCase_ ,						UpperCamelCase_       ) for image in images]

      __magic_name__     =      {'''pixel_values''': images}
      return BatchFeature(data=UpperCamelCase_ ,						tensor_type=UpperCamelCase_       )

