






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


UpperCamelCase__			=      logging.getLogger(__name__)


@dataclass(frozen=snake_case      )
class 							_UpperCAmelCase    :
    __lowerCamelCase: str
    __lowerCamelCase: str
    __lowerCamelCase: Optional[str]									=     None
    __lowerCamelCase: Optional[str]									=     None
    __lowerCamelCase: Optional[str]									=     None


@dataclass(frozen=snake_case      )
class 							_UpperCAmelCase    :
    __lowerCamelCase: List[int]
    __lowerCamelCase: Optional[List[int]]									=     None
    __lowerCamelCase: Optional[List[int]]									=     None
    __lowerCamelCase: Optional[Union[int, float]]									=     None
    __lowerCamelCase: Optional[int]									=     None


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


 class 							_UpperCAmelCase    (		snake_case      ):
     __lowerCamelCase: List[InputFeatures]




     def __init__(      self	:							Any	,  a	:							str	,  a	:							PreTrainedTokenizer	,  a	:							str	,  a	:							Optional[int] = None	,  a	:							int=False	,  a	:							bool = False	,  ):






        '''simple docstring'''






        lowercase_						:     str							     =  hans_processors[task]()

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

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

              lowercase_						:     List[str]							     =  (
                  processor.get_dev_examples(a      ) if evaluate else processor.get_train_examples(a      )
              )

              logger.info("Training examples: %s"	,  len(a      )      )
              lowercase_						:     Optional[int]							     =  hans_convert_examples_to_features(a	,  a	,  a	,  a      )
              logger.info("Saving features into cached file %s"	,  a      )
              torch.save(self.features	,  a      )




     def __len__(      self	:							Optional[Any]      ):






        '''simple docstring'''






        return len(self.features      )




     def __getitem__(      self	:							Optional[int]	,  a	:							Any      ):






        '''simple docstring'''






        return self.features[i]





     def 					lowerCAmelCase__					(      self	:							str      ):






        '''simple docstring'''






        return self.label_list


if is_tf_available():
 import tensorflow as tf


 class 							_UpperCAmelCase    :
     __lowerCamelCase: List[InputFeatures]




     def __init__(      self	:							Optional[int]	,  a	:							str	,  a	:							PreTrainedTokenizer	,  a	:							str	,  a	:							Optional[int] = 1_2_8	,  a	:							List[Any]=False	,  a	:							bool = False	,  ):






        '''simple docstring'''






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

        lowercase_						:     str							     =  processor.get_dev_examples(a      ) if evaluate else processor.get_train_examples(a      )
        lowercase_						:     List[str]							     =  hans_convert_examples_to_features(a	,  a	,  a	,  a      )

        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(a      ))      )

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

        lowercase_						:     Dict							     =  tf.data.Dataset.from_generator(
            a	,  (
                {
                    "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	:							List[Any]      ):






        '''simple docstring'''






        return self.dataset




     def __len__(      self	:							int      ):






        '''simple docstring'''






        return len(self.features      )




     def __getitem__(      self	:							Any	,  a	:							int      ):






        '''simple docstring'''






        return self.features[i]





     def 					lowerCAmelCase__					(      self	:							Optional[int]      ):






        '''simple docstring'''






        return self.label_list


class 							_UpperCAmelCase    (		snake_case      ):




    def 					lowerCAmelCase__					(      self	:							int	,  a	:							str      ):






       '''simple docstring'''






       return self._create_examples(self._read_tsv(os.path.join(a	,  "heuristics_train_set.txt"      )      )	,  "train"      )




    def 					lowerCAmelCase__					(      self	:							str	,  a	:							str      ):






       '''simple docstring'''






       return self._create_examples(self._read_tsv(os.path.join(a	,  "heuristics_evaluation_set.txt"      )      )	,  "dev"      )




    def 					lowerCAmelCase__					(      self	:							int      ):






       '''simple docstring'''






       return ["contradiction", "entailment", "neutral"]





    def 					lowerCAmelCase__					(      self	:							str	,  a	:							Dict	,  a	:							int      ):






       '''simple docstring'''






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




def 		__SCREAMING_SNAKE_CASE       (	_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		):


   """simple docstring"""




   lowercase_						:     Dict							     =  {label: i for i, label in enumerate(_UpperCamelCase				)}

   lowercase_						:     List[Any]							     =  []
   for ex_index, example in tqdm.tqdm(enumerate(_UpperCamelCase				)    ,		desc="convert examples to features"				):
      if ex_index % 1_0000 == 0:
         logger.info("Writing example %d" % (ex_index)				)

      lowercase_						:     List[Any]							     =  tokenizer(
          example.text_a    ,		example.text_b    ,		add_special_tokens=_UpperCamelCase    ,		max_length=_UpperCamelCase    ,		padding="max_length"    ,		truncation=_UpperCamelCase    ,		return_overflowing_tokens=_UpperCamelCase    ,		)

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

      lowercase_						:     Dict							     =  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


UpperCamelCase__			=      {
    'hans': 3,
}





UpperCamelCase__			=      {
    'hans': HansProcessor,
}












'''simple docstring'''
from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


UpperCamelCase__			=      {
    'configuration_layoutlmv3': [
        'LAYOUTLMV3_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'LayoutLMv3Config',
        'LayoutLMv3OnnxConfig',
    ],
    'processing_layoutlmv3': ['LayoutLMv3Processor'],
    'tokenization_layoutlmv3': ['LayoutLMv3Tokenizer'],
}

try:
 if not is_tokenizers_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCamelCase__			=      ['LayoutLMv3TokenizerFast']

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCamelCase__			=      [
     'LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST',
     'LayoutLMv3ForQuestionAnswering',
     'LayoutLMv3ForSequenceClassification',
     'LayoutLMv3ForTokenClassification',
     'LayoutLMv3Model',
     'LayoutLMv3PreTrainedModel',
 ]

try:
 if not is_tf_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCamelCase__			=      [
     'TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST',
     'TFLayoutLMv3ForQuestionAnswering',
     'TFLayoutLMv3ForSequenceClassification',
     'TFLayoutLMv3ForTokenClassification',
     'TFLayoutLMv3Model',
     'TFLayoutLMv3PreTrainedModel',
 ]

try:
 if not is_vision_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCamelCase__			=      ['LayoutLMv3FeatureExtractor']
 UpperCamelCase__			=      ['LayoutLMv3ImageProcessor']


if TYPE_CHECKING:
 from .configuration_layoutlmva import (
     LAYOUTLMV3_PRETRAINED_CONFIG_ARCHIVE_MAP,
     LayoutLMvaConfig,
     LayoutLMvaOnnxConfig,
 )
 from .processing_layoutlmva import LayoutLMvaProcessor
 from .tokenization_layoutlmva import LayoutLMvaTokenizer

 try:
  if not is_tokenizers_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_layoutlmva_fast import LayoutLMvaTokenizerFast

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_layoutlmva import (
      LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST,
      LayoutLMvaForQuestionAnswering,
      LayoutLMvaForSequenceClassification,
      LayoutLMvaForTokenClassification,
      LayoutLMvaModel,
      LayoutLMvaPreTrainedModel,
  )

 try:
  if not is_tf_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_tf_layoutlmva import (
      TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST,
      TFLayoutLMvaForQuestionAnswering,
      TFLayoutLMvaForSequenceClassification,
      TFLayoutLMvaForTokenClassification,
      TFLayoutLMvaModel,
      TFLayoutLMvaPreTrainedModel,
  )

 try:
  if not is_vision_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .feature_extraction_layoutlmva import LayoutLMvaFeatureExtractor
  from .image_processing_layoutlmva import LayoutLMvaImageProcessor

else:
 import sys

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




