



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  ,	_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE=1E-1_2   ):
	__lowercase		       =			jnp.divide(emb_a.T  ,	jnp.clip(jnp.linalg.norm(_SCREAMING_SNAKE_CASE  ,	axis=1   )  ,	a_min=_SCREAMING_SNAKE_CASE   )   ).T
	__lowercase		       =			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   _A   ( nn.Module	):


						'''simple docstring'''





						_snake_case			:     CLIPConfig
						_snake_case			:     jnp.dtype      							=      jnp.floataa



						def     _snake_case	(	self	:							Union[str, Any]	):






							'''simple docstring'''



							__lowercase		       =			FlaxCLIPVisionModule(self.config.vision_config	)
							__lowercase		       =			nn.Dense(self.config.projection_dim							,      use_bias=lowerCamelCase							,      dtype=self.dtype	)

							__lowercase		       =			self.param("concept_embeds"							,      jax.nn.initializers.ones							,      (17, self.config.projection_dim)	)
							__lowercase		       =			self.param(
							    "special_care_embeds"							,      jax.nn.initializers.ones							,      (3, self.config.projection_dim)	)

							__lowercase		       =			self.param("concept_embeds_weights"							,      jax.nn.initializers.ones							,      (17,)	)
							__lowercase		       =			self.param("special_care_embeds_weights"							,      jax.nn.initializers.ones							,      (3,)	)



						def __call__(	self	:							str							,      lowerCamelCase	:							Optional[Any]	):






							'''simple docstring'''



							__lowercase		       =			self.vision_model(lowerCamelCase	)[1]
							__lowercase		       =			self.visual_projection(lowerCamelCase	)

							__lowercase		       =			jax_cosine_distance(lowerCamelCase							,      self.special_care_embeds	)
							__lowercase		       =			jax_cosine_distance(lowerCamelCase							,      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		       =			0.0

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

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

							return has_nsfw_concepts

class   _A   ( _lowercase	):


						'''simple docstring'''





						_snake_case			:     str      							=      CLIPConfig
						_snake_case			:     Optional[int]      							=      """clip_input"""
						_snake_case			:     Optional[Any]      							=      FlaxStableDiffusionSafetyCheckerModule



						def __init__(	self	:							Tuple							,      lowerCamelCase	:							CLIPConfig							,      lowerCamelCase	:							Optional[Tuple] = None							,      lowerCamelCase	:							int = 0							,      lowerCamelCase	:							jnp.dtype = jnp.floataa							,      lowerCamelCase	:							bool = True							,      **lowerCamelCase	:							List[Any]							,      ):






							'''simple docstring'''



							if input_shape is None:
								__lowercase		       =			(1, 224, 224, 3)
							__lowercase		       =			self.module_class(config=lowerCamelCase							,      dtype=lowerCamelCase							,      **lowerCamelCase	)
							super().__init__(lowerCamelCase							,      lowerCamelCase							,      input_shape=lowerCamelCase							,      seed=lowerCamelCase							,      dtype=lowerCamelCase							,      _do_init=_do_init	)



						def     _snake_case	(	self	:							List[Any]							,      lowerCamelCase	:							jax.random.KeyArray							,      lowerCamelCase	:							Tuple							,      lowerCamelCase	:							FrozenDict = None	):






							'''simple docstring'''



							__lowercase		       =			jax.random.normal(lowerCamelCase							,      lowerCamelCase	)

							__lowercase       ,    __lowercase		       =			jax.random.split(lowerCamelCase	)
							__lowercase		       =			{"params": params_rng, "dropout": dropout_rng}

							__lowercase		       =			self.module.init(lowerCamelCase							,      lowerCamelCase	)["params"]

							return random_params



						def __call__(	self	:							Optional[Any]							,      lowerCamelCase	:							Optional[int]							,      lowerCamelCase	:							dict = None							,      ):






							'''simple docstring'''



							__lowercase		       =			jnp.transpose(lowerCamelCase							,      (0, 2, 3, 1)	)

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








# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from ..models.whisper import WhisperForConditionalGeneration, WhisperProcessor
from .base import PipelineTool

class   _A   ( _lowercase	):


						'''simple docstring'''





						_snake_case			:     Dict      							=      """openai/whisper-base"""
						_snake_case			:     Union[str, Any]      							=      (
						    """This is a tool that transcribes an audio into text. It takes an input named `audio` and returns the """
						    """transcribed text."""
						)
						_snake_case			:     Any      							=      """transcriber"""
						_snake_case			:     Any      							=      WhisperProcessor
						_snake_case			:     Optional[int]      							=      WhisperForConditionalGeneration

						_snake_case			:     str      							=      ["""audio"""]
						_snake_case			:     Optional[int]      							=      ["""text"""]



						def     _snake_case	(	self	:							List[str]							,      lowerCamelCase	:							Optional[int]	):






							'''simple docstring'''



							return self.pre_processor(lowerCamelCase							,      return_tensors="pt"	).input_features



						def     _snake_case	(	self	:							str							,      lowerCamelCase	:							List[Any]	):






							'''simple docstring'''



							return self.model.generate(inputs=lowerCamelCase	)



						def     _snake_case	(	self	:							List[str]							,      lowerCamelCase	:							Optional[Any]	):






							'''simple docstring'''



							return self.pre_processor.batch_decode(lowerCamelCase							,      skip_special_tokens=lowerCamelCase	)[0]



