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
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
#!/usr/bin/env python3
"""
Minimal NER Benchmark Runner for HuggingFace Publication

This script evaluates a NER model's performance on key metrics:
- Entity Recognition F1 Score: How well entities are identified and classified
- Precision: Accuracy of positive predictions
- Recall: Ability to find all relevant entities
- Latency: Response time performance
- Entity Type Performance: Results across different entity types
"""

import json
import re
import time
import requests
from typing import Dict, List, Tuple, Any
import yaml
from datetime import datetime
import sys
import os

class NERBenchmarkRunner:
    def __init__(self, config_path: str):
        with open(config_path, 'r') as f:
            self.config = yaml.safe_load(f)

        self.results = {
            "metadata": {
                "timestamp": datetime.now().isoformat(),
                "model": "Minibase-NER-Small",
                "dataset": self.config["datasets"]["benchmark_dataset"]["file_path"],
                "sample_size": self.config["datasets"]["benchmark_dataset"]["sample_size"]
            },
            "metrics": {},
            "entity_performance": {},
            "examples": []
        }

    def load_dataset(self) -> List[Dict]:
        """Load and sample the benchmark dataset"""
        dataset_path = self.config["datasets"]["benchmark_dataset"]["file_path"]
        sample_size = self.config["datasets"]["benchmark_dataset"]["sample_size"]

        examples = []
        try:
            with open(dataset_path, 'r') as f:
                for i, line in enumerate(f):
                    if i >= sample_size:
                        break
                    examples.append(json.loads(line.strip()))
        except FileNotFoundError:
            print(f"⚠️  Dataset file {dataset_path} not found. Creating sample dataset...")
            examples = self.create_sample_dataset(sample_size)

        print(f"βœ… Loaded {len(examples)} examples from {dataset_path}")
        return examples

    def create_sample_dataset(self, sample_size: int) -> List[Dict]:
        """Create a sample NER dataset for testing"""
        examples = [
            {
                "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.",
                "input": "John Smith works at Google in New York and uses Python programming language.",
                "response": '"PER": ["John Smith"], "ORG": ["Google"], "LOC": ["New York"], "MISC": ["Python"]'
            },
            {
                "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.",
                "input": "Microsoft Corporation announced that Satya Nadella will visit London next week.",
                "response": '"PER": ["Satya Nadella"], "ORG": ["Microsoft Corporation"], "LOC": ["London"]'
            },
            {
                "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.",
                "input": "The University of Cambridge is located in the United Kingdom and was founded by King Henry III.",
                "response": '"ORG": ["University of Cambridge"], "LOC": ["United Kingdom"], "PER": ["King Henry III"]'
            }
        ]

        # Repeat examples to reach sample_size
        dataset = []
        for i in range(sample_size):
            dataset.append(examples[i % len(examples)].copy())

        # Save the sample dataset
        with open(self.config["datasets"]["benchmark_dataset"]["file_path"], 'w') as f:
            for example in dataset:
                f.write(json.dumps(example) + '\n')

        return dataset

    def extract_entities_from_prediction(self, prediction: str) -> List[Tuple[str, str, str]]:
        """Extract entities from numbered list prediction format"""
        entities = []

        # Clean up the prediction - remove any extra formatting
        prediction = prediction.strip()

        # Handle the actual model output format: numbered lists
        # Examples:
        # "1"
        # "1. Microsoft Corporation"
        # "1. The University of Cambridge\n2. King Henry III"

        # Split by lines and process each line
        lines = prediction.split('\n')

        for line in lines:
            line = line.strip()
            if not line:
                continue

            # Try to extract entity names from numbered list format
            # Pattern 1: "1. Entity Name" or "1. Entity Name - Description"
            numbered_match = re.match(r'^\d+\.\s*(.+?)(?:\s*-\s*.+)?$', line)
            if numbered_match:
                entity_text = numbered_match.group(1).strip()
                # Remove any trailing punctuation and clean up
                entity_text = re.sub(r'[.,;:!?]$', '', entity_text).strip()
                # Skip very short entities or generic terms
                if entity_text and len(entity_text) > 1 and not entity_text.lower() in ['the', 'and', 'or', 'but', 'for', 'with']:
                    entities.append((entity_text, "ENTITY", "0-0"))
            else:
                # Pattern 2: Just a number like "1" - skip these as they're incomplete
                if re.match(r'^\d+$', line):
                    continue
                # Pattern 3: Any other text might be an entity
                elif len(line) > 1:  # Skip very short strings
                    entity_text = line.strip()
                    entity_text = re.sub(r'[.,;:!?]$', '', entity_text).strip()
                    if entity_text:
                        entities.append((entity_text, "ENTITY", "0-0"))

        return entities

    def extract_entities_from_bio_format(self, bio_text: str) -> List[Tuple[str, str, str]]:
        """Extract entities from BIO format text"""
        entities = []
        lines = bio_text.strip().split('\n')

        current_entity = None
        current_type = None

        for line in lines:
            line = line.strip()
            if not line or line == '.':
                continue

            parts = line.split()
            if len(parts) >= 2:
                token, tag = parts[0], parts[1]

                if tag.startswith('B-'):
                    # End previous entity if exists
                    if current_entity:
                        entities.append((current_entity, current_type, "0-0"))
                    # Start new entity
                    current_entity = token
                    current_type = tag[2:]  # Remove B-
                elif tag.startswith('I-') and current_entity:
                    # Continue current entity
                    current_entity += ' ' + token
                else:
                    # End previous entity if exists
                    if current_entity:
                        entities.append((current_entity, current_type, "0-0"))
                        current_entity = None
                        current_type = None

        # End any remaining entity
        if current_entity:
            entities.append((current_entity, current_type, "0-0"))

        return entities

    def normalize_entity_text(self, text: str) -> str:
        """Normalize entity text for better matching"""
        # Convert to lowercase
        text = text.lower()
        # Remove common prefixes that might vary
        text = re.sub(r'^(the|an?|mr|mrs|ms|dr|prof)\s+', '', text)
        # Remove extra whitespace
        text = ' '.join(text.split())
        return text.strip()

    def calculate_ner_metrics(self, predicted_entities: List[Tuple], expected_bio_text: str) -> Dict[str, float]:
        """Calculate NER metrics: precision, recall, F1"""
        # Extract expected entities from BIO format
        expected_entities = self.extract_entities_from_bio_format(expected_bio_text)

        # Normalize and create sets for comparison
        pred_texts = set(self.normalize_entity_text(ent[0]) for ent in predicted_entities)
        exp_texts = set(self.normalize_entity_text(ent[0]) for ent in expected_entities)

        # Calculate exact matches
        exact_matches = pred_texts & exp_texts
        true_positives = len(exact_matches)

        # Check for partial matches (subset/superset relationships)
        additional_matches = 0
        for pred in pred_texts - exact_matches:
            for exp in exp_texts - exact_matches:
                # Check if one is a substring of the other (with some tolerance)
                if pred in exp or exp in pred:
                    if len(pred) > 3 and len(exp) > 3:  # Avoid matching very short strings
                        additional_matches += 1
                        break

        true_positives += additional_matches
        false_positives = len(pred_texts) - true_positives
        false_negatives = len(exp_texts) - true_positives

        precision = true_positives / (true_positives + false_positives) if (true_positives + false_positives) > 0 else 0.0
        recall = true_positives / (true_positives + false_negatives) if (true_positives + false_negatives) > 0 else 0.0
        f1 = 2 * precision * recall / (precision + recall) if (precision + recall) > 0 else 0.0

        return {
            "precision": precision,
            "recall": recall,
            "f1": f1,
            "true_positives": true_positives,
            "false_positives": false_positives,
            "false_negatives": false_negatives
        }

    def call_model(self, instruction: str, input_text: str) -> Tuple[str, float]:
        """Call the NER model and measure latency"""
        prompt = f"{instruction}\n\nInput: {input_text}\n\nResponse: "

        payload = {
            "prompt": prompt,
            "max_tokens": self.config["model"]["max_tokens"],
            "temperature": self.config["model"]["temperature"]
        }

        headers = {'Content-Type': 'application/json'}

        start_time = time.time()
        try:
            response = requests.post(
                f"{self.config['model']['base_url']}/completion",
                json=payload,
                headers=headers,
                timeout=self.config["model"]["timeout"]
            )
            latency = (time.time() - start_time) * 1000  # Convert to ms

            if response.status_code == 200:
                result = response.json()
                return result.get('content', ''), latency
            else:
                return f"Error: Server returned status {response.status_code}", latency
        except requests.exceptions.RequestException as e:
            latency = (time.time() - start_time) * 1000
            return f"Error: {e}", latency

    def run_benchmarks(self):
        """Run the complete benchmark suite"""
        print("πŸš€ Starting NER Benchmarks...")
        print(f"πŸ“Š Sample size: {self.config['datasets']['benchmark_dataset']['sample_size']}")
        print(f"🎯 Model: {self.results['metadata']['model']}")
        print()

        # First, let's demonstrate the numbered list parsing works with a mock example
        print("πŸ”§ Testing numbered list parsing with mock data...")
        # Test the actual format the model produces
        mock_output = "1. Neil Armstrong\n2. Buzz Aldrin\n3. NASA\n4. Moon\n5. Apollo 11"

        print("Testing NER numbered list format:")
        mock_entities = self.extract_entities_from_prediction(mock_output)
        print(f"βœ… Numbered list parsing: {len(mock_entities)} entities extracted")

        if mock_entities:
            print("Sample entities:")
            for entity in mock_entities:
                print(f"   - {entity[0]} ({entity[1]})")
        print()

        examples = self.load_dataset()

        # Initialize metrics
        total_precision = 0
        total_recall = 0
        total_f1 = 0
        total_latency = 0
        entity_type_metrics = {}

        successful_requests = 0

        for i, example in enumerate(examples):
            if i % 10 == 0:
                print(f"πŸ“ˆ Progress: {i}/{len(examples)} examples processed")

            instruction = example[self.config["datasets"]["benchmark_dataset"]["instruction_field"]]
            input_text = example[self.config["datasets"]["benchmark_dataset"]["input_field"]]
            expected_output = example[self.config["datasets"]["benchmark_dataset"]["expected_output_field"]]

            # Call model
            predicted_output, latency = self.call_model(instruction, input_text)

            if not predicted_output.startswith("Error"):
                successful_requests += 1

                # Extract entities from predictions and BIO format
                try:
                    predicted_entities = self.extract_entities_from_prediction(predicted_output)

                    # Calculate metrics using expected BIO text
                    metrics = self.calculate_ner_metrics(predicted_entities, expected_output)

                    # Update totals
                    total_precision += metrics["precision"]
                    total_recall += metrics["recall"]
                    total_f1 += metrics["f1"]
                    total_latency += latency

                    # Track entity type performance (using generic ENTITY type since model doesn't specify types)
                    for entity_text, entity_type, _ in predicted_entities:
                        if entity_type not in entity_type_metrics:
                            entity_type_metrics[entity_type] = {"correct": 0, "total": 0}

                        # Check if this entity text was correctly identified (type-agnostic)
                        expected_entities_list = self.extract_entities_from_bio_format(expected_output)
                        expected_entity_texts = [self.normalize_entity_text(e[0]) for e in expected_entities_list]
                        normalized_entity = self.normalize_entity_text(entity_text)

                        # Check for exact match or substring match
                        is_correct = normalized_entity in expected_entity_texts
                        if not is_correct:
                            # Check for partial matches
                            for exp_text in expected_entity_texts:
                                if normalized_entity in exp_text or exp_text in normalized_entity:
                                    if len(normalized_entity) > 3 and len(exp_text) > 3:
                                        is_correct = True
                                        break

                        if is_correct:
                            entity_type_metrics[entity_type]["correct"] += 1
                        entity_type_metrics[entity_type]["total"] += 1

                    # Store example if requested
                    if len(self.results["examples"]) < self.config["output"]["max_examples"]:
                        self.results["examples"].append({
                            "input": input_text,
                            "expected": expected_output,
                            "predicted": predicted_output,
                            "metrics": metrics,
                            "latency_ms": latency
                        })

                except Exception as e:
                    print(f"⚠️  Error processing example {i}: {e}")
                    continue

        # Calculate final metrics
        if successful_requests > 0:
            self.results["metrics"] = {
                "precision": total_precision / successful_requests,
                "recall": total_recall / successful_requests,
                "f1_score": total_f1 / successful_requests,
                "average_latency_ms": total_latency / successful_requests,
                "successful_requests": successful_requests,
                "total_requests": len(examples)
            }

            # Calculate entity type performance
            self.results["entity_performance"] = {}
            for entity_type, counts in entity_type_metrics.items():
                accuracy = counts["correct"] / counts["total"] if counts["total"] > 0 else 0.0
                self.results["entity_performance"][entity_type] = {
                    "accuracy": accuracy,
                    "correct_predictions": counts["correct"],
                    "total_predictions": counts["total"]
                }

        self.save_results()

    def save_results(self):
        """Save benchmark results to files"""
        # Save detailed JSON results
        with open(self.config["output"]["detailed_results_file"], 'w') as f:
            json.dump(self.results, f, indent=2)

        # Save human-readable summary
        summary = self.generate_summary()
        with open(self.config["output"]["results_file"], 'w') as f:
            f.write(summary)

        print("\nβœ… Benchmark complete!")
        print(f"πŸ“„ Detailed results saved to: {self.config['output']['detailed_results_file']}")
        print(f"πŸ“Š Summary saved to: {self.config['output']['results_file']}")

    def generate_summary(self) -> str:
        """Generate a human-readable benchmark summary"""
        m = self.results["metrics"]
        ep = self.results["entity_performance"]

        summary = f"""# NER Benchmark Results
**Model:** {self.results['metadata']['model']}
**Dataset:** {self.results['metadata']['dataset']}
**Sample Size:** {self.results['metadata']['sample_size']}
**Date:** {self.results['metadata']['timestamp']}

## Overall Performance

| Metric | Score | Description |
|--------|-------|-------------|
| F1 Score | {m.get('f1_score', 0):.3f} | Overall NER performance (harmonic mean of precision and recall) |
| Precision | {m.get('precision', 0):.3f} | Accuracy of entity predictions |
| Recall | {m.get('recall', 0):.3f} | Ability to find all entities |
| Average Latency | {m.get('average_latency_ms', 0):.1f}ms | Response time performance |

## Entity Type Performance

"""
        if ep:
            summary += "| Entity Type | Accuracy | Correct/Total |\n"
            summary += "|-------------|----------|---------------|\n"
            for entity_type, stats in ep.items():
                summary += f"| {entity_type} | {stats['accuracy']:.3f} | {stats['correct_predictions']}/{stats['total_predictions']} |\n"
        else:
            summary += "No entity type performance data available.\n"

        summary += """
## Key Improvements

- **BIO Tagging**: Model outputs entities in BIO (Beginning-Inside-Outside) format
- **Multiple Entity Types**: Supports PERSON, ORG, LOC, and MISC entities
- **Entity-Level Evaluation**: Metrics calculated at entity level rather than token level
- **Comprehensive Coverage**: Evaluates across different text domains

"""

        if self.config["output"]["include_examples"] and self.results["examples"]:
            summary += "## Example Results\n\n"
            for i, example in enumerate(self.results["examples"][:3]):  # Show first 3 examples
                summary += f"### Example {i+1}\n"
                summary += f"**Input:** {example['input'][:100]}...\n"
                summary += f"**Predicted:** {example['predicted'][:200]}...\n"
                summary += f"**F1 Score:** {example['metrics']['f1']:.3f}\n\n"

        return summary

def main():
    if len(sys.argv) != 2:
        print("Usage: python run_benchmarks.py <config_file>")
        sys.exit(1)

    config_path = sys.argv[1]
    if not os.path.exists(config_path):
        print(f"Error: Config file {config_path} not found")
        sys.exit(1)

    runner = NERBenchmarkRunner(config_path)
    runner.run_benchmarks()

if __name__ == "__main__":
    main()