"""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  _snake_case		(    __snake_case ):







				"""simple docstring"""






				a			=						CLIPConfig

				a			=						["CLIPEncoderLayer"]





				def __init__(      self							:							Optional[Any]      ,    _A							:							CLIPConfig):



											"""simple docstring"""







											super().__init__(_A)

											_SCREAMING_SNAKE_CASE		:						Tuple			  =    CLIPVisionModelWithProjection(config.vision_config)

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





				@torch.no_grad()
				def 		_lowerCAmelCase		(      self							:							List[Any]      ,    _A							:							int      ,    _A							:							int      ,    _A							:							Any=0.5      ,    _A							:							List[str]=0.5):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Any			  =    self.vision_model(_A)[0]

											_SCREAMING_SNAKE_CASE		:						int			  =    self.p_head(_A)
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    nsfw_detected.flatten()
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    nsfw_detected > p_threshold
											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    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_:
																									_SCREAMING_SNAKE_CASE		:						str			  =    np.zeros(images[idx].shape)

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

											return images, nsfw_detected, watermark_detected



"""simple docstring"""





import warnings

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




class  _snake_case		(    __snake_case ):







				"""simple docstring"""






				a			=						["image_processor", "tokenizer"]
				a			=						"ChineseCLIPImageProcessor"
				a			=						("BertTokenizer", "BertTokenizerFast")





				def __init__(      self							:							Dict      ,    _A							:							Tuple=None      ,    _A							:							List[Any]=None      ,    **_A							:							int):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Tuple			  =    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      ,    )
																		_SCREAMING_SNAKE_CASE		:						str			  =    kwargs.pop("""feature_extractor""")

											_SCREAMING_SNAKE_CASE		:						int			  =    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)
											_SCREAMING_SNAKE_CASE		:						Dict			  =    self.image_processor





				def __call__(      self							:							Optional[int]      ,    _A							:							Optional[Any]=None      ,    _A							:							Any=None      ,    _A							:							Tuple=None      ,    **_A							:							int):



											"""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:
																		_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    self.tokenizer(_A      ,    return_tensors=_A      ,    **_A)

											if images is not None:
																		_SCREAMING_SNAKE_CASE		:						List[Any]			  =    self.image_processor(_A      ,    return_tensors=_A      ,    **_A)

											if text is not None and images is not None:
																		_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    image_features.pixel_values
																		return encoding
											elif text is not None:
																		return encoding
											else:
																		return BatchEncoding(data=dict(**_A)      ,    tensor_type=_A)





				def 		_lowerCAmelCase		(      self							:							str      ,    *_A							:							Any      ,    **_A							:							Any):



											"""simple docstring"""







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





				def 		_lowerCAmelCase		(      self							:							Union[str, Any]      ,    *_A							:							List[Any]      ,    **_A							:							Any):



											"""simple docstring"""







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





				@property
				def 		_lowerCAmelCase		(      self							:							str):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    self.tokenizer.model_input_names
											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    self.image_processor.model_input_names
											return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names))





				@property
				def 		_lowerCAmelCase		(      self							:							List[str]):



											"""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


