-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_common.py
More file actions
101 lines (81 loc) · 2.95 KB
/
Copy path_common.py
File metadata and controls
101 lines (81 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""Shared helpers for benchmark evaluation scripts."""
from __future__ import annotations
import json
import logging
import tempfile
from dataclasses import dataclass
from pathlib import Path
from typing import Callable, Iterable, List, Optional
from audiotoolagent.agent import AudioToolAgent, console_stream_callback
logger = logging.getLogger(__name__)
@dataclass
class PreparedSample:
sample_id: str
audio_path: str
question: str
options: List[str]
answer: Optional[str]
metadata: Optional[dict] = None
PrepareFn = Callable[[dict, str], PreparedSample]
def run_benchmark(
*,
config_path: str,
raw_samples: Iterable[dict],
prepare_fn: PrepareFn,
limit: Optional[int] = None,
stream: bool = True,
output_path: Optional[str] = None,
) -> dict:
"""Evaluate a stream of raw dataset entries against the agent."""
agent = AudioToolAgent(config_path)
stream_cb = console_stream_callback if stream else None
totals = {
"count": 0,
"correct": 0,
"results": [],
}
with tempfile.TemporaryDirectory(prefix="audio_eval_") as work_dir:
for idx, raw in enumerate(raw_samples):
if limit is not None and idx >= limit:
break
prepared = prepare_fn(raw, work_dir)
logger.info("Processing sample %s", prepared.sample_id)
result = agent.process(
audio_path=prepared.audio_path,
question=prepared.question,
options=prepared.options or None,
stream_callback=stream_cb,
)
predicted = result.get("selected_option") or result.get("answer", "")
is_correct = bool(prepared.answer) and predicted and prepared.answer.strip() == predicted.strip()
totals["count"] += 1
totals["correct"] += int(is_correct)
result_payload = {
"id": prepared.sample_id,
"question": prepared.question,
"options": prepared.options,
"ground_truth": prepared.answer,
"prediction": predicted,
"raw_answer": result.get("answer"),
"metadata": prepared.metadata or {},
"correct": is_correct,
}
totals["results"].append(result_payload)
accuracy = (totals["correct"] / totals["count"]) if totals["count"] else 0.0
summary = {
"config": config_path,
"samples": totals["count"],
"correct": totals["correct"],
"accuracy": accuracy,
}
logger.info("Evaluation complete: %s", summary)
if output_path:
out_path = Path(output_path)
out_path.parent.mkdir(parents=True, exist_ok=True)
data = {
"summary": summary,
"results": totals["results"],
}
out_path.write_text(json.dumps(data, indent=2), encoding="utf-8")
logger.info("Saved detailed results to %s", out_path)
return summary