-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate_table.py
More file actions
339 lines (272 loc) · 11.8 KB
/
Copy pathevaluate_table.py
File metadata and controls
339 lines (272 loc) · 11.8 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/env python3
"""
Evaluate LLM table-reading accuracy against images from generate_tables.py.
The model is asked to read every cell in the table and to compute the sum of
each row and each column. Three separate scores are reported:
cell_score — fraction of cells whose value was read correctly
row_sum_score — fraction of row sums computed correctly
col_sum_score — fraction of column sums computed correctly
overall — mean of the three scores above
Usage:
uv run evaluate_table.py config.toml
The [evaluation] table_dir in config.toml must point to a directory
produced by generate_tables.py (i.e. it must contain a validation.json with
a "grid" field in each record).
"""
import json
import re
import sys
import tomllib
from pathlib import Path
from llm_clients import get_client
# ---------------------------------------------------------------------------
# Config (mirrors evaluate.py)
# ---------------------------------------------------------------------------
def load_config(path: str | Path) -> dict:
with open(path, "rb") as f:
return tomllib.load(f)
def resolve_config(cfg: dict, config_path: Path) -> tuple[dict, dict, dict]:
model_cfg = cfg.get("model", {})
if "provider" not in model_cfg:
raise ValueError("config [model] section must include 'provider'.")
eval_cfg = cfg.get("evaluation", {})
if "table_dir" not in eval_cfg:
raise ValueError("config [evaluation] section must include 'table_dir'.")
table_dir = Path(eval_cfg["table_dir"])
if not table_dir.is_absolute():
table_dir = config_path.parent / table_dir
eval_cfg["table_dir"] = table_dir
output_cfg = cfg.get("output", {})
return model_cfg, eval_cfg, output_cfg
# ---------------------------------------------------------------------------
# Response parsing
# ---------------------------------------------------------------------------
def extract_json(text: str) -> dict:
"""Extract the outermost JSON object from a model response."""
text = re.sub(r"```(?:json)?\s*", "", text)
text = text.replace("```", "")
# Find the first '{' then use the json decoder to consume exactly one object
start = text.find("{")
if start == -1:
raise ValueError(f"No JSON object found in response: {text!r}")
decoder = json.JSONDecoder()
obj, _ = decoder.raw_decode(text, start)
return obj
def _to_number(v: object) -> float | None:
"""Parse v as float, covering both integer ('3') and decimal ('3.7') strings."""
try:
return float(v)
except (TypeError, ValueError):
return None
def normalise_grid(raw: object, rows: int, cols: int) -> list[list[float | None]]:
"""Coerce the model's grid to a 2-D list of numbers (None where unreadable)."""
grid: list[list[float | None]] = []
for r in range(rows):
row_vals: list[float | None] = []
for c in range(cols):
try:
val = _to_number(raw[r][c])
except (IndexError, TypeError):
val = None
row_vals.append(val)
grid.append(row_vals)
return grid
def normalise_sums(raw: object, length: int) -> list[float | None]:
result: list[float | None] = []
for i in range(length):
try:
result.append(_to_number(raw[i]))
except (IndexError, TypeError):
result.append(None)
return result
# ---------------------------------------------------------------------------
# Ground-truth computation
# ---------------------------------------------------------------------------
def ground_truth_from_grid(str_grid: list[list[str]]) -> tuple[list[list[float]], list[float], list[float]]:
"""Return (num_grid, row_sums, col_sums) derived from the string grid."""
rows = len(str_grid)
cols = len(str_grid[0]) if rows else 0
num_grid = [[float(v) for v in row] for row in str_grid]
row_sums = [round(sum(num_grid[r]), 10) for r in range(rows)]
col_sums = [round(sum(num_grid[r][c] for r in range(rows)), 10) for c in range(cols)]
return num_grid, row_sums, col_sums
# ---------------------------------------------------------------------------
# Scoring
# ---------------------------------------------------------------------------
def score_response(
predicted: dict,
actual_int_grid: list[list[float]],
actual_row_sums: list[float],
actual_col_sums: list[float],
rows: int,
cols: int,
) -> dict:
pred_grid = normalise_grid(predicted.get("grid"), rows, cols)
pred_row_sums = normalise_sums(predicted.get("row_sums"), rows)
pred_col_sums = normalise_sums(predicted.get("col_sums"), cols)
correct_cells = sum(
1 for r in range(rows) for c in range(cols)
if pred_grid[r][c] == actual_int_grid[r][c]
)
correct_row_sums = sum(
1 for r in range(rows) if pred_row_sums[r] == actual_row_sums[r]
)
correct_col_sums = sum(
1 for c in range(cols) if pred_col_sums[c] == actual_col_sums[c]
)
cell_score = round(correct_cells / (rows * cols), 4)
row_sum_score = round(correct_row_sums / rows, 4)
col_sum_score = round(correct_col_sums / cols, 4)
overall = round((cell_score + row_sum_score + col_sum_score) / 3, 4)
return {
"cell_score": cell_score,
"row_sum_score": row_sum_score,
"col_sum_score": col_sum_score,
"overall": overall,
"correct_cells": correct_cells,
"total_cells": rows * cols,
"correct_row_sums": correct_row_sums,
"correct_col_sums": correct_col_sums,
"predicted_grid": pred_grid,
"predicted_row_sums": pred_row_sums,
"predicted_col_sums": pred_col_sums,
}
# ---------------------------------------------------------------------------
# Prompt construction
# ---------------------------------------------------------------------------
def build_prompt(base_prompt: str, rows: int, cols: int) -> str:
"""Append structured-output instructions to the base config prompt."""
example_grid = [[f"r{r}c{c}" for c in range(cols)] for r in range(rows)]
example = {
"grid": example_grid,
"row_sums": [f"sum of row {r}" for r in range(rows)],
"col_sums": [f"sum of col {c}" for c in range(cols)],
}
suffix = (
f"\n\nThe table has {rows} rows and {cols} columns."
f"\n\nFor every cell read the integer shown. Then compute the sum of each"
f" row and the sum of each column."
f"\n\nReturn ONLY a valid JSON object in exactly this format"
f" (replace placeholders with integers):\n"
f"{json.dumps(example, indent=2)}"
)
return base_prompt.rstrip() + suffix
# ---------------------------------------------------------------------------
# Main evaluation loop
# ---------------------------------------------------------------------------
DEFAULT_BASE_PROMPT = (
"Look at this table carefully and read every number shown in each cell."
)
def evaluate(config_path: str | Path) -> None:
config_path = Path(config_path).resolve()
cfg = load_config(config_path)
model_cfg, eval_cfg, output_cfg = resolve_config(cfg, config_path)
table_dir: Path = eval_cfg["table_dir"]
base_prompt: str = eval_cfg.get("prompt_table", DEFAULT_BASE_PROMPT)
# Load and validate the validation file
validation_path = table_dir / "validation.json"
if not validation_path.exists():
print(f"Error: validation.json not found in {table_dir}")
sys.exit(1)
ground_truth: list[dict] = json.loads(validation_path.read_text())
if not ground_truth or "grid" not in ground_truth[0]:
print("Error: validation.json does not contain table grid data.")
print(" Run generate_tables.py to produce compatible images.")
sys.exit(1)
rows: int = ground_truth[0]["rows"]
cols: int = ground_truth[0]["cols"]
client = get_client(
provider=model_cfg["provider"],
model=model_cfg.get("name"),
api_key=model_cfg.get("api_key"),
temperature=model_cfg.get("temperature", 0.0),
max_tokens=model_cfg.get("max_tokens", 2048),
)
print(f"Provider : {client.provider_name}")
print(f"Model : {client.model}")
print(f"Directory: {table_dir}")
print(f"Grid : {rows} rows × {cols} cols ({rows * cols} cells)")
print(f"Images : {len(ground_truth)}\n")
full_prompt = build_prompt(base_prompt, rows, cols)
prompt_path = table_dir / "prompt_table.txt"
prompt_path.write_text(full_prompt, encoding="utf-8")
print(f"Prompt : {prompt_path}\n")
model_slug = re.sub(r"[^a-zA-Z0-9]+", "-", client.model).strip("-")
log_path = table_dir / f"log_table-{model_slug}.jsonl"
log_file = log_path.open("w", encoding="utf-8")
results: list[dict] = []
totals = {"cell_score": 0.0, "row_sum_score": 0.0, "col_sum_score": 0.0, "overall": 0.0}
evaluated = 0
for entry in ground_truth:
fname = entry["filename"]
image_path = table_dir / fname
if not image_path.exists():
print(f" SKIP {fname} (file not found)")
continue
actual_int_grid, actual_row_sums, actual_col_sums = ground_truth_from_grid(entry["grid"])
print(f" [{evaluated + 1}/{len(ground_truth)}] {fname} ...", end=" ", flush=True)
raw_text = ""
parse_error = None
predicted = {}
try:
response = client.analyze_image(image_path, full_prompt)
raw_text = response.text
predicted = extract_json(raw_text)
except Exception as exc:
parse_error = str(exc)
print(f"ERROR: {exc}")
log_file.write(json.dumps({
"filename": fname,
"raw_response": raw_text,
"predicted": predicted,
"actual_grid": entry["grid"],
"actual_row_sums": actual_row_sums,
"actual_col_sums": actual_col_sums,
"error": parse_error,
}) + "\n")
log_file.flush()
scores = score_response(predicted, actual_int_grid, actual_row_sums, actual_col_sums, rows, cols)
record: dict = {
"filename": fname,
"actual_grid": entry["grid"],
"actual_row_sums": actual_row_sums,
"actual_col_sums": actual_col_sums,
}
record.update(scores)
results.append(record)
for k in totals:
totals[k] += scores[k]
evaluated += 1
if parse_error is None:
print(
f"cells={scores['cell_score']:.2f} "
f"row_sums={scores['row_sum_score']:.2f} "
f"col_sums={scores['col_sum_score']:.2f} "
f"overall={scores['overall']:.2f}"
)
# Summary
if evaluated > 0:
summary: dict = {"filename": "__summary__", "total_images": evaluated}
for k in totals:
summary[k] = round(totals[k] / evaluated, 4)
results.append(summary)
print(f"\nSummary:")
print(f" {'cell_score':20s} {summary['cell_score']:.4f}")
print(f" {'row_sum_score':20s} {summary['row_sum_score']:.4f}")
print(f" {'col_sum_score':20s} {summary['col_sum_score']:.4f}")
print(f" {'overall':20s} {summary['overall']:.4f}")
log_file.close()
print(f"\nLog file : {log_path}")
score_path_default = table_dir / f"score_table-{model_slug}.json"
score_path = Path(output_cfg.get("score_file", score_path_default))
if not score_path.is_absolute():
score_path = table_dir / score_path
score_path.write_text(json.dumps(results, indent=2))
print(f"Score file: {score_path}")
def main() -> None:
if len(sys.argv) != 2:
print("Usage: uv run evaluate_table.py <config.toml>")
sys.exit(1)
evaluate(sys.argv[1])
if __name__ == "__main__":
main()