File size: 7,809 Bytes
1fc2038
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Health Check and Monitoring for GAIA Agent HuggingFace Space
Provides system status, capability checks, and performance monitoring.
"""

import os
import sys
import time
import json
from datetime import datetime
from pathlib import Path

class GAIAHealthCheck:
    """Comprehensive health check for GAIA Agent system."""
    
    def __init__(self):
        self.start_time = time.time()
        self.check_results = {}
    
    def check_dependencies(self):
        """Check availability of key dependencies."""
        dependencies = {
            'gradio': False,
            'smolagents': False,
            'litellm': False,
            'transformers': False,
            'torch': False,
            'google.generativeai': False,
            'pandas': False,
            'chess': False
        }
        
        for dep in dependencies:
            try:
                __import__(dep)
                dependencies[dep] = True
            except ImportError:
                dependencies[dep] = False
        
        return dependencies
    
    def check_api_keys(self):
        """Check availability of API keys."""
        api_keys = {
            'GEMINI_API_KEY': bool(os.getenv('GEMINI_API_KEY')),
            'HUGGINGFACE_TOKEN': bool(os.getenv('HUGGINGFACE_TOKEN')),
            'KLUSTER_API_KEY': bool(os.getenv('KLUSTER_API_KEY'))
        }
        return api_keys
    
    def check_core_components(self):
        """Check availability of core GAIA components."""
        components = {
            'main_solver': False,
            'hybrid_solver': False,
            'gaia_tools': False,
            'question_classifier': False,
            'async_testing': False,
            'advanced_testing': False
        }
        
        try:
            from main import GAIASolver
            components['main_solver'] = True
        except:
            pass
        
        try:
            from main_hybrid import HybridGAIASolver
            components['hybrid_solver'] = True
        except:
            pass
        
        try:
            from gaia_tools import GAIA_TOOLS
            components['gaia_tools'] = len(GAIA_TOOLS) > 0
        except:
            pass
        
        try:
            from question_classifier import QuestionClassifier
            components['question_classifier'] = True
        except:
            pass
        
        try:
            from async_complete_test_hf import run_hf_comprehensive_test
            components['async_testing'] = True
        except:
            pass
        
        try:
            from async_complete_test import AsyncGAIATestSystem
            components['advanced_testing'] = True
        except:
            pass
        
        return components
    
    def check_file_system(self):
        """Check file system and required files."""
        files = {
            'main.py': False,
            'app.py': False,
            'gaia_tools.py': False,
            'requirements.txt': False,
            'CLAUDE.md': False
        }
        
        for file in files:
            files[file] = Path(file).exists()
        
        return files
    
    def get_system_metrics(self):
        """Get system performance metrics."""
        metrics = {
            'uptime_seconds': time.time() - self.start_time,
            'python_version': sys.version,
            'platform': sys.platform,
            'memory_usage': 'unknown',
            'cpu_usage': 'unknown'
        }
        
        try:
            import psutil
            process = psutil.Process()
            metrics['memory_usage'] = f"{process.memory_info().rss / 1024 / 1024:.1f} MB"
            metrics['cpu_usage'] = f"{process.cpu_percent():.1f}%"
        except ImportError:
            pass
        
        return metrics
    
    def run_comprehensive_check(self):
        """Run all health checks and return comprehensive report."""
        print("πŸ” Running comprehensive health check...")
        
        self.check_results = {
            'timestamp': datetime.now().isoformat(),
            'dependencies': self.check_dependencies(),
            'api_keys': self.check_api_keys(),
            'components': self.check_core_components(),
            'files': self.check_file_system(),
            'metrics': self.get_system_metrics()
        }
        
        # Calculate overall health score
        self.check_results['health_score'] = self._calculate_health_score()
        self.check_results['status'] = self._get_overall_status()
        
        return self.check_results
    
    def _calculate_health_score(self):
        """Calculate overall health score (0-100)."""
        scores = {
            'dependencies': self._score_dict(self.check_results['dependencies']),
            'api_keys': self._score_dict(self.check_results['api_keys']),
            'components': self._score_dict(self.check_results['components']),
            'files': self._score_dict(self.check_results['files'])
        }
        
        # Weighted average
        weights = {'dependencies': 0.3, 'api_keys': 0.2, 'components': 0.4, 'files': 0.1}
        total_score = sum(scores[key] * weights[key] for key in weights)
        
        return round(total_score, 1)
    
    def _score_dict(self, data_dict):
        """Calculate score for a dictionary of boolean values."""
        if not data_dict:
            return 0
        return (sum(1 for v in data_dict.values() if v) / len(data_dict)) * 100
    
    def _get_overall_status(self):
        """Get overall system status."""
        score = self.check_results['health_score']
        
        if score >= 90:
            return "🟒 EXCELLENT"
        elif score >= 75:
            return "🟑 GOOD"
        elif score >= 50:
            return "🟠 FAIR"
        else:
            return "πŸ”΄ POOR"
    
    def print_report(self):
        """Print formatted health check report."""
        if not self.check_results:
            self.run_comprehensive_check()
        
        print("\n" + "="*60)
        print("πŸ₯ GAIA AGENT HEALTH CHECK REPORT")
        print("="*60)
        print(f"Timestamp: {self.check_results['timestamp']}")
        print(f"Overall Status: {self.check_results['status']}")
        print(f"Health Score: {self.check_results['health_score']}/100")
        
        print("\nπŸ“¦ Dependencies:")
        for dep, status in self.check_results['dependencies'].items():
            icon = "βœ…" if status else "❌"
            print(f"  {icon} {dep}")
        
        print("\nπŸ”‘ API Keys:")
        for key, status in self.check_results['api_keys'].items():
            icon = "βœ…" if status else "❌"
            print(f"  {icon} {key}")
        
        print("\n🧩 Components:")
        for comp, status in self.check_results['components'].items():
            icon = "βœ…" if status else "❌"
            print(f"  {icon} {comp}")
        
        print("\nπŸ“ Files:")
        for file, status in self.check_results['files'].items():
            icon = "βœ…" if status else "❌"
            print(f"  {icon} {file}")
        
        print("\nπŸ“Š System Metrics:")
        for metric, value in self.check_results['metrics'].items():
            print(f"  πŸ“ˆ {metric}: {value}")
        
        print("\n" + "="*60)
    
    def get_json_report(self):
        """Get health check report as JSON."""
        if not self.check_results:
            self.run_comprehensive_check()
        return json.dumps(self.check_results, indent=2)

def main():
    """Main function for health check CLI."""
    health_check = GAIAHealthCheck()
    
    if len(sys.argv) > 1 and sys.argv[1] == "--json":
        print(health_check.get_json_report())
    else:
        health_check.print_report()

if __name__ == "__main__":
    main()