




'''simple docstring'''







import argparse
import json
import re
import string
import sys
from collections import Counter






def 		_lowercase		(     lowercase_  ):



		'''simple docstring'''







		def remove_articles(lowercase_  ):
				return re.sub(r"\b(a|an|the)\b"						,							" "						,							lowercase_  )

		def white_space_fix(lowercase_  ):
				return " ".join(text.split()  )

		def remove_punc(lowercase_  ):
				_snake_case     	=	set(string.punctuation  )
				return "".join(ch for ch in text if ch not in exclude  )

		def lower(lowercase_  ):
				return text.lower()

		return white_space_fix(remove_articles(remove_punc(lower(lowercase_  )  )  )  )






def 		_lowercase		(     lowercase_						,							lowercase_  ):



		'''simple docstring'''







		_snake_case     	=	normalize_answer(lowercase_  ).split()
		_snake_case     	=	normalize_answer(lowercase_  ).split()
		_snake_case     	=	Counter(lowercase_  ) & Counter(lowercase_  )
		_snake_case     	=	sum(common.values()  )
		if num_same == 0:
				return 0
		_snake_case     	=	1.0 * num_same / len(lowercase_  )
		_snake_case     	=	1.0 * num_same / len(lowercase_  )
		_snake_case     	=	(2 * precision * recall) / (precision + recall)
		return fa






def 		_lowercase		(     lowercase_						,							lowercase_  ):



		'''simple docstring'''







		return normalize_answer(lowercase_  ) == normalize_answer(lowercase_  )






def 		_lowercase		(     lowercase_						,							lowercase_						,							lowercase_  ):



		'''simple docstring'''







		_snake_case     	=	[]
		for ground_truth in ground_truths:
				_snake_case     	=	metric_fn(lowercase_						,							lowercase_  )
				scores_for_ground_truths.append(lowercase_  )
		return max(lowercase_  )






def 		_lowercase		(     lowercase_						,							lowercase_  ):



		'''simple docstring'''







		_snake_case     	=	_snake_case     	=	_snake_case     	=	0
		_snake_case     	=	[]
		for passage in dataset:
				for qa in passage["qas"]:
						total += 1
						if qa["id"] not in predictions:
								_snake_case     	=	f'''Unanswered question {qa['id']} will receive score 0.'''
								print(lowercase_						,							file=sys.stderr  )
								continue

						_snake_case     	=	[x["text"] for x in qa["answers"]]
						_snake_case     	=	predictions[qa["id"]]

						_snake_case     	=	metric_max_over_ground_truths(lowercase_						,							lowercase_						,							lowercase_  )
						if int(_exact_match  ) == 1:
								correct_ids.append(qa["id"]  )
						exact_match += _exact_match

						fa += metric_max_over_ground_truths(lowercase_						,							lowercase_						,							lowercase_  )

		_snake_case     	=	exact_match / total
		_snake_case     	=	fa / total

		return {"exact_match": exact_match, "f1": fa}, correct_ids


if __name__ == "__main__":
						_lowerCamelCase							:						List[str]							     =  '1.0'
						_lowerCamelCase							:						List[str]							     =  argparse.ArgumentParser('Official evaluation script for ReCoRD v1.0.')
						parser.add_argument('data_file', help='The dataset file in JSON format.')
						parser.add_argument('pred_file', help='The model prediction file in JSON format.')
						parser.add_argument('--output_correct_ids', action='store_true', help='Output the correctly answered query IDs.')
						_lowerCamelCase							:						Union[str, Any]							     =  parser.parse_args()

						with open(args.data_file) as data_file:
												_lowerCamelCase							:						List[str]							     =  json.load(data_file)
												if dataset_json["version"] != expected_version:
																		print(
																		    F'''Evaluation expects v-{expected_version}, but got dataset with v-{dataset_json['version']}''',
																		    file=sys.stderr,
																		)
												_lowerCamelCase							:						Any							     =  dataset_json['data']

						with open(args.pred_file) as pred_file:
												_lowerCamelCase							:						Dict							     =  json.load(pred_file)

						_lowerCamelCase     ,_lowerCamelCase							:						Tuple							     =  evaluate(dataset, predictions)

						if args.output_correct_ids:
												print(F'''Output {len(correct_ids)} correctly answered question IDs.''')
												with open('correct_ids.json', 'w') as f:
																		json.dump(correct_ids, f)











'''simple docstring'''







import logging
import os
import sys
from dataclasses import dataclass, field
from typing import Optional

import transformers
from transformers import (
    AutoConfig,
    AutoModelForQuestionAnswering,
    AutoTokenizer,
    DataCollatorWithPadding,
    HfArgumentParser,
    SquadDataset,
    Trainer,
    TrainingArguments,
)
from transformers import SquadDataTrainingArguments as DataTrainingArguments
from transformers.trainer_utils import is_main_process


_lowerCamelCase							:						Tuple							     =  logging.getLogger(__name__)





@dataclass
class 					lowerCamelCase_							:




					'''simple docstring'''






					lowercase__   =			field(
					    metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""}							)
					lowercase__   =			field(
					    default=UpperCAmelCase_,	metadata={"""help""": """Pretrained config name or path if not the same as model_name"""}							)
					lowercase__   =			field(
					    default=UpperCAmelCase_,	metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""}							)
					lowercase__   =			field(default=UpperCAmelCase_,	metadata={"""help""": """Set this flag to use fast tokenization."""}							)
					# If you want to tweak more attributes on your tokenizer, you should do it in a distinct script,
					# or just modify its tokenizer_config.json.
					lowercase__   =			field(
					    default=UpperCAmelCase_,	metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""},	)






def 		_lowercase		(     ):



		'''simple docstring'''







		_snake_case     	=	HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)  )

		if len(sys.argv  ) == 2 and sys.argv[1].endswith(".json"  ):
				# If we pass only one argument to the script and it's the path to a json file,
				# let's parse it to get our arguments.
				_snake_case	,   _snake_case	,   _snake_case     	=	parser.parse_json_file(json_file=os.path.abspath(sys.argv[1]  )  )
		else:
				_snake_case	,   _snake_case	,   _snake_case     	=	parser.parse_args_into_dataclasses()

		if (
		    os.path.exists(training_args.output_dir  )
		    and os.listdir(training_args.output_dir  )
		    and training_args.do_train
		    and not training_args.overwrite_output_dir
		):
				raise ValueError(
				    f'''Output directory ({training_args.output_dir}) already exists and is not empty. Use'''
				    " --overwrite_output_dir to overcome."  )

		# Setup logging
		logging.basicConfig(
		    format="%(asctime)s - %(levelname)s - %(name)s - %(message)s"						,							datefmt="%m/%d/%Y %H:%M:%S"						,							level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN						,							)
		logger.warning(
		    "Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s"						,							training_args.local_rank						,							training_args.device						,							training_args.n_gpu						,							bool(training_args.local_rank != -1  )						,							training_args.fpaa						,							)
		# Set the verbosity to info of the Transformers logger (on main process only):
		if is_main_process(training_args.local_rank  ):
				transformers.utils.logging.set_verbosity_info()
				transformers.utils.logging.enable_default_handler()
				transformers.utils.logging.enable_explicit_format()
		logger.info("Training/evaluation parameters %s"						,							lowercase_  )

		# Prepare Question-Answering task
		# Load pretrained model and tokenizer
		#
		# Distributed training:
		# The .from_pretrained methods guarantee that only one local process can concurrently
		# download model & vocab.

		_snake_case     	=	AutoConfig.from_pretrained(
		    model_args.config_name if model_args.config_name else model_args.model_name_or_path						,							cache_dir=model_args.cache_dir						,							)
		_snake_case     	=	AutoTokenizer.from_pretrained(
		    model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path						,							cache_dir=model_args.cache_dir						,							use_fast=lowercase_						,							)
		_snake_case     	=	AutoModelForQuestionAnswering.from_pretrained(
		    model_args.model_name_or_path						,							from_tf=bool(".ckpt" in model_args.model_name_or_path  )						,							config=lowercase_						,							cache_dir=model_args.cache_dir						,							)

		# Get datasets
		_snake_case     	=	hasattr(model.config						,							"lang2id"  )
		_snake_case     	=	(
		    SquadDataset(
		        lowercase_						,							tokenizer=lowercase_						,							is_language_sensitive=lowercase_						,							cache_dir=model_args.cache_dir  )
		    if training_args.do_train
		    else None
		)
		_snake_case     	=	(
		    SquadDataset(
		        lowercase_						,							tokenizer=lowercase_						,							mode="dev"						,							is_language_sensitive=lowercase_						,							cache_dir=model_args.cache_dir						,							)
		    if training_args.do_eval
		    else None
		)

		# Data collator
		_snake_case     	=	DataCollatorWithPadding(lowercase_						,							pad_to_multiple_of=8  ) if training_args.fpaa else None

		# Initialize our Trainer
		_snake_case     	=	Trainer(
		    model=lowercase_						,							args=lowercase_						,							train_dataset=lowercase_						,							eval_dataset=lowercase_						,							data_collator=lowercase_						,							)

		# Training
		if training_args.do_train:
				trainer.train(
				    model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path  ) else None  )
				trainer.save_model()
				# For convenience, we also re-save the tokenizer to the same directory,
				# so that you can share your model easily on huggingface.co/models =)
				if trainer.is_world_master():
						tokenizer.save_pretrained(training_args.output_dir  )






def 		_lowercase		(     lowercase_  ):



		'''simple docstring'''







		main()


if __name__ == "__main__":
						main()





