-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_executor.py
More file actions
771 lines (643 loc) · 32.3 KB
/
Copy pathtest_executor.py
File metadata and controls
771 lines (643 loc) · 32.3 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
import os
import json
import time
from datetime import datetime
from typing import Dict, List, Any, Optional
import logging
import traceback
from client_factory import ClientFactory
from evaluators.evaluator_factory import EvaluatorFactory
from utils.logger import get_logger
from utils.file_utils import ensure_directory_exists, load_yaml, save_yaml, save_json
logger = get_logger(__name__)
class TestExecutor:
"""
Main class responsible for executing tests across multiple models and test cases.
"""
def __init__(self, test_config_path: str, output_dir: str, log_level: str = "INFO"):
"""
Initialize the test executor.
Args:
test_config_path: Path to the test configuration file
output_dir: Directory to store test results
log_level: Logging level
"""
self.test_config_path = test_config_path
self.output_dir = output_dir
self.log_level = log_level
# Load test configuration
self.test_config = load_yaml(test_config_path)
if not self.test_config:
raise ValueError(f"Failed to load test configuration from {test_config_path}")
# Initialize empty containers
self.test_cases = {}
self.clients = {}
self.results = {}
# Create output directory
ensure_directory_exists(output_dir)
# Create run-specific directory with timestamp
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
self.run_dir = os.path.join(output_dir, f"run_{timestamp}")
ensure_directory_exists(self.run_dir)
# Save a copy of the configuration
# Fix: Swap the parameter order - file path first, then data
save_yaml(os.path.join(self.run_dir, "config.yaml"), self.test_config)
logger.info(f"Test executor initialized with config: {test_config_path}")
logger.info(f"Results will be stored in: {self.run_dir}")
def load_test_cases(self):
"""
Load test cases from the test_cases directory based on configuration.
"""
test_cases_dir = "test_cases"
categories = self.test_config.get("categories", [])
if not categories:
# If no categories specified, use all available
categories = [d for d in os.listdir(test_cases_dir)
if os.path.isdir(os.path.join(test_cases_dir, d))]
logger.info(f"Loading test cases for categories: {categories}")
for category in categories:
category_dir = os.path.join(test_cases_dir, category)
if not os.path.exists(category_dir):
logger.warning(f"Category directory not found: {category_dir}")
continue
self.test_cases[category] = {}
test_files = [f for f in os.listdir(category_dir) if f.endswith('.json')]
for test_file in test_files:
test_path = os.path.join(category_dir, test_file)
test_name = test_file[:-5] # Remove .json extension
try:
with open(test_path, 'r') as f:
test_case = json.load(f)
self.test_cases[category][test_name] = test_case
logger.debug(f"Loaded test case: {category}/{test_name}")
except Exception as e:
logger.error(f"Error loading test case {test_path}: {e}")
total_tests = sum(len(tests) for tests in self.test_cases.values())
logger.info(f"Loaded {total_tests} test cases across {len(self.test_cases)} categories")
def initialize_clients(self):
"""
Initialize model clients based on configuration.
"""
factory = ClientFactory()
selected_models = self.test_config.get("selected_models", [])
if not selected_models:
# If no models specified, use all available
selected_models = factory.get_available_models()
logger.info(f"Initializing clients for models: {selected_models}")
for model_name in selected_models:
try:
client = factory.get_client(model_name)
self.clients[model_name] = client
logger.debug(f"Initialized client for model: {model_name}")
except Exception as e:
logger.error(f"Failed to initialize client for model {model_name}: {e}")
logger.error(traceback.format_exc())
logger.info(f"Initialized {len(self.clients)} model clients")
def execute_tests(self):
"""
Execute all test cases across all models.
"""
if not self.test_cases:
raise ValueError("No test cases loaded. Call load_test_cases() first.")
if not self.clients:
raise ValueError("No model clients initialized. Call initialize_clients() first.")
# Initialize results structure
for model_name in self.clients.keys():
self.results[model_name] = {}
for category in self.test_cases.keys():
self.results[model_name][category] = {}
# Create evaluator factory
evaluator_factory = EvaluatorFactory()
# Track execution statistics - UPDATED to handle multiple prompts per test
total_prompt_executions = 0
for category_tests in self.test_cases.values():
for test_case in category_tests.values():
prompts = test_case.get("prompts", [])
if not prompts and test_case.get("prompt"):
prompts = [{"text": test_case.get("prompt"), "id": "default"}]
total_prompt_executions += len(prompts) if prompts else 1
total_models = len(self.clients)
total_executions = total_prompt_executions * total_models
completed = 0
start_time = time.time()
logger.info(f"Starting test execution: {total_executions} total executions "
f"({total_prompt_executions} prompt tests × {total_models} models)")
# Execute tests for each model and category
for category, tests in self.test_cases.items():
# Get the appropriate evaluator for this category
evaluator = evaluator_factory.get_evaluator(category)
if not evaluator:
logger.warning(f"No evaluator found for category: {category}, skipping")
continue
category_dir = os.path.join(self.run_dir, category)
ensure_directory_exists(category_dir)
for test_name, test_case in tests.items():
test_dir = os.path.join(category_dir, test_name)
ensure_directory_exists(test_dir)
# Save the test case for reference
save_json(os.path.join(test_dir, "test_case.json"), test_case)
# Handle multiple prompts within a test case - FIXED PROMPT EXTRACTION
prompts = test_case.get("prompts", [])
# If no prompts array, try to get direct prompt (legacy format)
if not prompts:
direct_prompt = test_case.get("prompt", "")
if direct_prompt:
prompts = [{"text": direct_prompt, "id": "default"}]
if not prompts:
logger.error(f"No prompts found in test case {category}/{test_name} - skipping")
continue
for prompt_idx, prompt_data in enumerate(prompts):
# Extract prompt information - FIXED
if isinstance(prompt_data, dict):
prompt = prompt_data.get("text", prompt_data.get("prompt", ""))
context = prompt_data.get("context", "")
parameters = prompt_data.get("parameters", {})
prompt_id = prompt_data.get("id", f"prompt_{prompt_idx}")
else:
# Handle legacy format where prompt_data might be a string
prompt = str(prompt_data)
context = ""
parameters = {}
prompt_id = f"prompt_{prompt_idx}"
# Safety check for empty prompts - ADDED
if not prompt or len(prompt.strip()) == 0:
logger.error(f"Empty prompt for test {category}/{test_name}, prompt {prompt_id} - skipping")
continue
test_identifier = f"{category}/{test_name}"
if len(prompts) > 1:
test_identifier += f"__{prompt_id}"
for model_name, client in self.clients.items():
model_dir = os.path.join(test_dir, model_name)
ensure_directory_exists(model_dir)
logger.info(f"Executing test: {test_identifier} with model: {model_name}")
logger.debug(f"Prompt preview: {prompt[:100]}...")
try:
# Record start time
test_start_time = time.time()
# Get response from model
response = client.generate(prompt, context, parameters)
# Record end time and calculate duration
test_end_time = time.time()
duration = test_end_time - test_start_time
# Prepare data for evaluation - FIXED EVALUATOR INTERFACE
response_data = {
"content": response,
"success": True,
"model": model_name,
"provider": getattr(client, "config", {}).get("provider", "unknown")
}
evaluation_test_case = {
"prompt": prompt,
"context": context,
"parameters": parameters,
"expected_elements": test_case.get("expected_elements", []),
"evaluation_criteria": test_case.get("evaluation_criteria", {}),
"reference_data": test_case.get("reference_data", {}),
"reference_facts": test_case.get("reference_facts", {})
}
# Evaluate the response - FIXED PARAMETER ORDER
evaluation = evaluator.evaluate(response_data, evaluation_test_case)
# Add metadata
result = {
"test_case": evaluation_test_case,
"response": response,
"evaluation": evaluation,
"metadata": {
"model": model_name,
"category": category,
"test_name": test_name,
"prompt_id": prompt_id,
"prompt_index": prompt_idx,
"timestamp": datetime.now().isoformat(),
"duration": duration
}
}
# Save result - UPDATED for multiple prompts
result_filename = f"result__{prompt_id}.json" if len(prompts) > 1 else "result.json"
save_json(os.path.join(model_dir, result_filename), result)
# Store in results dictionary - UPDATED for multiple prompts
result_key = f"{test_name}__{prompt_id}" if len(prompts) > 1 else test_name
self.results[model_name][category][result_key] = result
score = evaluation.get("score", "N/A")
logger.info(f"Test completed: {test_identifier} with model: {model_name}")
logger.info(f"Score: {score}, Duration: {duration:.2f}s")
except Exception as e:
logger.error(f"Error executing test {test_identifier} with model {model_name}: {e}")
logger.error(traceback.format_exc())
# Record failure
error_result = {
"test_case": {
"prompt": prompt,
"context": context,
"parameters": parameters
},
"error": str(e),
"traceback": traceback.format_exc(),
"metadata": {
"model": model_name,
"category": category,
"test_name": test_name,
"prompt_id": prompt_id,
"prompt_index": prompt_idx,
"timestamp": datetime.now().isoformat(),
"status": "failed"
}
}
# Save error result - UPDATED for multiple prompts
error_filename = f"error__{prompt_id}.json" if len(prompts) > 1 else "error.json"
save_json(os.path.join(model_dir, error_filename), error_result)
# Store in results dictionary - UPDATED for multiple prompts
result_key = f"{test_name}__{prompt_id}" if len(prompts) > 1 else test_name
self.results[model_name][category][result_key] = error_result
# Update progress
completed += 1
elapsed = time.time() - start_time
if completed > 0:
avg_time = elapsed / completed
remaining = avg_time * (total_executions - completed)
else:
remaining = 0
logger.info(f"Progress: {completed}/{total_executions} "
f"({completed/total_executions*100:.1f}%) - "
f"Elapsed: {elapsed:.1f}s, Remaining: {remaining:.1f}s")
# Save complete results
save_json(os.path.join(self.run_dir, "all_results.json"), self.results)
total_time = time.time() - start_time
logger.info(f"Test execution completed: {completed}/{total_executions} tests in {total_time:.1f}s")
return self.results
def generate_report(self):
"""
Generate a comprehensive report from test results.
Returns:
Path to the generated report
"""
if not self.results:
raise ValueError("No test results available. Execute tests first.")
logger.info("Generating test report...")
# Create report directory
report_dir = os.path.join(self.run_dir, "report")
ensure_directory_exists(report_dir)
# Aggregate results
aggregated_results = self._aggregate_results()
# Save aggregated results
# Fix: Swap parameter order - file path first, then data
save_json(os.path.join(report_dir, "aggregated_results.json"), aggregated_results)
# Generate summary report
summary = self._generate_summary(aggregated_results)
# Fix: Swap parameter order - file path first, then data
save_json(os.path.join(report_dir, "summary.json"), summary)
# Generate HTML report
html_report_path = os.path.join(report_dir, "report.html")
self._generate_html_report(summary, aggregated_results, html_report_path)
logger.info(f"Report generated at: {report_dir}")
return report_dir
def _aggregate_results(self):
"""
Aggregate test results for reporting.
Returns:
Dictionary with aggregated results
"""
aggregated = {
"models": {},
"categories": {},
"overall": {
"total_tests": 0,
"total_passed": 0,
"total_failed": 0,
"average_score": 0,
"average_duration": 0
}
}
total_score = 0
total_duration = 0
test_count = 0
# Aggregate by model and category
for model_name, model_results in self.results.items():
if model_name not in aggregated["models"]:
aggregated["models"][model_name] = {
"total_tests": 0,
"passed": 0,
"failed": 0,
"average_score": 0,
"average_duration": 0,
"categories": {}
}
model_total_score = 0
model_total_duration = 0
model_test_count = 0
for category, category_results in model_results.items():
if category not in aggregated["categories"]:
aggregated["categories"][category] = {
"total_tests": 0,
"passed": 0,
"failed": 0,
"average_score": 0,
"average_duration": 0,
"models": {}
}
if category not in aggregated["models"][model_name]["categories"]:
aggregated["models"][model_name]["categories"][category] = {
"total_tests": 0,
"passed": 0,
"failed": 0,
"average_score": 0,
"average_duration": 0
}
if model_name not in aggregated["categories"][category]["models"]:
aggregated["categories"][category]["models"][model_name] = {
"total_tests": 0,
"passed": 0,
"failed": 0,
"average_score": 0,
"average_duration": 0
}
category_total_score = 0
category_total_duration = 0
category_test_count = 0
for test_name, result in category_results.items():
# Check if test failed
failed = "error" in result
# Get score and duration
score = 0
duration = 0
if not failed:
evaluation = result.get("evaluation", {})
score = evaluation.get("score", 0)
metadata = result.get("metadata", {})
duration = metadata.get("duration", 0)
# Update counts
aggregated["models"][model_name]["total_tests"] += 1
aggregated["categories"][category]["total_tests"] += 1
aggregated["models"][model_name]["categories"][category]["total_tests"] += 1
aggregated["categories"][category]["models"][model_name]["total_tests"] += 1
aggregated["overall"]["total_tests"] += 1
if failed:
aggregated["models"][model_name]["failed"] += 1
aggregated["categories"][category]["failed"] += 1
aggregated["models"][model_name]["categories"][category]["failed"] += 1
aggregated["categories"][category]["models"][model_name]["failed"] += 1
aggregated["overall"]["total_failed"] += 1
else:
aggregated["models"][model_name]["passed"] += 1
aggregated["categories"][category]["passed"] += 1
aggregated["models"][model_name]["categories"][category]["passed"] += 1
aggregated["categories"][category]["models"][model_name]["passed"] += 1
aggregated["overall"]["total_passed"] += 1
# Update scores and durations
category_total_score += score
category_total_duration += duration
category_test_count += 1
model_total_score += score
model_total_duration += duration
model_test_count += 1
total_score += score
total_duration += duration
test_count += 1
# Calculate averages for this model/category combination
if category_test_count > 0:
avg_score = category_total_score / category_test_count
avg_duration = category_total_duration / category_test_count
aggregated["models"][model_name]["categories"][category]["average_score"] = avg_score
aggregated["models"][model_name]["categories"][category]["average_duration"] = avg_duration
aggregated["categories"][category]["models"][model_name]["average_score"] = avg_score
aggregated["categories"][category]["models"][model_name]["average_duration"] = avg_duration
# Calculate averages for this model
if model_test_count > 0:
aggregated["models"][model_name]["average_score"] = model_total_score / model_test_count
aggregated["models"][model_name]["average_duration"] = model_total_duration / model_test_count
# Calculate category averages
for category, category_data in aggregated["categories"].items():
category_score = 0
category_duration = 0
category_count = 0
for model_name, model_data in category_data["models"].items():
if model_data["passed"] > 0:
category_score += model_data["average_score"] * model_data["passed"]
category_duration += model_data["average_duration"] * model_data["passed"]
category_count += model_data["passed"]
if category_count > 0:
aggregated["categories"][category]["average_score"] = category_score / category_count
aggregated["categories"][category]["average_duration"] = category_duration / category_count
# Calculate overall averages
if test_count > 0:
aggregated["overall"]["average_score"] = total_score / test_count
aggregated["overall"]["average_duration"] = total_duration / test_count
return aggregated
def _generate_summary(self, aggregated_results):
"""
Generate a summary of test results.
Args:
aggregated_results: Aggregated test results
Returns:
Dictionary with summary information
"""
summary = {
"timestamp": datetime.now().isoformat(),
"config": self.test_config,
"overall": aggregated_results["overall"],
"model_rankings": [],
"category_rankings": [],
"model_category_matrix": {}
}
# Create model rankings
models = []
for model_name, model_data in aggregated_results["models"].items():
models.append({
"name": model_name,
"average_score": model_data["average_score"],
"average_duration": model_data["average_duration"],
"passed": model_data["passed"],
"failed": model_data["failed"],
"total": model_data["total_tests"]
})
# Sort by average score (descending)
summary["model_rankings"] = sorted(models, key=lambda x: x["average_score"], reverse=True)
# Create category rankings
categories = []
for category_name, category_data in aggregated_results["categories"].items():
categories.append({
"name": category_name,
"average_score": category_data["average_score"],
"average_duration": category_data["average_duration"],
"passed": category_data["passed"],
"failed": category_data["failed"],
"total": category_data["total_tests"]
})
# Sort by name
summary["category_rankings"] = sorted(categories, key=lambda x: x["name"])
# Create model/category matrix
for model_name, model_data in aggregated_results["models"].items():
summary["model_category_matrix"][model_name] = {}
for category_name, category_data in aggregated_results["categories"].items():
if category_name in model_data["categories"]:
model_category = model_data["categories"][category_name]
summary["model_category_matrix"][model_name][category_name] = {
"average_score": model_category["average_score"],
"passed": model_category["passed"],
"failed": model_category["failed"]
}
else:
summary["model_category_matrix"][model_name][category_name] = {
"average_score": 0,
"passed": 0,
"failed": 0
}
return summary
def _generate_html_report(self, summary, aggregated_results, output_path):
"""
Generate an HTML report from the test results.
Args:
summary: Summary information
aggregated_results: Aggregated test results
output_path: Path to save the HTML report
"""
# Simple HTML report template - FIXED CSS escaping
html_template = """
<!DOCTYPE html>
<html>
<head>
<title>Foundation Model Test Report</title>
<style>
body {{ font-family: Arial, sans-serif; margin: 20px; }}
h1, h2, h3 {{ color: #333; }}
table {{ border-collapse: collapse; width: 100%; margin-bottom: 20px; }}
th, td {{ border: 1px solid #ddd; padding: 8px; text-align: left; }}
th {{ background-color: #f2f2f2; }}
tr:nth-child(even) {{ background-color: #f9f9f9; }}
.score-high {{ color: green; }}
.score-medium {{ color: orange; }}
.score-low {{ color: red; }}
.summary-box {{ background-color: #f5f5f5; padding: 15px; border-radius: 5px; margin-bottom: 20px; }}
</style>
</head>
<body>
<h1>Foundation Model Test Report</h1>
<div class="summary-box">
<h2>Summary</h2>
<p>Timestamp: {timestamp}</p>
<p>Total Tests: {total_tests}</p>
<p>Passed: {passed} ({pass_rate:.1f}%)</p>
<p>Failed: {failed}</p>
<p>Average Score: {avg_score:.2f}</p>
<p>Average Duration: {avg_duration:.2f}s</p>
</div>
<h2>Model Rankings</h2>
<table>
<tr>
<th>Rank</th>
<th>Model</th>
<th>Average Score</th>
<th>Pass Rate</th>
<th>Average Duration</th>
</tr>
{model_rows}
</table>
<h2>Category Performance</h2>
<table>
<tr>
<th>Category</th>
<th>Average Score</th>
<th>Pass Rate</th>
<th>Best Model</th>
</tr>
{category_rows}
</table>
<h2>Model-Category Matrix</h2>
<table>
<tr>
<th>Model / Category</th>
{category_headers}
</tr>
{matrix_rows}
</table>
</body>
</html>
"""
# Format timestamp
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Get overall stats
total_tests = summary["overall"]["total_tests"]
passed = summary["overall"]["total_passed"]
failed = summary["overall"]["total_failed"]
pass_rate = (passed / total_tests * 100) if total_tests > 0 else 0
avg_score = summary["overall"]["average_score"]
avg_duration = summary["overall"]["average_duration"]
# Generate model ranking rows
model_rows = ""
for i, model in enumerate(summary["model_rankings"]):
model_pass_rate = (model["passed"] / model["total"] * 100) if model["total"] > 0 else 0
score_class = "score-high" if model["average_score"] >= 0.8 else "score-medium" if model["average_score"] >= 0.5 else "score-low"
model_rows += f"""
<tr>
<td>{i+1}</td>
<td>{model["name"]}</td>
<td class="{score_class}">{model["average_score"]:.2f}</td>
<td>{model_pass_rate:.1f}% ({model["passed"]}/{model["total"]})</td>
<td>{model["average_duration"]:.2f}s</td>
</tr>
"""
# Generate category rows
category_rows = ""
for category in summary["category_rankings"]:
category_pass_rate = (category["passed"] / category["total"] * 100) if category["total"] > 0 else 0
score_class = "score-high" if category["average_score"] >= 0.8 else "score-medium" if category["average_score"] >= 0.5 else "score-low"
# Find best model for this category
best_model = "N/A"
best_score = 0
for model_name, categories in summary["model_category_matrix"].items():
if category["name"] in categories:
model_category_score = categories[category["name"]]["average_score"]
if model_category_score > best_score:
best_score = model_category_score
best_model = model_name
category_rows += f"""
<tr>
<td>{category["name"]}</td>
<td class="{score_class}">{category["average_score"]:.2f}</td>
<td>{category_pass_rate:.1f}% ({category["passed"]}/{category["total"]})</td>
<td>{best_model} ({best_score:.2f})</td>
</tr>
"""
# Generate category headers for matrix
category_headers = ""
for category in summary["category_rankings"]:
category_headers += f"<th>{category['name']}</th>"
# Generate matrix rows
matrix_rows = ""
for model in summary["model_rankings"]:
model_name = model["name"]
row = f"<tr><td>{model_name}</td>"
for category in summary["category_rankings"]:
category_name = category["name"]
if category_name in summary["model_category_matrix"][model_name]:
cell_data = summary["model_category_matrix"][model_name][category_name]
score = cell_data["average_score"]
score_class = "score-high" if score >= 0.8 else "score-medium" if score >= 0.5 else "score-low"
row += f"""
<td class="{score_class}">
{score:.2f}<br>
({cell_data["passed"]}/{cell_data["passed"] + cell_data["failed"]})
</td>
"""
else:
row += "<td>N/A</td>"
row += "</tr>"
matrix_rows += row
# Fill in the template
html_content = html_template.format(
timestamp=timestamp,
total_tests=total_tests,
passed=passed,
failed=failed,
pass_rate=pass_rate,
avg_score=avg_score,
avg_duration=avg_duration,
model_rows=model_rows,
category_rows=category_rows,
category_headers=category_headers,
matrix_rows=matrix_rows
)
# Write to file
with open(output_path, 'w') as f:
f.write(html_content)
return output_path