



"""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 		__snake_case		( SCREAMING_SNAKE_CASE__      :		Any		,    SCREAMING_SNAKE_CASE__      :		Optional[int]		,    SCREAMING_SNAKE_CASE__      :		List[str]=1E-12							)    ->  int:





     '''simple docstring'''

     _UpperCAmelCase     :      List[str]														=       jnp.divide(emb_a.T		,    jnp.clip(jnp.linalg.norm(SCREAMING_SNAKE_CASE__		,    axis=1							)		,    a_min=SCREAMING_SNAKE_CASE__							)							).T
     _UpperCAmelCase     :      str														=       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       UpperCAmelCase_    (							nn.Module		):
      __SCREAMING_SNAKE_CASE		:  CLIPConfig
      __SCREAMING_SNAKE_CASE		:  jnp.dtype						=					jnp.floataa



      def 		snake_case_   (	self	:      int		):
           _UpperCAmelCase     :      List[str]														=       FlaxCLIPVisionModule(self.config.vision_config		)
           _UpperCAmelCase     :      Any														=       nn.Dense(self.config.projection_dim	,      use_bias=A	,      dtype=self.dtype		)

           _UpperCAmelCase     :      Any														=       self.param("concept_embeds"	,      jax.nn.initializers.ones	,      (1_7, self.config.projection_dim)		)
           _UpperCAmelCase     :      Optional[Any]														=       self.param(
               "special_care_embeds"	,      jax.nn.initializers.ones	,      (3, self.config.projection_dim)		)

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



      def __call__(	self	:      Optional[Any]	,      A	:      int		):
           _UpperCAmelCase     :      List[Any]														=       self.vision_model(A		)[1]
           _UpperCAmelCase     :      List[Any]														=       self.visual_projection(A		)

           _UpperCAmelCase     :      List[str]														=       jax_cosine_distance(A	,      self.special_care_embeds		)
           _UpperCAmelCase     :      Dict														=       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
           _UpperCAmelCase     :      Optional[Any]														=       0.0

           _UpperCAmelCase     :      int														=       special_cos_dist - self.special_care_embeds_weights[None, :] + adjustment
           _UpperCAmelCase     :      Any														=       jnp.round(A	,      3		)
           _UpperCAmelCase     :      Optional[int]														=       jnp.any(special_scores > 0	,      axis=1	,      keepdims=A		)
           # Use a lower threshold if an image has any special care concept
           _UpperCAmelCase     :      List[Any]														=       is_special_care * 0.01

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

           return has_nsfw_concepts







class       UpperCAmelCase_    (							_UpperCamelCase		):
      __SCREAMING_SNAKE_CASE		:  int						=					CLIPConfig
      __SCREAMING_SNAKE_CASE		:  List[Any]						=					'clip_input'
      __SCREAMING_SNAKE_CASE		:  Optional[Any]						=					FlaxStableDiffusionSafetyCheckerModule



      def __init__(	self	:      List[str]	,      A	:      CLIPConfig	,      A	:      Optional[Tuple] = None	,      A	:      int = 0	,      A	:      jnp.dtype = jnp.floataa	,      A	:      bool = True	,      **A	:      str	,      ):
           if input_shape is None:
                _UpperCAmelCase     :      str														=       (1, 2_2_4, 2_2_4, 3)
           _UpperCAmelCase     :      Optional[int]														=       self.module_class(config=A	,      dtype=A	,      **A		)
           super().__init__(A	,      A	,      input_shape=A	,      seed=A	,      dtype=A	,      _do_init=_do_init		)



      def 		snake_case_   (	self	:      Tuple	,      A	:      jax.random.KeyArray	,      A	:      Tuple	,      A	:      FrozenDict = None		):
           # init input tensor
           _UpperCAmelCase     :      str														=       jax.random.normal(A	,      A		)

           _UpperCAmelCase							,			_UpperCAmelCase     :      Optional[int]														=       jax.random.split(A		)
           _UpperCAmelCase     :      int														=       {"params": params_rng, "dropout": dropout_rng}

           _UpperCAmelCase     :      List[str]														=       self.module.init(A	,      A		)["params"]

           return random_params



      def __call__(	self	:      List[Any]	,      A	:      Optional[int]	,      A	:      dict = None	,      ):
           _UpperCAmelCase     :      Optional[Any]														=       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 is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
 from PIL import Image
else:






 class       UpperCAmelCase_    :



       @staticmethod
       def 		snake_case_   (	*A	:      Union[str, Any]	,      **A	:      Dict		):
            pass






@is_pipeline_test
@require_vision
class       UpperCAmelCase_    (							unittest.TestCase		):



      @require_torch
      def 		snake_case_   (	self	:      Union[str, Any]		):
           _UpperCAmelCase     :      Union[str, Any]														=       pipeline(
               model="hf-internal-testing/tiny-random-clip-zero-shot-image-classification"	,      )
           _UpperCAmelCase     :      Union[str, Any]														=       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
           _UpperCAmelCase     :      Tuple														=       image_classifier(A	,      candidate_labels=["a", "b", "c"]		)

           # The floating scores are so close, we enter floating error approximation and the order is not guaranteed across
           # python and torch versions.
           self.assertIn(
               nested_simplify(A		)	,      [
                   [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "b"}, {"score": 0.333, "label": "c"}],
                   [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "c"}, {"score": 0.333, "label": "b"}],
               ]	,      )

           _UpperCAmelCase     :      str														=       image_classifier([image] * 5	,      candidate_labels=["A", "B", "C"]	,      batch_size=2		)
           self.assertEqual(
               nested_simplify(A		)	,      [
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
               ]	,      )



      @require_tf
      def 		snake_case_   (	self	:      Any		):
           _UpperCAmelCase     :      List[str]														=       pipeline(
               model="hf-internal-testing/tiny-random-clip-zero-shot-image-classification"	,      framework="tf"		)
           _UpperCAmelCase     :      int														=       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
           _UpperCAmelCase     :      Union[str, Any]														=       image_classifier(A	,      candidate_labels=["a", "b", "c"]		)

           self.assertEqual(
               nested_simplify(A		)	,      [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "b"}, {"score": 0.333, "label": "c"}]	,      )

           _UpperCAmelCase     :      Optional[Any]														=       image_classifier([image] * 5	,      candidate_labels=["A", "B", "C"]	,      batch_size=2		)
           self.assertEqual(
               nested_simplify(A		)	,      [
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
               ]	,      )



      @slow
      @require_torch
      def 		snake_case_   (	self	:      Any		):
           _UpperCAmelCase     :      Optional[Any]														=       pipeline(
               task="zero-shot-image-classification"	,      model="openai/clip-vit-base-patch32"	,      )
           # This is an image of 2 cats with remotes and no planes
           _UpperCAmelCase     :      Dict														=       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
           _UpperCAmelCase     :      List[Any]														=       image_classifier(A	,      candidate_labels=["cat", "plane", "remote"]		)

           self.assertEqual(
               nested_simplify(A		)	,      [
                   {"score": 0.511, "label": "remote"},
                   {"score": 0.485, "label": "cat"},
                   {"score": 0.004, "label": "plane"},
               ]	,      )

           _UpperCAmelCase     :      Tuple														=       image_classifier([image] * 5	,      candidate_labels=["cat", "plane", "remote"]	,      batch_size=2		)
           self.assertEqual(
               nested_simplify(A		)	,      [
                   [
                       {"score": 0.511, "label": "remote"},
                       {"score": 0.485, "label": "cat"},
                       {"score": 0.004, "label": "plane"},
                   ],
               ]
               * 5	,      )



      @slow
      @require_tf
      def 		snake_case_   (	self	:      Any		):
           _UpperCAmelCase     :      List[Any]														=       pipeline(
               task="zero-shot-image-classification"	,      model="openai/clip-vit-base-patch32"	,      framework="tf"		)
           # This is an image of 2 cats with remotes and no planes
           _UpperCAmelCase     :      Union[str, Any]														=       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
           _UpperCAmelCase     :      Union[str, Any]														=       image_classifier(A	,      candidate_labels=["cat", "plane", "remote"]		)
           self.assertEqual(
               nested_simplify(A		)	,      [
                   {"score": 0.511, "label": "remote"},
                   {"score": 0.485, "label": "cat"},
                   {"score": 0.004, "label": "plane"},
               ]	,      )

           _UpperCAmelCase     :      Union[str, Any]														=       image_classifier([image] * 5	,      candidate_labels=["cat", "plane", "remote"]	,      batch_size=2		)
           self.assertEqual(
               nested_simplify(A		)	,      [
                   [
                       {"score": 0.511, "label": "remote"},
                       {"score": 0.485, "label": "cat"},
                       {"score": 0.004, "label": "plane"},
                   ],
               ]
               * 5	,      )
