#!/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()