""" CPU Optimization ML Dashboard - Simplified Version Lightweight, dependency-minimal dashboard for model visualization """ import json import joblib import os import numpy as np from pathlib import Path # ============================================================================ # CONFIGURATION # ============================================================================ MODELS_DIR = "models" METRICS_DIR = "metrics" EVAL_METRICS_DIR = f"{METRICS_DIR}/eval" DATA_DIR = "data/processed" # ============================================================================ # UTILITY FUNCTIONS # ============================================================================ def load_metrics(): """Load training and evaluation metrics""" metrics_data = {} # Load training metrics train_metrics_file = f"{METRICS_DIR}/train_metrics.json" if os.path.exists(train_metrics_file): with open(train_metrics_file, 'r') as f: metrics_data['train'] = json.load(f) # Load evaluation metrics lr_metrics_file = f"{EVAL_METRICS_DIR}/LinearRegression_metrics.json" rf_metrics_file = f"{EVAL_METRICS_DIR}/RandomForest_metrics.json" if os.path.exists(lr_metrics_file): with open(lr_metrics_file, 'r') as f: metrics_data['lr_eval'] = json.load(f) if os.path.exists(rf_metrics_file): with open(rf_metrics_file, 'r') as f: metrics_data['rf_eval'] = json.load(f) return metrics_data def load_model(model_name): """Load a model from disk""" model_path = os.path.join(MODELS_DIR, f"{model_name}.pkl") if os.path.exists(model_path): try: return joblib.load(model_path) except: return None return None def get_model_info(): """Get available models and their info""" models = {} try: model_files = [f for f in os.listdir(MODELS_DIR) if f.endswith('.pkl')] for model_file in model_files: model_name = model_file.replace('.pkl', '') file_path = os.path.join(MODELS_DIR, model_file) models[model_name] = { 'path': file_path, 'file_size': os.path.getsize(file_path) / 1024 # KB } except: pass return models def get_artifacts_info(): """Get artifact information as formatted text""" artifacts_text = "### Available Model Artifacts\n\n" try: models = get_model_info() for model_name, info in models.items(): artifacts_text += f"**{model_name.upper()}**\n" artifacts_text += f"- Size: {info['file_size']:.2f} KB\n" artifacts_text += f"- Path: {info['path']}\n" artifacts_text += f"- Status: ✓ Available\n\n" except Exception as e: artifacts_text += f"Error loading artifacts: {e}\n" return artifacts_text def format_metrics_text(metrics, model_filter=None): """Format metrics as text with optional model filtering""" text = "### Model Metrics Summary\n\n" if 'train' in metrics: train = metrics['train'] text += "#### Training Metrics\n" if model_filter is None or model_filter == 'Random Forest': text += f"- Random Forest R² Score: {train.get('r2_rf', 'N/A')}\n" text += f"- Random Forest RMSE: {train.get('rmse_rf', 'N/A')}\n" if model_filter is None or model_filter == 'Linear Regression': text += f"- Linear Regression R² Score: {train.get('r2_lr', 'N/A')}\n" text += f"- Linear Regression RMSE: {train.get('rmse_lr', 'N/A')}\n" text += "\n" if model_filter is None or model_filter == 'Random Forest': if 'rf_eval' in metrics: rf = metrics['rf_eval'] text += "#### Random Forest Evaluation\n" text += f"- R² Score: {rf.get('r2', 'N/A')}\n" text += f"- RMSE: {rf.get('rmse', 'N/A')}\n\n" if model_filter is None or model_filter == 'Linear Regression': if 'lr_eval' in metrics: lr = metrics['lr_eval'] text += "#### Linear Regression Evaluation\n" text += f"- R² Score: {lr.get('r2', 'N/A')}\n" text += f"- RMSE: {lr.get('rmse', 'N/A')}\n\n" return text def create_comparison_charts(metrics): """Create SVG comparison charts for R2 and RMSE""" if not metrics: return "", "" # Extract values rf_r2_train = metrics.get('train', {}).get('r2_rf', 0) lr_r2_train = metrics.get('train', {}).get('r2_lr', 0) rf_rmse_train = metrics.get('train', {}).get('rmse_rf', 0) lr_rmse_train = metrics.get('train', {}).get('rmse_lr', 0) # R² Chart (normalized to 0-1) r2_chart = f""" """ # RMSE Chart (inverse - lower is better, so we show as bars) # Scale RMSE to reasonable display height (multiply by 100 for visibility) rmse_scale = 500 if max(rf_rmse_train, lr_rmse_train) > 0 else 1 rmse_chart = f""" """ return r2_chart, rmse_chart def get_summary_info(): """Get summary information""" metrics = load_metrics() summary = """ # 🚀 CPU Optimization ML Dashboard ## Overview Real-time visualization of machine learning models for CPU usage prediction. ## Key Metrics """ if 'train' in metrics: train = metrics['train'] summary += f"- **Best Model**: Random Forest\n" summary += f"- **Best R² Score**: {train.get('r2_rf', 'N/A')} (Training)\n" summary += f"- **Best RMSE**: {train.get('rmse_rf', 'N/A')} (Training)\n\n" summary += """ ## Available Features ### 📦 Models & Artifacts - View loaded models and their metadata - Monitor model file sizes - Check artifact availability ### 📊 Metrics & Performance - Real-time metric comparison - R² Score analysis - RMSE comparison ### 🔮 Make Predictions - Test models with custom inputs - Instant predictions - Model selection ### ℹ️ System Information - Dashboard configuration - Model information - Performance overview ## Models Available 1. **Random Forest** - Advanced ensemble method 2. **Linear Regression** - Linear baseline model ## Quick Start ```python # Load metrics metrics = load_metrics() # Load a model model = load_model('rf_model') # Make prediction prediction = model.predict([[0.5, 0.6, 0.7]]) ``` """ return summary # ============================================================================ # MAIN APPLICATION # ============================================================================ def create_html_dashboard(): """Create an HTML-based dashboard (no Gradio)""" metrics = load_metrics() artifacts = get_artifacts_info() summary = get_summary_info() metrics_text = format_metrics_text(metrics) r2_chart, rmse_chart = create_comparison_charts(metrics) html = f"""
Real-time visualization of machine learning models for CPU usage prediction
🏆 Best Model: Random Forest
Random Forest outperforms Linear Regression by:
Dashboard Version: 2.0.0 (Enhanced)
Gradio Version: 3.50.0
Python Version: 3.11+
Deployment: Hugging Face Spaces Ready
Models Framework: Scikit-learn
Tracking: MLflow + DagsHub
Visualization: SVG Charts
Frontend: HTML5 + CSS3