|
| 1 | +# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import json |
| 16 | +import logging |
| 17 | +import shutil |
| 18 | +import subprocess |
| 19 | +from pathlib import Path |
| 20 | + |
| 21 | +from nemo_skills.evaluation.evaluator.base import BaseEvaluatorConfig |
| 22 | +from nemo_skills.utils import get_logger_name |
| 23 | + |
| 24 | +LOG = logging.getLogger(get_logger_name(__file__)) |
| 25 | + |
| 26 | + |
| 27 | +def eval_koif(cfg): |
| 28 | + cfg = BaseEvaluatorConfig(**cfg) |
| 29 | + jsonl_file = cfg.input_file |
| 30 | + jsonl_path = Path(jsonl_file).resolve() |
| 31 | + output_dir = jsonl_path.parent / f"{jsonl_path.stem}_metrics_tmp" |
| 32 | + output_dir.mkdir(parents=True, exist_ok=True) |
| 33 | + cmd = ( |
| 34 | + "python -m koifeval.evaluation_main " |
| 35 | + f"--input_data={jsonl_file} " |
| 36 | + f"--input_response_data={jsonl_file} " |
| 37 | + f"--output_dir={output_dir} " |
| 38 | + ) |
| 39 | + subprocess.run(cmd, shell=True, check=True) |
| 40 | + # fusing eval metrics back into the generation file |
| 41 | + with open(jsonl_file, "rt", encoding="utf-8") as f: |
| 42 | + samples = [json.loads(line) for line in f] |
| 43 | + |
| 44 | + with open(output_dir / "eval_results_loose.jsonl", "rt", encoding="utf-8") as f: |
| 45 | + eval_results = [json.loads(line) for line in f] |
| 46 | + for sample, eval_result in zip(samples, eval_results): |
| 47 | + sample["loose_eval"] = eval_result |
| 48 | + |
| 49 | + with open(output_dir / "eval_results_strict.jsonl", "rt", encoding="utf-8") as f: |
| 50 | + eval_results = [json.loads(line) for line in f] |
| 51 | + for sample, eval_result in zip(samples, eval_results): |
| 52 | + sample["strict_eval"] = eval_result |
| 53 | + |
| 54 | + with open(jsonl_file, "wt", encoding="utf-8") as f: |
| 55 | + for sample in samples: |
| 56 | + f.write(json.dumps(sample) + "\n") |
| 57 | + |
| 58 | + # removing temporary metric directory to avoid reusing it |
| 59 | + shutil.rmtree(output_dir) |
0 commit comments