Spaces:
Runtime error
Runtime error
#!/usr/bin/env python3 | |
""" | |
Fast Enhanced Data Explorer (skips slow AI setup) | |
""" | |
import pandas as pd | |
import os | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import seaborn as sns | |
import warnings | |
warnings.filterwarnings('ignore') | |
# CSV file path | |
csv_file_path = "C:/Users/Cosmo/Desktop/NTU Peak Singtel/outsystems_sample_logs_6months.csv" | |
class FastDataExplorer: | |
"""Fast data explorer with optional AI capabilities""" | |
def __init__(self, csv_path=csv_file_path): | |
self.csv_path = csv_path | |
self.df = None | |
self.agent = None | |
print("π Fast Data Explorer initialized!") | |
print("π‘ AI features can be setup later via option 9") | |
def setup_ai_on_demand(self): | |
"""Setup AI only when requested""" | |
if self.agent is not None: | |
print("β AI already configured!") | |
return True | |
print("π€ Setting up AI on demand...") | |
try: | |
# Try Ollama first | |
import ollama | |
models = ollama.list() | |
if models and 'models' in models and len(models['models']) > 0: | |
print("β Ollama detected - configuring...") | |
# Simple Ollama wrapper | |
class SimpleOllama: | |
def run(self, prompt): | |
try: | |
response = ollama.generate(model='llama2', prompt=prompt) | |
return response['response'] | |
except Exception as e: | |
return f"Error: {e}" | |
self.agent = SimpleOllama() | |
print("β AI configured with Ollama!") | |
return True | |
except Exception as e: | |
print(f"β οΈ AI setup failed: {e}") | |
# Fallback: No AI | |
print("β AI not available - using manual analysis only") | |
return False | |
def load_data(self): | |
"""Load the CSV data""" | |
print(f"\nπ Loading data from: {self.csv_path}") | |
try: | |
if not os.path.exists(self.csv_path): | |
print(f"β Error: File not found at {self.csv_path}") | |
return None | |
self.df = pd.read_csv(self.csv_path) | |
print("=== DATA LOADED SUCCESSFULLY ===") | |
print(f"π File: {os.path.basename(self.csv_path)}") | |
print(f"π Dataset shape: {self.df.shape}") | |
print(f"π Columns: {list(self.df.columns)}") | |
print("\n=== FIRST 5 ROWS ===") | |
print(self.df.head()) | |
return self.df | |
except Exception as e: | |
print(f"Error loading data: {str(e)}") | |
return None | |
def quick_analysis(self): | |
"""Quick manual analysis""" | |
if self.df is None: | |
print("β No data loaded.") | |
return | |
print("\n=== QUICK ANALYSIS ===") | |
print(f"π Shape: {self.df.shape}") | |
print(f"π Columns: {list(self.df.columns)}") | |
# Log level analysis | |
if 'LogLevel' in self.df.columns: | |
print("\nπ Log Level Distribution:") | |
print(self.df['LogLevel'].value_counts()) | |
# Error analysis | |
if 'ErrorId' in self.df.columns: | |
error_count = self.df['ErrorId'].notna().sum() | |
print(f"\nπ¨ Errors found: {error_count} out of {len(self.df)} records") | |
# Time analysis | |
if 'Timestamp' in self.df.columns: | |
print(f"\nπ Time range: {self.df['Timestamp'].min()} to {self.df['Timestamp'].max()}") | |
def ai_analysis(self, query): | |
"""AI analysis with on-demand setup""" | |
if self.df is None: | |
print("β No data loaded.") | |
return | |
if self.agent is None: | |
print("π€ Setting up AI...") | |
if not self.setup_ai_on_demand(): | |
return | |
print(f"\nπ Analyzing: {query}") | |
# Prepare simple data summary | |
data_summary = f""" | |
Data Analysis Request: | |
Dataset has {self.df.shape[0]} rows and {self.df.shape[1]} columns. | |
Columns: {list(self.df.columns)} | |
Sample data: | |
{self.df.head(2).to_string()} | |
Question: {query} | |
Please provide insights about this OutSystems log data. | |
""" | |
try: | |
response = self.agent.run(data_summary) | |
print("\n" + "="*50) | |
print("π€ AI ANALYSIS RESULT") | |
print("="*50) | |
print(response) | |
print("="*50) | |
except Exception as e: | |
print(f"β AI analysis failed: {e}") | |
def interactive_menu(self): | |
"""Interactive menu""" | |
while True: | |
print("\n" + "="*40) | |
print("π FAST DATA EXPLORER") | |
print("="*40) | |
print("1. Load data") | |
print("2. Quick analysis") | |
print("3. Show data summary") | |
print("4. AI analysis (auto-setup)") | |
print("5. Setup AI manually") | |
print("6. Exit") | |
print("="*40) | |
choice = input("Choice (1-6): ").strip() | |
if choice == '1': | |
self.load_data() | |
elif choice == '2': | |
self.quick_analysis() | |
elif choice == '3': | |
if self.df is not None: | |
print(f"\nπ Summary: {self.df.shape[0]} rows, {self.df.shape[1]} columns") | |
print(f"π Columns: {list(self.df.columns)}") | |
else: | |
print("β No data loaded.") | |
elif choice == '4': | |
query = input("π¬ Your question: ").strip() | |
if query: | |
self.ai_analysis(query) | |
else: | |
print("β No question entered.") | |
elif choice == '5': | |
self.setup_ai_on_demand() | |
elif choice == '6': | |
print("π Goodbye!") | |
break | |
else: | |
print("β Invalid choice.") | |
if __name__ == "__main__": | |
explorer = FastDataExplorer() | |
explorer.interactive_menu() | |