

'''simple docstring'''







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,
)


__lowerCamelCase		       =    logging.getLogger(__name__)


@dataclass(frozen=__UpperCAmelCase			)
class 					A__      :
    lowercase						=		42
    lowercase						=		42
    lowercase						=		None
    lowercase						=		None
    lowercase						=		None


@dataclass(frozen=__UpperCAmelCase			)
class 					A__      :
    lowercase						=		42
    lowercase						=		None
    lowercase						=		None
    lowercase						=		None
    lowercase						=		None


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


      class 					A__      (      __UpperCAmelCase			):
          lowercase						=		42



          def __init__(					self		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__ = None		,				UpperCamelCase__=False		,				UpperCamelCase__ = False		,				)						->      List[Any]:





                 '''simple docstring'''
                 A_						=      hans_processors[task]()

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

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

                               A_						=      (
                                   processor.get_dev_examples(__SCREAMING_SNAKE_CASE       ) if evaluate else processor.get_train_examples(__SCREAMING_SNAKE_CASE       )
                               )

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



          def __len__(					self       )						->      str:





                 '''simple docstring'''
                 return len(self.features       )



          def __getitem__(					self		,				UpperCamelCase__       )						->      Any:





                 '''simple docstring'''
                 return self.features[i]







          def 					snake_case_			(					self       )						->      int:





                 '''simple docstring'''
                 return self.label_list


if is_tf_available():
      import tensorflow as tf


      class 					A__      :
          lowercase						=		42



          def __init__(					self		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__ = 128		,				UpperCamelCase__=False		,				UpperCamelCase__ = False		,				)						->      List[Any]:





                 '''simple docstring'''
                 A_						=      hans_processors[task]()
                 A_						=      processor.get_labels()
                 if tokenizer.__class__ in (
                     RobertaTokenizer,
                     RobertaTokenizerFast,
                     XLMRobertaTokenizer,
                     BartTokenizer,
                     BartTokenizerFast,
                 ):
                        # HACK(label indices are swapped in RoBERTa pretrained model)
                        A_					,			A_						=      label_list[2], label_list[1]
                 A_						=      label_list

                 A_						=      processor.get_dev_examples(__SCREAMING_SNAKE_CASE       ) if evaluate else processor.get_train_examples(__SCREAMING_SNAKE_CASE       )
                 A_						=      hans_convert_examples_to_features(__SCREAMING_SNAKE_CASE		,				__SCREAMING_SNAKE_CASE		,				__SCREAMING_SNAKE_CASE		,				__SCREAMING_SNAKE_CASE       )

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

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

                 A_						=      tf.data.Dataset.from_generator(
                     __SCREAMING_SNAKE_CASE		,				(
                         {
                             """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 					snake_case_			(					self       )						->      Union[str, Any]:





                 '''simple docstring'''
                 return self.dataset



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





                 '''simple docstring'''
                 return len(self.features       )



          def __getitem__(					self		,				UpperCamelCase__       )						->      str:





                 '''simple docstring'''
                 return self.features[i]







          def 					snake_case_			(					self       )						->      List[str]:





                 '''simple docstring'''
                 return self.label_list


class 					A__      (      __UpperCAmelCase			):



    def 					snake_case_			(					self		,				UpperCamelCase__       )						->      Tuple:





           '''simple docstring'''
           return self._create_examples(self._read_tsv(os.path.join(__SCREAMING_SNAKE_CASE		,				"""heuristics_train_set.txt"""       )       )		,				"""train"""       )



    def 					snake_case_			(					self		,				UpperCamelCase__       )						->      List[Any]:





           '''simple docstring'''
           return self._create_examples(self._read_tsv(os.path.join(__SCREAMING_SNAKE_CASE		,				"""heuristics_evaluation_set.txt"""       )       )		,				"""dev"""       )



    def 					snake_case_			(					self       )						->      Any:





           '''simple docstring'''
           return ["contradiction", "entailment", "neutral"]







    def 					snake_case_			(					self		,				UpperCamelCase__		,				UpperCamelCase__       )						->      int:





           '''simple docstring'''
           A_						=      []
           for i, line in enumerate(__SCREAMING_SNAKE_CASE       ):
                  if i == 0:
                         continue
                  A_						=      """%s-%s""" % (set_type, line[0])
                  A_						=      line[5]
                  A_						=      line[6]
                  A_						=      line[7][2:] if line[7].startswith("""ex"""       ) else line[7]
                  A_						=      line[0]
                  examples.append(InputExample(guid=__SCREAMING_SNAKE_CASE		,				text_a=__SCREAMING_SNAKE_CASE		,				text_b=__SCREAMING_SNAKE_CASE		,				label=__SCREAMING_SNAKE_CASE		,				pairID=__SCREAMING_SNAKE_CASE       )       )
           return examples

def       UpperCAmelCase__    (      UpperCAmelCase__,       UpperCAmelCase__,       UpperCAmelCase__,       UpperCAmelCase__,       )			->			str:
       A_						=      {label: i for i, label in enumerate(_UpperCAmelCase				)}

       A_						=      []
       for ex_index, example in tqdm.tqdm(enumerate(_UpperCAmelCase				),       desc="""convert examples to features"""				):
              if ex_index % 1_00_00 == 0:
                     logger.info("""Writing example %d""" % (ex_index)				)

              A_						=      tokenizer(
                  example.text_a,       example.text_b,       add_special_tokens=_UpperCAmelCase,       max_length=_UpperCAmelCase,       padding="""max_length""",       truncation=_UpperCAmelCase,       return_overflowing_tokens=_UpperCAmelCase,       )

              A_						=      label_map[example.label] if example.label in label_map else 0

              A_						=      int(example.pairID				)

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

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

       return features


__lowerCamelCase		       =    {
    '''hans''': 3,
}







__lowerCamelCase		       =    {
    '''hans''': HansProcessor,
}



'''simple docstring'''







from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding


class 					A__      (      _snake_case			):
    lowercase						=		"ClapFeatureExtractor"
    lowercase						=		("RobertaTokenizer", "RobertaTokenizerFast")



    def __init__(					self		,				UpperCamelCase__		,				UpperCamelCase__       )						->      Tuple:





           '''simple docstring'''
           super().__init__(UpperCamelCase__		,				UpperCamelCase__       )



    def __call__(					self		,				UpperCamelCase__=None		,				UpperCamelCase__=None		,				UpperCamelCase__=None		,				**UpperCamelCase__       )						->      Tuple:





           '''simple docstring'''
           A_						=      kwargs.pop("""sampling_rate"""		,				UpperCamelCase__       )

           if text is None and audios is None:
                  raise ValueError("""You have to specify either text or audios. Both cannot be none."""       )

           if text is not None:
                  A_						=      self.tokenizer(UpperCamelCase__		,				return_tensors=UpperCamelCase__		,				**UpperCamelCase__       )

           if audios is not None:
                  A_						=      self.feature_extractor(
                      UpperCamelCase__		,				sampling_rate=UpperCamelCase__		,				return_tensors=UpperCamelCase__		,				**UpperCamelCase__       )

           if text is not None and audios is not None:
                  A_						=      audio_features.input_features
                  return encoding
           elif text is not None:
                  return encoding
           else:
                  return BatchEncoding(data=dict(**UpperCamelCase__       )		,				tensor_type=UpperCamelCase__       )



    def 					snake_case_			(					self		,				*UpperCamelCase__		,				**UpperCamelCase__       )						->      Optional[int]:





           '''simple docstring'''
           return self.tokenizer.batch_decode(*UpperCamelCase__		,				**UpperCamelCase__       )



    def 					snake_case_			(					self		,				*UpperCamelCase__		,				**UpperCamelCase__       )						->      str:





           '''simple docstring'''
           return self.tokenizer.decode(*UpperCamelCase__		,				**UpperCamelCase__       )







    @property
    def 					snake_case_			(					self       )						->      int:





           '''simple docstring'''
           A_						=      self.tokenizer.model_input_names
           A_						=      self.feature_extractor.model_input_names
           return list(dict.fromkeys(tokenizer_input_names + feature_extractor_input_names       )       )
