





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

from ...utils import logging


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

class   lowerCAmelCase				(   __UpperCamelCase     ):
 UpperCAmelCase__						=						CLIPConfig

 UpperCAmelCase__						=						["""CLIPEncoderLayer"""]
 def __init__( self    : int     ,	UpperCAmelCase    : CLIPConfig      )     ->      Union[str, Any]:
     super().__init__(UpperCAmelCase      )

     lowerCamelCase__       : List[Any]   					=						CLIPVisionModelWithProjection(config.vision_config      )

     lowerCamelCase__       : str   					=						nn.Linear(config.vision_config.projection_dim     ,	1      )
     lowerCamelCase__       : int   					=						nn.Linear(config.vision_config.projection_dim     ,	1      )







 @torch.no_grad()
 def 	A_      ( self    : Union[str, Any]     ,	UpperCAmelCase    : Tuple     ,	UpperCAmelCase    : Union[str, Any]     ,	UpperCAmelCase    : str=0.5     ,	UpperCAmelCase    : Dict=0.5      )     ->      Union[str, Any]:
     lowerCamelCase__       : Union[str, Any]   					=						self.vision_model(UpperCAmelCase      )[0]

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

     lowerCamelCase__       : Any   					=						self.w_head(UpperCAmelCase      )
     lowerCamelCase__       : Tuple   					=						watermark_detected.flatten()
     lowerCamelCase__       : int   					=						watermark_detected > w_threshold
     lowerCamelCase__       : 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_:
             lowerCamelCase__       : int   					=						np.zeros(images[idx].shape      )

     return images, nsfw_detected, watermark_detected












from __future__ import annotations

import time
from collections.abc import Sequence
from random import randint

from matplotlib import pyplot as plt


def 			SCREAMING_SNAKE_CASE (			_UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase					)     ->	tuple[int | None, int | None, float]:
    if not arr:
        return None, None, 0
    if low == high:
        return low, high, arr[low]

    lowerCamelCase__       : Union[str, Any]   					=						(low + high) // 2
    lowerCamelCase__					,				lowerCamelCase__					,				lowerCamelCase__       : str   					=						max_subarray(_UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase					)
    lowerCamelCase__					,				lowerCamelCase__					,				lowerCamelCase__       : Optional[Any]   					=						max_subarray(_UpperCAmelCase		,      mid + 1		,      _UpperCAmelCase					)
    lowerCamelCase__					,				lowerCamelCase__					,				lowerCamelCase__       : int   					=						max_cross_sum(_UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase					)
    if left_sum >= right_sum and left_sum >= cross_sum:
        return left_low, left_high, left_sum
    elif right_sum >= left_sum and right_sum >= cross_sum:
        return right_low, right_high, right_sum
    return cross_left, cross_right, cross_sum


def 			SCREAMING_SNAKE_CASE (			_UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase					)     ->	tuple[int, int, float]:
    lowerCamelCase__					,				lowerCamelCase__       : Union[str, Any]   					=						float('-inf'					), -1
    lowerCamelCase__					,				lowerCamelCase__       : Tuple   					=						float('-inf'					), -1

    lowerCamelCase__       : int | float   					=						0
    for i in range(_UpperCAmelCase		,      low - 1		,      -1					):
        summ += arr[i]
        if summ > left_sum:
            lowerCamelCase__       : Optional[int]   					=						summ
            lowerCamelCase__       : Tuple   					=						i

    lowerCamelCase__       : int   					=						0
    for i in range(mid + 1		,      high + 1					):
        summ += arr[i]
        if summ > right_sum:
            lowerCamelCase__       : int   					=						summ
            lowerCamelCase__       : List[Any]   					=						i

    return max_left, max_right, (left_sum + right_sum)


def 			SCREAMING_SNAKE_CASE (			_UpperCAmelCase					)     ->	float:
    lowerCamelCase__       : Any   					=						[randint(1		,      _UpperCAmelCase					) for _ in range(_UpperCAmelCase					)]
    lowerCamelCase__       : Tuple   					=						time.time()
    max_subarray(_UpperCAmelCase		,      0		,      input_size - 1					)
    lowerCamelCase__       : List[Any]   					=						time.time()
    return end - start


def 			SCREAMING_SNAKE_CASE (			)     ->	None:
    lowerCamelCase__       : Optional[Any]   					=						[10, 100, 1000, 1_0000, 5_0000, 10_0000, 20_0000, 30_0000, 40_0000, 50_0000]
    lowerCamelCase__       : Optional[Any]   					=						[time_max_subarray(_UpperCAmelCase					) for input_size in input_sizes]
    print('No of Inputs\t\tTime Taken'					)
    for input_size, runtime in zip(_UpperCAmelCase		,      _UpperCAmelCase					):
        print(_UpperCAmelCase		,      '\t\t'		,      _UpperCAmelCase					)
    plt.plot(_UpperCAmelCase		,      _UpperCAmelCase					)
    plt.xlabel('Number of Inputs'					)
    plt.ylabel('Time taken in seconds'					)
    plt.show()


if __name__ == "__main__":
    from doctest import testmod

    testmod()





