
"""simple docstring"""




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   __magic_name__	(      __snake_case				:       Any     , __snake_case				:       Any     , __snake_case				:       Dict=1E-12  ) ->				Dict:
       lowercase    :				List[Any]       		=							jnp.divide(emb_a.T     , jnp.clip(jnp.linalg.norm(__snake_case     , axis=1  )     , a_min=__snake_case  )  ).T
       lowercase    :				str       		=							jnp.divide(emb_a.T     , jnp.clip(jnp.linalg.norm(__snake_case     , axis=1  )     , a_min=__snake_case  )  ).T
       return jnp.matmul(__snake_case     , norm_emb_a.T  )

class 		a__						(      nn.Module						):
   __lowerCAmelCase            =				42
   __lowerCAmelCase            =				jnp.floataa
   def 			__magic_name__			(			self				):
          lowercase    :				int       		=							FlaxCLIPVisionModule(self.config.vision_config				)
          lowercase    :				Union[str, Any]       		=							nn.Dense(self.config.projection_dim	,    use_bias=_a	,    dtype=self.dtype				)

          lowercase    :				Optional[Any]       		=							self.param("concept_embeds"	,    jax.nn.initializers.ones	,    (17, self.config.projection_dim)				)
          lowercase    :				int       		=							self.param(
              "special_care_embeds"	,    jax.nn.initializers.ones	,    (3, self.config.projection_dim)				)

          lowercase    :				str       		=							self.param("concept_embeds_weights"	,    jax.nn.initializers.ones	,    (17,)				)
          lowercase    :				Dict       		=							self.param("special_care_embeds_weights"	,    jax.nn.initializers.ones	,    (3,)				)

   def __call__(			self	,    _a				):
          lowercase    :				Any       		=							self.vision_model(_a				)[1]
          lowercase    :				Union[str, Any]       		=							self.visual_projection(_a				)

          lowercase    :				List[Any]       		=							jax_cosine_distance(_a	,    self.special_care_embeds				)
          lowercase    :				str       		=							jax_cosine_distance(_a	,    self.concept_embeds				)

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

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

          lowercase    :				Optional[Any]       		=							cos_dist - self.concept_embeds_weights[None, :] + special_adjustment
          lowercase    :				Union[str, Any]       		=							jnp.round(_a	,    3				)
          lowercase    :				Tuple       		=							jnp.any(concept_scores > 0	,    axis=1				)

          return has_nsfw_concepts


class 		a__						(      a_						):
   __lowerCAmelCase            =				CLIPConfig
   __lowerCAmelCase            =				"""clip_input"""
   __lowerCAmelCase            =				FlaxStableDiffusionSafetyCheckerModule
   def __init__(			self	,    _a	,    _a = None	,    _a = 0	,    _a = jnp.floataa	,    _a = True	,    **_a	,    ):
          if input_shape is None:
                 lowercase    :				List[Any]       		=							(1, 224, 224, 3)
          lowercase    :				Union[str, Any]       		=							self.module_class(config=_a	,    dtype=_a	,    **_a				)
          super().__init__(_a	,    _a	,    input_shape=_a	,    seed=_a	,    dtype=_a	,    _do_init=_do_init				)
   def 			__magic_name__			(			self	,    _a	,    _a	,    _a = None				):
          # init input tensor
          lowercase    :				Tuple       		=							jax.random.normal(_a	,    _a				)

          lowercase		,    lowercase    :				int       		=							jax.random.split(_a				)
          lowercase    :				List[str]       		=							{"params": params_rng, "dropout": dropout_rng}

          lowercase    :				Union[str, Any]       		=							self.module.init(_a	,    _a				)["params"]

          return random_params

   def __call__(			self	,    _a	,    _a = None	,    ):
          lowercase    :				str       		=							jnp.transpose(_a	,    (0, 2, 3, 1)				)

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




"""simple docstring"""




import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin


_A			:       int   		=       """
Hugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf originally as a company that developed a chatbot app targeted at teenagers.[2] After open-sourcing the model behind the chatbot, the company pivoted to focus on being a platform for machine learning.

In March 2021, Hugging Face raised $40 million in a Series B funding round.[3]

On April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model.[4] In 2022, the workshop concluded with the announcement of BLOOM, a multilingual large language model with 176 billion parameters.[5]
"""

class 		a__						(      unittest.TestCase,			a_						):
   def 			__magic_name__			(			self				):
          lowercase    :				Tuple       		=							load_tool("text-question-answering"				)
          self.tool.setup()
          lowercase    :				Dict       		=							load_tool("text-question-answering"	,    remote=_a				)
   def 			__magic_name__			(			self				):
          lowercase    :				str       		=							self.tool(_a	,    "What did Hugging Face do in April 2021?"				)
          self.assertEqual(_a	,    "launched the BigScience Research Workshop"				)
   def 			__magic_name__			(			self				):
          lowercase    :				Union[str, Any]       		=							self.remote_tool(_a	,    "What did Hugging Face do in April 2021?"				)
          self.assertEqual(_a	,    "launched the BigScience Research Workshop"				)
   def 			__magic_name__			(			self				):
          lowercase    :				int       		=							self.tool(text=_a	,    question="What did Hugging Face do in April 2021?"				)
          self.assertEqual(_a	,    "launched the BigScience Research Workshop"				)

   def 			__magic_name__			(			self				):
          lowercase    :				Optional[Any]       		=							self.remote_tool(text=_a	,    question="What did Hugging Face do in April 2021?"				)
          self.assertEqual(_a	,    "launched the BigScience Research Workshop"				)


