File size: 10,720 Bytes
0e4235d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Iterative evaluation script for fine-tuned models.
This script helps evaluate model performance on Pashto text
and provides guidance for iterative improvements.
"""

import argparse
import json
import os
import random
import time
from collections import defaultdict

import torch
import numpy as np
from tqdm import tqdm
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel

def parse_args():
    parser = argparse.ArgumentParser(description="Evaluate and iteratively improve a fine-tuned model")
    parser.add_argument(
        "--model_name",
        type=str,
        required=True,
        help="Hugging Face model name or local path",
    )
    parser.add_argument(
        "--dataset_file",
        type=str,
        default="zamai_pashto_dataset.json",
        help="JSON file with the dataset",
    )
    parser.add_argument(
        "--num_samples",
        type=int,
        default=10,
        help="Number of samples to evaluate",
    )
    parser.add_argument(
        "--max_new_tokens",
        type=int,
        default=200,
        help="Maximum new tokens to generate",
    )
    parser.add_argument(
        "--temperature",
        type=float,
        default=0.7,
        help="Sampling temperature",
    )
    parser.add_argument(
        "--output_file",
        type=str,
        default="model_evaluation_results.json",
        help="File to save evaluation results",
    )
    parser.add_argument(
        "--merge_lora",
        action="store_true",
        help="Merge LoRA weights with the base model",
    )
    parser.add_argument(
        "--seed",
        type=int,
        default=42,
        help="Random seed for reproducibility",
    )
    return parser.parse_args()

def categorize_samples(data):
    """Categorize dataset samples by type and length for better analysis"""
    categories = defaultdict(list)
    length_bins = {
        "short": (0, 50),
        "medium": (50, 200),
        "long": (200, float('inf'))
    }
    
    for idx, item in enumerate(data):
        # Skip samples without input or output
        if not item.get('input') or not item.get('output'):
            continue
        
        # Determine length category
        input_len = len(item['input'])
        for length_cat, (min_len, max_len) in length_bins.items():
            if min_len <= input_len < max_len:
                categories[length_cat].append(idx)
                break
                
    return categories

def evaluate_model(model, tokenizer, data, sample_indices, max_new_tokens, temperature):
    """Evaluate model on selected samples"""
    results = []
    
    for idx in tqdm(sample_indices, desc="Evaluating samples"):
        try:
            item = data[idx]
            input_text = item['input']
            reference_output = item['output']
            
            # Record start time
            start_time = time.time()
            
            # Generate text
            inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
            with torch.no_grad():
                outputs = model.generate(
                    **inputs, 
                    max_new_tokens=max_new_tokens,
                    do_sample=True,
                    temperature=temperature
                )
            model_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
            
            # Remove the input prompt from the output if it's there
            if model_output.startswith(input_text):
                model_output = model_output[len(input_text):].strip()
            
            # Calculate time taken
            time_taken = time.time() - start_time
            
            results.append({
                "index": idx,
                "input": input_text,
                "reference": reference_output,
                "model_output": model_output,
                "time_taken": time_taken,
                "output_length": len(model_output)
            })
            
        except Exception as e:
            print(f"Error processing sample {idx}: {e}")
            
    return results

def analyze_results(results):
    """Analyze evaluation results and provide recommendations"""
    if not results:
        return "No results to analyze"
    
    analysis = {}
    
    # Calculate statistics
    output_lengths = [r["output_length"] for r in results]
    time_taken = [r["time_taken"] for r in results]
    
    analysis["statistics"] = {
        "avg_output_length": sum(output_lengths) / len(output_lengths),
        "min_output_length": min(output_lengths),
        "max_output_length": max(output_lengths),
        "avg_time_per_token": sum(time_taken) / sum(output_lengths) if sum(output_lengths) > 0 else 0,
        "total_samples": len(results)
    }
    
    # Generate recommendations
    recommendations = []
    
    # Check if outputs are too short
    if analysis["statistics"]["avg_output_length"] < 30:
        recommendations.append(
            "The model's outputs are very short. Try fine-tuning with more diverse examples "
            "or adjusting the temperature parameter for generation."
        )
    
    # Check completion rate (how many reached max tokens)
    max_token_samples = sum(1 for r in results if r["output_length"] >= 0.9 * args.max_new_tokens)
    if max_token_samples > 0.5 * len(results):
        recommendations.append(
            f"{max_token_samples} samples reached near max token length, which might indicate truncated outputs. "
            f"Consider increasing max_new_tokens for evaluation."
        )
    
    # Analyze language balance in outputs
    pashto_chars = sum(1 for r in results for c in r["model_output"] if '\u0600' <= c <= '\u06FF')
    latin_chars = sum(1 for r in results for c in r["model_output"] if 'a' <= c.lower() <= 'z')
    
    if latin_chars > pashto_chars * 0.5:
        recommendations.append(
            "Model outputs contain substantial Latin script. For a Pashto model, consider "
            "more Pashto examples in your training data or longer fine-tuning."
        )
    
    # Add recommendations to analysis
    analysis["recommendations"] = recommendations
    
    # Identify areas for adding more examples
    failing_areas = []
    # This is a simplified analysis - in a real scenario, you might use embeddings or clustering
    if any(len(r["model_output"]) < 20 for r in results):
        failing_areas.append("short responses")
    
    analysis["areas_for_data_augmentation"] = failing_areas
    
    return analysis

def main():
    args = parse_args()
    random.seed(args.seed)
    
    # Load dataset
    print(f"Loading dataset from {args.dataset_file}")
    with open(args.dataset_file, 'r', encoding='utf-8') as f:
        data = json.load(f)
    print(f"Dataset loaded with {len(data)} examples")
    
    # Categorize samples
    print("Categorizing samples...")
    categories = categorize_samples(data)
    for category, indices in categories.items():
        print(f"  {category}: {len(indices)} examples")
    
    # Load model and tokenizer
    print(f"Loading model {args.model_name}")
    tokenizer = AutoTokenizer.from_pretrained(args.model_name)
    
    if args.merge_lora:
        print("Loading and merging LoRA model...")
        base_model_name = None  # You'd need to specify the base model or extract it from config
        if not base_model_name:
            print("For merging LoRA models, please specify the base model name in the script.")
            return
            
        base_model = AutoModelForCausalLM.from_pretrained(
            base_model_name,
            torch_dtype=torch.float16,
            device_map="auto"
        )
        model = PeftModel.from_pretrained(base_model, args.model_name)
        model = model.merge_and_unload()
    else:
        model = AutoModelForCausalLM.from_pretrained(
            args.model_name,
            torch_dtype=torch.float16,
            device_map="auto"
        )
    
    # Select samples from each category
    all_sample_indices = []
    samples_per_category = max(1, args.num_samples // len(categories))
    
    for category, indices in categories.items():
        if indices:
            # Take at least one sample from each category, up to samples_per_category
            category_samples = random.sample(indices, min(samples_per_category, len(indices)))
            all_sample_indices.extend(category_samples)
    
    # If we need more samples to reach args.num_samples, take random ones
    if len(all_sample_indices) < args.num_samples:
        remaining_indices = list(set(range(len(data))) - set(all_sample_indices))
        if remaining_indices:
            additional_samples = random.sample(
                remaining_indices, 
                min(args.num_samples - len(all_sample_indices), len(remaining_indices))
            )
            all_sample_indices.extend(additional_samples)
    
    print(f"Selected {len(all_sample_indices)} samples for evaluation")
    
    # Evaluate model
    results = evaluate_model(
        model, tokenizer, data, all_sample_indices, 
        args.max_new_tokens, args.temperature
    )
    
    # Analyze results
    print("\nAnalyzing results...")
    analysis = analyze_results(results)
    
    # Save results
    output = {
        "model_name": args.model_name,
        "evaluation_samples": len(results),
        "results": results,
        "analysis": analysis
    }
    
    with open(args.output_file, 'w', encoding='utf-8') as f:
        json.dump(output, f, ensure_ascii=False, indent=2)
    
    print(f"Results saved to {args.output_file}")
    
    # Display summary
    print("\n===== Evaluation Summary =====")
    print(f"Model: {args.model_name}")
    print(f"Samples evaluated: {len(results)}")
    print("\nStatistics:")
    for key, value in analysis["statistics"].items():
        print(f"  {key}: {value:.2f}" if isinstance(value, float) else f"  {key}: {value}")
    
    if analysis["recommendations"]:
        print("\nRecommendations for improvement:")
        for i, rec in enumerate(analysis["recommendations"], 1):
            print(f"{i}. {rec}")
    
    print("\nIterative Process Guide:")
    print("1. Review the generated outputs in the results file")
    print("2. Based on recommendations, consider:")
    print("   - Adjusting training parameters (learning rate, epochs)")
    print("   - Adding more examples to underrepresented categories")
    print("   - Cleaning existing training data")
    print("3. Update your dataset and start a new training run")
    print("4. Evaluate again and compare results")
    
if __name__ == "__main__":
    main()