diff --git a/aai_cli/commands/evaluate/_data.py b/aai_cli/commands/evaluate/_data.py index dc974d30..4bbea070 100644 --- a/aai_cli/commands/evaluate/_data.py +++ b/aai_cli/commands/evaluate/_data.py @@ -177,7 +177,7 @@ def _row_reference(cells: dict[str, object], text_col: str, item_id: str) -> str def _manifest_rows(path: Path) -> list[dict[str, object]]: if path.suffix == ".csv": - with path.open(newline="", encoding="utf-8") as handle: + with path.open(newline="", encoding="utf-8-sig") as handle: return [dict(row) for row in csv.DictReader(handle)] rows: list[dict[str, object]] = [] for lineno, line in enumerate(path.read_text(encoding="utf-8").splitlines(), start=1): diff --git a/tests/test_eval_csv_encoding.py b/tests/test_eval_csv_encoding.py new file mode 100644 index 00000000..2f833d54 --- /dev/null +++ b/tests/test_eval_csv_encoding.py @@ -0,0 +1,54 @@ +"""BOM and ordinary UTF-8 CSVs have identical column and scoring semantics.""" + +from __future__ import annotations + +import wave +from types import SimpleNamespace + +import pytest +from typer.testing import CliRunner + +from aai_cli.commands.evaluate import _data as eval_data +from aai_cli.core import client, config +from aai_cli.main import app + + +@pytest.mark.parametrize("bom", [b"", b"\xef\xbb\xbf"]) +@pytest.mark.parametrize("explicit", [False, True]) +@pytest.mark.parametrize("newline", ["\n", "\r\n"]) +def test_csv_encoding_preserves_columns_and_wer(tmp_path, monkeypatch, bom, explicit, newline): + audio = tmp_path / "call.wav" + with wave.open(str(audio), "wb") as handle: + handle.setnchannels(1) + handle.setsampwidth(2) + handle.setframerate(16000) + handle.writeframes(b"\0\0" * 160) + manifest = tmp_path / "calls.csv" + manifest.write_bytes( + bom + f'"audio","text"{newline}"call.wav","héllo, world"{newline}'.encode() + ) + data = eval_data.load( + str(manifest), + audio_column="audio" if explicit else None, + text_column="text" if explicit else None, + limit=10, + ) + assert data.items == [eval_data.EvalItem("call.wav", str(audio), "héllo, world")] + monkeypatch.setattr(config, "resolve_api_key", lambda **_: "test-key") + calls = [] + + def transcribe(api_key, source, *, config): + calls.append(source) + return SimpleNamespace(text="héllo world") + + monkeypatch.setattr(client, "transcribe", transcribe) + args = ["eval", str(manifest), "--json"] + if explicit: + args += ["--audio-column", "audio", "--text-column", "text"] + result = CliRunner().invoke(app, args) + assert result.exit_code == 0, result.output + assert calls == [str(audio)] + assert '"words": 2' in result.output + assert '"errors": 0' in result.output + assert '"wer": 0.0' in result.output + assert "\ufeff" not in result.output