


'''simple docstring'''

import torch
import torch.nn as nn
from transformers import CLIPConfig, CLIPVisionModel, PreTrainedModel

from ...utils import logging


lowerCamelCase							:   int  						=	logging.get_logger(__name__)

def _SCREAMING_SNAKE_CASE      (A  , A       )       ->       Optional[int]:






       """simple docstring"""







       lowercase__      			= nn.functional.normalize(A       )
       lowercase__      			= nn.functional.normalize(A       )
       return torch.mm(A  , normalized_text_embeds.t()       )

class   __lowerCAmelCase						(lowercase_     ):





      '''simple docstring'''






      lowerCAmelCase__					:     Any  					=							CLIPConfig

      lowerCAmelCase__					:     Union[str, Any]  					=							["""CLIPEncoderLayer"""]



      def __init__(self    :    List[str]   ,						UpperCamelCase    :    CLIPConfig ):

             '''simple docstring'''







             super().__init__(UpperCamelCase )

             lowercase__      			= CLIPVisionModel(config.vision_config )
             lowercase__      			= nn.Linear(config.vision_config.hidden_size   ,						config.projection_dim   ,						bias=UpperCamelCase )

             lowercase__      			= nn.Parameter(torch.ones(17   ,						config.projection_dim )   ,						requires_grad=UpperCamelCase )
             lowercase__      			= nn.Parameter(torch.ones(3   ,						config.projection_dim )   ,						requires_grad=UpperCamelCase )

             lowercase__      			= nn.Parameter(torch.ones(17 )   ,						requires_grad=UpperCamelCase )
             lowercase__      			= nn.Parameter(torch.ones(3 )   ,						requires_grad=UpperCamelCase )



      @torch.no_grad()
      def       UpperCamelCase__		(self    :    Union[str, Any]   ,						UpperCamelCase    :    Dict   ,						UpperCamelCase    :    int ):

             '''simple docstring'''







             lowercase__      			= self.vision_model(UpperCamelCase )[1]  # pooled_output
             lowercase__      			= self.visual_projection(UpperCamelCase )

             # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
             lowercase__      			= cosine_distance(UpperCamelCase   ,						self.special_care_embeds ).cpu().float().numpy()
             lowercase__      			= cosine_distance(UpperCamelCase   ,						self.concept_embeds ).cpu().float().numpy()

             lowercase__      			= []
             lowercase__      			= image_embeds.shape[0]
             for i in range(UpperCamelCase ):
                    lowercase__      			= {'''special_scores''': {}, '''special_care''': [], '''concept_scores''': {}, '''bad_concepts''': []}

                    # increase this value to create a stronger `nfsw` filter
                    # at the cost of increasing the possibility of filtering benign images
                    lowercase__      			= 0.0

                    for concept_idx in range(len(special_cos_dist[0] ) ):
                           lowercase__      			= special_cos_dist[i][concept_idx]
                           lowercase__      			= self.special_care_embeds_weights[concept_idx].item()
                           lowercase__      			= round(concept_cos - concept_threshold + adjustment   ,						3 )
                           if result_img["special_scores"][concept_idx] > 0:
                                  result_img["special_care"].append({concept_idx, result_img['''special_scores'''][concept_idx]} )
                                  lowercase__      			= 0.01

                    for concept_idx in range(len(cos_dist[0] ) ):
                           lowercase__      			= cos_dist[i][concept_idx]
                           lowercase__      			= self.concept_embeds_weights[concept_idx].item()
                           lowercase__      			= round(concept_cos - concept_threshold + adjustment   ,						3 )
                           if result_img["concept_scores"][concept_idx] > 0:
                                  result_img["bad_concepts"].append(UpperCamelCase )

                    result.append(UpperCamelCase )

             lowercase__      			= [len(res['''bad_concepts'''] ) > 0 for res in result]

             return images, has_nsfw_concepts






      @torch.no_grad()
      def       UpperCamelCase__		(self    :    Tuple   ,						UpperCamelCase    :    torch.FloatTensor   ,						UpperCamelCase    :    torch.FloatTensor ):

             '''simple docstring'''







             lowercase__      			= self.vision_model(UpperCamelCase )[1]  # pooled_output
             lowercase__      			= self.visual_projection(UpperCamelCase )

             lowercase__      			= cosine_distance(UpperCamelCase   ,						self.special_care_embeds )
             lowercase__      			= cosine_distance(UpperCamelCase   ,						self.concept_embeds )

             # increase this value to create a stronger `nsfw` filter
             # at the cost of increasing the possibility of filtering benign images
             lowercase__      			= 0.0

             lowercase__      			= special_cos_dist - self.special_care_embeds_weights + adjustment
             # special_scores = special_scores.round(decimals=3)
             lowercase__      			= torch.any(special_scores > 0   ,						dim=1 )
             lowercase__      			= special_care * 0.01
             lowercase__      			= special_adjustment.unsqueeze(1 ).expand(-1   ,						cos_dist.shape[1] )

             lowercase__      			= (cos_dist - self.concept_embeds_weights) + special_adjustment
             # concept_scores = concept_scores.round(decimals=3)
             lowercase__      			= torch.any(concept_scores > 0   ,						dim=1 )

             return images, has_nsfw_concepts











from pathlib import Path

import fire



def    __lowercase   (      _SCREAMING_SNAKE_CASE ,						_SCREAMING_SNAKE_CASE ,						_SCREAMING_SNAKE_CASE	) ->   List[str]:


      '''simple docstring'''


      SCREAMING_SNAKE_CASE		=						Path(_SCREAMING_SNAKE_CASE	)
      SCREAMING_SNAKE_CASE		=						Path(_SCREAMING_SNAKE_CASE	)
      dest_dir.mkdir(exist_ok=_SCREAMING_SNAKE_CASE	)
      for path in src_dir.iterdir():
            SCREAMING_SNAKE_CASE		=						[x.rstrip() for x in list(path.open().readlines()	)][:n]
            SCREAMING_SNAKE_CASE		=						dest_dir.joinpath(path.name	)
            print(_SCREAMING_SNAKE_CASE	)
            dest_path.open("""w"""	).write("""\n""".join(_SCREAMING_SNAKE_CASE	)	)


if __name__ == "__main__":
      fire.Fire(minify)


