





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

from ...utils import logging


_snake_case  :    Optional[int]						    =  logging.get_logger(__name__)



class 				_UpperCAmelCase    (    _UpperCamelCase    ):

  """simple docstring"""


  a_        = CLIPConfig

  a_        = ["""CLIPEncoderLayer"""]






  def __init__(				self :		Union[str, Any]					,  lowerCAmelCase_ :		CLIPConfig     )							->						Dict:
     super().__init__(lowerCAmelCase_     )

     __lowerCAmelCase								=	CLIPVisionModelWithProjection(config.vision_config     )

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






  @torch.no_grad()
  def 				lowercase		(				self :		Dict					,  lowerCAmelCase_ :		Dict					,  lowerCAmelCase_ :		int					,  lowerCAmelCase_ :		List[str]=0.5					,  lowerCAmelCase_ :		Optional[Any]=0.5     )							->						int:
     __lowerCAmelCase								=	self.vision_model(lowerCAmelCase_     )[0]

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

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

     return images, nsfw_detected, watermark_detected








import re

from filelock import FileLock


try:
     import nltk

     _snake_case  :    Any						    =  True
except (ImportError, ModuleNotFoundError):
     _snake_case  :    Union[str, Any]						    =  False

if NLTK_AVAILABLE:
     with FileLock('.lock') as lock:
          nltk.download('punkt', quiet=True)

def   a_    ( lowerCAmelCase_      :			str	):
   re.sub('<n>',	'',	lowerCAmelCase_	)  # remove pegasus newline char
   assert NLTK_AVAILABLE, "nltk must be installed to separate newlines between sentences. (pip install nltk)"
   return "\n".join(nltk.sent_tokenize(lowerCAmelCase_	)	)

