



import logging
import os
from dataclasses import dataclass
from typing import List, Optional, Union

import tqdm
from filelock import FileLock

from transformers import (
    BartTokenizer,
    BartTokenizerFast,
    DataProcessor,
    PreTrainedTokenizer,
    RobertaTokenizer,
    RobertaTokenizerFast,
    XLMRobertaTokenizer,
    is_tf_available,
    is_torch_available,
)


snake_case    				=		logging.getLogger(__name__)


@dataclass(frozen=lowerCAmelCase					)
class  SCREAMING_SNAKE_CASE			:
     '''simple docstring'''


     UpperCamelCase_							:		str
     UpperCamelCase_							:		str
     UpperCamelCase_							:		Optional[str]			=	None
     UpperCamelCase_							:		Optional[str]			=	None
     UpperCamelCase_							:		Optional[str]			=	None





@dataclass(frozen=lowerCAmelCase					)
class  SCREAMING_SNAKE_CASE			:
     '''simple docstring'''


     UpperCamelCase_							:		List[int]
     UpperCamelCase_							:		Optional[List[int]]			=	None
     UpperCamelCase_							:		Optional[List[int]]			=	None
     UpperCamelCase_							:		Optional[Union[int, float]]			=	None
     UpperCamelCase_							:		Optional[int]			=	None


if is_torch_available():
   import torch
   from torch.utils.data import Dataset


   class  SCREAMING_SNAKE_CASE			(			lowerCAmelCase					):
        '''simple docstring'''


        UpperCamelCase_							:		List[InputFeatures]


        def __init__(				self       :	Tuple					, UpperCAmelCase_       :	str					, UpperCAmelCase_       :	PreTrainedTokenizer					, UpperCAmelCase_       :	str					, UpperCAmelCase_       :	Optional[int] = None					, UpperCAmelCase_       :	Optional[int]=False					, UpperCAmelCase_       :	bool = False					, ):
          SCREAMING_SNAKE_CASE       :      List[str]							=    hans_processors[task]()

          SCREAMING_SNAKE_CASE       :      List[Any]							=    os.path.join(
              UpperCAmelCase_					, "cached_{}_{}_{}_{}".format(
                  "dev" if evaluate else "train"					, tokenizer.__class__.__name__					, str(UpperCAmelCase_      )					, UpperCAmelCase_					, )					, )
          SCREAMING_SNAKE_CASE       :      str							=    processor.get_labels()
          if tokenizer.__class__ in (
              RobertaTokenizer,
              RobertaTokenizerFast,
              XLMRobertaTokenizer,
              BartTokenizer,
              BartTokenizerFast,
          ):
            # HACK(label indices are swapped in RoBERTa pretrained model)
            SCREAMING_SNAKE_CASE       :      Dict							=    label_list[2], label_list[1]
          SCREAMING_SNAKE_CASE       :      str							=    label_list

          # Make sure only the first process in distributed training processes the dataset,
          # and the others will use the cache.
          SCREAMING_SNAKE_CASE       :      int							=    cached_features_file + ".lock"
          with FileLock(UpperCAmelCase_      ):
            if os.path.exists(UpperCAmelCase_      ) and not overwrite_cache:
              logger.info(f'''Loading features from cached file {cached_features_file}'''      )
              SCREAMING_SNAKE_CASE       :      str							=    torch.load(UpperCAmelCase_      )
            else:
              logger.info(f'''Creating features from dataset file at {data_dir}'''      )

              SCREAMING_SNAKE_CASE       :      str							=    (
                  processor.get_dev_examples(UpperCAmelCase_      ) if evaluate else processor.get_train_examples(UpperCAmelCase_      )
              )

              logger.info("Training examples: %s"					, len(UpperCAmelCase_      )      )
              SCREAMING_SNAKE_CASE       :      Dict							=    hans_convert_examples_to_features(UpperCAmelCase_					, UpperCAmelCase_					, UpperCAmelCase_					, UpperCAmelCase_      )
              logger.info("Saving features into cached file %s"					, UpperCAmelCase_      )
              torch.save(self.features					, UpperCAmelCase_      )


        def __len__(				self       :	Dict      ):
          return len(self.features      )


        def __getitem__(				self       :	Tuple					, UpperCAmelCase_       :	List[str]      ):
          return self.features[i]






        def 				_A       (				self       :	Optional[Any]      ):
          return self.label_list


if is_tf_available():
   import tensorflow as tf


   class  SCREAMING_SNAKE_CASE			:
        '''simple docstring'''


        UpperCamelCase_							:		List[InputFeatures]


        def __init__(				self       :	Any					, UpperCAmelCase_       :	str					, UpperCAmelCase_       :	PreTrainedTokenizer					, UpperCAmelCase_       :	str					, UpperCAmelCase_       :	Optional[int] = 128					, UpperCAmelCase_       :	Optional[int]=False					, UpperCAmelCase_       :	bool = False					, ):
          SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    hans_processors[task]()
          SCREAMING_SNAKE_CASE       :      str							=    processor.get_labels()
          if tokenizer.__class__ in (
              RobertaTokenizer,
              RobertaTokenizerFast,
              XLMRobertaTokenizer,
              BartTokenizer,
              BartTokenizerFast,
          ):
            # HACK(label indices are swapped in RoBERTa pretrained model)
            SCREAMING_SNAKE_CASE       :      Optional[Any]							=    label_list[2], label_list[1]
          SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    label_list

          SCREAMING_SNAKE_CASE       :      Tuple							=    processor.get_dev_examples(UpperCAmelCase_      ) if evaluate else processor.get_train_examples(UpperCAmelCase_      )
          SCREAMING_SNAKE_CASE       :      Dict							=    hans_convert_examples_to_features(UpperCAmelCase_					, UpperCAmelCase_					, UpperCAmelCase_					, UpperCAmelCase_      )

          def gen():
            for ex_index, ex in tqdm.tqdm(enumerate(self.features      )					, desc="convert examples to features"      ):
              if ex_index % 1_0000 == 0:
                logger.info("Writing example %d of %d" % (ex_index, len(UpperCAmelCase_      ))      )

              yield (
                  {
                      "example_id": 0,
                      "input_ids": ex.input_ids,
                      "attention_mask": ex.attention_mask,
                      "token_type_ids": ex.token_type_ids,
                  },
                  ex.label,
              )

          SCREAMING_SNAKE_CASE       :      str							=    tf.data.Dataset.from_generator(
              UpperCAmelCase_					, (
                  {
                      "example_id": tf.intaa,
                      "input_ids": tf.intaa,
                      "attention_mask": tf.intaa,
                      "token_type_ids": tf.intaa,
                  },
                  tf.intaa,
              )					, (
                  {
                      "example_id": tf.TensorShape([]      ),
                      "input_ids": tf.TensorShape([None, None]      ),
                      "attention_mask": tf.TensorShape([None, None]      ),
                      "token_type_ids": tf.TensorShape([None, None]      ),
                  },
                  tf.TensorShape([]      ),
              )					, )


        def 				_A       (				self       :	List[str]      ):
          return self.dataset


        def __len__(				self       :	List[Any]      ):
          return len(self.features      )


        def __getitem__(				self       :	str					, UpperCAmelCase_       :	Dict      ):
          return self.features[i]






        def 				_A       (				self       :	Dict      ):
          return self.label_list


class  SCREAMING_SNAKE_CASE			(			lowerCAmelCase					):
     '''simple docstring'''


     def 				_A       (				self       :	List[Any]					, UpperCAmelCase_       :	Dict      ):
       return self._create_examples(self._read_tsv(os.path.join(UpperCAmelCase_					, "heuristics_train_set.txt"      )      )					, "train"      )


     def 				_A       (				self       :	Tuple					, UpperCAmelCase_       :	Any      ):
       return self._create_examples(self._read_tsv(os.path.join(UpperCAmelCase_					, "heuristics_evaluation_set.txt"      )      )					, "dev"      )


     def 				_A       (				self       :	Any      ):
       return ["contradiction", "entailment", "neutral"]






     def 				_A       (				self       :	Union[str, Any]					, UpperCAmelCase_       :	str					, UpperCAmelCase_       :	Tuple      ):
       SCREAMING_SNAKE_CASE       :      Any							=    []
       for i, line in enumerate(UpperCAmelCase_      ):
         if i == 0:
           continue
         SCREAMING_SNAKE_CASE       :      List[Any]							=    "%s-%s" % (set_type, line[0])
         SCREAMING_SNAKE_CASE       :      Dict							=    line[5]
         SCREAMING_SNAKE_CASE       :      Tuple							=    line[6]
         SCREAMING_SNAKE_CASE       :      Optional[int]							=    line[7][2:] if line[7].startswith("ex"      ) else line[7]
         SCREAMING_SNAKE_CASE       :      Dict							=    line[0]
         examples.append(InputExample(guid=UpperCAmelCase_					, text_a=UpperCAmelCase_					, text_b=UpperCAmelCase_					, label=UpperCAmelCase_					, pairID=UpperCAmelCase_      )      )
       return examples





def   lowerCamelCase__			(     lowercase						,     lowercase						,     lowercase						,     lowercase						,     ):

  """simple docstring"""






  SCREAMING_SNAKE_CASE       :      List[str]							=    {label: i for i, label in enumerate(lowercase				)}

  SCREAMING_SNAKE_CASE       :      int							=    []
  for ex_index, example in tqdm.tqdm(enumerate(lowercase				)						,     desc="convert examples to features"				):
    if ex_index % 10000 == 0:
      logger.info("Writing example %d" % (ex_index)				)

    SCREAMING_SNAKE_CASE       :      Dict							=    tokenizer(
        example.text_a						,     example.text_b						,     add_special_tokens=lowercase						,     max_length=lowercase						,     padding="max_length"						,     truncation=lowercase						,     return_overflowing_tokens=lowercase						,     )

    SCREAMING_SNAKE_CASE       :      List[str]							=    label_map[example.label] if example.label in label_map else 0

    SCREAMING_SNAKE_CASE       :      int							=    int(example.pairID				)

    features.append(InputFeatures(**lowercase						,     label=lowercase						,     pairID=lowercase				)				)

  for i, example in enumerate(examples[:5]				):
    logger.info("*** Example ***"				)
    logger.info(F'''guid: {example}'''				)
    logger.info(F'''features: {features[i]}'''				)

  return features


snake_case    				=		{
    """hans""": 3,
}

snake_case    				=		{
    """hans""": HansProcessor,
}














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

from ...utils import logging


snake_case    				=		logging.get_logger(__name__)


class  SCREAMING_SNAKE_CASE			(			lowerCAmelCase					):
     '''simple docstring'''


     UpperCamelCase_							:		Union[str, Any]			=	CLIPConfig

     UpperCamelCase_							:		Optional[int]			=	['''CLIPEncoderLayer''']


     def __init__(				self       :	List[Any]					, UpperCAmelCase_       :	CLIPConfig      ):
       super().__init__(UpperCAmelCase_      )

       SCREAMING_SNAKE_CASE       :      Any							=    CLIPVisionModelWithProjection(config.vision_config      )

       SCREAMING_SNAKE_CASE       :      List[Any]							=    nn.Linear(config.vision_config.projection_dim					, 1      )
       SCREAMING_SNAKE_CASE       :      List[str]							=    nn.Linear(config.vision_config.projection_dim					, 1      )






     @torch.no_grad()
     def 				_A       (				self       :	Union[str, Any]					, UpperCAmelCase_       :	int					, UpperCAmelCase_       :	Dict					, UpperCAmelCase_       :	List[Any]=0.5					, UpperCAmelCase_       :	Optional[Any]=0.5      ):
       SCREAMING_SNAKE_CASE       :      str							=    self.vision_model(UpperCAmelCase_      )[0]

       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    self.p_head(UpperCAmelCase_      )
       SCREAMING_SNAKE_CASE       :      List[str]							=    nsfw_detected.flatten()
       SCREAMING_SNAKE_CASE       :      List[str]							=    nsfw_detected > p_threshold
       SCREAMING_SNAKE_CASE       :      List[Any]							=    nsfw_detected.tolist()

       if any(UpperCAmelCase_      ):
         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(UpperCAmelCase_      ):
         if nsfw_detected_:
           SCREAMING_SNAKE_CASE       :      Dict							=    np.zeros(images[idx].shape      )

       SCREAMING_SNAKE_CASE       :      Any							=    self.w_head(UpperCAmelCase_      )
       SCREAMING_SNAKE_CASE       :      List[Any]							=    watermark_detected.flatten()
       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    watermark_detected > w_threshold
       SCREAMING_SNAKE_CASE       :      Optional[int]							=    watermark_detected.tolist()

       if any(UpperCAmelCase_      ):
         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(UpperCAmelCase_      ):
         if watermark_detected_:
           SCREAMING_SNAKE_CASE       :      Optional[int]							=    np.zeros(images[idx].shape      )

       return images, nsfw_detected, watermark_detected






