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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Minimal NER Benchmark Runner for HuggingFace Publication
|
| 4 |
+
|
| 5 |
+
This script evaluates a NER model's performance on key metrics:
|
| 6 |
+
- Entity Recognition F1 Score: How well entities are identified and classified
|
| 7 |
+
- Precision: Accuracy of positive predictions
|
| 8 |
+
- Recall: Ability to find all relevant entities
|
| 9 |
+
- Latency: Response time performance
|
| 10 |
+
- Entity Type Performance: Results across different entity types
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
import json
|
| 14 |
+
import re
|
| 15 |
+
import time
|
| 16 |
+
import requests
|
| 17 |
+
from typing import Dict, List, Tuple, Any
|
| 18 |
+
import yaml
|
| 19 |
+
from datetime import datetime
|
| 20 |
+
import sys
|
| 21 |
+
import os
|
| 22 |
+
|
| 23 |
+
class NERBenchmarkRunner:
|
| 24 |
+
def __init__(self, config_path: str):
|
| 25 |
+
with open(config_path, 'r') as f:
|
| 26 |
+
self.config = yaml.safe_load(f)
|
| 27 |
+
|
| 28 |
+
self.results = {
|
| 29 |
+
"metadata": {
|
| 30 |
+
"timestamp": datetime.now().isoformat(),
|
| 31 |
+
"model": "Minibase-NER-Standard",
|
| 32 |
+
"dataset": self.config["datasets"]["benchmark_dataset"]["file_path"],
|
| 33 |
+
"sample_size": self.config["datasets"]["benchmark_dataset"]["sample_size"]
|
| 34 |
+
},
|
| 35 |
+
"metrics": {},
|
| 36 |
+
"entity_performance": {},
|
| 37 |
+
"examples": []
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
def load_dataset(self) -> List[Dict]:
|
| 41 |
+
"""Load and sample the benchmark dataset"""
|
| 42 |
+
dataset_path = self.config["datasets"]["benchmark_dataset"]["file_path"]
|
| 43 |
+
sample_size = self.config["datasets"]["benchmark_dataset"]["sample_size"]
|
| 44 |
+
|
| 45 |
+
examples = []
|
| 46 |
+
try:
|
| 47 |
+
with open(dataset_path, 'r') as f:
|
| 48 |
+
for i, line in enumerate(f):
|
| 49 |
+
if i >= sample_size:
|
| 50 |
+
break
|
| 51 |
+
examples.append(json.loads(line.strip()))
|
| 52 |
+
except FileNotFoundError:
|
| 53 |
+
print(f"β οΈ Dataset file {dataset_path} not found. Creating sample dataset...")
|
| 54 |
+
examples = self.create_sample_dataset(sample_size)
|
| 55 |
+
|
| 56 |
+
print(f"β
Loaded {len(examples)} examples from {dataset_path}")
|
| 57 |
+
return examples
|
| 58 |
+
|
| 59 |
+
def create_sample_dataset(self, sample_size: int) -> List[Dict]:
|
| 60 |
+
"""Create a sample NER dataset for testing"""
|
| 61 |
+
examples = [
|
| 62 |
+
{
|
| 63 |
+
"instruction": "Extract all named entities from the following text. Return them in JSON format with entity types as keys and lists of entities as values.",
|
| 64 |
+
"input": "John Smith works at Google in New York and uses Python programming language.",
|
| 65 |
+
"response": '"PER": ["John Smith"], "ORG": ["Google"], "LOC": ["New York"], "MISC": ["Python"]'
|
| 66 |
+
},
|
| 67 |
+
{
|
| 68 |
+
"instruction": "Extract all named entities from the following text. Return them in JSON format with entity types as keys and lists of entities as values.",
|
| 69 |
+
"input": "Microsoft Corporation announced that Satya Nadella will visit London next week.",
|
| 70 |
+
"response": '"PER": ["Satya Nadella"], "ORG": ["Microsoft Corporation"], "LOC": ["London"]'
|
| 71 |
+
},
|
| 72 |
+
{
|
| 73 |
+
"instruction": "Extract all named entities from the following text. Return them in JSON format with entity types as keys and lists of entities as values.",
|
| 74 |
+
"input": "The University of Cambridge is located in the United Kingdom and was founded by King Henry III.",
|
| 75 |
+
"response": '"ORG": ["University of Cambridge"], "LOC": ["United Kingdom"], "PER": ["King Henry III"]'
|
| 76 |
+
}
|
| 77 |
+
]
|
| 78 |
+
|
| 79 |
+
# Repeat examples to reach sample_size
|
| 80 |
+
dataset = []
|
| 81 |
+
for i in range(sample_size):
|
| 82 |
+
dataset.append(examples[i % len(examples)].copy())
|
| 83 |
+
|
| 84 |
+
# Save the sample dataset
|
| 85 |
+
with open(self.config["datasets"]["benchmark_dataset"]["file_path"], 'w') as f:
|
| 86 |
+
for example in dataset:
|
| 87 |
+
f.write(json.dumps(example) + '\n')
|
| 88 |
+
|
| 89 |
+
return dataset
|
| 90 |
+
|
| 91 |
+
def extract_entities_from_prediction(self, prediction: str) -> List[Tuple[str, str, str]]:
|
| 92 |
+
"""Extract entities from JSON prediction format"""
|
| 93 |
+
entities = []
|
| 94 |
+
|
| 95 |
+
# Clean up the prediction - remove any extra formatting
|
| 96 |
+
prediction = prediction.strip()
|
| 97 |
+
|
| 98 |
+
# Try to parse the JSON structure (NER_Standard outputs proper JSON)
|
| 99 |
+
try:
|
| 100 |
+
# Handle the JSON format: {"PER": ["entity1"], "ORG": ["entity2"], etc.}
|
| 101 |
+
import ast
|
| 102 |
+
# Try to parse as Python literal (dict)
|
| 103 |
+
try:
|
| 104 |
+
parsed = ast.literal_eval(prediction)
|
| 105 |
+
if isinstance(parsed, dict):
|
| 106 |
+
for entity_type, entity_list in parsed.items():
|
| 107 |
+
if isinstance(entity_list, list):
|
| 108 |
+
for entity_text in entity_list:
|
| 109 |
+
if entity_text and entity_text.strip(): # Skip empty strings
|
| 110 |
+
# Map common abbreviations to full entity types
|
| 111 |
+
type_mapping = {
|
| 112 |
+
"PER": "PERSON",
|
| 113 |
+
"ORG": "ORG",
|
| 114 |
+
"LOC": "LOC",
|
| 115 |
+
"MISC": "MISC"
|
| 116 |
+
}
|
| 117 |
+
mapped_type = type_mapping.get(entity_type.upper(), entity_type.upper())
|
| 118 |
+
entities.append((entity_text.strip(), mapped_type, "0-0"))
|
| 119 |
+
except Exception as e:
|
| 120 |
+
# If direct parsing fails, try regex-based extraction
|
| 121 |
+
pass
|
| 122 |
+
|
| 123 |
+
except Exception as e:
|
| 124 |
+
# Fallback: try to extract using regex patterns for partial JSON
|
| 125 |
+
pattern = r'"(\w+)":\s*\[([^\]]+)\]'
|
| 126 |
+
matches = re.findall(pattern, prediction)
|
| 127 |
+
|
| 128 |
+
for entity_type, entity_list_str in matches:
|
| 129 |
+
# Extract individual entities from the list
|
| 130 |
+
entity_matches = re.findall(r'"([^"]+)"', entity_list_str)
|
| 131 |
+
for entity_text in entity_matches:
|
| 132 |
+
# Map common abbreviations to full entity types
|
| 133 |
+
type_mapping = {
|
| 134 |
+
"PER": "PERSON",
|
| 135 |
+
"ORG": "ORG",
|
| 136 |
+
"LOC": "LOC",
|
| 137 |
+
"MISC": "MISC"
|
| 138 |
+
}
|
| 139 |
+
mapped_type = type_mapping.get(entity_type.upper(), entity_type.upper())
|
| 140 |
+
entities.append((entity_text.strip(), mapped_type, "0-0"))
|
| 141 |
+
|
| 142 |
+
return entities
|
| 143 |
+
|
| 144 |
+
def extract_entities_from_bio_format(self, bio_text: str) -> List[Tuple[str, str, str]]:
|
| 145 |
+
"""Extract entities from BIO format text"""
|
| 146 |
+
entities = []
|
| 147 |
+
lines = bio_text.strip().split('\n')
|
| 148 |
+
|
| 149 |
+
current_entity = None
|
| 150 |
+
current_type = None
|
| 151 |
+
|
| 152 |
+
for line in lines:
|
| 153 |
+
line = line.strip()
|
| 154 |
+
if not line or line == '.':
|
| 155 |
+
continue
|
| 156 |
+
|
| 157 |
+
parts = line.split()
|
| 158 |
+
if len(parts) >= 2:
|
| 159 |
+
token, tag = parts[0], parts[1]
|
| 160 |
+
|
| 161 |
+
if tag.startswith('B-'):
|
| 162 |
+
# End previous entity if exists
|
| 163 |
+
if current_entity:
|
| 164 |
+
entities.append((current_entity, current_type, "0-0"))
|
| 165 |
+
# Start new entity
|
| 166 |
+
current_entity = token
|
| 167 |
+
current_type = tag[2:] # Remove B-
|
| 168 |
+
elif tag.startswith('I-') and current_entity:
|
| 169 |
+
# Continue current entity
|
| 170 |
+
current_entity += ' ' + token
|
| 171 |
+
else:
|
| 172 |
+
# End previous entity if exists
|
| 173 |
+
if current_entity:
|
| 174 |
+
entities.append((current_entity, current_type, "0-0"))
|
| 175 |
+
current_entity = None
|
| 176 |
+
current_type = None
|
| 177 |
+
|
| 178 |
+
# End any remaining entity
|
| 179 |
+
if current_entity:
|
| 180 |
+
entities.append((current_entity, current_type, "0-0"))
|
| 181 |
+
|
| 182 |
+
return entities
|
| 183 |
+
|
| 184 |
+
def normalize_entity_text(self, text: str) -> str:
|
| 185 |
+
"""Normalize entity text for better matching"""
|
| 186 |
+
# Convert to lowercase
|
| 187 |
+
text = text.lower()
|
| 188 |
+
# Remove common prefixes that might vary
|
| 189 |
+
text = re.sub(r'^(the|an?|mr|mrs|ms|dr|prof)\s+', '', text)
|
| 190 |
+
# Remove extra whitespace
|
| 191 |
+
text = ' '.join(text.split())
|
| 192 |
+
return text.strip()
|
| 193 |
+
|
| 194 |
+
def calculate_ner_metrics(self, predicted_entities: List[Tuple], expected_bio_text: str) -> Dict[str, float]:
|
| 195 |
+
"""Calculate NER metrics: precision, recall, F1"""
|
| 196 |
+
# Extract expected entities from BIO format
|
| 197 |
+
expected_entities = self.extract_entities_from_bio_format(expected_bio_text)
|
| 198 |
+
|
| 199 |
+
# Normalize and create sets for comparison
|
| 200 |
+
pred_texts = set(self.normalize_entity_text(ent[0]) for ent in predicted_entities)
|
| 201 |
+
exp_texts = set(self.normalize_entity_text(ent[0]) for ent in expected_entities)
|
| 202 |
+
|
| 203 |
+
# Calculate exact matches
|
| 204 |
+
exact_matches = pred_texts & exp_texts
|
| 205 |
+
true_positives = len(exact_matches)
|
| 206 |
+
|
| 207 |
+
# Check for partial matches (subset/superset relationships)
|
| 208 |
+
additional_matches = 0
|
| 209 |
+
for pred in pred_texts - exact_matches:
|
| 210 |
+
for exp in exp_texts - exact_matches:
|
| 211 |
+
# Check if one is a substring of the other (with some tolerance)
|
| 212 |
+
if pred in exp or exp in pred:
|
| 213 |
+
if len(pred) > 3 and len(exp) > 3: # Avoid matching very short strings
|
| 214 |
+
additional_matches += 1
|
| 215 |
+
break
|
| 216 |
+
|
| 217 |
+
true_positives += additional_matches
|
| 218 |
+
false_positives = len(pred_texts) - true_positives
|
| 219 |
+
false_negatives = len(exp_texts) - true_positives
|
| 220 |
+
|
| 221 |
+
precision = true_positives / (true_positives + false_positives) if (true_positives + false_positives) > 0 else 0.0
|
| 222 |
+
recall = true_positives / (true_positives + false_negatives) if (true_positives + false_negatives) > 0 else 0.0
|
| 223 |
+
f1 = 2 * precision * recall / (precision + recall) if (precision + recall) > 0 else 0.0
|
| 224 |
+
|
| 225 |
+
return {
|
| 226 |
+
"precision": precision,
|
| 227 |
+
"recall": recall,
|
| 228 |
+
"f1": f1,
|
| 229 |
+
"true_positives": true_positives,
|
| 230 |
+
"false_positives": false_positives,
|
| 231 |
+
"false_negatives": false_negatives
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
def call_model(self, instruction: str, input_text: str) -> Tuple[str, float]:
|
| 235 |
+
"""Call the NER model and measure latency"""
|
| 236 |
+
prompt = f"{instruction}\n\nInput: {input_text}\n\nResponse: "
|
| 237 |
+
|
| 238 |
+
payload = {
|
| 239 |
+
"prompt": prompt,
|
| 240 |
+
"max_tokens": self.config["model"]["max_tokens"],
|
| 241 |
+
"temperature": self.config["model"]["temperature"]
|
| 242 |
+
}
|
| 243 |
+
|
| 244 |
+
headers = {'Content-Type': 'application/json'}
|
| 245 |
+
|
| 246 |
+
start_time = time.time()
|
| 247 |
+
try:
|
| 248 |
+
response = requests.post(
|
| 249 |
+
f"{self.config['model']['base_url']}/completion",
|
| 250 |
+
json=payload,
|
| 251 |
+
headers=headers,
|
| 252 |
+
timeout=self.config["model"]["timeout"]
|
| 253 |
+
)
|
| 254 |
+
latency = (time.time() - start_time) * 1000 # Convert to ms
|
| 255 |
+
|
| 256 |
+
if response.status_code == 200:
|
| 257 |
+
result = response.json()
|
| 258 |
+
return result.get('content', ''), latency
|
| 259 |
+
else:
|
| 260 |
+
return f"Error: Server returned status {response.status_code}", latency
|
| 261 |
+
except requests.exceptions.RequestException as e:
|
| 262 |
+
latency = (time.time() - start_time) * 1000
|
| 263 |
+
return f"Error: {e}", latency
|
| 264 |
+
|
| 265 |
+
def run_benchmarks(self):
|
| 266 |
+
"""Run the complete benchmark suite"""
|
| 267 |
+
print("π Starting NER Benchmarks...")
|
| 268 |
+
print(f"π Sample size: {self.config['datasets']['benchmark_dataset']['sample_size']}")
|
| 269 |
+
print(f"π― Model: {self.results['metadata']['model']}")
|
| 270 |
+
print()
|
| 271 |
+
|
| 272 |
+
# First, let's demonstrate the numbered list parsing works with a mock example
|
| 273 |
+
print("π§ Testing numbered list parsing with mock data...")
|
| 274 |
+
# Test the actual format the model produces
|
| 275 |
+
mock_output = "1. Neil Armstrong\n2. Buzz Aldrin\n3. NASA\n4. Moon\n5. Apollo 11"
|
| 276 |
+
|
| 277 |
+
print("Testing NER numbered list format:")
|
| 278 |
+
mock_entities = self.extract_entities_from_prediction(mock_output)
|
| 279 |
+
print(f"β
Numbered list parsing: {len(mock_entities)} entities extracted")
|
| 280 |
+
|
| 281 |
+
if mock_entities:
|
| 282 |
+
print("Sample entities:")
|
| 283 |
+
for entity in mock_entities:
|
| 284 |
+
print(f" - {entity[0]} ({entity[1]})")
|
| 285 |
+
print()
|
| 286 |
+
|
| 287 |
+
examples = self.load_dataset()
|
| 288 |
+
|
| 289 |
+
# Initialize metrics
|
| 290 |
+
total_precision = 0
|
| 291 |
+
total_recall = 0
|
| 292 |
+
total_f1 = 0
|
| 293 |
+
total_latency = 0
|
| 294 |
+
entity_type_metrics = {}
|
| 295 |
+
|
| 296 |
+
successful_requests = 0
|
| 297 |
+
|
| 298 |
+
for i, example in enumerate(examples):
|
| 299 |
+
if i % 10 == 0:
|
| 300 |
+
print(f"π Progress: {i}/{len(examples)} examples processed")
|
| 301 |
+
|
| 302 |
+
instruction = example[self.config["datasets"]["benchmark_dataset"]["instruction_field"]]
|
| 303 |
+
input_text = example[self.config["datasets"]["benchmark_dataset"]["input_field"]]
|
| 304 |
+
expected_output = example[self.config["datasets"]["benchmark_dataset"]["expected_output_field"]]
|
| 305 |
+
|
| 306 |
+
# Call model
|
| 307 |
+
predicted_output, latency = self.call_model(instruction, input_text)
|
| 308 |
+
|
| 309 |
+
if not predicted_output.startswith("Error"):
|
| 310 |
+
successful_requests += 1
|
| 311 |
+
|
| 312 |
+
# Extract entities from predictions and BIO format
|
| 313 |
+
try:
|
| 314 |
+
predicted_entities = self.extract_entities_from_prediction(predicted_output)
|
| 315 |
+
|
| 316 |
+
# Calculate metrics using expected BIO text
|
| 317 |
+
metrics = self.calculate_ner_metrics(predicted_entities, expected_output)
|
| 318 |
+
|
| 319 |
+
# Update totals
|
| 320 |
+
total_precision += metrics["precision"]
|
| 321 |
+
total_recall += metrics["recall"]
|
| 322 |
+
total_f1 += metrics["f1"]
|
| 323 |
+
total_latency += latency
|
| 324 |
+
|
| 325 |
+
# Track entity type performance (using generic ENTITY type since model doesn't specify types)
|
| 326 |
+
for entity_text, entity_type, _ in predicted_entities:
|
| 327 |
+
if entity_type not in entity_type_metrics:
|
| 328 |
+
entity_type_metrics[entity_type] = {"correct": 0, "total": 0}
|
| 329 |
+
|
| 330 |
+
# Check if this entity text was correctly identified (type-agnostic)
|
| 331 |
+
expected_entities_list = self.extract_entities_from_bio_format(expected_output)
|
| 332 |
+
expected_entity_texts = [self.normalize_entity_text(e[0]) for e in expected_entities_list]
|
| 333 |
+
normalized_entity = self.normalize_entity_text(entity_text)
|
| 334 |
+
|
| 335 |
+
# Check for exact match or substring match
|
| 336 |
+
is_correct = normalized_entity in expected_entity_texts
|
| 337 |
+
if not is_correct:
|
| 338 |
+
# Check for partial matches
|
| 339 |
+
for exp_text in expected_entity_texts:
|
| 340 |
+
if normalized_entity in exp_text or exp_text in normalized_entity:
|
| 341 |
+
if len(normalized_entity) > 3 and len(exp_text) > 3:
|
| 342 |
+
is_correct = True
|
| 343 |
+
break
|
| 344 |
+
|
| 345 |
+
if is_correct:
|
| 346 |
+
entity_type_metrics[entity_type]["correct"] += 1
|
| 347 |
+
entity_type_metrics[entity_type]["total"] += 1
|
| 348 |
+
|
| 349 |
+
# Store example if requested
|
| 350 |
+
if len(self.results["examples"]) < self.config["output"]["max_examples"]:
|
| 351 |
+
self.results["examples"].append({
|
| 352 |
+
"input": input_text,
|
| 353 |
+
"expected": expected_output,
|
| 354 |
+
"predicted": predicted_output,
|
| 355 |
+
"metrics": metrics,
|
| 356 |
+
"latency_ms": latency
|
| 357 |
+
})
|
| 358 |
+
|
| 359 |
+
except Exception as e:
|
| 360 |
+
print(f"β οΈ Error processing example {i}: {e}")
|
| 361 |
+
continue
|
| 362 |
+
|
| 363 |
+
# Calculate final metrics
|
| 364 |
+
if successful_requests > 0:
|
| 365 |
+
self.results["metrics"] = {
|
| 366 |
+
"precision": total_precision / successful_requests,
|
| 367 |
+
"recall": total_recall / successful_requests,
|
| 368 |
+
"f1_score": total_f1 / successful_requests,
|
| 369 |
+
"average_latency_ms": total_latency / successful_requests,
|
| 370 |
+
"successful_requests": successful_requests,
|
| 371 |
+
"total_requests": len(examples)
|
| 372 |
+
}
|
| 373 |
+
|
| 374 |
+
# Calculate entity type performance
|
| 375 |
+
self.results["entity_performance"] = {}
|
| 376 |
+
for entity_type, counts in entity_type_metrics.items():
|
| 377 |
+
accuracy = counts["correct"] / counts["total"] if counts["total"] > 0 else 0.0
|
| 378 |
+
self.results["entity_performance"][entity_type] = {
|
| 379 |
+
"accuracy": accuracy,
|
| 380 |
+
"correct_predictions": counts["correct"],
|
| 381 |
+
"total_predictions": counts["total"]
|
| 382 |
+
}
|
| 383 |
+
|
| 384 |
+
self.save_results()
|
| 385 |
+
|
| 386 |
+
def save_results(self):
|
| 387 |
+
"""Save benchmark results to files"""
|
| 388 |
+
# Save detailed JSON results
|
| 389 |
+
with open(self.config["output"]["detailed_results_file"], 'w') as f:
|
| 390 |
+
json.dump(self.results, f, indent=2)
|
| 391 |
+
|
| 392 |
+
# Save human-readable summary
|
| 393 |
+
summary = self.generate_summary()
|
| 394 |
+
with open(self.config["output"]["results_file"], 'w') as f:
|
| 395 |
+
f.write(summary)
|
| 396 |
+
|
| 397 |
+
print("\nβ
Benchmark complete!")
|
| 398 |
+
print(f"π Detailed results saved to: {self.config['output']['detailed_results_file']}")
|
| 399 |
+
print(f"π Summary saved to: {self.config['output']['results_file']}")
|
| 400 |
+
|
| 401 |
+
def generate_summary(self) -> str:
|
| 402 |
+
"""Generate a human-readable benchmark summary"""
|
| 403 |
+
m = self.results["metrics"]
|
| 404 |
+
ep = self.results["entity_performance"]
|
| 405 |
+
|
| 406 |
+
summary = f"""# NER Benchmark Results
|
| 407 |
+
**Model:** {self.results['metadata']['model']}
|
| 408 |
+
**Dataset:** {self.results['metadata']['dataset']}
|
| 409 |
+
**Sample Size:** {self.results['metadata']['sample_size']}
|
| 410 |
+
**Date:** {self.results['metadata']['timestamp']}
|
| 411 |
+
|
| 412 |
+
## Overall Performance
|
| 413 |
+
|
| 414 |
+
| Metric | Score | Description |
|
| 415 |
+
|--------|-------|-------------|
|
| 416 |
+
| F1 Score | {m.get('f1_score', 0):.3f} | Overall NER performance (harmonic mean of precision and recall) |
|
| 417 |
+
| Precision | {m.get('precision', 0):.3f} | Accuracy of entity predictions |
|
| 418 |
+
| Recall | {m.get('recall', 0):.3f} | Ability to find all entities |
|
| 419 |
+
| Average Latency | {m.get('average_latency_ms', 0):.1f}ms | Response time performance |
|
| 420 |
+
|
| 421 |
+
## Entity Type Performance
|
| 422 |
+
|
| 423 |
+
"""
|
| 424 |
+
if ep:
|
| 425 |
+
summary += "| Entity Type | Accuracy | Correct/Total |\n"
|
| 426 |
+
summary += "|-------------|----------|---------------|\n"
|
| 427 |
+
for entity_type, stats in ep.items():
|
| 428 |
+
summary += f"| {entity_type} | {stats['accuracy']:.3f} | {stats['correct_predictions']}/{stats['total_predictions']} |\n"
|
| 429 |
+
else:
|
| 430 |
+
summary += "No entity type performance data available.\n"
|
| 431 |
+
|
| 432 |
+
summary += """
|
| 433 |
+
## Key Improvements
|
| 434 |
+
|
| 435 |
+
- **BIO Tagging**: Model outputs entities in BIO (Beginning-Inside-Outside) format
|
| 436 |
+
- **Multiple Entity Types**: Supports PERSON, ORG, LOC, and MISC entities
|
| 437 |
+
- **Entity-Level Evaluation**: Metrics calculated at entity level rather than token level
|
| 438 |
+
- **Comprehensive Coverage**: Evaluates across different text domains
|
| 439 |
+
|
| 440 |
+
"""
|
| 441 |
+
|
| 442 |
+
if self.config["output"]["include_examples"] and self.results["examples"]:
|
| 443 |
+
summary += "## Example Results\n\n"
|
| 444 |
+
for i, example in enumerate(self.results["examples"][:3]): # Show first 3 examples
|
| 445 |
+
summary += f"### Example {i+1}\n"
|
| 446 |
+
summary += f"**Input:** {example['input'][:100]}...\n"
|
| 447 |
+
summary += f"**Predicted:** {example['predicted'][:200]}...\n"
|
| 448 |
+
summary += f"**F1 Score:** {example['metrics']['f1']:.3f}\n\n"
|
| 449 |
+
|
| 450 |
+
return summary
|
| 451 |
+
|
| 452 |
+
def main():
|
| 453 |
+
if len(sys.argv) != 2:
|
| 454 |
+
print("Usage: python run_benchmarks.py <config_file>")
|
| 455 |
+
sys.exit(1)
|
| 456 |
+
|
| 457 |
+
config_path = sys.argv[1]
|
| 458 |
+
if not os.path.exists(config_path):
|
| 459 |
+
print(f"Error: Config file {config_path} not found")
|
| 460 |
+
sys.exit(1)
|
| 461 |
+
|
| 462 |
+
runner = NERBenchmarkRunner(config_path)
|
| 463 |
+
runner.run_benchmarks()
|
| 464 |
+
|
| 465 |
+
if __name__ == "__main__":
|
| 466 |
+
main()
|