


'''simple docstring'''



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

from ...utils import logging


A__		: List[str]												=  logging.get_logger(__name__)



def       UpperCAmelCase__ (     UpperCAmelCase_   :    Tuple				,   UpperCAmelCase_   :    Dict							)					->		str:
					__lowerCamelCase       :     str        =		nn.functional.normalize(UpperCAmelCase_							)
					__lowerCamelCase       :     Union[str, Any]        =		nn.functional.normalize(UpperCAmelCase_							)
					return torch.mm(UpperCAmelCase_				,   normalized_text_embeds.t()							)





class        UpperCAmelCase_  (_UpperCAmelCase  ):





				"""simple docstring"""






				lowerCamelCase  : Optional[int]												=      CLIPConfig

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







				def __init__(							self      ,					SCREAMING_SNAKE_CASE_     )    ->					Dict:
									super().__init__(SCREAMING_SNAKE_CASE_     )

									__lowerCamelCase       :     Dict        =		CLIPVisionModel(config.vision_config     )
									__lowerCamelCase       :     int        =		nn.Linear(config.vision_config.hidden_size      ,					config.projection_dim      ,					bias=SCREAMING_SNAKE_CASE_     )

									__lowerCamelCase       :     Optional[int]        =		nn.Parameter(torch.ones(17      ,					config.projection_dim     )      ,					requires_grad=SCREAMING_SNAKE_CASE_     )
									__lowerCamelCase       :     Tuple        =		nn.Parameter(torch.ones(3      ,					config.projection_dim     )      ,					requires_grad=SCREAMING_SNAKE_CASE_     )

									__lowerCamelCase       :     Union[str, Any]        =		nn.Parameter(torch.ones(17     )      ,					requires_grad=SCREAMING_SNAKE_CASE_     )
									__lowerCamelCase       :     Dict        =		nn.Parameter(torch.ones(3     )      ,					requires_grad=SCREAMING_SNAKE_CASE_     )







				@torch.no_grad()
				def 				lowercase_    (							self      ,					SCREAMING_SNAKE_CASE_      ,					SCREAMING_SNAKE_CASE_     )    ->					Union[str, Any]:
									__lowerCamelCase       :     Optional[int]        =		self.vision_model(SCREAMING_SNAKE_CASE_     )[1]  # pooled_output
									__lowerCamelCase       :     str        =		self.visual_projection(SCREAMING_SNAKE_CASE_     )

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

									__lowerCamelCase       :     Tuple        =		[]
									__lowerCamelCase       :     Any        =		image_embeds.shape[0]
									for i in range(SCREAMING_SNAKE_CASE_     ):
														__lowerCamelCase       :     Dict        =		{'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
														__lowerCamelCase       :     int        =		0.0

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

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

														result.append(SCREAMING_SNAKE_CASE_     )

									__lowerCamelCase       :     int        =		[len(res['bad_concepts']     ) > 0 for res in result]

									for idx, has_nsfw_concept in enumerate(SCREAMING_SNAKE_CASE_     ):
														if has_nsfw_concept:
																			if torch.is_tensor(SCREAMING_SNAKE_CASE_     ) or torch.is_tensor(images[0]     ):
																								__lowerCamelCase       :     str        =		torch.zeros_like(images[idx]     )  # black image
																			else:
																								__lowerCamelCase       :     List[str]        =		np.zeros(images[idx].shape     )  # black image

									if any(SCREAMING_SNAKE_CASE_     ):
														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 				lowercase_    (							self      ,					SCREAMING_SNAKE_CASE_      ,					SCREAMING_SNAKE_CASE_     )    ->					Optional[int]:
									__lowerCamelCase       :     Optional[Any]        =		self.vision_model(SCREAMING_SNAKE_CASE_     )[1]  # pooled_output
									__lowerCamelCase       :     Any        =		self.visual_projection(SCREAMING_SNAKE_CASE_     )

									__lowerCamelCase       :     Any        =		cosine_distance(SCREAMING_SNAKE_CASE_      ,					self.special_care_embeds     )
									__lowerCamelCase       :     int        =		cosine_distance(SCREAMING_SNAKE_CASE_      ,					self.concept_embeds     )

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

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

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

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

									return images, has_nsfw_concepts










'''simple docstring'''



from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


A__		: str												=  {"""configuration_hubert""": ["""HUBERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """HubertConfig"""]}

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      A__		: List[str]												=  [
          """HUBERT_PRETRAINED_MODEL_ARCHIVE_LIST""",
          """HubertForCTC""",
          """HubertForSequenceClassification""",
          """HubertModel""",
          """HubertPreTrainedModel""",
      ]


try:
      if not is_tf_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      A__		: Optional[int]												=  [
          """TF_HUBERT_PRETRAINED_MODEL_ARCHIVE_LIST""",
          """TFHubertForCTC""",
          """TFHubertModel""",
          """TFHubertPreTrainedModel""",
      ]

if TYPE_CHECKING:
      from .configuration_hubert import HUBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, HubertConfig

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_hubert import (
                HUBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
                HubertForCTC,
                HubertForSequenceClassification,
                HubertModel,
                HubertPreTrainedModel,
            )

      try:
            if not is_tf_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_tf_hubert import (
                TF_HUBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
                TFHubertForCTC,
                TFHubertModel,
                TFHubertPreTrainedModel,
            )


else:
      import sys

      A__		: Optional[int]												=  _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)






