



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

from ...utils import logging


_lowerCamelCase         =	logging.get_logger(__name__)






def 						_lowerCAmelCase    (					__lowerCamelCase :  str		,						__lowerCamelCase :  Any  ):



 """simple docstring"""





 __SCREAMING_SNAKE_CASE							:     List[str]							=						nn.functional.normalize(__lowerCamelCase  )
 __SCREAMING_SNAKE_CASE							:     int							=						nn.functional.normalize(__lowerCamelCase  )
 return torch.mm(__lowerCamelCase		,						normalized_text_embeds.t()  )






class    _SCREAMING_SNAKE_CASE						(UpperCamelCase			):
     lowerCAmelCase       =      CLIPConfig

     lowerCAmelCase       =      ["""CLIPEncoderLayer"""]





     def __init__(							self			:				int  ,	UpperCamelCase			:				CLIPConfig					)->Optional[Any]:
      super().__init__(UpperCamelCase					)

      __SCREAMING_SNAKE_CASE							:     int							=						CLIPVisionModel(config.vision_config					)
      __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						nn.Linear(config.vision_config.hidden_size  ,	config.projection_dim  ,	bias=UpperCamelCase					)

      __SCREAMING_SNAKE_CASE							:     str							=						nn.Parameter(torch.ones(1_7  ,	config.projection_dim					)  ,	requires_grad=UpperCamelCase					)
      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						nn.Parameter(torch.ones(3  ,	config.projection_dim					)  ,	requires_grad=UpperCamelCase					)

      __SCREAMING_SNAKE_CASE							:     List[Any]							=						nn.Parameter(torch.ones(1_7					)  ,	requires_grad=UpperCamelCase					)
      __SCREAMING_SNAKE_CASE							:     Dict							=						nn.Parameter(torch.ones(3					)  ,	requires_grad=UpperCamelCase					)





     @torch.no_grad()
     def 						__snake_case     (							self			:				List[Any]  ,	UpperCamelCase			:				Any  ,	UpperCamelCase			:				str					)->str:
      __SCREAMING_SNAKE_CASE							:     int							=						self.vision_model(UpperCamelCase					)[1]  # pooled_output
      __SCREAMING_SNAKE_CASE							:     List[Any]							=						self.visual_projection(UpperCamelCase					)

      # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						cosine_distance(UpperCamelCase  ,	self.special_care_embeds					).cpu().float().numpy()
      __SCREAMING_SNAKE_CASE							:     str							=						cosine_distance(UpperCamelCase  ,	self.concept_embeds					).cpu().float().numpy()

      __SCREAMING_SNAKE_CASE							:     Dict							=						[]
      __SCREAMING_SNAKE_CASE							:     List[str]							=						image_embeds.shape[0]
      for i in range(UpperCamelCase					):
       __SCREAMING_SNAKE_CASE							:     List[str]							=						{"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
       __SCREAMING_SNAKE_CASE							:     List[Any]							=						0.0

       for concept_idx in range(len(special_cos_dist[0]					)					):
        __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						special_cos_dist[i][concept_idx]
        __SCREAMING_SNAKE_CASE							:     Optional[int]							=						self.special_care_embeds_weights[concept_idx].item()
        __SCREAMING_SNAKE_CASE							:     Dict							=						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]}					)
         __SCREAMING_SNAKE_CASE							:     Dict							=						0.0_1

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

       result.append(UpperCamelCase					)

      __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						[len(res["bad_concepts"]					) > 0 for res in result]

      for idx, has_nsfw_concept in enumerate(UpperCamelCase					):
       if has_nsfw_concept:
        if torch.is_tensor(UpperCamelCase					) or torch.is_tensor(images[0]					):
         __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						torch.zeros_like(images[idx]					)  # black image
        else:
         __SCREAMING_SNAKE_CASE							:     Tuple							=						np.zeros(images[idx].shape					)  # black image

      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."					)

      return images, has_nsfw_concepts






     @torch.no_grad()
     def 						__snake_case     (							self			:				Any  ,	UpperCamelCase			:				torch.FloatTensor  ,	UpperCamelCase			:				torch.FloatTensor					)->int:
      __SCREAMING_SNAKE_CASE							:     Dict							=						self.vision_model(UpperCamelCase					)[1]  # pooled_output
      __SCREAMING_SNAKE_CASE							:     str							=						self.visual_projection(UpperCamelCase					)

      __SCREAMING_SNAKE_CASE							:     Dict							=						cosine_distance(UpperCamelCase  ,	self.special_care_embeds					)
      __SCREAMING_SNAKE_CASE							:     str							=						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
      __SCREAMING_SNAKE_CASE							:     List[Any]							=						0.0

      __SCREAMING_SNAKE_CASE							:     Tuple							=						special_cos_dist - self.special_care_embeds_weights + adjustment
      # special_scores = special_scores.round(decimals=3)
      __SCREAMING_SNAKE_CASE							:     Tuple							=						torch.any(special_scores > 0  ,	dim=1					)
      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						special_care * 0.0_1
      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						special_adjustment.unsqueeze(1					).expand(-1  ,	cos_dist.shape[1]					)

      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						(cos_dist - self.concept_embeds_weights) + special_adjustment
      # concept_scores = concept_scores.round(decimals=3)
      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						torch.any(concept_scores > 0  ,	dim=1					)

      __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						0.0  # black image

      return images, has_nsfw_concepts










import numpy as np  # noqa: E402

from ...configuration_utils import ConfigMixin, register_to_config
from ...schedulers.scheduling_utils import SchedulerMixin


try:
  import librosa  # noqa: E402

  __UpperCAmelCase			:			Union[str, Any]			      =					True
  __UpperCAmelCase			:			Optional[int]			      =					""
except Exception as e:
  __UpperCAmelCase			:			Union[str, Any]			      =					False
  __UpperCAmelCase			:			Tuple			      =					(
      f'''Cannot import librosa because {e}. Make sure to correctly install librosa to be able to install it.'''
  )


from PIL import Image  # noqa: E402



class  UpperCAmelCase_						(				_a,  _a):
       '''simple docstring'''
       __UpperCamelCase   :  Tuple				=       "mel_config.json"






       @register_to_config
       def __init__(  self			,       __SCREAMING_SNAKE_CASE = 256			,       __SCREAMING_SNAKE_CASE = 256			,       __SCREAMING_SNAKE_CASE = 22_050			,       __SCREAMING_SNAKE_CASE = 2_048			,       __SCREAMING_SNAKE_CASE = 512			,       __SCREAMING_SNAKE_CASE = 80			,       __SCREAMING_SNAKE_CASE = 32			,       ):




              """simple docstring"""




              UpperCamelCase     :						Union[str, Any] 						=		hop_length
              UpperCamelCase     :						List[str] 						=		sample_rate
              UpperCamelCase     :						List[Any] 						=		n_fft
              UpperCamelCase     :						List[str] 						=		top_db
              UpperCamelCase     :						Tuple 						=		n_iter
              self.set_resolution(__SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE    )
              UpperCamelCase     :						Any 						=		None

              if not _librosa_can_be_imported:
                     raise ValueError(_import_error    )






       def 			_lowercase (  self			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE    ):




              """simple docstring"""




              UpperCamelCase     :						Any 						=		x_res
              UpperCamelCase     :						Union[str, Any] 						=		y_res
              UpperCamelCase     :						Any 						=		self.y_res
              UpperCamelCase     :						Union[str, Any] 						=		self.x_res * self.hop_length - 1






       def 			_lowercase (  self			,       __SCREAMING_SNAKE_CASE = None			,       __SCREAMING_SNAKE_CASE = None    ):




              """simple docstring"""




              if audio_file is not None:
                     UpperCamelCase						,			UpperCamelCase     :						Tuple 						=		librosa.load(__SCREAMING_SNAKE_CASE			,       mono=__SCREAMING_SNAKE_CASE			,       sr=self.sr    )
              else:
                     UpperCamelCase     :						Tuple 						=		raw_audio

              # Pad with silence if necessary.
              if len(self.audio    ) < self.x_res * self.hop_length:
                     UpperCamelCase     :						List[str] 						=		np.concatenate([self.audio, np.zeros((self.x_res * self.hop_length - len(self.audio    ),)    )]    )






       def 			_lowercase (  self    ):




              """simple docstring"""




              return len(self.audio    ) // self.slice_size






       def 			_lowercase (  self			,       __SCREAMING_SNAKE_CASE = 0    ):




              """simple docstring"""




              return self.audio[self.slice_size * slice : self.slice_size * (slice + 1)]






       def 			_lowercase (  self    ):




              """simple docstring"""




              return self.sr






       def 			_lowercase (  self			,       __SCREAMING_SNAKE_CASE    ):




              """simple docstring"""




              UpperCamelCase     :						List[str] 						=		librosa.feature.melspectrogram(
                  y=self.get_audio_slice(__SCREAMING_SNAKE_CASE    )			,       sr=self.sr			,       n_fft=self.n_fft			,       hop_length=self.hop_length			,       n_mels=self.n_mels    )
              UpperCamelCase     :						Optional[int] 						=		librosa.power_to_db(__SCREAMING_SNAKE_CASE			,       ref=np.max			,       top_db=self.top_db    )
              UpperCamelCase     :						Optional[int] 						=		(((log_S + self.top_db) * 255 / self.top_db).clip(0			,       255    ) + 0.5).astype(np.uinta    )
              UpperCamelCase     :						List[Any] 						=		Image.fromarray(__SCREAMING_SNAKE_CASE    )
              return image






       def 			_lowercase (  self			,       __SCREAMING_SNAKE_CASE    ):




              """simple docstring"""




              UpperCamelCase     :						int 						=		np.frombuffer(image.tobytes()			,       dtype='''uint8'''    ).reshape((image.height, image.width)    )
              UpperCamelCase     :						Tuple 						=		bytedata.astype('''float'''    ) * self.top_db / 255 - self.top_db
              UpperCamelCase     :						List[Any] 						=		librosa.db_to_power(__SCREAMING_SNAKE_CASE    )
              UpperCamelCase     :						Optional[int] 						=		librosa.feature.inverse.mel_to_audio(
                  __SCREAMING_SNAKE_CASE			,       sr=self.sr			,       n_fft=self.n_fft			,       hop_length=self.hop_length			,       n_iter=self.n_iter    )
              return audio

