


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


A	:  List[str]            =     logging.getLogger(__name__)
@dataclass(frozen=lowerCAmelCase__						)
class     _UpperCamelCase    :
							'''simple docstring'''







							__UpperCAmelCase		:       str
							__UpperCAmelCase		:       str
							__UpperCAmelCase		:       Optional[str]							 =None
							__UpperCAmelCase		:       Optional[str]							 =None
							__UpperCAmelCase		:       Optional[str]							 =None
@dataclass(frozen=lowerCAmelCase__						)
class     _UpperCamelCase    :
							'''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     _UpperCamelCase    (  lowerCAmelCase__						):
								'''simple docstring'''







								__UpperCAmelCase		:       List[InputFeatures]

								def __init__(	self       ,  __a       ,  __a       ,  __a       ,  __a = None       ,  __a=False       ,  __a = False       ,  ):
											__lowerCAmelCase							       =     hans_processors[task]()

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

											# Make sure only the first process in distributed training processes the dataset,
											# and the others will use the cache.
											__lowerCAmelCase							       =     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}"      )
																	__lowerCAmelCase							       =     torch.load(__a      )
														else:
																	logger.info(f"Creating features from dataset file at {data_dir}"      )

																	__lowerCAmelCase							       =     (
																	    processor.get_dev_examples(__a      ) if evaluate else processor.get_train_examples(__a      )
																	)

																	logger.info("Training examples: %s"       ,  len(__a      )      )
																	__lowerCAmelCase							       =     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      ):
											return len(self.features      )

								def __getitem__(	self       ,  __a      ):
											return self.features[i]



								def      snake_case							(	self      ):
											return self.label_list


if is_tf_available():
	import tensorflow as tf
	class     _UpperCamelCase    :
								'''simple docstring'''







								__UpperCAmelCase		:       List[InputFeatures]

								def __init__(	self       ,  __a       ,  __a       ,  __a       ,  __a = 1_28       ,  __a=False       ,  __a = False       ,  ):
											__lowerCAmelCase							       =     hans_processors[task]()
											__lowerCAmelCase							       =     processor.get_labels()
											if tokenizer.__class__ in (
											    RobertaTokenizer,
											    RobertaTokenizerFast,
											    XLMRobertaTokenizer,
											    BartTokenizer,
											    BartTokenizerFast,
											):
														# HACK(label indices are swapped in RoBERTa pretrained model)
														__lowerCAmelCase       ,      __lowerCAmelCase							       =     label_list[2], label_list[1]
											__lowerCAmelCase							       =     label_list

											__lowerCAmelCase							       =     processor.get_dev_examples(__a      ) if evaluate else processor.get_train_examples(__a      )
											__lowerCAmelCase							       =     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_00_00 == 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,
																	)

											__lowerCAmelCase							       =     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      snake_case							(	self      ):
											return self.dataset

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

								def __getitem__(	self       ,  __a      ):
											return self.features[i]



								def      snake_case							(	self      ):
											return self.label_list
class     _UpperCamelCase    (  lowerCAmelCase__						):
							'''simple docstring'''







							def      snake_case							(	self       ,  __a      ):
										return self._create_examples(self._read_tsv(os.path.join(__a       ,  "heuristics_train_set.txt"      )      )       ,  "train"      )

							def      snake_case							(	self       ,  __a      ):
										return self._create_examples(self._read_tsv(os.path.join(__a       ,  "heuristics_evaluation_set.txt"      )      )       ,  "dev"      )

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



							def      snake_case							(	self       ,  __a       ,  __a      ):
										__lowerCAmelCase							       =     []
										for i, line in enumerate(__a      ):
													if i == 0:
																continue
													__lowerCAmelCase							       =     "%s-%s" % (set_type, line[0])
													__lowerCAmelCase							       =     line[5]
													__lowerCAmelCase							       =     line[6]
													__lowerCAmelCase							       =     line[7][2:] if line[7].startswith("ex"      ) else line[7]
													__lowerCAmelCase							       =     line[0]
													examples.append(InputExample(guid=__a       ,  text_a=__a       ,  text_b=__a       ,  label=__a       ,  pairID=__a      )      )
										return examples


def _lowerCamelCase      (						_UpperCamelCase       ,					_UpperCamelCase       ,					_UpperCamelCase       ,					_UpperCamelCase       ,					):

			'''simple docstring'''




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

			__lowerCAmelCase							       =     []
			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) )

						__lowerCAmelCase							       =     tokenizer(
						    example.text_a       ,					example.text_b       ,					add_special_tokens=_UpperCamelCase       ,					max_length=_UpperCamelCase       ,					padding="max_length"       ,					truncation=_UpperCamelCase       ,					return_overflowing_tokens=_UpperCamelCase       ,					)

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

						__lowerCAmelCase							       =     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


A	:  Dict            =     {
    "hans": 3,
}


A	:  List[Any]            =     {
    "hans": HansProcessor,
}








"""simple docstring"""


import copy
from dataclasses import dataclass, field
from typing import ClassVar, Dict

from ..features import ClassLabel, Features, Image
from .base import TaskTemplate
@dataclass(frozen=lowerCAmelCase__						)
class     _UpperCamelCase    (  lowerCAmelCase__						):
							'''simple docstring'''







							__UpperCAmelCase		:       str							 =field(default="""image-classification"""			,metadata={"""include_in_asdict_even_if_is_default""": True}						)
							__UpperCAmelCase		:       ClassVar[Features]							 =Features({"""image""": Image()}						)
							__UpperCAmelCase		:       ClassVar[Features]							 =Features({"""labels""": ClassLabel}						)
							__UpperCAmelCase		:       str							 ="image"
							__UpperCAmelCase		:       str							 ="labels"

							def      snake_case							(	self       ,  __a      ):
										if self.label_column not in features:
													raise ValueError(f"Column {self.label_column} is not present in features."      )
										if not isinstance(features[self.label_column]       ,  __a      ):
													raise ValueError(f"Column {self.label_column} is not a ClassLabel."      )
										__lowerCAmelCase							       =     copy.deepcopy(self      )
										__lowerCAmelCase							       =     self.label_schema.copy()
										__lowerCAmelCase							       =     features[self.label_column]
										__lowerCAmelCase							       =     label_schema
										return task_template



							@property
							def      snake_case							(	self      ):
										return {
										    self.image_column: "image",
										    self.label_column: "labels",
										}




