



from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


snake_case    				=		{
    """configuration_roberta""": ["""ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP""", """RobertaConfig""", """RobertaOnnxConfig"""],
    """tokenization_roberta""": ["""RobertaTokenizer"""],
}

try:
   if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   snake_case    				=		["""RobertaTokenizerFast"""]

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   snake_case    				=		[
       """ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST""",
       """RobertaForCausalLM""",
       """RobertaForMaskedLM""",
       """RobertaForMultipleChoice""",
       """RobertaForQuestionAnswering""",
       """RobertaForSequenceClassification""",
       """RobertaForTokenClassification""",
       """RobertaModel""",
       """RobertaPreTrainedModel""",
   ]

try:
   if not is_tf_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   snake_case    				=		[
       """TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST""",
       """TFRobertaForCausalLM""",
       """TFRobertaForMaskedLM""",
       """TFRobertaForMultipleChoice""",
       """TFRobertaForQuestionAnswering""",
       """TFRobertaForSequenceClassification""",
       """TFRobertaForTokenClassification""",
       """TFRobertaMainLayer""",
       """TFRobertaModel""",
       """TFRobertaPreTrainedModel""",
   ]

try:
   if not is_flax_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   snake_case    				=		[
       """FlaxRobertaForCausalLM""",
       """FlaxRobertaForMaskedLM""",
       """FlaxRobertaForMultipleChoice""",
       """FlaxRobertaForQuestionAnswering""",
       """FlaxRobertaForSequenceClassification""",
       """FlaxRobertaForTokenClassification""",
       """FlaxRobertaModel""",
       """FlaxRobertaPreTrainedModel""",
   ]


if TYPE_CHECKING:
   from .configuration_roberta import ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, RobertaConfig, RobertaOnnxConfig
   from .tokenization_roberta import RobertaTokenizer

   try:
      if not is_tokenizers_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .tokenization_roberta_fast import RobertaTokenizerFast

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_roberta import (
          ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST,
          RobertaForCausalLM,
          RobertaForMaskedLM,
          RobertaForMultipleChoice,
          RobertaForQuestionAnswering,
          RobertaForSequenceClassification,
          RobertaForTokenClassification,
          RobertaModel,
          RobertaPreTrainedModel,
      )

   try:
      if not is_tf_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_tf_roberta import (
          TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST,
          TFRobertaForCausalLM,
          TFRobertaForMaskedLM,
          TFRobertaForMultipleChoice,
          TFRobertaForQuestionAnswering,
          TFRobertaForSequenceClassification,
          TFRobertaForTokenClassification,
          TFRobertaMainLayer,
          TFRobertaModel,
          TFRobertaPreTrainedModel,
      )

   try:
      if not is_flax_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_flax_roberta import (
          FlaxRobertaForCausalLM,
          FlaxRobertaForMaskedLM,
          FlaxRobertaForMultipleChoice,
          FlaxRobertaForQuestionAnswering,
          FlaxRobertaForSequenceClassification,
          FlaxRobertaForTokenClassification,
          FlaxRobertaModel,
          FlaxRobertaPreTrainedModel,
      )

else:
   import sys

   snake_case    				=		_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)














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

from ...utils import logging


snake_case    				=		logging.get_logger(__name__)


class  SCREAMING_SNAKE_CASE			(			lowerCAmelCase					):
     '''simple docstring'''


     UpperCamelCase_							:		Union[str, Any]			=	CLIPConfig

     UpperCamelCase_							:		Optional[int]			=	['''CLIPEncoderLayer''']


     def __init__(				self       :	List[Any]					, UpperCAmelCase_       :	CLIPConfig      ):
       super().__init__(UpperCAmelCase_      )

       SCREAMING_SNAKE_CASE       :      Any							=    CLIPVisionModelWithProjection(config.vision_config      )

       SCREAMING_SNAKE_CASE       :      List[Any]							=    nn.Linear(config.vision_config.projection_dim					, 1      )
       SCREAMING_SNAKE_CASE       :      List[str]							=    nn.Linear(config.vision_config.projection_dim					, 1      )






     @torch.no_grad()
     def 				_A       (				self       :	Union[str, Any]					, UpperCAmelCase_       :	int					, UpperCAmelCase_       :	Dict					, UpperCAmelCase_       :	List[Any]=0.5					, UpperCAmelCase_       :	Optional[Any]=0.5      ):
       SCREAMING_SNAKE_CASE       :      str							=    self.vision_model(UpperCAmelCase_      )[0]

       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    self.p_head(UpperCAmelCase_      )
       SCREAMING_SNAKE_CASE       :      List[str]							=    nsfw_detected.flatten()
       SCREAMING_SNAKE_CASE       :      List[str]							=    nsfw_detected > p_threshold
       SCREAMING_SNAKE_CASE       :      List[Any]							=    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_:
           SCREAMING_SNAKE_CASE       :      Dict							=    np.zeros(images[idx].shape      )

       SCREAMING_SNAKE_CASE       :      Any							=    self.w_head(UpperCAmelCase_      )
       SCREAMING_SNAKE_CASE       :      List[Any]							=    watermark_detected.flatten()
       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    watermark_detected > w_threshold
       SCREAMING_SNAKE_CASE       :      Optional[int]							=    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_:
           SCREAMING_SNAKE_CASE       :      Optional[int]							=    np.zeros(images[idx].shape      )

       return images, nsfw_detected, watermark_detected






