



from manim import *


class  SCREAMING_SNAKE_CASE			(			UpperCamelCase_					):
     '''simple docstring'''


     def 				_A       (				self       :	Optional[Any]      ):
       SCREAMING_SNAKE_CASE       :      Any							=    Rectangle(height=0.5					, width=0.5      )
       SCREAMING_SNAKE_CASE       :      str							=    Rectangle(height=0.46					, width=0.46      ).set_stroke(width=0      )

       SCREAMING_SNAKE_CASE       :      List[str]							=    [mem.copy() for i in range(6      )]
       SCREAMING_SNAKE_CASE       :      Optional[Any]							=    [mem.copy() for i in range(6      )]
       SCREAMING_SNAKE_CASE       :      List[str]							=    VGroup(*_a      ).arrange(_a					, buff=0      )
       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    VGroup(*_a      ).arrange(_a					, buff=0      )
       SCREAMING_SNAKE_CASE       :      Tuple							=    VGroup(_a					, _a      ).arrange(_a					, buff=0      )
       SCREAMING_SNAKE_CASE       :      int							=    Text("CPU"					, font_size=24      )
       SCREAMING_SNAKE_CASE       :      List[str]							=    Group(_a					, _a      ).arrange(_a					, buff=0.5					, aligned_edge=_a      )
       cpu.move_to([-2.5, -0.5, 0]      )
       self.add(_a      )

       SCREAMING_SNAKE_CASE       :      List[Any]							=    [mem.copy() for i in range(1      )]
       SCREAMING_SNAKE_CASE       :      str							=    VGroup(*_a      ).arrange(_a					, buff=0      )
       SCREAMING_SNAKE_CASE       :      List[str]							=    Text("GPU"					, font_size=24      )
       SCREAMING_SNAKE_CASE       :      List[Any]							=    Group(_a					, _a      ).arrange(_a					, buff=0.5					, aligned_edge=_a      )
       gpu.align_to(_a					, _a      )
       gpu.set_x(gpu.get_x() - 1      )

       self.add(_a      )

       SCREAMING_SNAKE_CASE       :      List[str]							=    [mem.copy() for i in range(6      )]
       SCREAMING_SNAKE_CASE       :      Optional[int]							=    VGroup(*_a      ).arrange(_a					, buff=0      )

       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    Text("Model"					, font_size=24      )
       SCREAMING_SNAKE_CASE       :      int							=    Group(_a					, _a      ).arrange(_a					, buff=0.5					, aligned_edge=_a      )
       model.move_to([3, -1.0, 0]      )

       self.play(
           Create(_a					, run_time=1      )					, Create(_a					, run_time=1      )					, Create(_a					, run_time=1      )					, )

       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    MarkupText(
           f'''First, an empty model skeleton is loaded\ninto <span fgcolor=\'{YELLOW}\'>memory</span> without using much RAM.'''					, font_size=24					, )

       SCREAMING_SNAKE_CASE       :      Optional[int]							=    Square(side_length=2.2      )
       key.move_to([-5, 2, 0]      )

       SCREAMING_SNAKE_CASE       :      int							=    MarkupText(
           f'''<b>Key:</b>\n\n<span fgcolor=\'{YELLOW}\'>●</span> Empty Model'''					, font_size=18					, )

       key_text.move_to([-5, 2.4, 0]      )

       step_a.move_to([2, 2, 0]      )
       self.play(Write(_a					, run_time=2.5      )					, Write(_a      )					, Write(_a      )      )

       self.add(_a      )

       SCREAMING_SNAKE_CASE       :      List[str]							=    []
       SCREAMING_SNAKE_CASE       :      str							=    []
       SCREAMING_SNAKE_CASE       :      Optional[Any]							=    []
       for i, rect in enumerate(_a      ):
         SCREAMING_SNAKE_CASE       :      Tuple							=    Rectangle(height=0.46					, width=0.46      ).set_stroke(width=0.0      ).set_fill(_a					, opacity=0.7      )
         cpu_target.move_to(_a      )
         cpu_target.generate_target()
         SCREAMING_SNAKE_CASE       :      Optional[int]							=    0.46 / 4
         SCREAMING_SNAKE_CASE       :      List[Any]							=    0.46 / 3

         if i == 0:
           cpu_target.target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT      )					, buff=0.02					, direction=_a      )
           cpu_target.target.set_x(cpu_target.target.get_x() + 0.1      )
         elif i == 3:
           cpu_target.target.next_to(cpu_targs[0].target					, direction=_a					, buff=0.0      )
         else:
           cpu_target.target.next_to(cpu_targs[i - 1].target					, direction=_a					, buff=0.0      )
         cpu_targs.append(_a      )

         first_animations.append(rect.animate(run_time=0.5      ).set_stroke(_a      )      )
         second_animations.append(MoveToTarget(_a					, run_time=1.5      )      )

       self.play(*_a      )
       self.play(*_a      )

       self.wait()














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

from ...utils import logging


snake_case    				=		logging.get_logger(__name__)


class  SCREAMING_SNAKE_CASE			(			lowerCAmelCase					):
     '''simple docstring'''


     UpperCamelCase_							:		Union[str, Any]			=	CLIPConfig

     UpperCamelCase_							:		Optional[int]			=	['''CLIPEncoderLayer''']


     def __init__(				self       :	List[Any]					, UpperCAmelCase_       :	CLIPConfig      ):
       super().__init__(UpperCAmelCase_      )

       SCREAMING_SNAKE_CASE       :      Any							=    CLIPVisionModelWithProjection(config.vision_config      )

       SCREAMING_SNAKE_CASE       :      List[Any]							=    nn.Linear(config.vision_config.projection_dim					, 1      )
       SCREAMING_SNAKE_CASE       :      List[str]							=    nn.Linear(config.vision_config.projection_dim					, 1      )






     @torch.no_grad()
     def 				_A       (				self       :	Union[str, Any]					, UpperCAmelCase_       :	int					, UpperCAmelCase_       :	Dict					, UpperCAmelCase_       :	List[Any]=0.5					, UpperCAmelCase_       :	Optional[Any]=0.5      ):
       SCREAMING_SNAKE_CASE       :      str							=    self.vision_model(UpperCAmelCase_      )[0]

       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    self.p_head(UpperCAmelCase_      )
       SCREAMING_SNAKE_CASE       :      List[str]							=    nsfw_detected.flatten()
       SCREAMING_SNAKE_CASE       :      List[str]							=    nsfw_detected > p_threshold
       SCREAMING_SNAKE_CASE       :      List[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_:
           SCREAMING_SNAKE_CASE       :      Dict							=    np.zeros(images[idx].shape      )

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

       return images, nsfw_detected, watermark_detected






