


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

from ...utils import logging


UpperCAmelCase_					:   Any 							=						logging.get_logger(__name__)





class      lowercase__    (							__A  ):
  __UpperCamelCase	     =  CLIPConfig

  __UpperCamelCase	     =  ['CLIPEncoderLayer']







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

       lowerCAmelCase_					:       List[Any]    =		CLIPVisionModelWithProjection(config.vision_config						)

       lowerCAmelCase_					:       Any    =		nn.Linear(config.vision_config.projection_dim    ,						1						)
       lowerCAmelCase_					:       int    =		nn.Linear(config.vision_config.projection_dim    ,						1						)







  @torch.no_grad()
  def 	UpperCAmelCase__		(	self    ,						_lowercase    ,						_lowercase    ,						_lowercase=0.5    ,						_lowercase=0.5						):
       lowerCAmelCase_					:       str    =		self.vision_model(_lowercase						)[0]

       lowerCAmelCase_					:       Union[str, Any]    =		self.p_head(_lowercase						)
       lowerCAmelCase_					:       Optional[Any]    =		nsfw_detected.flatten()
       lowerCAmelCase_					:       Dict    =		nsfw_detected > p_threshold
       lowerCAmelCase_					:       List[Any]    =		nsfw_detected.tolist()

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

       lowerCAmelCase_					:       int    =		self.w_head(_lowercase						)
       lowerCAmelCase_					:       Union[str, Any]    =		watermark_detected.flatten()
       lowerCAmelCase_					:       List[str]    =		watermark_detected > w_threshold
       lowerCAmelCase_					:       str    =		watermark_detected.tolist()

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

       return images, nsfw_detected, watermark_detected







from __future__ import annotations






def 							_lowerCAmelCase (  _a   :					list[int]			)	->			list[int]:  # This function is recursive
     lowerCAmelCase_					:       List[Any]    =		len(_a			)
     # If the array contains only one element, we return it (it's the stop condition of
     # recursion)
     if array_length <= 1:
          return array
          # Else
     lowerCAmelCase_					:       Union[str, Any]    =		array[0]
     lowerCAmelCase_					:       str    =		False
     lowerCAmelCase_					:       Optional[int]    =		1
     lowerCAmelCase_					:       list[int]    =		[]
     while not is_found and i < array_length:
          if array[i] < pivot:
               lowerCAmelCase_					:       Optional[Any]    =		True
               lowerCAmelCase_					:       Tuple    =		[element for element in array[i:] if element >= array[i]]
               lowerCAmelCase_					:       Any    =		longest_subsequence(_a			)
               if len(_a			) > len(_a			):
                    lowerCAmelCase_					:       List[Any]    =		temp_array
          else:
               i += 1

     lowerCAmelCase_					:       Tuple    =		[element for element in array[1:] if element >= pivot]
     lowerCAmelCase_					:       List[Any]    =		[pivot, *longest_subsequence(_a			)]
     if len(_a			) > len(_a			):
          return temp_array
     else:
          return longest_subseq


if __name__ == "__main__":
      import doctest

      doctest.testmod()
