# Helpers for evals_gguf task yamls (lm_eval imports this file by path; ifeval_fr reuses the stock IFEval scorer). import re, string, collections from lm_eval.tasks.ifeval.utils import process_results as ifeval_process_results, agg_inst_level_acc # noqa: F401 def cap2000(ds): # big dev splits: first 2000 rows, file order (fixed, reproducible) return ds.select(range(min(2000, len(ds)))) def fracas_docs(ds): # clean 3-way gold only (drops the "Yes, on one reading" style notes) return ds.filter(lambda d: d["answer"] in ("yes", "no", "unknown")) def _norm(s): # SQuAD-style normalisation, French articles s = s.lower() s = "".join(c for c in s if c not in set(string.punctuation) | {"«", "»", "’"}) s = re.sub(r"\b(le|la|les|l|un|une|des|du|de|d)\b", " ", s) return " ".join(s.split()) def _f1(p, g): p, g = _norm(p).split(), _norm(g).split() common = sum((collections.Counter(p) & collections.Counter(g)).values()) if not p or not g: return float(p == g) if common == 0: return 0.0 pr, rc = common / len(p), common / len(g) return 2 * pr * rc / (pr + rc) def qa_results(doc, results): pred = results[0].strip().split("\n")[0] golds = doc["answers"]["text"] return {"exact_match": max(float(_norm(pred) == _norm(g)) for g in golds), "f1": max(_f1(pred, g) for g in golds)} def has_answer(ds): # extractive QA: drop rows with no gold span return ds.filter(lambda d: len(d["answers"]["text"]) > 0) # MGSM (human-translated GSM8K): numeric answer extraction that reads French and English number formats. Stock # strict-match wants "The answer is N" / "La réponse est N", which chat models rarely write, and flexible-extract # breaks on "70 000 $". Order: the number after a final-answer cue, else the last bold number, else the last number. import re as _re _NUM = r"-?\d[\d\s  .,]*" _CUE = _re.compile(r"(?:r[ée]ponse finale|final answer|r[ée]ponse|answer|la r[ée]ponse est|the answer is|conclusion)\s*\**\s*:", _re.I) def _to_num(s, lang): s = _re.sub(r"[\s\u00a0\u202f]", "", s).rstrip(".,") if lang == "fr": s = s.replace(".", "").replace(",", ".") if s.count(",") <= 1 else s.replace(",", "") else: s = s.replace(",", "") try: return float(s) except ValueError: return None def _first_num(part, lang, last=False): bold = [b for b in _re.findall(r"\*\*([^*\n]{1,60}?)\*\*", part) if _re.search(r"\d", b)] nums = _re.findall(_NUM, (bold[-1] if last else bold[0]) if bold else part) for n in (nums[-1:] if last or not bold else nums[:1]) if nums else []: return _to_num(n, lang) return None def _extract(text, lang): cues = list(_CUE.finditer(text)) if cues: # the last final-answer cue: its first (bold) number v = _first_num(text[cues[-1].end():], lang) if v is not None: return v return _first_num(text, lang, last=True) # else the last bold number, else the last number def _mgsm(doc, results, lang): v = _extract(results[0], lang) return {"exact_match": float(v is not None and abs(v - float(doc["answer_number"])) < 1e-6)} def mgsm_en(doc, results): return _mgsm(doc, results, "en") def mgsm_fr(doc, results): return _mgsm(doc, results, "fr")