Upload metrics.py with huggingface_hub
Browse files219 unmodified lines220221222223224225226227228229230231232140 unmodified lines3733743753763773783791 unmodified line38138238338438538638738840 unmodified lines429430431432433434219 unmodified linesmetric_name: str = Nonemain_score: str = Nonescale: float = 1.0def prepare(self):super().prepare()self.metric = evaluate.load(self.metric_name)def compute(self, references: List[List[str]], predictions: List[str]) -> dict:result = self.metric.compute(predictions=predictions, references=references)if self.scale != 1.0:for key in result:if isinstance(result[key], float):140 unmodified linesmain_score = "rougeL"scale = 1.0def prepare(self):super().prepare()import nltk1 unmodified lineself.sent_tokenize = nltk.sent_tokenizedef compute(self, references, predictions):predictions = ["\n".join(self.sent_tokenize(prediction.strip())) for prediction in predictions]references = [["\n".join(self.sent_tokenize(r.strip())) for r in reference] for reference in references]return super().compute(references, predictions)40 unmodified linesscale = 1.0class MatthewsCorrelation(HuggingfaceMetric):metric_name = "matthews_correlation"main_score = "matthews_correlation"219 unmodified lines220221222223224225226227228229230231232233140 unmodified lines3743753763773783793803813823833843853863871 unmodified line38939039139239339439539639740 unmodified lines438439440441442443444445446447448449219 unmodified linesmetric_name: str = Nonemain_score: str = Nonescale: float = 1.0hf_compute_args: dict = {}def prepare(self):super().prepare()self.metric = evaluate.load(self.metric_name)def compute(self, references: List[List[str]], predictions: List[str]) -> dict:result = self.metric.compute(predictions=predictions, references=references, **self.hf_compute_args)if self.scale != 1.0:for key in result:if isinstance(result[key], float):140 unmodified linesmain_score = "rougeL"scale = 1.0use_aggregator: bool = Truerouge_types: List[str] = ["rouge1", "rouge2", "rougeL", "rougeLsum"]sent_split_newline: bool = Truedef prepare(self):self.hf_compute_args = {"use_aggregator": self.use_aggregator, "rouge_types": self.rouge_types}super().prepare()import nltk1 unmodified lineself.sent_tokenize = nltk.sent_tokenizedef compute(self, references, predictions):if self.sent_split_newline:predictions = ["\n".join(self.sent_tokenize(prediction.strip())) for prediction in predictions]references = [["\n".join(self.sent_tokenize(r.strip())) for r in reference] for reference in references]return super().compute(references, predictions)40 unmodified linesscale = 1.0class SacreBleu(HuggingfaceMetric):metric_name = "sacrebleu"main_score = "score"scale = 1.0class MatthewsCorrelation(HuggingfaceMetric):metric_name = "matthews_correlation"main_score = "matthews_correlation"
metrics.py+18-3