



import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding





class    lowerCamelCase__  (						UpperCAmelCase_):
     """simple docstring"""



     _A       =			['image_processor', 'tokenizer']
     _A       =			'CLIPImageProcessor'
     _A       =			('CLIPTokenizer', 'CLIPTokenizerFast')







     def __init__(self				,			__a=None				,			__a=None				,			**__a    ):






           '''simple docstring'''



           lowerCamelCase								=		None
           if "feature_extractor" in kwargs:
                 warnings.warn(
                     "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
                     " instead."				,			__a				,			)
                 lowerCamelCase								=		kwargs.pop("feature_extractor"    )

           lowerCamelCase								=		image_processor if image_processor is not None else feature_extractor
           if image_processor is None:
                 raise ValueError("You need to specify an `image_processor`."    )
           if tokenizer is None:
                 raise ValueError("You need to specify a `tokenizer`."    )

           super().__init__(__a				,			__a    )







     def __call__(self				,			__a=None				,			__a=None				,			__a=None				,			**__a    ):






           '''simple docstring'''



           if text is None and images is None:
                 raise ValueError("You have to specify either text or images. Both cannot be none."    )

           if text is not None:
                 lowerCamelCase								=		self.tokenizer(__a				,			return_tensors=__a				,			**__a    )

           if images is not None:
                 lowerCamelCase								=		self.image_processor(__a				,			return_tensors=__a				,			**__a    )

           if text is not None and images is not None:
                 lowerCamelCase								=		image_features.pixel_values
                 return encoding
           elif text is not None:
                 return encoding
           else:
                 return BatchEncoding(data=dict(**__a    )				,			tensor_type=__a    )







     def 						_a      (self				,			*__a				,			**__a    ):






           '''simple docstring'''



           return self.tokenizer.batch_decode(*__a				,			**__a    )







     def 						_a      (self				,			*__a				,			**__a    ):






           '''simple docstring'''



           return self.tokenizer.decode(*__a				,			**__a    )







     @property
     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		self.tokenizer.model_input_names
           lowerCamelCase								=		self.image_processor.model_input_names
           return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names    )    )







     @property
     def 						_a      (self    ):






           '''simple docstring'''



           warnings.warn(
               "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."				,			__a				,			)
           return self.image_processor_class







     @property
     def 						_a      (self    ):






           '''simple docstring'''



           warnings.warn(
               "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."				,			__a				,			)
           return self.image_processor




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

from ...utils import logging


a_						:     Tuple	     =		logging.get_logger(__name__)





class    lowerCamelCase__  (						UpperCAmelCase_):
     """simple docstring"""



     _A       =			CLIPConfig

     _A       =			['CLIPEncoderLayer']







     def __init__(self				,			__a    ):






           '''simple docstring'''



           super().__init__(__a    )

           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 						_a      (self				,			__a				,			__a				,			__a=0.5				,			__a=0.5    ):






           '''simple docstring'''



           lowerCamelCase								=		self.vision_model(__a    )[0]

           lowerCamelCase								=		self.p_head(__a    )
           lowerCamelCase								=		nsfw_detected.flatten()
           lowerCamelCase								=		nsfw_detected > p_threshold
           lowerCamelCase								=		nsfw_detected.tolist()

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

           lowerCamelCase								=		self.w_head(__a    )
           lowerCamelCase								=		watermark_detected.flatten()
           lowerCamelCase								=		watermark_detected > w_threshold
           lowerCamelCase								=		watermark_detected.tolist()

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

           return images, nsfw_detected, watermark_detected