File size: 6,019 Bytes
c69ba8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Fixed SmoLagent Data Analysis - Working Version
"""
import pandas as pd
from smolagents import CodeAgent, DuckDuckGoSearchTool
import warnings
warnings.filterwarnings('ignore')

# Your CSV file path
csv_file_path = "C:/Users/Cosmo/Desktop/NTU Peak Singtel/outsystems_sample_logs_6months.csv"

class FixedDataExplorer:
    """Working SmoLagent data explorer"""
    
    def __init__(self, csv_path=csv_file_path):
        self.csv_path = csv_path
        self.df = None
        self.agent = None
        self.load_data()
        self.setup_agent()
    
    def load_data(self):
        """Load the CSV data"""
        try:
            self.df = pd.read_csv(self.csv_path)
            print(f"βœ… Data loaded: {self.df.shape[0]} rows, {self.df.shape[1]} columns")
            return True
        except Exception as e:
            print(f"❌ Data loading failed: {e}")
            return False
    
    def setup_agent(self):
        """Setup SmoLagent with proper model"""
        try:
            print("πŸ€– Setting up SmoLagent...")
            
            # Option 1: Try Ollama (if running locally)
            try:
                from smolagents import OllamaModel
                model = OllamaModel(model_id="llama2")
                self.agent = CodeAgent(
                    tools=[DuckDuckGoSearchTool()],
                    model=model
                )
                print("βœ… SmoLagent configured with Ollama")
                return
            except:
                pass
            
            # Option 2: Use OpenAI (requires API key)
            try:
                from smolagents import OpenAIModel
                import os
                if os.getenv('OPENAI_API_KEY'):
                    model = OpenAIModel(model_id="gpt-3.5-turbo")
                    self.agent = CodeAgent(
                        tools=[DuckDuckGoSearchTool()],
                        model=model
                    )
                    print("βœ… SmoLagent configured with OpenAI")
                    return
            except:
                pass
            
            # Option 3: Use smaller HuggingFace model (lighter download)
            try:
                from smolagents import TransformersModel
                model = TransformersModel(model_id="microsoft/DialoGPT-small")  # Smaller model
                self.agent = CodeAgent(
                    tools=[DuckDuckGoSearchTool()],
                    model=model
                )
                print("βœ… SmoLagent configured with small Transformers model")
                return
            except Exception as e:
                print(f"❌ Model setup failed: {e}")
            
            print("❌ No AI model could be configured")
            print("πŸ’‘ You can still use basic data analysis features")
            
        except Exception as e:
            print(f"❌ Agent setup failed: {e}")
    
    def basic_analysis(self):
        """Run basic data analysis without AI"""
        if self.df is None:
            print("❌ No data loaded")
            return
        
        print("\nπŸ“Š BASIC DATA ANALYSIS")
        print("=" * 40)
        
        # Basic stats
        print(f"πŸ“‹ Dataset: {self.df.shape[0]} rows, {self.df.shape[1]} columns")
        print(f"πŸ“‹ Columns: {list(self.df.columns)}")
        
        # Log level analysis
        if 'LogLevel' in self.df.columns:
            log_counts = self.df['LogLevel'].value_counts()
            print(f"\nπŸ“ˆ Log Level Distribution:")
            for level, count in log_counts.items():
                pct = count / len(self.df) * 100
                print(f"   {level}: {count} ({pct:.1f}%)")
        
        # Error analysis
        if 'LogLevel' in self.df.columns:
            errors = self.df[self.df['LogLevel'] == 'Error']
            if not errors.empty and 'Module' in errors.columns:
                print(f"\n🚨 Top Error Modules:")
                top_errors = errors['Module'].value_counts().head(3)
                for module, count in top_errors.items():
                    print(f"   β€’ {module}: {count} errors")
        
        # Missing data
        missing = self.df.isnull().sum()
        print(f"\n❌ Missing Data:")
        for col, count in missing.items():
            if count > 0:
                pct = count / len(self.df) * 100
                print(f"   β€’ {col}: {count} ({pct:.1f}%)")
    
    def ai_analysis(self, query):
        """Run AI-powered analysis"""
        if self.agent is None:
            print("❌ AI agent not available. Please configure a model first.")
            return
        
        if self.df is None:
            print("❌ No data loaded")
            return
        
        try:
            # Prepare data context for AI
            data_summary = f"""
            Dataset: {self.df.shape[0]} rows, {self.df.shape[1]} columns
            Columns: {list(self.df.columns)}
            Sample data: {self.df.head(2).to_string()}
            """
            
            full_query = f"""
            Analyze this OutSystems log data:
            {data_summary}
            
            User question: {query}
            """
            
            print(f"πŸ€– AI analyzing: {query}")
            response = self.agent.run(full_query)
            print(f"πŸ€– AI Response: {response}")
            
        except Exception as e:
            print(f"❌ AI analysis failed: {e}")

def main():
    """Main function"""
    print("πŸš€ FIXED SMOLAGENT DATA ANALYZER")
    print("=" * 50)
    
    # Create explorer
    explorer = FixedDataExplorer()
    
    # Run basic analysis
    explorer.basic_analysis()
    
    # Test AI if available
    if explorer.agent:
        print(f"\nπŸ€– AI FEATURES AVAILABLE!")
        print("   Try: explorer.ai_analysis('What are the main error types?')")
    else:
        print(f"\nπŸ’‘ AI features not available - need model configuration")
    
    return explorer

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