Spaces:
Sleeping
Sleeping
Update app_hf.py
Browse files
app_hf.py
CHANGED
|
@@ -1,181 +1,194 @@
|
|
| 1 |
"""
|
| 2 |
Hugging Face Spaces Deployment Entry Point
|
| 3 |
-
Lightweight dashboard entry point
|
| 4 |
"""
|
| 5 |
import os
|
| 6 |
import sys
|
| 7 |
-
|
| 8 |
-
# Set up environment
|
| 9 |
-
os.environ['GRADIO_SERVER_NAME'] = '0.0.0.0'
|
| 10 |
-
os.environ['GRADIO_SERVER_PORT'] = '7860'
|
| 11 |
-
|
| 12 |
-
# Import Gradio
|
| 13 |
import gradio as gr
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
from dashboard_simple import (
|
| 16 |
load_metrics,
|
| 17 |
-
get_summary_info,
|
| 18 |
create_comparison_charts,
|
| 19 |
-
format_metrics_text,
|
| 20 |
get_artifacts_info
|
| 21 |
)
|
| 22 |
|
| 23 |
-
# -----------
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
""
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
if __name__ == "__main__":
|
| 48 |
|
|
|
|
| 49 |
metrics = load_metrics()
|
| 50 |
r2_chart, rmse_chart = create_comparison_charts(metrics)
|
| 51 |
artifacts = get_artifacts_info()
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
with gr.Blocks(title="CPU Optimization Dashboard") as demo:
|
| 67 |
|
| 68 |
-
# Inject CSS
|
| 69 |
gr.HTML(custom_css)
|
| 70 |
|
| 71 |
gr.Markdown("# 🚀 CPU Optimization ML Dashboard")
|
| 72 |
gr.Markdown("Real-time visualization of machine learning models for CPU usage prediction")
|
| 73 |
|
| 74 |
-
# ------------
|
| 75 |
-
with gr.Row():
|
| 76 |
-
model_selector = gr.Dropdown(
|
| 77 |
-
["Random Forest", "Linear Regression"],
|
| 78 |
-
label="Select Model",
|
| 79 |
-
value="Random Forest"
|
| 80 |
-
)
|
| 81 |
-
model_output = gr.Markdown()
|
| 82 |
-
|
| 83 |
-
model_selector.change(show_model_details, inputs=model_selector, outputs=model_output)
|
| 84 |
-
|
| 85 |
-
# ---------------------- TABS ----------------------
|
| 86 |
with gr.Tabs():
|
| 87 |
|
| 88 |
-
# OVERVIEW
|
| 89 |
with gr.Tab("📋 Overview"):
|
| 90 |
gr.Markdown(f"""
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
✓ Dashboard is running and all models are loaded successfully
|
| 94 |
|
| 95 |
-
|
| 96 |
- **Best Model:** Random Forest
|
| 97 |
- **Best R² Score:** {rf_r2}
|
| 98 |
- **Best RMSE:** {rf_rmse}
|
| 99 |
- **Models Available:** 2
|
| 100 |
-
|
| 101 |
-
<h4>Quick Stats</h4>
|
| 102 |
-
- ✓ Random Forest Model: Loaded
|
| 103 |
-
- ✓ Linear Regression Model: Loaded
|
| 104 |
-
- ✓ Training Metrics: Available
|
| 105 |
-
- ✓ Evaluation Metrics: Available
|
| 106 |
-
</div>
|
| 107 |
""")
|
| 108 |
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
with gr.Tab("📈 Comparison"):
|
| 111 |
gr.Markdown(f"""
|
| 112 |
-
|
| 113 |
-
<h3>Model Performance Comparison</h3>
|
| 114 |
|
| 115 |
-
|
| 116 |
R² = {rf_r2}, RMSE = {rf_rmse}
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
<b>Linear Regression:</b><br>
|
| 121 |
R² = {lr_r2}, RMSE = {lr_rmse}
|
| 122 |
-
|
| 123 |
-
<h4>🎯 Winner: Random Forest 🏆</h4>
|
| 124 |
-
- ✓ Higher R² Score
|
| 125 |
-
- ✓ Lower RMSE
|
| 126 |
-
</div>
|
| 127 |
""")
|
|
|
|
| 128 |
gr.HTML(r2_chart)
|
| 129 |
gr.HTML(rmse_chart)
|
| 130 |
|
| 131 |
-
# METRICS
|
| 132 |
with gr.Tab("📊 Metrics"):
|
| 133 |
gr.Markdown(f"""
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
-
|
| 142 |
-
-
|
| 143 |
-
</div>
|
| 144 |
-
|
| 145 |
-
<div class='metric-box'>
|
| 146 |
-
<h4>📏 Linear Regression</h4>
|
| 147 |
-
- R² Score: {lr_r2}<br>
|
| 148 |
-
- RMSE: {lr_rmse}<br>
|
| 149 |
-
- Model Type: Linear (Baseline)<br>
|
| 150 |
-
- Performance: Baseline
|
| 151 |
-
</div>
|
| 152 |
-
</div>
|
| 153 |
""")
|
| 154 |
|
| 155 |
-
# ARTIFACTS
|
| 156 |
with gr.Tab("📦 Artifacts"):
|
| 157 |
-
gr.Markdown(f"
|
| 158 |
|
| 159 |
-
# INFO
|
| 160 |
with gr.Tab("ℹ️ Information"):
|
| 161 |
gr.Markdown("""
|
| 162 |
-
|
| 163 |
-
<h3>System Information</h3>
|
| 164 |
-
|
| 165 |
-
<b>Dashboard Configuration</b><br>
|
| 166 |
- Dashboard Version: 2.0.0
|
| 167 |
- Gradio Version: 3.50.0
|
| 168 |
-
- Python Version: 3.
|
| 169 |
- Deployment: Hugging Face Spaces
|
| 170 |
-
|
| 171 |
-
<br><b>Deployment Ready</b><br>
|
| 172 |
-
- ✓ Works in HF Spaces
|
| 173 |
-
- ✓ Docker Compatible
|
| 174 |
-
- ✓ Cloud-Deployable
|
| 175 |
-
</div>
|
| 176 |
""")
|
| 177 |
|
| 178 |
gr.Markdown("---")
|
| 179 |
gr.Markdown("**CPU Optimization ML Dashboard** | Built with Gradio & Scikit-learn")
|
| 180 |
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
Hugging Face Spaces Deployment Entry Point
|
| 3 |
+
Lightweight dashboard entry point with CSS + Dropdown
|
| 4 |
"""
|
| 5 |
import os
|
| 6 |
import sys
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
+
# Set environment
|
| 10 |
+
os.environ["GRADIO_SERVER_NAME"] = "0.0.0.0"
|
| 11 |
+
os.environ["GRADIO_SERVER_PORT"] = "7860"
|
| 12 |
+
|
| 13 |
+
# Import dashboard functions
|
| 14 |
from dashboard_simple import (
|
| 15 |
load_metrics,
|
|
|
|
| 16 |
create_comparison_charts,
|
|
|
|
| 17 |
get_artifacts_info
|
| 18 |
)
|
| 19 |
|
| 20 |
+
# -------------------------------------------------------------------
|
| 21 |
+
# DROPDOWN HANDLER
|
| 22 |
+
# -------------------------------------------------------------------
|
| 23 |
+
def show_model_details(selected):
|
| 24 |
+
"""Returns metrics for selected model."""
|
| 25 |
+
|
| 26 |
+
if selected == "Random Forest":
|
| 27 |
+
return (
|
| 28 |
+
f"### 🌳 Random Forest Metrics\n"
|
| 29 |
+
f"- R² Score: {rf_r2}\n"
|
| 30 |
+
f"- RMSE: {rf_rmse}\n"
|
| 31 |
+
f"- Model Type: Ensemble (100 trees)"
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
elif selected == "Linear Regression":
|
| 35 |
+
return (
|
| 36 |
+
f"### 📏 Linear Regression Metrics\n"
|
| 37 |
+
f"- R² Score: {lr_r2}\n"
|
| 38 |
+
f"- RMSE: {lr_rmse}\n"
|
| 39 |
+
f"- Model Type: Linear (Baseline)"
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
return "Select a model to view details."
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
# -------------------------------------------------------------------
|
| 46 |
+
# MAIN
|
| 47 |
+
# -------------------------------------------------------------------
|
| 48 |
if __name__ == "__main__":
|
| 49 |
|
| 50 |
+
# Load data
|
| 51 |
metrics = load_metrics()
|
| 52 |
r2_chart, rmse_chart = create_comparison_charts(metrics)
|
| 53 |
artifacts = get_artifacts_info()
|
| 54 |
|
| 55 |
+
# Extract metrics
|
| 56 |
+
rf_r2 = metrics.get("train", {}).get("r2_rf", "N/A")
|
| 57 |
+
rf_rmse = metrics.get("train", {}).get("rmse_rf", "N/A")
|
| 58 |
+
lr_r2 = metrics.get("train", {}).get("r2_lr", "N/A")
|
| 59 |
+
lr_rmse = metrics.get("train", {}).get("rmse_lr", "N/A")
|
| 60 |
+
|
| 61 |
+
# -------------------------------------------------------------------
|
| 62 |
+
# CUSTOM CSS
|
| 63 |
+
# -------------------------------------------------------------------
|
| 64 |
+
custom_css = """
|
| 65 |
+
<style>
|
| 66 |
+
|
| 67 |
+
body {
|
| 68 |
+
background: #0d1117 !important;
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
.gradio-container {
|
| 72 |
+
background: #0d1117 !important;
|
| 73 |
+
color: #ffffff !important;
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
h1, h2, h3, h4, h5, h6, p, label {
|
| 77 |
+
color: #ffffff !important;
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
.tab-nav button {
|
| 81 |
+
background: #161b22 !important;
|
| 82 |
+
color: #fff !important;
|
| 83 |
+
border-radius: 6px !important;
|
| 84 |
+
margin-right: 8px !important;
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
.tab-nav .selected {
|
| 88 |
+
background: #238636 !important;
|
| 89 |
+
font-weight: bold !important;
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
.card {
|
| 93 |
+
background: #161b22 !important;
|
| 94 |
+
padding: 20px;
|
| 95 |
+
border-radius: 12px;
|
| 96 |
+
border: 1px solid #30363d;
|
| 97 |
+
margin-bottom: 20px;
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
</style>
|
| 101 |
+
"""
|
| 102 |
+
|
| 103 |
+
# -------------------------------------------------------------------
|
| 104 |
+
# GRADIO UI
|
| 105 |
+
# -------------------------------------------------------------------
|
| 106 |
with gr.Blocks(title="CPU Optimization Dashboard") as demo:
|
| 107 |
|
| 108 |
+
# Inject CSS
|
| 109 |
gr.HTML(custom_css)
|
| 110 |
|
| 111 |
gr.Markdown("# 🚀 CPU Optimization ML Dashboard")
|
| 112 |
gr.Markdown("Real-time visualization of machine learning models for CPU usage prediction")
|
| 113 |
|
| 114 |
+
# -------------------------- TABS --------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
with gr.Tabs():
|
| 116 |
|
| 117 |
+
# OVERVIEW TAB -------------------------------------------------
|
| 118 |
with gr.Tab("📋 Overview"):
|
| 119 |
gr.Markdown(f"""
|
| 120 |
+
## Dashboard Overview
|
| 121 |
+
✓ Dashboard is running and all models are loaded successfully
|
|
|
|
| 122 |
|
| 123 |
+
### Key Metrics
|
| 124 |
- **Best Model:** Random Forest
|
| 125 |
- **Best R² Score:** {rf_r2}
|
| 126 |
- **Best RMSE:** {rf_rmse}
|
| 127 |
- **Models Available:** 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
""")
|
| 129 |
|
| 130 |
+
# DROPDOWN
|
| 131 |
+
gr.Markdown("### 🔽 Select Model to View Details")
|
| 132 |
+
model_dropdown = gr.Dropdown(
|
| 133 |
+
["Random Forest", "Linear Regression"],
|
| 134 |
+
label="Select Model",
|
| 135 |
+
value="Random Forest"
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
model_output = gr.Markdown()
|
| 139 |
+
model_dropdown.change(show_model_details, model_dropdown, model_output)
|
| 140 |
+
|
| 141 |
+
# COMPARISON TAB ------------------------------------------------
|
| 142 |
with gr.Tab("📈 Comparison"):
|
| 143 |
gr.Markdown(f"""
|
| 144 |
+
# Model Performance Comparison
|
|
|
|
| 145 |
|
| 146 |
+
**Random Forest:**
|
| 147 |
R² = {rf_r2}, RMSE = {rf_rmse}
|
| 148 |
|
| 149 |
+
**Linear Regression:**
|
|
|
|
|
|
|
| 150 |
R² = {lr_r2}, RMSE = {lr_rmse}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
""")
|
| 152 |
+
|
| 153 |
gr.HTML(r2_chart)
|
| 154 |
gr.HTML(rmse_chart)
|
| 155 |
|
| 156 |
+
# METRICS TAB ---------------------------------------------------
|
| 157 |
with gr.Tab("📊 Metrics"):
|
| 158 |
gr.Markdown(f"""
|
| 159 |
+
# Model Metrics Detail
|
| 160 |
+
|
| 161 |
+
## 🌳 Random Forest
|
| 162 |
+
- R² Score: {rf_r2}
|
| 163 |
+
- RMSE: {rf_rmse}
|
| 164 |
+
|
| 165 |
+
## 📏 Linear Regression
|
| 166 |
+
- R² Score: {lr_r2}
|
| 167 |
+
- RMSE: {lr_rmse}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
""")
|
| 169 |
|
| 170 |
+
# ARTIFACTS TAB -------------------------------------------------
|
| 171 |
with gr.Tab("📦 Artifacts"):
|
| 172 |
+
gr.Markdown(f"# Model Artifacts\n\n{artifacts}")
|
| 173 |
|
| 174 |
+
# INFO TAB ------------------------------------------------------
|
| 175 |
with gr.Tab("ℹ️ Information"):
|
| 176 |
gr.Markdown("""
|
| 177 |
+
# System Information
|
|
|
|
|
|
|
|
|
|
| 178 |
- Dashboard Version: 2.0.0
|
| 179 |
- Gradio Version: 3.50.0
|
| 180 |
+
- Python Version: 3.10
|
| 181 |
- Deployment: Hugging Face Spaces
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
""")
|
| 183 |
|
| 184 |
gr.Markdown("---")
|
| 185 |
gr.Markdown("**CPU Optimization ML Dashboard** | Built with Gradio & Scikit-learn")
|
| 186 |
|
| 187 |
+
# -------------------------------------------------------------------
|
| 188 |
+
# LAUNCH
|
| 189 |
+
# -------------------------------------------------------------------
|
| 190 |
+
demo.launch(
|
| 191 |
+
server_name="0.0.0.0",
|
| 192 |
+
server_port=7860,
|
| 193 |
+
share=False
|
| 194 |
+
)
|