


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


lowercase				=logging.getLogger(__name__)






@dataclass(frozen=lowerCAmelCase							)
class    __magic_name__				:
       UpperCAmelCase		 =42
       UpperCAmelCase		 =42
       UpperCAmelCase		 =None
       UpperCAmelCase		 =None
       UpperCAmelCase		 =None






@dataclass(frozen=lowerCAmelCase							)
class    __magic_name__				:
       UpperCAmelCase		 =42
       UpperCAmelCase		 =None
       UpperCAmelCase		 =None
       UpperCAmelCase		 =None
       UpperCAmelCase		 =None


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






 class    __magic_name__				(						lowerCAmelCase							):
        UpperCAmelCase		 =42






        def __init__(     self    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case = None    ,				snake_case=False    ,				snake_case = False    ,				)		->    Union[str, Any]:




           '''simple docstring'''
           _UpperCAmelCase  :  List[str]				       =hans_processors[task]()

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

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

                 _UpperCAmelCase  :  int				       =(
                     processor.get_dev_examples(snake_case) if evaluate else processor.get_train_examples(snake_case)
                 )

                 logger.info('Training examples: %s'    ,				len(snake_case))
                 _UpperCAmelCase  :  List[str]				       =hans_convert_examples_to_features(snake_case    ,				snake_case    ,				snake_case    ,				snake_case)
                 logger.info('Saving features into cached file %s'    ,				snake_case)
                 torch.save(self.features    ,				snake_case)






        def __len__(     self)		->    Union[str, Any]:




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






        def __getitem__(     self    ,				snake_case)		->    InputFeatures:




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






        def      lowerCAmelCase						(     self)		->    Dict:




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


if is_tf_available():
 import tensorflow as tf






 class    __magic_name__				:
        UpperCAmelCase		 =42






        def __init__(     self    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case = 1_2_8    ,				snake_case=False    ,				snake_case = False    ,				)		->    Dict:




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

           _UpperCAmelCase  :  Any				       =processor.get_dev_examples(snake_case) if evaluate else processor.get_train_examples(snake_case)
           _UpperCAmelCase  :  str				       =hans_convert_examples_to_features(snake_case    ,				snake_case    ,				snake_case    ,				snake_case)

           def gen():
              for ex_index, ex in tqdm.tqdm(enumerate(self.features)    ,				desc='convert examples to features'):
                 if ex_index % 1_0_0_0_0 == 0:
                    logger.info('Writing example %d of %d' % (ex_index, len(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,
                 )

           _UpperCAmelCase  :  Optional[int]				       =tf.data.Dataset.from_generator(
               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      lowerCAmelCase						(     self)		->    int:




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






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




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






        def __getitem__(     self    ,				snake_case)		->    InputFeatures:




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






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




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






class    __magic_name__				(						lowerCAmelCase							):






       def      lowerCAmelCase						(     self    ,				snake_case)		->    Any:




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






       def      lowerCAmelCase						(     self    ,				snake_case)		->    Dict:




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






       def      lowerCAmelCase						(     self)		->    Union[str, Any]:




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






       def      lowerCAmelCase						(     self    ,				snake_case    ,				snake_case)		->    Optional[Any]:




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






def lowerCamelCase__     (		__lowerCamelCase			:				List[InputExample]			,       __lowerCamelCase			:				List[str]			,       __lowerCamelCase			:				int			,       __lowerCamelCase			:				PreTrainedTokenizer			,       ):







   '''simple docstring'''




   _UpperCAmelCase  :  Union[str, Any]				       ={label: i for i, label in enumerate(__lowerCamelCase				)}

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

      _UpperCAmelCase  :  Tuple				       =tokenizer(
          example.text_a			,       example.text_b			,       add_special_tokens=__lowerCamelCase			,       max_length=__lowerCamelCase			,       padding='max_length'			,       truncation=__lowerCamelCase			,       return_overflowing_tokens=__lowerCamelCase			,       )

      _UpperCAmelCase  :  int				       =label_map[example.label] if example.label in label_map else 0

      _UpperCAmelCase  :  Union[str, Any]				       =int(example.pairID				)

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

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

   return features


lowercase				={
    'hans': 3,
}




lowercase				={
    'hans': HansProcessor,
}





'''simple docstring'''






from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


lowercase				={
    'configuration_table_transformer': [
        'TABLE_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'TableTransformerConfig',
        'TableTransformerOnnxConfig',
    ]
}

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowercase				=[
     'TABLE_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST',
     'TableTransformerForObjectDetection',
     'TableTransformerModel',
     'TableTransformerPreTrainedModel',
 ]


if TYPE_CHECKING:
 from .configuration_table_transformer import (
     TABLE_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP,
     TableTransformerConfig,
     TableTransformerOnnxConfig,
 )

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_table_transformer import (
      TABLE_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
      TableTransformerForObjectDetection,
      TableTransformerModel,
      TableTransformerPreTrainedModel,
  )

else:
 import sys

 lowercase				=_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)

