Spaces:
Runtime error
Runtime error
#!/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() | |