




import argparse
import collections
import json
import os
import re
import string
import sys

import numpy as np


lowercase__				:     Tuple 		=						re.compile(R"\b(a|an|the)\b", re.UNICODE)

lowercase__				:     Optional[int] 		=						None


def 						lowerCamelCase__    (		):







	'''simple docstring'''





	snake_case_		    =     argparse.ArgumentParser("Official evaluation script for SQuAD version 2.0."							)
	parser.add_argument("data_file"     ,	metavar="data.json"     ,	help="Input data JSON file."							)
	parser.add_argument("pred_file"     ,	metavar="pred.json"     ,	help="Model predictions."							)
	parser.add_argument(
	    "--out-file"     ,	"-o"     ,	metavar="eval.json"     ,	help="Write accuracy metrics to file (default is stdout)."							)
	parser.add_argument(
	    "--na-prob-file"     ,	"-n"     ,	metavar="na_prob.json"     ,	help="Model estimates of probability of no answer."							)
	parser.add_argument(
	    "--na-prob-thresh"     ,	"-t"     ,	type=_A     ,	default=1.0     ,	help="Predict \"\" if no-answer probability exceeds this (default = 1.0)."     ,	)
	parser.add_argument(
	    "--out-image-dir"     ,	"-p"     ,	metavar="out_images"     ,	default=_A     ,	help="Save precision-recall curves to directory."							)
	parser.add_argument("--verbose"     ,	"-v"     ,	action="store_true"							)
	if len(sys.argv							) == 1:
		parser.print_help()
		sys.exit(1							)
	return parser.parse_args()


def 						lowerCamelCase__    (		_A							):







	'''simple docstring'''





	snake_case_		    =     {}
	for article in dataset:
		for p in article["paragraphs"]:
			for qa in p["qas"]:
				snake_case_		    =     bool(qa["answers"]["text"]							)
	return qid_to_has_ans


def 						lowerCamelCase__    (		_A							):







	'''simple docstring'''





	def remove_articles(_A							):
		return ARTICLES_REGEX.sub(" "     ,	_A							)

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

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

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

	return white_space_fix(remove_articles(remove_punc(lower(_A							)							)							)							)


def 						lowerCamelCase__    (		_A							):







	'''simple docstring'''





	if not s:
		return []
	return normalize_answer(_A							).split()


def 						lowerCamelCase__    (		_A     ,	_A							):







	'''simple docstring'''





	return int(normalize_answer(_A							) == normalize_answer(_A							)							)


def 						lowerCamelCase__    (		_A     ,	_A							):







	'''simple docstring'''





	snake_case_		    =     get_tokens(_A							)
	snake_case_		    =     get_tokens(_A							)
	snake_case_		    =     collections.Counter(_A							) & collections.Counter(_A							)
	snake_case_		    =     sum(common.values()							)
	if len(_A							) == 0 or len(_A							) == 0:
		# If either is no-answer, then F1 is 1 if they agree, 0 otherwise
		return int(gold_toks == pred_toks							)
	if num_same == 0:
		return 0
	snake_case_		    =     1.0 * num_same / len(_A							)
	snake_case_		    =     1.0 * num_same / len(_A							)
	snake_case_		    =     (2 * precision * recall) / (precision + recall)
	return fa


def 						lowerCamelCase__    (		_A     ,	_A							):







	'''simple docstring'''





	snake_case_		    =     {}
	snake_case_		    =     {}
	for article in dataset:
		for p in article["paragraphs"]:
			for qa in p["qas"]:
				snake_case_		    =     qa["id"]
				snake_case_		    =     [t for t in qa["answers"]["text"] if normalize_answer(_A							)]
				if not gold_answers:
					# For unanswerable questions, only correct answer is empty string
					snake_case_		    =     [""]
				if qid not in preds:
					print(f"Missing prediction for {qid}"							)
					continue
				snake_case_		    =     preds[qid]
				# Take max over all gold answers
				snake_case_		    =     max(compute_exact(_A     ,	_A							) for a in gold_answers							)
				snake_case_		    =     max(compute_fa(_A     ,	_A							) for a in gold_answers							)
	return exact_scores, fa_scores


def 						lowerCamelCase__    (		_A     ,	_A     ,	_A     ,	_A							):







	'''simple docstring'''





	snake_case_		    =     {}
	for qid, s in scores.items():
		snake_case_		    =     na_probs[qid] > na_prob_thresh
		if pred_na:
			snake_case_		    =     float(not qid_to_has_ans[qid]							)
		else:
			snake_case_		    =     s
	return new_scores


def 						lowerCamelCase__    (		_A     ,	_A     ,	_A=None							):







	'''simple docstring'''





	if not qid_list:
		snake_case_		    =     len(_A							)
		return collections.OrderedDict(
		    [
		        ("exact", 1_00.0 * sum(exact_scores.values()							) / total),
		        ("f1", 1_00.0 * sum(fa_scores.values()							) / total),
		        ("total", total),
		    ]							)
	else:
		snake_case_		    =     len(_A							)
		return collections.OrderedDict(
		    [
		        ("exact", 1_00.0 * sum(exact_scores[k] for k in qid_list							) / total),
		        ("f1", 1_00.0 * sum(fa_scores[k] for k in qid_list							) / total),
		        ("total", total),
		    ]							)


def 						lowerCamelCase__    (		_A     ,	_A     ,	_A							):







	'''simple docstring'''





	for k in new_eval:
		snake_case_		    =     new_eval[k]


def 						lowerCamelCase__    (		_A     ,	_A     ,	_A     ,	_A							):







	'''simple docstring'''





	plt.step(_A     ,	_A     ,	color="b"     ,	alpha=0.2     ,	where="post"							)
	plt.fill_between(_A     ,	_A     ,	step="post"     ,	alpha=0.2     ,	color="b"							)
	plt.xlabel("Recall"							)
	plt.ylabel("Precision"							)
	plt.xlim([0.0, 1.05]							)
	plt.ylim([0.0, 1.05]							)
	plt.title(_A							)
	plt.savefig(_A							)
	plt.clf()


def 						lowerCamelCase__    (		_A     ,	_A     ,	_A     ,	_A     ,	_A=None     ,	_A=None							):







	'''simple docstring'''





	snake_case_		    =     sorted(_A     ,	key=lambda _A							: na_probs[k]							)
	snake_case_		    =     0.0
	snake_case_		    =     1.0
	snake_case_		    =     0.0
	snake_case_		    =     [1.0]
	snake_case_		    =     [0.0]
	snake_case_		    =     0.0
	for i, qid in enumerate(_A							):
		if qid_to_has_ans[qid]:
			true_pos += scores[qid]
		snake_case_		    =     true_pos / float(i + 1							)
		snake_case_		    =     true_pos / float(_A							)
		if i == len(_A							) - 1 or na_probs[qid] != na_probs[qid_list[i + 1]]:
			# i.e., if we can put a threshold after this point
			avg_prec += cur_p * (cur_r - recalls[-1])
			precisions.append(_A							)
			recalls.append(_A							)
	if out_image:
		plot_pr_curve(_A     ,	_A     ,	_A     ,	_A							)
	return {"ap": 1_00.0 * avg_prec}


def 						lowerCamelCase__    (		_A     ,	_A     ,	_A     ,	_A     ,	_A     ,	_A							):







	'''simple docstring'''





	if out_image_dir and not os.path.exists(_A							):
		os.makedirs(_A							)
	snake_case_		    =     sum(1 for v in qid_to_has_ans.values() if v							)
	if num_true_pos == 0:
		return
	snake_case_		    =     make_precision_recall_eval(
	    _A     ,	_A     ,	_A     ,	_A     ,	out_image=os.path.join(_A     ,	"pr_exact.png"							)     ,	title="Precision-Recall curve for Exact Match score"     ,	)
	snake_case_		    =     make_precision_recall_eval(
	    _A     ,	_A     ,	_A     ,	_A     ,	out_image=os.path.join(_A     ,	"pr_f1.png"							)     ,	title="Precision-Recall curve for F1 score"     ,	)
	snake_case_		    =     {k: float(_A							) for k, v in qid_to_has_ans.items()}
	snake_case_		    =     make_precision_recall_eval(
	    _A     ,	_A     ,	_A     ,	_A     ,	out_image=os.path.join(_A     ,	"pr_oracle.png"							)     ,	title="Oracle Precision-Recall curve (binary task of HasAns vs. NoAns)"     ,	)
	merge_eval(_A     ,	_A     ,	"pr_exact"							)
	merge_eval(_A     ,	_A     ,	"pr_f1"							)
	merge_eval(_A     ,	_A     ,	"pr_oracle"							)


def 						lowerCamelCase__    (		_A     ,	_A     ,	_A     ,	_A							):







	'''simple docstring'''





	if not qid_list:
		return
	snake_case_		    =     [na_probs[k] for k in qid_list]
	snake_case_		    =     np.ones_like(_A							) / float(len(_A							)							)
	plt.hist(_A     ,	weights=_A     ,	bins=20     ,	range=(0.0, 1.0)							)
	plt.xlabel("Model probability of no-answer"							)
	plt.ylabel("Proportion of dataset"							)
	plt.title(f"Histogram of no-answer probability: {name}"							)
	plt.savefig(os.path.join(_A     ,	f"na_prob_hist_{name}.png"							)							)
	plt.clf()


def 						lowerCamelCase__    (		_A     ,	_A     ,	_A     ,	_A							):







	'''simple docstring'''





	snake_case_		    =     sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k]							)
	snake_case_		    =     num_no_ans
	snake_case_		    =     cur_score
	snake_case_		    =     0.0
	snake_case_		    =     sorted(_A     ,	key=lambda _A							: na_probs[k]							)
	for i, qid in enumerate(_A							):
		if qid not in scores:
			continue
		if qid_to_has_ans[qid]:
			snake_case_		    =     scores[qid]
		else:
			if preds[qid]:
				snake_case_		    =     -1
			else:
				snake_case_		    =     0
		cur_score += diff
		if cur_score > best_score:
			snake_case_		    =     cur_score
			snake_case_		    =     na_probs[qid]
	return 1_00.0 * best_score / len(_A							), best_thresh


def 						lowerCamelCase__    (		_A     ,	_A     ,	_A     ,	_A     ,	_A     ,	_A							):







	'''simple docstring'''





	snake_case_    ,      snake_case_		    =     find_best_thresh(_A     ,	_A     ,	_A     ,	_A							)
	snake_case_    ,      snake_case_		    =     find_best_thresh(_A     ,	_A     ,	_A     ,	_A							)
	snake_case_		    =     best_exact
	snake_case_		    =     exact_thresh
	snake_case_		    =     best_fa
	snake_case_		    =     fa_thresh


def 						lowerCamelCase__    (		):







	'''simple docstring'''





	with open(OPTS.data_file							) as f:
		snake_case_		    =     json.load(_A							)
		snake_case_		    =     dataset_json["data"]
	with open(OPTS.pred_file							) as f:
		snake_case_		    =     json.load(_A							)
	if OPTS.na_prob_file:
		with open(OPTS.na_prob_file							) as f:
			snake_case_		    =     json.load(_A							)
	else:
		snake_case_		    =     {k: 0.0 for k in preds}
	snake_case_		    =     make_qid_to_has_ans(_A							)  # maps qid to True/False
	snake_case_		    =     [k for k, v in qid_to_has_ans.items() if v]
	snake_case_		    =     [k for k, v in qid_to_has_ans.items() if not v]
	snake_case_    ,      snake_case_		    =     get_raw_scores(_A     ,	_A							)
	snake_case_		    =     apply_no_ans_threshold(_A     ,	_A     ,	_A     ,	OPTS.na_prob_thresh							)
	snake_case_		    =     apply_no_ans_threshold(_A     ,	_A     ,	_A     ,	OPTS.na_prob_thresh							)
	snake_case_		    =     make_eval_dict(_A     ,	_A							)
	if has_ans_qids:
		snake_case_		    =     make_eval_dict(_A     ,	_A     ,	qid_list=_A							)
		merge_eval(_A     ,	_A     ,	"HasAns"							)
	if no_ans_qids:
		snake_case_		    =     make_eval_dict(_A     ,	_A     ,	qid_list=_A							)
		merge_eval(_A     ,	_A     ,	"NoAns"							)
	if OPTS.na_prob_file:
		find_all_best_thresh(_A     ,	_A     ,	_A     ,	_A     ,	_A     ,	_A							)
	if OPTS.na_prob_file and OPTS.out_image_dir:
		run_precision_recall_analysis(_A     ,	_A     ,	_A     ,	_A     ,	_A     ,	OPTS.out_image_dir							)
		histogram_na_prob(_A     ,	_A     ,	OPTS.out_image_dir     ,	"hasAns"							)
		histogram_na_prob(_A     ,	_A     ,	OPTS.out_image_dir     ,	"noAns"							)
	if OPTS.out_file:
		with open(OPTS.out_file     ,	"w"							) as f:
			json.dump(_A     ,	_A							)
	else:
		print(json.dumps(_A     ,	indent=2							)							)


if __name__ == "__main__":
		lowercase__				:     Union[str, Any] 		=						parse_args()
		if OPTS.out_image_dir:
				import matplotlib

				matplotlib.use("Agg")
				import matplotlib.pyplot as plt
		main()






import itertools
import json
import linecache
import os
import pickle
import re
import socket
import string
from collections import Counter
from logging import getLogger
from pathlib import Path
from typing import Callable, Dict, Iterable, List

import git
import torch
from torch.utils.data import Dataset

from transformers import BartTokenizer, RagTokenizer, TaTokenizer


def 						lowerCamelCase__    (		_A     ,	_A     ,	_A     ,	_A     ,	_A=True     ,	_A="pt"							):







	'''simple docstring'''





	snake_case_		    =     {"add_prefix_space": True} if isinstance(_A     ,	_A							) and not line.startswith(" "							) else {}
	snake_case_		    =     padding_side
	return tokenizer(
	    [line]     ,	max_length=_A     ,	padding="max_length" if pad_to_max_length else None     ,	truncation=_A     ,	return_tensors=_A     ,	add_special_tokens=_A     ,	**_A     ,	)


def 						lowerCamelCase__    (		_A     ,	_A     ,	_A=None     ,	):







	'''simple docstring'''





	snake_case_		    =     input_ids.ne(_A							).any(dim=0							)
	if attention_mask is None:
		return input_ids[:, keep_column_mask]
	else:
		return (input_ids[:, keep_column_mask], attention_mask[:, keep_column_mask])
class 		UpperCAmelCase   (				UpperCAmelCase__			):







						'''simple docstring'''





						def __init__(						self							:     int       ,	__lowercase							:     List[Any]       ,	__lowercase							:     Tuple       ,	__lowercase							:     List[Any]       ,	__lowercase							:     str       ,	__lowercase							:     Tuple="train"       ,	__lowercase							:     List[str]=None       ,	__lowercase							:     List[Any]=None       ,	__lowercase							:     Optional[Any]=None       ,	__lowercase							:     Union[str, Any]=""       ,	):






							"""simple docstring"""






							super().__init__()
							snake_case_		    =     Path(__lowercase						).joinpath(type_path + ".source"						)
							snake_case_		    =     Path(__lowercase						).joinpath(type_path + ".target"						)
							snake_case_		    =     self.get_char_lens(self.src_file						)
							snake_case_		    =     max_source_length
							snake_case_		    =     max_target_length
							assert min(self.src_lens						) > 0, f"found empty line in {self.src_file}"
							snake_case_		    =     tokenizer
							snake_case_		    =     prefix
							if n_obs is not None:
								snake_case_		    =     self.src_lens[:n_obs]
							snake_case_		    =     src_lang
							snake_case_		    =     tgt_lang






						def __len__(						self							:     List[Any]						):






							"""simple docstring"""






							return len(self.src_lens						)






						def __getitem__(						self							:     List[Any]       ,	__lowercase							:     Dict						):






							"""simple docstring"""






							snake_case_		    =     index + 1  # linecache starts at 1
							snake_case_		    =     self.prefix + linecache.getline(str(self.src_file						)       ,	__lowercase						).rstrip("\n"						)
							snake_case_		    =     linecache.getline(str(self.tgt_file						)       ,	__lowercase						).rstrip("\n"						)
							assert source_line, f"empty source line for index {index}"
							assert tgt_line, f"empty tgt line for index {index}"

							# Need to add eos token manually for T5
							if isinstance(self.tokenizer       ,	__lowercase						):
								source_line += self.tokenizer.eos_token
								tgt_line += self.tokenizer.eos_token

							# Pad source and target to the right
							snake_case_		    =     (
							    self.tokenizer.question_encoder if isinstance(self.tokenizer       ,	__lowercase						) else self.tokenizer
							)
							snake_case_		    =     self.tokenizer.generator if isinstance(self.tokenizer       ,	__lowercase						) else self.tokenizer

							snake_case_		    =     encode_line(__lowercase       ,	__lowercase       ,	self.max_source_length       ,	"right"						)
							snake_case_		    =     encode_line(__lowercase       ,	__lowercase       ,	self.max_target_length       ,	"right"						)

							snake_case_		    =     source_inputs["input_ids"].squeeze()
							snake_case_		    =     target_inputs["input_ids"].squeeze()
							snake_case_		    =     source_inputs["attention_mask"].squeeze()
							return {
							    "input_ids": source_ids,
							    "attention_mask": src_mask,
							    "decoder_input_ids": target_ids,
							}






						@staticmethod
						def        snake_case__						(						__lowercase							:     Optional[int]						):






							"""simple docstring"""






							return [len(__lowercase						) for x in Path(__lowercase						).open().readlines()]






						def        snake_case__						(						self							:     Dict       ,	__lowercase							:     Union[str, Any]						):






							"""simple docstring"""






							snake_case_		    =     torch.stack([x["input_ids"] for x in batch]						)
							snake_case_		    =     torch.stack([x["attention_mask"] for x in batch]						)
							snake_case_		    =     torch.stack([x["decoder_input_ids"] for x in batch]						)
							snake_case_		    =     (
							    self.tokenizer.generator.pad_token_id
							    if isinstance(self.tokenizer       ,	__lowercase						)
							    else self.tokenizer.pad_token_id
							)
							snake_case_		    =     (
							    self.tokenizer.question_encoder.pad_token_id
							    if isinstance(self.tokenizer       ,	__lowercase						)
							    else self.tokenizer.pad_token_id
							)
							snake_case_		    =     trim_batch(__lowercase       ,	__lowercase						)
							snake_case_    ,      snake_case_		    =     trim_batch(__lowercase       ,	__lowercase       ,	attention_mask=__lowercase						)
							snake_case_		    =     {
							    "input_ids": source_ids,
							    "attention_mask": source_mask,
							    "decoder_input_ids": y,
							}
							return batch







lowercase__				:     str 		=						getLogger(__name__)


def 						lowerCamelCase__    (		_A							):







	'''simple docstring'''





	return list(itertools.chain.from_iterable(_A							)							)


def 						lowerCamelCase__    (		_A							):







	'''simple docstring'''





	snake_case_		    =     get_git_info()
	save_json(_A     ,	os.path.join(_A     ,	"git_log.json"							)							)


def 						lowerCamelCase__    (		_A     ,	_A     ,	_A=4     ,	**_A							):







	'''simple docstring'''





	with open(_A     ,	"w"							) as f:
		json.dump(_A     ,	_A     ,	indent=_A     ,	**_A							)


def 						lowerCamelCase__    (		_A							):







	'''simple docstring'''





	with open(_A							) as f:
		return json.load(_A							)


def 						lowerCamelCase__    (		):







	'''simple docstring'''





	snake_case_		    =     git.Repo(search_parent_directories=_A							)
	snake_case_		    =     {
	    "repo_id": str(_A							),
	    "repo_sha": str(repo.head.object.hexsha							),
	    "repo_branch": str(repo.active_branch							),
	    "hostname": str(socket.gethostname()							),
	}
	return repo_infos


def 						lowerCamelCase__    (		_A     ,	_A							):







	'''simple docstring'''





	return list(map(_A     ,	_A							)							)


def 						lowerCamelCase__    (		_A     ,	_A							):







	'''simple docstring'''





	with open(_A     ,	"wb"							) as f:
		return pickle.dump(_A     ,	_A							)


def 						lowerCamelCase__    (		_A							):







	'''simple docstring'''





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

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

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

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

	return white_space_fix(remove_articles(remove_punc(lower(_A							)							)							)							)


def 						lowerCamelCase__    (		_A     ,	_A							):







	'''simple docstring'''





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


def 						lowerCamelCase__    (		_A     ,	_A							):







	'''simple docstring'''





	return normalize_answer(_A							) == normalize_answer(_A							)


def 						lowerCamelCase__    (		_A     ,	_A							):







	'''simple docstring'''





	assert len(_A							) == len(_A							)
	snake_case_		    =     0
	for hypo, pred in zip(_A     ,	_A							):
		em += exact_match_score(_A     ,	_A							)
	if len(_A							) > 0:
		em /= len(_A							)
	return {"em": em}


def 						lowerCamelCase__    (		_A							):







	'''simple docstring'''





	return model_prefix.startswith("rag"							)






def 						lowerCamelCase__    (		_A     ,	_A     ,	_A							):







	'''simple docstring'''





	snake_case_		    =     {p: p for p in extra_params}
	# T5 models don't have `dropout` param, they have `dropout_rate` instead
	snake_case_		    =     "dropout_rate"
	for p in extra_params:
		if getattr(_A     ,	_A     ,	_A							):
			if not hasattr(_A     ,	_A							) and not hasattr(_A     ,	equivalent_param[p]							):
				logger.info("config doesn't have a `{}` attribute".format(_A							)							)
				delattr(_A     ,	_A							)
				continue
			snake_case_		    =     p if hasattr(_A     ,	_A							) else equivalent_param[p]
			setattr(_A     ,	_A     ,	getattr(_A     ,	_A							)							)
			delattr(_A     ,	_A							)
	return hparams, config
