


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



def      lowerCAmelCase	(						lowerCAmelCase_	)->	str:

				def remove_articles(lowerCAmelCase_	):
								return re.sub(r'''\b(a|an|the)\b'''	,				''' '''	,				_lowerCAmelCase	)

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

				def remove_punc(lowerCAmelCase_	):
								lowerCAmelCase_	: Optional[int]      			=			set(string.punctuation	)
								return "".join(ch for ch in text if ch not in exclude	)

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

				return white_space_fix(remove_articles(remove_punc(lower(_lowerCAmelCase	)	)	)	)



def      lowerCAmelCase	(						lowerCAmelCase_	,				lowerCAmelCase_	)->	Dict:
				lowerCAmelCase_	: int      			=			normalize_answer(_lowerCAmelCase	).split()
				lowerCAmelCase_	: Union[str, Any]      			=			normalize_answer(_lowerCAmelCase	).split()
				lowerCAmelCase_	: Dict      			=			Counter(_lowerCAmelCase	) & Counter(_lowerCAmelCase	)
				lowerCAmelCase_	: Optional[int]      			=			sum(common.values()	)
				if num_same == 0:
								return 0
				lowerCAmelCase_	: Optional[Any]      			=			1.0 * num_same / len(_lowerCAmelCase	)
				lowerCAmelCase_	: List[str]      			=			1.0 * num_same / len(_lowerCAmelCase	)
				lowerCAmelCase_	: str      			=			(2 * precision * recall) / (precision + recall)
				return fa



def      lowerCAmelCase	(						lowerCAmelCase_	,				lowerCAmelCase_	)->	str:
				return normalize_answer(_lowerCAmelCase	) == normalize_answer(_lowerCAmelCase	)



def      lowerCAmelCase	(						lowerCAmelCase_	,				lowerCAmelCase_	,				lowerCAmelCase_	)->	Any:
				lowerCAmelCase_	: Optional[Any]      			=			[]
				for ground_truth in ground_truths:
								lowerCAmelCase_	: Any      			=			metric_fn(_lowerCAmelCase	,				_lowerCAmelCase	)
								scores_for_ground_truths.append(_lowerCAmelCase	)
				return max(_lowerCAmelCase	)



def      lowerCAmelCase	(						lowerCAmelCase_	,				lowerCAmelCase_	)->	List[str]:
				lowerCAmelCase_	: Optional[int]      			=			0
				for article in dataset:
								for paragraph in article["paragraphs"]:
												for qa in paragraph["qas"]:
																total += 1
																if qa["id"] not in predictions:
																				lowerCAmelCase_	: Optional[Any]      			=			'''Unanswered question ''' + qa['''id'''] + ''' will receive score 0.'''
																				print(_lowerCAmelCase	,				file=sys.stderr	)
																				continue
																lowerCAmelCase_	: Optional[Any]      			=			[x['''text'''] for x in qa['''answers''']]
																lowerCAmelCase_	: Union[str, Any]      			=			predictions[qa['''id''']]
																exact_match += metric_max_over_ground_truths(_lowerCAmelCase	,				_lowerCAmelCase	,				_lowerCAmelCase	)
																fa += metric_max_over_ground_truths(_lowerCAmelCase	,				_lowerCAmelCase	,				_lowerCAmelCase	)

				lowerCAmelCase_	: int      			=			100.0 * exact_match / total
				lowerCAmelCase_	: Union[str, Any]      			=			100.0 * fa / total

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


if __name__ == "__main__":
						_UpperCAmelCase     :					Tuple       ="""1.1"""
						_UpperCAmelCase     :					Dict       =argparse.ArgumentParser(description="""Evaluation for SQuAD """ + expected_version)
						parser.add_argument("""dataset_file""", help="""Dataset file""")
						parser.add_argument("""prediction_file""", help="""Prediction File""")
						_UpperCAmelCase     :					str       =parser.parse_args()
						with open(args.dataset_file) as dataset_file:
												_UpperCAmelCase     :					Dict       =json.load(dataset_file)
												if dataset_json["version"] != expected_version:
																		print(
																		    """Evaluation expects v-""" + expected_version + """, but got dataset with v-""" + dataset_json["""version"""],
																		    file=sys.stderr,
																		)
												_UpperCAmelCase     :					Dict       =dataset_json["""data"""]
						with open(args.prediction_file) as prediction_file:
												_UpperCAmelCase     :					List[str]       =json.load(prediction_file)
						print(json.dumps(evaluate(dataset, predictions)))



from typing import List, Optional, Tuple, Union

from . import logging
from .import_utils import is_torch_available, is_torch_version


if is_torch_available():
						import torch

_UpperCAmelCase     :					List[Any]       =logging.get_logger(__name__)  # pylint: disable=invalid-name

try:
						from torch._dynamo import allow_in_graph as maybe_allow_in_graph
except (ImportError, ModuleNotFoundError):



						def      lowerCAmelCase	(						cls	)->	int:
										return cls



def      lowerCAmelCase	(						lowerCAmelCase_	,				lowerCAmelCase_ = None	,				lowerCAmelCase_ = None	,				lowerCAmelCase_ = None	,				lowerCAmelCase_ = None	,				)->	int:
				lowerCAmelCase_	: Tuple      			=			device
				lowerCAmelCase_	: List[str]      			=			shape[0]

				lowerCAmelCase_	: Union[str, Any]      			=			layout or torch.strided
				lowerCAmelCase_	: Union[str, Any]      			=			device or torch.device('''cpu'''	)

				if generator is not None:
								lowerCAmelCase_	: List[str]      			=			generator.device.type if not isinstance(lowerCAmelCase_	,				lowerCAmelCase_	) else generator[0].device.type
								if gen_device_type != device.type and gen_device_type == "cpu":
												lowerCAmelCase_	: Optional[Any]      			=			'''cpu'''
												if device != "mps":
																logger.info(
																    f"""The passed generator was created on 'cpu' even though a tensor on {device} was expected."""
																    f""" Tensors will be created on 'cpu' and then moved to {device}. Note that one can probably"""
																    f""" slighly speed up this function by passing a generator that was created on the {device} device."""	)
								elif gen_device_type != device.type and gen_device_type == "cuda":
												raise ValueError(f"""Cannot generate a {device} tensor from a generator of type {gen_device_type}."""	)

				if isinstance(lowerCAmelCase_	,				lowerCAmelCase_	):
								lowerCAmelCase_	: Dict      			=			(1,) + shape[1:]
								lowerCAmelCase_	: Union[str, Any]      			=			[
								    torch.randn(lowerCAmelCase_	,				generator=generator[i]	,				device=lowerCAmelCase_	,				dtype=lowerCAmelCase_	,				layout=lowerCAmelCase_	)
								    for i in range(lowerCAmelCase_	)
								]
								lowerCAmelCase_	: Tuple      			=			torch.cat(lowerCAmelCase_	,				dim=0	).to(lowerCAmelCase_	)
				else:
								lowerCAmelCase_	: int      			=			torch.randn(lowerCAmelCase_	,				generator=lowerCAmelCase_	,				device=lowerCAmelCase_	,				dtype=lowerCAmelCase_	,				layout=lowerCAmelCase_	).to(lowerCAmelCase_	)

				return latents




def      lowerCAmelCase	(						lowerCAmelCase_	)->	Optional[Any]:
				if is_torch_version('''<'''	,				'''2.0.0'''	) or not hasattr(lowerCAmelCase_	,				'''_dynamo'''	):
								return False
				return isinstance(lowerCAmelCase_	,				torch._dynamo.eval_frame.OptimizedModule	)