File size: 15,783 Bytes
c262d1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Level-Specific GAIA Testing with Real-Time Accuracy Tracking
Focus on achieving 30% Level 1 accuracy through strategic testing and breakthrough leveraging.
"""

import json
import time
import argparse
import logging
import sys
from datetime import datetime
from typing import Dict, List, Optional
from collections import defaultdict
from pathlib import Path

# Add parent directory to path for imports
sys.path.append(str(Path(__file__).parent.parent))

from gaia_web_loader import GAIAQuestionLoaderWeb
from main import GAIASolver
from question_classifier import QuestionClassifier

class LevelSpecificGAIATester:
    """Enhanced GAIA testing with level-specific focus and real-time accuracy tracking"""
    
    def __init__(self, target_level: str = "1", target_accuracy: float = 0.30):
        self.target_level = target_level
        self.target_accuracy = target_accuracy
        self.loader = GAIAQuestionLoaderWeb()
        self.classifier = QuestionClassifier()
        self.solver = GAIASolver(use_kluster=True, kluster_model="qwen3-235b")
        self.results = []
        self.breakthrough_categories = ['chess', 'wikipedia', 'video', 'excel', 'research']
        
        # Create logs directory if it doesn't exist
        Path("logs").mkdir(exist_ok=True)
        
        # Setup logging
        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
        self.log_file = f"logs/level{target_level}_test_{timestamp}.log"
        
        logging.basicConfig(
            level=logging.INFO,
            format='%(asctime)s - %(levelname)s - %(message)s',
            handlers=[
                logging.FileHandler(self.log_file),
                logging.StreamHandler()
            ]
        )
        self.logger = logging.getLogger(__name__)
        
        # Load validation metadata for accuracy tracking
        self.validation_data = self.load_validation_metadata()
        
    def load_validation_metadata(self):
        """Load GAIA validation metadata for answer checking"""
        try:
            validation_data = {}
            with open('gaia_validation_metadata.jsonl', 'r') as f:
                for line in f:
                    if line.strip():
                        entry = json.loads(line)
                        validation_data[entry['task_id']] = entry
            self.logger.info(f"πŸ“‹ Loaded {len(validation_data)} validation entries")
            return validation_data
        except Exception as e:
            self.logger.error(f"Failed to load validation metadata: {e}")
            return {}
    
    def get_questions_by_level(self, level: str) -> List[Dict]:
        """Get all questions for a specific level"""
        level_questions = []
        
        for question in self.loader.questions:
            # Check validation metadata for level information
            task_id = question.get('task_id')
            if task_id in self.validation_data:
                question_level = str(self.validation_data[task_id].get('Level', ''))
                if question_level == level:
                    level_questions.append(question)
        
        self.logger.info(f"🎯 Found {len(level_questions)} Level {level} questions")
        return level_questions
    
    def classify_question_type(self, question: Dict) -> str:
        """Classify question to identify breakthrough opportunities"""
        question_text = question.get('question', '').lower()
        
        # Check for breakthrough categories
        if any(keyword in question_text for keyword in ['chess', 'move', 'position', 'algebraic']):
            return 'chess'
        elif any(keyword in question_text for keyword in ['wikipedia', 'featured article', 'nominated']):
            return 'wikipedia'
        elif any(keyword in question_text for keyword in ['video', 'youtube', 'audio', 'dialogue']):
            return 'video'
        elif any(keyword in question_text for keyword in ['excel', 'spreadsheet', 'sales', 'total']):
            return 'excel'
        elif any(keyword in question_text for keyword in ['research', 'find', 'search', 'who', 'what', 'when']):
            return 'research'
        else:
            return 'general'
    
    def calculate_real_time_accuracy(self) -> Dict:
        """Calculate real-time accuracy metrics for Level 1 progress"""
        if not self.results:
            return {
                'total_tested': 0,
                'correct_answers': 0,
                'current_accuracy': 0.0,
                'target_needed': int(53 * self.target_accuracy),  # 16 for 30%
                'remaining_to_target': int(53 * self.target_accuracy),
                'on_target': False
            }
        
        level_results = [r for r in self.results if r.get('level') == self.target_level]
        correct_count = len([r for r in level_results if r.get('validation_status') == 'CORRECT'])
        total_tested = len(level_results)
        current_accuracy = correct_count / total_tested if total_tested > 0 else 0.0
        
        target_needed = int(53 * self.target_accuracy)  # 16 for 30%
        remaining_to_target = max(0, target_needed - correct_count)
        on_target = current_accuracy >= self.target_accuracy
        
        return {
            'total_tested': total_tested,
            'correct_answers': correct_count,
            'current_accuracy': current_accuracy,
            'target_needed': target_needed,
            'remaining_to_target': remaining_to_target,
            'on_target': on_target
        }
    
    def validate_answer(self, task_id: str, our_answer: str) -> str:
        """Validate answer against GAIA metadata"""
        if task_id not in self.validation_data:
            return 'UNKNOWN'
        
        expected_answer = self.validation_data[task_id].get('Final answer', '').strip()
        our_answer = str(our_answer).strip()
        
        # Normalize for comparison
        def normalize(text):
            return str(text).lower().strip().replace(',', ', ').replace('  ', ' ')
        
        expected_normalized = normalize(expected_answer)
        our_normalized = normalize(our_answer)
        
        if expected_normalized == our_normalized:
            return 'CORRECT'
        elif expected_normalized in our_normalized or our_normalized in expected_normalized:
            return 'PARTIAL'
        else:
            return 'INCORRECT'
    
    def test_question(self, question: Dict) -> Dict:
        """Test a single question with enhanced validation"""
        task_id = question.get('task_id', 'unknown')
        question_text = question.get('question', '')
        question_type = self.classify_question_type(question)
        
        # Get level from validation metadata
        level = str(self.validation_data.get(task_id, {}).get('Level', 'unknown'))
        
        self.logger.info(f"\nπŸ§ͺ Testing {task_id} (Level {level}, Type: {question_type})")
        self.logger.info(f"πŸ“ Question: {question_text[:100]}...")
        
        start_time = time.time()
        
        try:
            # Use extended timeout for complex questions
            timeout = 1800 if question_type in self.breakthrough_categories else 900
            answer = self.solver.solve_question(question)
            solve_time = time.time() - start_time
            
            # Validate answer
            validation_status = self.validate_answer(task_id, answer)
            expected_answer = self.validation_data.get(task_id, {}).get('Final answer', 'Unknown')
            
            result = {
                'task_id': task_id,
                'level': level,
                'question_type': question_type,
                'question': question_text[:200] + "...",
                'our_answer': answer,
                'expected_answer': expected_answer,
                'validation_status': validation_status,
                'solve_time': solve_time,
                'breakthrough_category': question_type in self.breakthrough_categories,
                'timestamp': datetime.now().isoformat()
            }
            
            self.results.append(result)
            
            # Log result with status emoji
            status_emoji = "βœ…" if validation_status == "CORRECT" else "❌" if validation_status == "INCORRECT" else "πŸ”Ά"
            self.logger.info(f"{status_emoji} Result: {validation_status}")
            self.logger.info(f"πŸ’‘ Our Answer: {answer}")
            self.logger.info(f"🎯 Expected: {expected_answer}")
            self.logger.info(f"⏱️  Time: {solve_time:.1f}s")
            
            # Calculate and display real-time progress
            progress = self.calculate_real_time_accuracy()
            self.logger.info(f"πŸ“Š Level {self.target_level} Progress: {progress['correct_answers']}/{progress['target_needed']} target ({progress['current_accuracy']:.1%})")
            
            if progress['on_target']:
                self.logger.info(f"πŸŽ‰ TARGET ACHIEVED! {progress['current_accuracy']:.1%} >= {self.target_accuracy:.1%}")
            
            return result
            
        except Exception as e:
            error_result = {
                'task_id': task_id,
                'level': level,
                'question_type': question_type,
                'question': question_text[:200] + "...",
                'our_answer': f"ERROR: {str(e)}",
                'expected_answer': self.validation_data.get(task_id, {}).get('Final answer', 'Unknown'),
                'validation_status': 'ERROR',
                'solve_time': time.time() - start_time,
                'breakthrough_category': False,
                'timestamp': datetime.now().isoformat()
            }
            
            self.results.append(error_result)
            self.logger.error(f"❌ Error testing {task_id}: {e}")
            return error_result
    
    def run_level_campaign(self, level: str = None, max_questions: int = None) -> Dict:
        """Run strategic testing campaign for specific level"""
        if level is None:
            level = self.target_level
            
        level_questions = self.get_questions_by_level(level)
        
        if max_questions:
            level_questions = level_questions[:max_questions]
        
        self.logger.info(f"\nπŸš€ Starting Level {level} Campaign")
        self.logger.info(f"🎯 Target: {self.target_accuracy:.1%} accuracy ({int(len(level_questions) * self.target_accuracy)} correct)")
        self.logger.info(f"πŸ“Š Questions to test: {len(level_questions)}")
        
        # Prioritize breakthrough categories
        breakthrough_questions = [q for q in level_questions if self.classify_question_type(q) in self.breakthrough_categories]
        other_questions = [q for q in level_questions if self.classify_question_type(q) not in self.breakthrough_categories]
        
        self.logger.info(f"πŸ† Breakthrough questions: {len(breakthrough_questions)}")
        self.logger.info(f"πŸ“ Other questions: {len(other_questions)}")
        
        # Test breakthrough questions first
        all_questions = breakthrough_questions + other_questions
        
        for i, question in enumerate(all_questions, 1):
            self.logger.info(f"\n--- Question {i}/{len(all_questions)} ---")
            self.test_question(question)
            
            # Check if target achieved early
            progress = self.calculate_real_time_accuracy()
            if progress['on_target'] and progress['total_tested'] >= 10:  # Minimum 10 questions for statistical validity
                self.logger.info(f"πŸŽ‰ EARLY TARGET ACHIEVEMENT! {progress['current_accuracy']:.1%} >= {self.target_accuracy:.1%}")
                break
        
        return self.generate_final_report()
    
    def generate_final_report(self) -> Dict:
        """Generate comprehensive test report"""
        progress = self.calculate_real_time_accuracy()
        
        # Category breakdown
        category_stats = defaultdict(lambda: {'total': 0, 'correct': 0})
        for result in self.results:
            if result.get('level') == self.target_level:
                category = result.get('question_type', 'unknown')
                category_stats[category]['total'] += 1
                if result.get('validation_status') == 'CORRECT':
                    category_stats[category]['correct'] += 1
        
        # Calculate category accuracy rates
        for category in category_stats:
            total = category_stats[category]['total']
            category_stats[category]['accuracy'] = category_stats[category]['correct'] / total if total > 0 else 0
        
        report = {
            'campaign_summary': {
                'target_level': self.target_level,
                'target_accuracy': self.target_accuracy,
                'achievement_status': 'ACHIEVED' if progress['on_target'] else 'IN_PROGRESS',
                'final_accuracy': progress['current_accuracy'],
                'correct_answers': progress['correct_answers'],
                'total_tested': progress['total_tested'],
                'target_needed': progress['target_needed']
            },
            'category_breakdown': dict(category_stats),
            'breakthrough_performance': {
                category: stats for category, stats in category_stats.items() 
                if category in self.breakthrough_categories
            },
            'detailed_results': self.results,
            'timestamp': datetime.now().isoformat(),
            'log_file': self.log_file
        }
        
        # Save report
        report_file = f"level{self.target_level}_campaign_report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
        with open(report_file, 'w') as f:
            json.dump(report, f, indent=2)
        
        self.logger.info(f"\nπŸ“‹ FINAL CAMPAIGN REPORT")
        self.logger.info(f"🎯 Target: {self.target_accuracy:.1%} Level {self.target_level} accuracy")
        self.logger.info(f"πŸ† Achievement: {progress['current_accuracy']:.1%} ({progress['correct_answers']}/{progress['total_tested']})")
        self.logger.info(f"πŸ“Š Status: {'βœ… TARGET ACHIEVED' if progress['on_target'] else 'πŸ”„ IN PROGRESS'}")
        self.logger.info(f"πŸ’Ύ Report saved: {report_file}")
        
        return report

def main():
    """Main function for level-specific GAIA testing"""
    parser = argparse.ArgumentParser(description='Level-Specific GAIA Testing')
    parser.add_argument('--level', type=str, default='1', help='Target level to test (1, 2, 3)')
    parser.add_argument('--target-accuracy', type=float, default=0.30, help='Target accuracy (0.30 = 30%)')
    parser.add_argument('--max-questions', type=int, help='Maximum questions to test')
    
    args = parser.parse_args()
    
    print(f"πŸš€ Level-Specific GAIA Testing Campaign")
    print(f"🎯 Level: {args.level}")
    print(f"πŸ“Š Target Accuracy: {args.target_accuracy:.1%}")
    print("=" * 60)
    
    tester = LevelSpecificGAIATester(
        target_level=args.level,
        target_accuracy=args.target_accuracy
    )
    
    try:
        report = tester.run_level_campaign(level=args.level, max_questions=args.max_questions)
        
        # Print summary
        summary = report['campaign_summary']
        print(f"\nπŸŽ‰ CAMPAIGN COMPLETE!")
        print(f"🎯 Target: {summary['target_accuracy']:.1%}")
        print(f"πŸ† Achieved: {summary['final_accuracy']:.1%}")
        print(f"πŸ“Š Status: {summary['achievement_status']}")
        print(f"πŸ’― Score: {summary['correct_answers']}/{summary['total_tested']}")
        
    except Exception as e:
        print(f"❌ Campaign failed: {e}")
        return 1
    
    return 0

if __name__ == "__main__":
    exit(main())