File size: 13,106 Bytes
fbd68f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# /// script
# dependencies = [
#     "transformers>=4.40.0",
#     "datasets>=2.18.0",
#     "torch>=2.0.0",
#     "rouge-score>=0.1.2",
#     "evaluate>=0.4.0",
#     "numpy>=1.24.0",
#     "pandas>=2.0.0",
#     "scikit-learn>=1.3.0",
#     "huggingface-hub>=0.20.0",
#     "accelerate>=0.27.0",
#     "trackio"
# ]
# ///

import os
import json
import pandas as pd
import numpy as np
from datetime import datetime
from datasets import load_dataset
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
from rouge_score import rouge_scorer
from sklearn.metrics import f1_score
import re
import trackio
from huggingface_hub import HfApi, upload_file
import torch

def normalize_text(text):
    """Normalize text for comparison"""
    if not isinstance(text, str):
        return ""
    # Remove extra whitespace and normalize
    text = re.sub(r'\s+', ' ', text.strip())
    return text.lower()

def compute_exact_match(pred, true):
    """Compute exact match score"""
    return float(normalize_text(pred) == normalize_text(true))

def compute_f1_score(pred, true):
    """Compute token-level F1 score"""
    pred_tokens = normalize_text(pred).split()
    true_tokens = normalize_text(true).split()

    if len(pred_tokens) == 0 and len(true_tokens) == 0:
        return 1.0
    if len(pred_tokens) == 0 or len(true_tokens) == 0:
        return 0.0

    # Convert to sets for intersection
    pred_set = set(pred_tokens)
    true_set = set(true_tokens)

    if len(pred_set) == 0 and len(true_set) == 0:
        return 1.0

    intersection = pred_set.intersection(true_set)
    precision = len(intersection) / len(pred_set) if pred_set else 0
    recall = len(intersection) / len(true_set) if true_set else 0

    if precision + recall == 0:
        return 0.0

    f1 = 2 * (precision * recall) / (precision + recall)
    return f1

def compute_rouge_l(pred, true):
    """Compute ROUGE-L score"""
    scorer = rouge_scorer.RougeScorer(['rougeL'], use_stemmer=True)
    scores = scorer.score(normalize_text(true), normalize_text(pred))
    return scores['rougeL'].fmeasure

def evaluate_model():
    # Initialize Trackio
    trackio.init()

    print("πŸš€ Starting model evaluation...")

    # Configuration
    model_name = "ligaments-enterprise/llama3.2-1b-instruct-sec-finetuned"
    dataset_name = "ligaments-enterprise/sec-data"

    print(f"πŸ“Š Loading dataset: {dataset_name}")
    try:
        # Try to load the dataset
        dataset = load_dataset(dataset_name, split="train")
        print(f"βœ… Dataset loaded successfully. Size: {len(dataset)}")
    except Exception as e:
        print(f"❌ Error loading dataset: {e}")
        # Try different splits
        try:
            dataset = load_dataset(dataset_name)
            if isinstance(dataset, dict):
                # Use the first available split
                split_name = list(dataset.keys())[0]
                dataset = dataset[split_name]
                print(f"βœ… Using split '{split_name}'. Size: {len(dataset)}")
        except Exception as e2:
            print(f"❌ Failed to load dataset: {e2}")
            return

    # Inspect dataset structure
    print(f"πŸ“‹ Dataset columns: {dataset.column_names}")
    print(f"πŸ“‹ First example: {dataset[0]}")

    # Determine input/output columns
    possible_input_cols = ['prompt', 'input', 'question', 'instruction', 'text']
    possible_output_cols = ['response', 'output', 'answer', 'completion', 'target']

    input_col = None
    output_col = None

    for col in possible_input_cols:
        if col in dataset.column_names:
            input_col = col
            break

    for col in possible_output_cols:
        if col in dataset.column_names:
            output_col = col
            break

    # Handle messages format
    if 'messages' in dataset.column_names:
        print("πŸ“‹ Detected messages format, extracting prompts and responses...")
        def extract_from_messages(example):
            messages = example['messages']
            if isinstance(messages, list) and len(messages) >= 2:
                # Find the last user message and assistant response
                user_msg = None
                assistant_msg = None
                for msg in messages:
                    if msg.get('role') == 'user':
                        user_msg = msg.get('content', '')
                    elif msg.get('role') == 'assistant':
                        assistant_msg = msg.get('content', '')

                return {
                    'input_text': user_msg or '',
                    'target_text': assistant_msg or ''
                }
            return {'input_text': '', 'target_text': ''}

        dataset = dataset.map(extract_from_messages)
        input_col = 'input_text'
        output_col = 'target_text'

    if not input_col or not output_col:
        print(f"❌ Could not identify input/output columns. Available: {dataset.column_names}")
        return

    print(f"βœ… Using input column: {input_col}, output column: {output_col}")

    print(f"πŸ€– Loading model: {model_name}")
    try:
        tokenizer = AutoTokenizer.from_pretrained(model_name)
        model = AutoModelForCausalLM.from_pretrained(
            model_name,
            torch_dtype=torch.float16,
            device_map="auto",
            trust_remote_code=True
        )

        # Set pad token if not set
        if tokenizer.pad_token is None:
            tokenizer.pad_token = tokenizer.eos_token

        print("βœ… Model loaded successfully")
    except Exception as e:
        print(f"❌ Error loading model: {e}")
        return

    # Create text generation pipeline
    generator = pipeline(
        "text-generation",
        model=model,
        tokenizer=tokenizer,
        torch_dtype=torch.float16,
        device_map="auto"
    )

    # Limit evaluation to reasonable size for demonstration
    eval_size = min(100, len(dataset))
    eval_dataset = dataset.select(range(eval_size))
    print(f"πŸ“Š Evaluating on {eval_size} samples...")

    results = []

    for i, example in enumerate(eval_dataset):
        if i % 10 == 0:
            print(f"πŸ“ˆ Processing sample {i+1}/{eval_size}")

        input_text = example[input_col]
        target_text = example[output_col]

        if not input_text or not target_text:
            continue

        # Generate prediction
        try:
            # Format prompt appropriately
            if not input_text.strip().endswith(('?', '.', '!', ':')):
                formatted_prompt = f"{input_text.strip()}:"
            else:
                formatted_prompt = input_text.strip()

            generated = generator(
                formatted_prompt,
                max_new_tokens=256,
                do_sample=False,  # Deterministic for evaluation
                temperature=0.1,
                pad_token_id=tokenizer.eos_token_id,
                return_full_text=False
            )

            prediction = generated[0]['generated_text'].strip()

            # Compute metrics
            exact_match = compute_exact_match(prediction, target_text)
            f1 = compute_f1_score(prediction, target_text)
            rouge_l = compute_rouge_l(prediction, target_text)

            # Error analysis
            error_type = "correct" if exact_match == 1.0 else "incorrect"
            if exact_match == 0 and f1 > 0.5:
                error_type = "partial_match"
            elif exact_match == 0 and rouge_l > 0.3:
                error_type = "semantic_similarity"
            elif len(prediction.split()) > len(target_text.split()) * 2:
                error_type = "too_verbose"
            elif len(prediction.split()) < len(target_text.split()) * 0.5:
                error_type = "too_brief"

            result = {
                'sample_id': i,
                'input': input_text,
                'target': target_text,
                'prediction': prediction,
                'exact_match': exact_match,
                'f1_score': f1,
                'rouge_l': rouge_l,
                'error_type': error_type,
                'input_length': len(input_text.split()),
                'target_length': len(target_text.split()),
                'prediction_length': len(prediction.split())
            }

            results.append(result)

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

    if not results:
        print("❌ No results generated")
        return

    # Compute summary statistics
    df_results = pd.DataFrame(results)

    summary_metrics = {
        'evaluation_timestamp': datetime.now().isoformat(),
        'model_name': model_name,
        'dataset_name': dataset_name,
        'total_samples': len(results),
        'exact_match_avg': df_results['exact_match'].mean(),
        'f1_score_avg': df_results['f1_score'].mean(),
        'rouge_l_avg': df_results['rouge_l'].mean(),
        'exact_match_std': df_results['exact_match'].std(),
        'f1_score_std': df_results['f1_score'].std(),
        'rouge_l_std': df_results['rouge_l'].std(),
        'perfect_matches': int(df_results['exact_match'].sum()),
        'perfect_match_rate': df_results['exact_match'].mean()
    }

    # Error analysis summary
    error_analysis = df_results['error_type'].value_counts().to_dict()
    summary_metrics['error_breakdown'] = error_analysis

    # Performance by length buckets
    df_results['target_length_bucket'] = pd.cut(
        df_results['target_length'],
        bins=[0, 10, 25, 50, 100, float('inf')],
        labels=['very_short', 'short', 'medium', 'long', 'very_long']
    )

    length_performance = df_results.groupby('target_length_bucket')[['exact_match', 'f1_score', 'rouge_l']].mean().to_dict()
    summary_metrics['performance_by_length'] = length_performance

    print("\nπŸ“Š EVALUATION RESULTS:")
    print(f"Total Samples: {summary_metrics['total_samples']}")
    print(f"Exact Match: {summary_metrics['exact_match_avg']:.4f} Β± {summary_metrics['exact_match_std']:.4f}")
    print(f"F1 Score: {summary_metrics['f1_score_avg']:.4f} Β± {summary_metrics['f1_score_std']:.4f}")
    print(f"ROUGE-L: {summary_metrics['rouge_l_avg']:.4f} Β± {summary_metrics['rouge_l_std']:.4f}")
    print(f"Perfect Matches: {summary_metrics['perfect_matches']}/{summary_metrics['total_samples']} ({summary_metrics['perfect_match_rate']:.2%})")

    print("\nπŸ” Error Breakdown:")
    for error_type, count in error_analysis.items():
        print(f"  {error_type}: {count} ({count/len(results):.2%})")

    # Save results locally first
    os.makedirs('eval_results', exist_ok=True)

    # Save detailed results
    df_results.to_csv('eval_results/detailed_results.csv', index=False)

    # Save summary metrics
    with open('eval_results/summary_metrics.json', 'w') as f:
        json.dump(summary_metrics, f, indent=2, default=str)

    # Save top errors for analysis
    worst_samples = df_results.nsmallest(10, 'f1_score')[['sample_id', 'input', 'target', 'prediction', 'f1_score', 'error_type']]
    worst_samples.to_csv('eval_results/worst_predictions.csv', index=False)

    # Save best samples
    best_samples = df_results.nlargest(10, 'f1_score')[['sample_id', 'input', 'target', 'prediction', 'f1_score', 'error_type']]
    best_samples.to_csv('eval_results/best_predictions.csv', index=False)

    print("\nπŸ’Ύ Results saved locally to eval_results/")

    # Upload results to model repository
    try:
        print("πŸš€ Uploading results to model repository...")
        api = HfApi()

        # Upload all result files
        files_to_upload = [
            ('eval_results/summary_metrics.json', 'eval_results/summary_metrics.json'),
            ('eval_results/detailed_results.csv', 'eval_results/detailed_results.csv'),
            ('eval_results/worst_predictions.csv', 'eval_results/worst_predictions.csv'),
            ('eval_results/best_predictions.csv', 'eval_results/best_predictions.csv')
        ]

        for local_path, repo_path in files_to_upload:
            api.upload_file(
                path_or_fileobj=local_path,
                path_in_repo=repo_path,
                repo_id=model_name,
                commit_message=f"Add evaluation results - {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
            )
            print(f"βœ… Uploaded {repo_path}")

        print(f"βœ… All evaluation results uploaded to {model_name}")

        # Log to Trackio
        trackio.log({
            "exact_match": summary_metrics['exact_match_avg'],
            "f1_score": summary_metrics['f1_score_avg'],
            "rouge_l": summary_metrics['rouge_l_avg'],
            "perfect_match_rate": summary_metrics['perfect_match_rate'],
            "total_samples": summary_metrics['total_samples']
        })

    except Exception as e:
        print(f"⚠️  Warning: Could not upload to repository: {e}")
        print("πŸ’Ύ Results are saved locally in eval_results/ directory")

    print("\nπŸŽ‰ Evaluation completed successfully!")
    return summary_metrics

if __name__ == "__main__":
    evaluate_model()