Singtel_Use_Case1 / fixed_upload.py
cosmoruler
problems fixed
c69ba8c
#!/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()