





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

from ...utils import logging


a_			=      logging.get_logger(__name__)






class 		__snake_case (		SCREAMING_SNAKE_CASE__					):





 """simple docstring"""


 _lowerCamelCase				=					CLIPConfig

 _lowerCamelCase				=					['''CLIPEncoderLayer''']

 def __init__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     super().__init__(_snake_case  )

     __A   :						Tuple									=					CLIPVisionModelWithProjection(config.vision_config  )

     __A   :						Optional[int]									=					nn.Linear(config.vision_config.projection_dim ,						1  )
     __A   :						List[Any]									=					nn.Linear(config.vision_config.projection_dim ,						1  )







 @torch.no_grad()
 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase=0.5 ,						__lowerCamelCase=0.5  ):



     '''simple docstring'''




     __A   :						Tuple									=					self.vision_model(_snake_case  )[0]

     __A   :						Any									=					self.p_head(_snake_case  )
     __A   :						str									=					nsfw_detected.flatten()
     __A   :						Dict									=					nsfw_detected > p_threshold
     __A   :						List[str]									=					nsfw_detected.tolist()

     if any(_snake_case  ):
         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.'''  )

     for idx, nsfw_detected_ in enumerate(_snake_case  ):
         if nsfw_detected_:
             __A   :						str									=					np.zeros(images[idx].shape  )

     __A   :						Optional[int]									=					self.w_head(_snake_case  )
     __A   :						int									=					watermark_detected.flatten()
     __A   :						str									=					watermark_detected > w_threshold
     __A   :						int									=					watermark_detected.tolist()

     if any(_snake_case  ):
         logger.warning(
             '''Potential watermarked content was detected in one or more images. A black image will be returned instead.'''
             ''' Try again with a different prompt and/or seed.'''  )

     for idx, watermark_detected_ in enumerate(_snake_case  ):
         if watermark_detected_:
             __A   :						Optional[Any]									=					np.zeros(images[idx].shape  )

     return images, nsfw_detected, watermark_detected











"""simple docstring"""



import json
import os
import unittest

from transformers.models.xlm.tokenization_xlm import VOCAB_FILES_NAMES, XLMTokenizer
from transformers.testing_utils import slow

from ...test_tokenization_common import TokenizerTesterMixin






class 		__snake_case (		SCREAMING_SNAKE_CASE__							,							unittest.TestCase					):





 """simple docstring"""


 _lowerCamelCase				=					XLMTokenizer
 _lowerCamelCase				=					False

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     super().setUp()

     # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
     __A   :						Tuple									=					[
         '''l''',
         '''o''',
         '''w''',
         '''e''',
         '''r''',
         '''s''',
         '''t''',
         '''i''',
         '''d''',
         '''n''',
         '''w</w>''',
         '''r</w>''',
         '''t</w>''',
         '''lo''',
         '''low''',
         '''er</w>''',
         '''low</w>''',
         '''lowest</w>''',
         '''newer</w>''',
         '''wider</w>''',
         '''<unk>''',
     ]
     __A   :						Dict									=					dict(zip(__lowerCamelCase ,						range(len(__lowerCamelCase  )  )  )  )
     __A   :						Union[str, Any]									=					['''l o 123''', '''lo w 1456''', '''e r</w> 1789''', '''''']

     __A   :						Optional[Any]									=					os.path.join(self.tmpdirname ,						VOCAB_FILES_NAMES['''vocab_file''']  )
     __A   :						Optional[Any]									=					os.path.join(self.tmpdirname ,						VOCAB_FILES_NAMES['''merges_file''']  )
     with open(self.vocab_file ,						'''w'''  ) as fp:
         fp.write(json.dumps(__lowerCamelCase  )  )
     with open(self.merges_file ,						'''w'''  ) as fp:
         fp.write('''\n'''.join(__lowerCamelCase  )  )

 def  UpperCamelCase__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						Optional[int]									=					'''lower newer'''
     __A   :						int									=					'''lower newer'''
     return input_text, output_text

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						Union[str, Any]									=					XLMTokenizer(self.vocab_file ,						self.merges_file  )

     __A   :						Optional[Any]									=					'''lower'''
     __A   :						Any									=					['''low''', '''er</w>''']
     __A   :						Tuple									=					tokenizer.tokenize(__lowerCamelCase  )
     self.assertListEqual(__lowerCamelCase ,						__lowerCamelCase  )

     __A   :						str									=					tokens + ['''<unk>''']
     __A   :						List[str]									=					[14, 15, 20]
     self.assertListEqual(tokenizer.convert_tokens_to_ids(__lowerCamelCase  ) ,						__lowerCamelCase  )







 @slow
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						Optional[int]									=					XLMTokenizer.from_pretrained('''xlm-mlm-en-2048'''  )

     __A   :						Union[str, Any]									=					tokenizer.encode('''sequence builders''' ,						add_special_tokens=__lowerCamelCase  )
     __A   :						int									=					tokenizer.encode('''multi-sequence build''' ,						add_special_tokens=__lowerCamelCase  )

     __A   :						int									=					tokenizer.build_inputs_with_special_tokens(__lowerCamelCase  )
     __A   :						List[Any]									=					tokenizer.build_inputs_with_special_tokens(__lowerCamelCase ,						__lowerCamelCase  )

     assert encoded_sentence == [0] + text + [1]
     assert encoded_pair == [0] + text + [1] + text_a + [1]




