




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_  :      str		=		logging.getLogger(__name__)





@dataclass(frozen=a_	)
class 					snake_case_   :



				'''simple docstring'''
				lowerCamelCase       =  42
				lowerCamelCase       =  42
				lowerCamelCase       =  None
				lowerCamelCase       =  None
				lowerCamelCase       =  None





@dataclass(frozen=a_	)
class 					snake_case_   :



				'''simple docstring'''
				lowerCamelCase       =  42
				lowerCamelCase       =  None
				lowerCamelCase       =  None
				lowerCamelCase       =  None
				lowerCamelCase       =  None


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





	class 					snake_case_   (     a_	):



					'''simple docstring'''
					lowerCamelCase       =  42





					def __init__(      self							:   Tuple      ,							__magic_name__							:   List[Any]      ,							__magic_name__							:   Union[str, Any]      ,							__magic_name__							:   int      ,							__magic_name__							:   int = None      ,							__magic_name__							:   List[Any]=False      ,							__magic_name__							:   str = False      ,							)							->			List[Any]:
												lowerCamelCase_			:    Optional[Any]   						=      hans_processors[task]()

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

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

																										lowerCamelCase_			:    int   						=      (
																										    processor.get_dev_examples(__magic_name__							) if evaluate else processor.get_train_examples(__magic_name__							)
																										)

																										logger.info("Training examples: %s"      ,							len(__magic_name__							)							)
																										lowerCamelCase_			:    List[Any]   						=      hans_convert_examples_to_features(__magic_name__      ,							__magic_name__      ,							__magic_name__      ,							__magic_name__							)
																										logger.info("Saving features into cached file %s"      ,							__magic_name__							)
																										torch.save(self.features      ,							__magic_name__							)





					def __len__(      self							:   Union[str, Any]							)							->			List[Any]:
												return len(self.features							)





					def __getitem__(      self							:   Optional[Any]      ,							__magic_name__							:   List[Any]							)							->			InputFeatures:
												return self.features[i]





					def    __SCREAMING_SNAKE_CASE     (      self							:   List[Any]							)							->			Tuple:
												return self.label_list


if is_tf_available():
	import tensorflow as tf





	class 					snake_case_   :



					'''simple docstring'''
					lowerCamelCase       =  42





					def __init__(      self							:   Tuple      ,							__magic_name__							:   str      ,							__magic_name__							:   Any      ,							__magic_name__							:   List[Any]      ,							__magic_name__							:   Dict = 128      ,							__magic_name__							:   Tuple=False      ,							__magic_name__							:   Optional[int] = False      ,							)							->			str:
												lowerCamelCase_			:    int   						=      hans_processors[task]()
												lowerCamelCase_			:    Optional[Any]   						=      processor.get_labels()
												if tokenizer.__class__ in (
												    RobertaTokenizer,
												    RobertaTokenizerFast,
												    XLMRobertaTokenizer,
												    BartTokenizer,
												    BartTokenizerFast,
												):
																			# HACK(label indices are swapped in RoBERTa pretrained model)
																			lowerCamelCase_						,						lowerCamelCase_			:    Any   						=      label_list[2], label_list[1]
												lowerCamelCase_			:    List[str]   						=      label_list

												lowerCamelCase_			:    int   						=      processor.get_dev_examples(__magic_name__							) if evaluate else processor.get_train_examples(__magic_name__							)
												lowerCamelCase_			:    Dict   						=      hans_convert_examples_to_features(__magic_name__      ,							__magic_name__      ,							__magic_name__      ,							__magic_name__							)

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

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

												lowerCamelCase_			:    List[str]   						=      tf.data.Dataset.from_generator(
												    __magic_name__      ,							(
												        {
												            "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    __SCREAMING_SNAKE_CASE     (      self							:   List[str]							)							->			Tuple:
												return self.dataset





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





					def __getitem__(      self							:   Union[str, Any]      ,							__magic_name__							:   Any							)							->			InputFeatures:
												return self.features[i]





					def    __SCREAMING_SNAKE_CASE     (      self							:   List[str]							)							->			str:
												return self.label_list





class 					snake_case_   (     a_	):



				'''simple docstring'''





				def    __SCREAMING_SNAKE_CASE     (      self							:   List[str]      ,							__magic_name__							:   Optional[int]							)							->			Optional[int]:
											return self._create_examples(self._read_tsv(os.path.join(__magic_name__      ,							"heuristics_train_set.txt"							)							)      ,							"train"							)





				def    __SCREAMING_SNAKE_CASE     (      self							:   Tuple      ,							__magic_name__							:   Tuple							)							->			int:
											return self._create_examples(self._read_tsv(os.path.join(__magic_name__      ,							"heuristics_evaluation_set.txt"							)							)      ,							"dev"							)





				def    __SCREAMING_SNAKE_CASE     (      self							:   List[Any]							)							->			List[str]:
											return ["contradiction", "entailment", "neutral"]





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



def        __a							(  __UpperCAmelCase		:						List[InputExample]   ,		__UpperCAmelCase		:						List[str]   ,		__UpperCAmelCase		:						int   ,		__UpperCAmelCase		:						PreTrainedTokenizer   ,		) ->					Optional[int]:
							"""simple docstring"""




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

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

														lowerCamelCase_			:    Union[str, Any]   						=      tokenizer(
														    example.text_a   ,		example.text_b   ,		add_special_tokens=lowerCamelCase__   ,		max_length=lowerCamelCase__   ,		padding="max_length"   ,		truncation=lowerCamelCase__   ,		return_overflowing_tokens=lowerCamelCase__   ,		)

														lowerCamelCase_			:    List[Any]   						=      label_map[example.label] if example.label in label_map else 0

														lowerCamelCase_			:    Optional[int]   						=      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


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





snake_case_  :      Tuple		=		{
    "hans": HansProcessor,
}





# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse

from .config import config_command_parser
from .config_args import default_config_file, load_config_from_file  # noqa: F401
from .default import default_command_parser
from .update import update_command_parser






def 	__lowerCamelCase						(							lowerCamelCase__		:						Any=None      ):





			'''simple docstring'''




			lowerCamelCase    		=  argparse.ArgumentParser(add_help=lowerCamelCase__  ,							allow_abbrev=lowerCamelCase__      )
			# The main config parser
			lowerCamelCase    		=  config_command_parser(lowerCamelCase__      )
			# The subparser to add commands to
			lowerCamelCase    		=  config_parser.add_subparsers(title="""subcommands"""  ,							dest="""subcommand"""      )

			# Then add other parsers with the parent parser
			default_command_parser(lowerCamelCase__  ,							parents=[parent_parser]      )
			update_command_parser(lowerCamelCase__  ,							parents=[parent_parser]      )

			return config_parser






def 	__lowerCamelCase						(							):





			'''simple docstring'''




			lowerCamelCase    		=  get_config_parser()
			lowerCamelCase    		=  config_parser.parse_args()

			if not hasattr(lowerCamelCase__  ,							"""func"""      ):
						config_parser.print_help()
						exit(1      )

			# Run
			args.func(lowerCamelCase__      )


if __name__ == "__main__":
		main()

