



from typing import Optional, Tuple

import jax
import jax.numpy as jnp
from flax import linen as nn
from flax.core.frozen_dict import FrozenDict
from transformers import CLIPConfig, FlaxPreTrainedModel
from transformers.models.clip.modeling_flax_clip import FlaxCLIPVisionModule

def       lowerCAmelCase_	(    _lowercase							:  str		,  _lowercase							:  List[str]		,  _lowercase							:  int=1e-1_2)				->			Tuple:







			"""simple docstring"""
			a__  :		List[Any]					=    jnp.divide(emb_a.T		,  jnp.clip(jnp.linalg.norm(_SCREAMING_SNAKE_CASE		,  axis=1)		,  a_min=_SCREAMING_SNAKE_CASE)).T
			a__  :		Any					=    jnp.divide(emb_a.T		,  jnp.clip(jnp.linalg.norm(_SCREAMING_SNAKE_CASE		,  axis=1)		,  a_min=_SCREAMING_SNAKE_CASE)).T
			return jnp.matmul(_SCREAMING_SNAKE_CASE		,  norm_emb_a.T)

class   snake_case__							(nn.Module						):







						"""simple docstring"""




						__lowerCAmelCase	:CLIPConfig
						__lowerCAmelCase	:jnp.dtype         =	jnp.floataa

						def 				SCREAMING_SNAKE_CASE__(  self    )   -> Dict:
									"""simple docstring"""




									a__  :		Optional[int]					=    FlaxCLIPVisionModule(self.config.vision_config    )
									a__  :		str					=    nn.Dense(self.config.projection_dim   ,  use_bias=__UpperCAmelCase   ,  dtype=self.dtype    )

									a__  :		Tuple					=    self.param("""concept_embeds"""   ,  jax.nn.initializers.ones   ,  (1_7, self.config.projection_dim)    )
									a__  :		int					=    self.param(
									    """special_care_embeds"""   ,  jax.nn.initializers.ones   ,  (3, self.config.projection_dim)    )

									a__  :		Any					=    self.param("""concept_embeds_weights"""   ,  jax.nn.initializers.ones   ,  (1_7,)    )
									a__  :		str					=    self.param("""special_care_embeds_weights"""   ,  jax.nn.initializers.ones   ,  (3,)    )

						def __call__(  self   ,  __lowercase    )   -> List[Any]:
									"""simple docstring"""




									a__  :		Optional[int]					=    self.vision_model(__UpperCAmelCase    )[1]
									a__  :		int					=    self.visual_projection(__UpperCAmelCase    )

									a__  :		Any					=    jax_cosine_distance(__UpperCAmelCase   ,  self.special_care_embeds    )
									a__  :		Dict					=    jax_cosine_distance(__UpperCAmelCase   ,  self.concept_embeds    )

									# increase this value to create a stronger `nfsw` filter
									# at the cost of increasing the possibility of filtering benign image inputs
									a__  :		str					=    0.0

									a__  :		Dict					=    special_cos_dist - self.special_care_embeds_weights[None, :] + adjustment
									a__  :		Optional[int]					=    jnp.round(__UpperCAmelCase   ,  3    )
									a__  :		List[str]					=    jnp.any(special_scores > 0   ,  axis=1   ,  keepdims=__UpperCAmelCase    )
									# Use a lower threshold if an image has any special care concept
									a__  :		Tuple					=    is_special_care * 0.0_1

									a__  :		Tuple					=    cos_dist - self.concept_embeds_weights[None, :] + special_adjustment
									a__  :		Tuple					=    jnp.round(__UpperCAmelCase   ,  3    )
									a__  :		Tuple					=    jnp.any(concept_scores > 0   ,  axis=1    )

									return has_nsfw_concepts




class   snake_case__							(A__						):







						"""simple docstring"""




						__lowerCAmelCase	:Optional[int]         =	CLIPConfig
						__lowerCAmelCase	:Any         =	"""clip_input"""
						__lowerCAmelCase	:Tuple         =	FlaxStableDiffusionSafetyCheckerModule

						def __init__(  self   ,  __lowercase   ,  __lowercase = None   ,  __lowercase = 0   ,  __lowercase = jnp.floataa   ,  __lowercase = True   ,  **__lowercase   ,  )   -> Optional[int]:
									"""simple docstring"""




									if input_shape is None:
												a__  :		Dict					=    (1, 2_2_4, 2_2_4, 3)
									a__  :		Optional[int]					=    self.module_class(config=__UpperCAmelCase   ,  dtype=__UpperCAmelCase   ,  **__UpperCAmelCase    )
									super().__init__(__UpperCAmelCase   ,  __UpperCAmelCase   ,  input_shape=__UpperCAmelCase   ,  seed=__UpperCAmelCase   ,  dtype=__UpperCAmelCase   ,  _do_init=_do_init    )

						def 				SCREAMING_SNAKE_CASE__(  self   ,  __lowercase   ,  __lowercase   ,  __lowercase = None    )   -> Any:
									"""simple docstring"""




									a__  :		Optional[Any]					=    jax.random.normal(__UpperCAmelCase   ,  __UpperCAmelCase    )

									a__  :		Optional[Any]					=    jax.random.split(__UpperCAmelCase    )
									a__  :		Union[str, Any]					=    {'params': params_rng, 'dropout': dropout_rng}

									a__  :		Optional[int]					=    self.module.init(__UpperCAmelCase   ,  __UpperCAmelCase    )['params']

									return random_params

						def __call__(  self   ,  __lowercase   ,  __lowercase = None   ,  )   -> Dict:
									"""simple docstring"""




									a__  :		Optional[int]					=    jnp.transpose(__UpperCAmelCase   ,  (0, 2, 3, 1)    )

									return self.module.apply(
									    {"""params""": params or self.params}   ,  jnp.array(__UpperCAmelCase   ,  dtype=jnp.floataa    )   ,  rngs={}   ,  )











"""simple docstring"""





def 					__A  (_SCREAMING_SNAKE_CASE     )   ->str:





  """simple docstring"""


  lowerCAmelCase__     :List[Any]											=				int(_SCREAMING_SNAKE_CASE     )
  if decimal in (0, 1):  # Exit cases for the recursion
    return str(_SCREAMING_SNAKE_CASE     )
  lowerCAmelCase__				,							lowerCAmelCase__     :int											=				divmod(_SCREAMING_SNAKE_CASE	,  2     )
  return binary_recursive(_SCREAMING_SNAKE_CASE     ) + str(_SCREAMING_SNAKE_CASE     )
def 					__A  (_SCREAMING_SNAKE_CASE     )   ->str:





  """simple docstring"""


  lowerCAmelCase__     :str											=				str(_SCREAMING_SNAKE_CASE     ).strip()
  if not number:
    raise ValueError('No input value was provided'     )
  lowerCAmelCase__     :Dict											=				'-' if number.startswith('-'     ) else ''
  lowerCAmelCase__     :int											=				number.lstrip('-'     )
  if not number.isnumeric():
    raise ValueError('Input value is not an integer'     )
  return F"{negative}0b{binary_recursive(int(_SCREAMING_SNAKE_CASE     )     )}"


if __name__ == "__main__":
  from doctest import testmod

  testmod()
