





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

from ...utils import logging


a_    :				Dict					=			logging.get_logger(__name__)




def   _SCREAMING_SNAKE_CASE      (	snake_case_		:						str ,     snake_case_		:						Tuple						):
     __magic_name__				=       nn.functional.normalize(snake_case_						)
     __magic_name__				=       nn.functional.normalize(snake_case_						)
     return torch.mm(snake_case_ ,     normalized_text_embeds.t()						)



class  SCREAMING_SNAKE_CASE_  ( SCREAMING_SNAKE_CASE__			):


  """simple docstring"""





  _a								=							CLIPConfig

  _a								=							["""CLIPEncoderLayer"""]





  def __init__(  self	,  A						)		->   List[Any]:





       '''simple docstring'''







       super().__init__(A						)

       __magic_name__				=       CLIPVisionModel(config.vision_config						)
       __magic_name__				=       nn.Linear(config.vision_config.hidden_size	,  config.projection_dim	,  bias=A						)

       __magic_name__				=       nn.Parameter(torch.ones(17	,  config.projection_dim						)	,  requires_grad=A						)
       __magic_name__				=       nn.Parameter(torch.ones(3	,  config.projection_dim						)	,  requires_grad=A						)

       __magic_name__				=       nn.Parameter(torch.ones(17						)	,  requires_grad=A						)
       __magic_name__				=       nn.Parameter(torch.ones(3						)	,  requires_grad=A						)





  @torch.no_grad()
  def 			__A      (  self	,  A	,  A						)		->   str:





       '''simple docstring'''







       __magic_name__				=       self.vision_model(A						)[1]  # pooled_output
       __magic_name__				=       self.visual_projection(A						)

       # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
       __magic_name__				=       cosine_distance(A	,  self.special_care_embeds						).cpu().float().numpy()
       __magic_name__				=       cosine_distance(A	,  self.concept_embeds						).cpu().float().numpy()

       __magic_name__				=       []
       __magic_name__				=       image_embeds.shape[0]
       for i in range(A						):
            __magic_name__				=       {'''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
            __magic_name__				=       0.0

            for concept_idx in range(len(special_cos_dist[0]						)						):
                 __magic_name__				=       special_cos_dist[i][concept_idx]
                 __magic_name__				=       self.special_care_embeds_weights[concept_idx].item()
                 __magic_name__				=       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]}						)
                      __magic_name__				=       0.01

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

            result.append(A						)

       __magic_name__				=       [len(res['''bad_concepts''']						) > 0 for res in result]

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

       if any(A						):
            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 			__A      (  self	,  A	,  A						)		->   Optional[int]:





       '''simple docstring'''







       __magic_name__				=       self.vision_model(A						)[1]  # pooled_output
       __magic_name__				=       self.visual_projection(A						)

       __magic_name__				=       cosine_distance(A	,  self.special_care_embeds						)
       __magic_name__				=       cosine_distance(A	,  self.concept_embeds						)

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

       __magic_name__				=       special_cos_dist - self.special_care_embeds_weights + adjustment
       # special_scores = special_scores.round(decimals=3)
       __magic_name__				=       torch.any(special_scores > 0	,  dim=1						)
       __magic_name__				=       special_care * 0.01
       __magic_name__				=       special_adjustment.unsqueeze(1						).expand(-1	,  cos_dist.shape[1]						)

       __magic_name__				=       (cos_dist - self.concept_embeds_weights) + special_adjustment
       # concept_scores = concept_scores.round(decimals=3)
       __magic_name__				=       torch.any(concept_scores > 0	,  dim=1						)

       __magic_name__				=       0.0  # black image

       return images, has_nsfw_concepts






import json
import os
import shutil
import tempfile
import unittest

from transformers.models.wavaveca import WavaVecaCTCTokenizer, WavaVecaFeatureExtractor, WavaVecaProcessor
from transformers.models.wavaveca.tokenization_wavaveca import VOCAB_FILES_NAMES
from transformers.utils import FEATURE_EXTRACTOR_NAME

from .test_feature_extraction_wavaveca import floats_list



class  SCREAMING_SNAKE_CASE_  ( unittest.TestCase			):


  """simple docstring"""





  def 			__A      (  self						)		->   Optional[int]:





       '''simple docstring'''







       __magic_name__				=       '''<pad> <s> </s> <unk> | E T A O N I H S R D L U M W C F G Y P B V K \' X J Q Z'''.split(''' '''						)
       __magic_name__				=       dict(zip(A	,  range(len(A						)						)						)						)

       __magic_name__				=       {
           '''pad_token''': '''<pad>''',
           '''unk_token''': '''<unk>''',
           '''bos_token''': '''<s>''',
           '''eos_token''': '''</s>''',
       }
       __magic_name__				=       {
           '''feature_size''': 1,
           '''padding_value''': 0.0,
           '''sampling_rate''': 1_60_00,
           '''return_attention_mask''': False,
           '''do_normalize''': True,
       }

       __magic_name__				=       tempfile.mkdtemp()
       __magic_name__				=       os.path.join(self.tmpdirname	,  VOCAB_FILES_NAMES['''vocab_file''']						)
       __magic_name__				=       os.path.join(self.tmpdirname	,  A						)
       with open(self.vocab_file	,  '''w'''	,  encoding='''utf-8'''						) as fp:
            fp.write(json.dumps(A						) + '''\n'''						)

       with open(self.feature_extraction_file	,  '''w'''	,  encoding='''utf-8'''						) as fp:
            fp.write(json.dumps(A						) + '''\n'''						)





  def 			__A      (  self	,  **A						)		->   str:





       '''simple docstring'''







       __magic_name__				=       self.add_kwargs_tokens_map.copy()
       kwargs.update(A						)
       return WavaVecaCTCTokenizer.from_pretrained(self.tmpdirname	,  **A						)





  def 			__A      (  self	,  **A						)		->   List[Any]:





       '''simple docstring'''







       return WavaVecaFeatureExtractor.from_pretrained(self.tmpdirname	,  **A						)





  def 			__A      (  self						)		->   List[Any]:





       '''simple docstring'''







       shutil.rmtree(self.tmpdirname						)





  def 			__A      (  self						)		->   List[Any]:





       '''simple docstring'''







       __magic_name__				=       self.get_tokenizer()
       __magic_name__				=       self.get_feature_extractor()

       __magic_name__				=       WavaVecaProcessor(tokenizer=A	,  feature_extractor=A						)

       processor.save_pretrained(self.tmpdirname						)
       __magic_name__				=       WavaVecaProcessor.from_pretrained(self.tmpdirname						)

       self.assertEqual(processor.tokenizer.get_vocab()	,  tokenizer.get_vocab()						)
       self.assertIsInstance(processor.tokenizer	,  A						)

       self.assertEqual(processor.feature_extractor.to_json_string()	,  feature_extractor.to_json_string()						)
       self.assertIsInstance(processor.feature_extractor	,  A						)





  def 			__A      (  self						)		->   Dict:





       '''simple docstring'''







       __magic_name__				=       WavaVecaProcessor(tokenizer=self.get_tokenizer()	,  feature_extractor=self.get_feature_extractor()						)
       processor.save_pretrained(self.tmpdirname						)

       __magic_name__				=       self.get_tokenizer(bos_token='''(BOS)'''	,  eos_token='''(EOS)'''						)
       __magic_name__				=       self.get_feature_extractor(do_normalize=A	,  padding_value=1.0						)

       __magic_name__				=       WavaVecaProcessor.from_pretrained(
           self.tmpdirname	,  bos_token='''(BOS)'''	,  eos_token='''(EOS)'''	,  do_normalize=A	,  padding_value=1.0						)

       self.assertEqual(processor.tokenizer.get_vocab()	,  tokenizer_add_kwargs.get_vocab()						)
       self.assertIsInstance(processor.tokenizer	,  A						)

       self.assertEqual(processor.feature_extractor.to_json_string()	,  feature_extractor_add_kwargs.to_json_string()						)
       self.assertIsInstance(processor.feature_extractor	,  A						)





  def 			__A      (  self						)		->   Dict:





       '''simple docstring'''







       __magic_name__				=       self.get_feature_extractor()
       __magic_name__				=       self.get_tokenizer()

       __magic_name__				=       WavaVecaProcessor(tokenizer=A	,  feature_extractor=A						)

       __magic_name__				=       floats_list((3, 10_00)						)

       __magic_name__				=       feature_extractor(A	,  return_tensors='''np'''						)
       __magic_name__				=       processor(A	,  return_tensors='''np'''						)

       for key in input_feat_extract.keys():
            self.assertAlmostEqual(input_feat_extract[key].sum()	,  input_processor[key].sum()	,  delta=1E-2						)





  def 			__A      (  self						)		->   Optional[Any]:





       '''simple docstring'''







       __magic_name__				=       self.get_feature_extractor()
       __magic_name__				=       self.get_tokenizer()

       __magic_name__				=       WavaVecaProcessor(tokenizer=A	,  feature_extractor=A						)

       __magic_name__				=       '''This is a test string'''

       __magic_name__				=       processor(text=A						)

       __magic_name__				=       tokenizer(A						)

       for key in encoded_tok.keys():
            self.assertListEqual(encoded_tok[key]	,  encoded_processor[key]						)





  def 			__A      (  self						)		->   Tuple:





       '''simple docstring'''







       __magic_name__				=       self.get_feature_extractor()
       __magic_name__				=       self.get_tokenizer()

       __magic_name__				=       WavaVecaProcessor(tokenizer=A	,  feature_extractor=A						)

       __magic_name__				=       [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

       __magic_name__				=       processor.batch_decode(A						)
       __magic_name__				=       tokenizer.batch_decode(A						)

       self.assertListEqual(A	,  A						)







  def 			__A      (  self						)		->   Dict:





       '''simple docstring'''







       __magic_name__				=       self.get_feature_extractor()
       __magic_name__				=       self.get_tokenizer()

       __magic_name__				=       WavaVecaProcessor(tokenizer=A	,  feature_extractor=A						)

       self.assertListEqual(
           processor.model_input_names	,  feature_extractor.model_input_names	,  msg='''`processor` and `feature_extractor` model input names do not match'''	,  )