

"""simple docstring"""







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

from ...utils import logging


lowerCAmelCase_	:    Tuple            =      logging.get_logger(__name__)





class   UpperCamelCase_ (       __snake_case  ):
  _A       :					Union[str, Any]				=	CLIPConfig

  _A       :					Optional[Any]				=	['CLIPEncoderLayer']



  def __init__(      self ,		snake_case__				)			->			Any:



         """simple docstring"""





         super().__init__(_lowercase				)

         UpperCAmelCase	    =			CLIPVisionModelWithProjection(config.vision_config				)

         UpperCAmelCase	    =			nn.Linear(config.vision_config.projection_dim ,		1				)
         UpperCAmelCase	    =			nn.Linear(config.vision_config.projection_dim ,		1				)



  @torch.no_grad()
  def 		UpperCamelCase_				(      self ,		snake_case__ ,		snake_case__ ,		snake_case__=0.5 ,		snake_case__=0.5				)			->			Dict:



         """simple docstring"""





         UpperCAmelCase	    =			self.vision_model(_lowercase				)[0]

         UpperCAmelCase	    =			self.p_head(_lowercase				)
         UpperCAmelCase	    =			nsfw_detected.flatten()
         UpperCAmelCase	    =			nsfw_detected > p_threshold
         UpperCAmelCase	    =			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_:
                       UpperCAmelCase	    =			np.zeros(images[idx].shape				)

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

         return images, nsfw_detected, watermark_detected








"""simple docstring"""







def       _lowerCAmelCase       (      lowerCAmelCase		):







       '''simple docstring'''
       UpperCAmelCase	    =			0
       UpperCAmelCase	    =			len(lowerCAmelCase		)

       for i in range(n - 1		):
              for j in range(i + 1	,      lowerCAmelCase		):
                     if arr[i] > arr[j]:
                            num_inversions += 1

       return num_inversions






def       _lowerCAmelCase       (      lowerCAmelCase		):







       '''simple docstring'''
       if len(lowerCAmelCase		) <= 1:
              return arr, 0
       UpperCAmelCase	    =			len(lowerCAmelCase		) // 2
       UpperCAmelCase	    =			arr[0:mid]
       UpperCAmelCase	    =			arr[mid:]

       UpperCAmelCase    ,       UpperCAmelCase	    =			count_inversions_recursive(lowerCAmelCase		)
       UpperCAmelCase    ,       UpperCAmelCase	    =			count_inversions_recursive(lowerCAmelCase		)
       UpperCAmelCase    ,       UpperCAmelCase	    =			_count_cross_inversions(lowerCAmelCase	,      lowerCAmelCase		)

       UpperCAmelCase	    =			inversion_p + inversions_q + cross_inversions
       return c, num_inversions






def       _lowerCAmelCase       (      lowerCAmelCase	,      lowerCAmelCase		):







       '''simple docstring'''
       UpperCAmelCase	    =			[]
       UpperCAmelCase	    =			UpperCAmelCase	    =			UpperCAmelCase	    =			0
       while i < len(lowerCAmelCase		) and j < len(lowerCAmelCase		):
              if p[i] > q[j]:
                     # if P[1] > Q[j], then P[k] > Q[k] for all  i < k <= len(P)
                     # These are all inversions. The claim emerges from the
                     # property that P is sorted.
                     num_inversion += len(lowerCAmelCase		) - i
                     r.append(q[j]		)
                     j += 1
              else:
                     r.append(p[i]		)
                     i += 1

       if i < len(lowerCAmelCase		):
              r.extend(p[i:]		)
       else:
              r.extend(q[j:]		)

       return r, num_inversion






def       _lowerCAmelCase       (      ):







       '''simple docstring'''
       UpperCAmelCase	    =			[10, 2, 1, 5, 5, 2, 11]

       # this arr has 8 inversions:
       # (10, 2), (10, 1), (10, 5), (10, 5), (10, 2), (2, 1), (5, 2), (5, 2)

       UpperCAmelCase	    =			count_inversions_bf(lowerCAmelCase		)
       UpperCAmelCase    ,       UpperCAmelCase	    =			count_inversions_recursive(lowerCAmelCase		)

       assert num_inversions_bf == num_inversions_recursive == 8

       print("""number of inversions = """	,      lowerCAmelCase		)

       # testing an array with zero inversion (a sorted arr_1)

       arr_a.sort()
       UpperCAmelCase	    =			count_inversions_bf(lowerCAmelCase		)
       UpperCAmelCase    ,       UpperCAmelCase	    =			count_inversions_recursive(lowerCAmelCase		)

       assert num_inversions_bf == num_inversions_recursive == 0
       print("""number of inversions = """	,      lowerCAmelCase		)

       # an empty list should also have zero inversions
       UpperCAmelCase	    =			[]
       UpperCAmelCase	    =			count_inversions_bf(lowerCAmelCase		)
       UpperCAmelCase    ,       UpperCAmelCase	    =			count_inversions_recursive(lowerCAmelCase		)

       assert num_inversions_bf == num_inversions_recursive == 0
       print("""number of inversions = """	,      lowerCAmelCase		)


if __name__ == "__main__":
 main()
