



'''simple docstring'''




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

from ...utils import logging


lowerCamelCase							:str       = logging.get_logger(__name__)







class      _lowerCAmelCase							(      __UpperCAmelCase			):
	__SCREAMING_SNAKE_CASE       :		Union[str, Any]		      = CLIPConfig

	__SCREAMING_SNAKE_CASE       :		List[str]		      = ['CLIPEncoderLayer']


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

				A_     :						Any     =					CLIPVisionModelWithProjection(config.vision_config							)

				A_     :						int     =					nn.Linear(config.vision_config.projection_dim	,				1							)
				A_     :						Union[str, Any]     =					nn.Linear(config.vision_config.projection_dim	,				1							)


	@torch.no_grad()
	def    _a     (self	,				lowercase	,				lowercase	,				lowercase=0.5	,				lowercase=0.5							):
				A_     :						Any     =					self.vision_model(lowercase							)[0]

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

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

				return images, nsfw_detected, watermark_detected




'''simple docstring'''




import unittest

import numpy as np
import torch

from .utils_summarization import build_mask, compute_token_type_ids, process_story, truncate_or_pad







class      _lowerCAmelCase							(      unittest.TestCase			):


	def    _a     (self							):
				A_     :						Dict     =					10


	def    _a     (self							):
				A_     :						str     =					[1, 2, 3, 4]
				A_     :						List[str]     =					[1, 2, 3, 4, 0, 0, 0, 0, 0, 0]
				self.assertEqual(truncate_or_pad(lowercase	,				self.block_size	,				0							)	,				lowercase							)


	def    _a     (self							):
				A_     :						str     =					[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
				A_     :						Tuple     =					[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
				self.assertEqual(truncate_or_pad(lowercase	,				self.block_size	,				0							)	,				lowercase							)


	def    _a     (self							):
				A_     :						List[Any]     =					[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
				A_     :						str     =					[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
				self.assertEqual(truncate_or_pad(lowercase	,				self.block_size	,				0							)	,				lowercase							)


	def    _a     (self							):
				A_     :						Any     =					"""It was the year of Our Lord one thousand seven hundred and
        seventy-five.\n\nSpiritual revelations were conceded to England at that
        favoured period, as at this."""
				A_,   A_     :						Optional[int]     =					process_story(lowercase							)
				self.assertEqual(lowercase	,				[]							)


	def    _a     (self							):
				A_     :						Union[str, Any]     =					""""""
				A_,   A_     :						Union[str, Any]     =					process_story(lowercase							)
				self.assertEqual(lowercase	,				[]							)
				self.assertEqual(lowercase	,				[]							)


	def    _a     (self							):
				A_     :						List[Any]     =					(
				    """It was the year of Our Lord one thousand seven hundred and """
				    """seventy-five\n\nSpiritual revelations were conceded to England """
				    """at that favoured period, as at this.\n@highlight\n\nIt was the best of times"""
				)
				A_,   A_     :						int     =					process_story(lowercase							)

				A_     :						List[Any]     =					[
				    """It was the year of Our Lord one thousand seven hundred and seventy-five.""",
				    """Spiritual revelations were conceded to England at that favoured period, as at this.""",
				]
				self.assertEqual(lowercase	,				lowercase							)

				A_     :						Tuple     =					["""It was the best of times."""]
				self.assertEqual(lowercase	,				lowercase							)


	def    _a     (self							):
				A_     :						Dict     =					torch.tensor([1, 2, 3, 4]							)
				A_     :						Union[str, Any]     =					torch.tensor([1, 1, 1, 1]							)
				np.testing.assert_array_equal(build_mask(lowercase	,				0							).numpy()	,				expected.numpy()							)


	def    _a     (self							):
				A_     :						Union[str, Any]     =					torch.tensor([1, 2, 3, 4, 23, 23, 23]							)
				A_     :						List[str]     =					torch.tensor([1, 1, 1, 1, 0, 0, 0]							)
				np.testing.assert_array_equal(build_mask(lowercase	,				23							).numpy()	,				expected.numpy()							)


	def    _a     (self							):
				A_     :						Optional[Any]     =					torch.tensor([8, 2, 3, 4, 1, 1, 1]							)
				A_     :						Tuple     =					torch.tensor([1, 1, 1, 1, 0, 0, 0]							)
				np.testing.assert_array_equal(build_mask(lowercase	,				1							).numpy()	,				expected.numpy()							)


	def    _a     (self							):
				A_     :						List[str]     =					101
				A_     :						str     =					torch.tensor([[1, 2, 3, 4, 5, 6], [1, 2, 3, 101, 5, 6], [1, 101, 3, 4, 101, 6]]							)
				A_     :						int     =					torch.tensor([[1, 1, 1, 1, 1, 1], [1, 1, 1, 0, 0, 0], [1, 0, 0, 0, 1, 1]]							)

				A_     :						Optional[Any]     =					compute_token_type_ids(lowercase	,				lowercase							)
				np.testing.assert_array_equal(lowercase	,				lowercase							)