Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
-
from pycaret.classification import setup as classification_setup, compare_models as compare_classification_models, evaluate_model as evaluate_classification_model, save_model as save_classification_model, plot_model as plot_classification_model
|
| 4 |
-
from pycaret.regression import setup as regression_setup, compare_models as compare_regression_models, evaluate_model as evaluate_regression_model, save_model as save_regression_model, plot_model as plot_regression_model
|
| 5 |
-
from pycaret.clustering import setup as clustering_setup, evaluate_model as evaluate_clustering_model, save_model as save_clustering_model, plot_model as plot_clustering_model
|
| 6 |
from ydata_profiling import ProfileReport
|
| 7 |
from streamlit_pandas_profiling import st_profile_report
|
| 8 |
import os
|
|
@@ -21,6 +21,16 @@ with st.sidebar:
|
|
| 21 |
st.markdown("**Dependencies**: `pycaret`, `pandas`, `streamlit`, `ydata-profiling`")
|
| 22 |
st.markdown("Created by Calvin Allen-Crawford | v2.0 | © 2025")
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
# Main App Sections
|
| 25 |
if app_mode == "Data Upload":
|
| 26 |
st.title("📤 Data Upload")
|
|
@@ -38,7 +48,6 @@ if app_mode == "Data Upload":
|
|
| 38 |
with col2: st.metric("Columns", df.shape[1])
|
| 39 |
with col3: st.metric("Missing Values", df.isna().sum().sum())
|
| 40 |
|
| 41 |
-
# Generate and display ydata-profiling report
|
| 42 |
st.write("---")
|
| 43 |
st.subheader("Exploratory Data Analysis (EDA)")
|
| 44 |
if st.button("Generate EDA Report"):
|
|
@@ -74,32 +83,79 @@ elif app_mode == "Model Training":
|
|
| 74 |
|
| 75 |
if st.session_state.get('setup_complete', False):
|
| 76 |
st.subheader("Train Models")
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
st.session_state['best_model'] = best_model
|
| 90 |
st.success(f"Best Model: {best_model}")
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
if 'best_model' in st.session_state and st.session_state['best_model'] is not None:
|
| 93 |
-
st.subheader("Model Evaluation")
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
if st.button("Save Model"):
|
| 105 |
if st.session_state['problem_type'] == "Classification":
|
|
@@ -115,7 +171,7 @@ elif app_mode == "Model Training":
|
|
| 115 |
elif app_mode == "Validation & Exploration":
|
| 116 |
st.title("🔍 Validation & Exploration")
|
| 117 |
if 'best_model' not in st.session_state or st.session_state['best_model'] is None:
|
| 118 |
-
st.warning("Please train a model first.
|
| 119 |
st.stop()
|
| 120 |
|
| 121 |
st.subheader("Model Performance")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
+
from pycaret.classification import setup as classification_setup, compare_models as compare_classification_models, evaluate_model as evaluate_classification_model, save_model as save_classification_model, plot_model as plot_classification_model, tune_model as tune_classification_model, get_config
|
| 4 |
+
from pycaret.regression import setup as regression_setup, compare_models as compare_regression_models, evaluate_model as evaluate_regression_model, save_model as save_regression_model, plot_model as plot_regression_model, tune_model as tune_regression_model, get_config
|
| 5 |
+
from pycaret.clustering import setup as clustering_setup, evaluate_model as evaluate_clustering_model, save_model as save_clustering_model, plot_model as plot_clustering_model, create_model as create_clustering_model, get_config
|
| 6 |
from ydata_profiling import ProfileReport
|
| 7 |
from streamlit_pandas_profiling import st_profile_report
|
| 8 |
import os
|
|
|
|
| 21 |
st.markdown("**Dependencies**: `pycaret`, `pandas`, `streamlit`, `ydata-profiling`")
|
| 22 |
st.markdown("Created by Calvin Allen-Crawford | v2.0 | © 2025")
|
| 23 |
|
| 24 |
+
# Helper function to get available models
|
| 25 |
+
def get_available_models(problem_type):
|
| 26 |
+
if problem_type == "Classification":
|
| 27 |
+
return list(get_config('available_estimators')['classification'].keys())
|
| 28 |
+
elif problem_type == "Regression":
|
| 29 |
+
return list(get_config('available_estimators')['regression'].keys())
|
| 30 |
+
elif problem_type == "Clustering":
|
| 31 |
+
return list(get_config('available_estimators')['clustering'].keys())
|
| 32 |
+
return []
|
| 33 |
+
|
| 34 |
# Main App Sections
|
| 35 |
if app_mode == "Data Upload":
|
| 36 |
st.title("📤 Data Upload")
|
|
|
|
| 48 |
with col2: st.metric("Columns", df.shape[1])
|
| 49 |
with col3: st.metric("Missing Values", df.isna().sum().sum())
|
| 50 |
|
|
|
|
| 51 |
st.write("---")
|
| 52 |
st.subheader("Exploratory Data Analysis (EDA)")
|
| 53 |
if st.button("Generate EDA Report"):
|
|
|
|
| 83 |
|
| 84 |
if st.session_state.get('setup_complete', False):
|
| 85 |
st.subheader("Train Models")
|
| 86 |
+
with st.expander("Advanced Options", expanded=False):
|
| 87 |
+
if problem_type in ["Classification", "Regression"]:
|
| 88 |
+
available_models = get_available_models(problem_type)
|
| 89 |
+
selected_models = st.multiselect("Select Models to Compare (leave empty for all)", available_models, default=None)
|
| 90 |
+
folds = st.number_input("Number of Cross-Validation Folds", min_value=2, max_value=20, value=10, step=1)
|
| 91 |
+
if problem_type == "Classification":
|
| 92 |
+
sort_metric = st.selectbox("Sort Metric", ["Accuracy", "AUC", "Recall", "Precision", "F1"], index=0)
|
| 93 |
+
else: # Regression
|
| 94 |
+
sort_metric = st.selectbox("Sort Metric", ["R2", "MAE", "MSE", "RMSE"], index=0)
|
| 95 |
+
elif problem_type == "Clustering":
|
| 96 |
+
available_models = get_available_models(problem_type)
|
| 97 |
+
selected_model = st.selectbox("Select Clustering Algorithm", available_models)
|
| 98 |
+
if selected_model == "kmeans":
|
| 99 |
+
num_clusters = st.number_input("Number of Clusters", min_value=2, max_value=20, value=4, step=1)
|
| 100 |
+
elif selected_model == "dbscan":
|
| 101 |
+
eps = st.number_input("Epsilon (eps)", min_value=0.1, max_value=10.0, value=0.5, step=0.1)
|
| 102 |
+
min_samples = st.number_input("Minimum Samples", min_value=2, max_value=20, value=5, step=1)
|
| 103 |
+
elif selected_model == "hclust":
|
| 104 |
+
num_clusters = st.number_input("Number of Clusters", min_value=2, max_value=20, value=4, step=1)
|
| 105 |
+
|
| 106 |
+
if problem_type in ["Classification", "Regression"]:
|
| 107 |
+
if st.button("Compare Models"):
|
| 108 |
+
with st.spinner("Comparing models..."):
|
| 109 |
+
if selected_models:
|
| 110 |
+
if problem_type == "Classification":
|
| 111 |
+
best_model = compare_classification_models(include=selected_models, fold=folds, sort=sort_metric)
|
| 112 |
+
else: # Regression
|
| 113 |
+
best_model = compare_regression_models(include=selected_models, fold=folds, sort=sort_metric)
|
| 114 |
+
else:
|
| 115 |
+
if problem_type == "Classification":
|
| 116 |
+
best_model = compare_classification_models(fold=folds, sort=sort_metric)
|
| 117 |
+
else: # Regression
|
| 118 |
+
best_model = compare_regression_models(fold=folds, sort=sort_metric)
|
| 119 |
st.session_state['best_model'] = best_model
|
| 120 |
st.success(f"Best Model: {best_model}")
|
| 121 |
|
| 122 |
+
elif problem_type == "Clustering":
|
| 123 |
+
if st.button("Create Model"):
|
| 124 |
+
with st.spinner("Creating model..."):
|
| 125 |
+
if selected_model == "kmeans":
|
| 126 |
+
best_model = create_clustering_model("kmeans", num_clusters=num_clusters)
|
| 127 |
+
elif selected_model == "dbscan":
|
| 128 |
+
best_model = create_clustering_model("dbscan", eps=eps, min_samples=min_samples)
|
| 129 |
+
elif selected_model == "hclust":
|
| 130 |
+
best_model = create_clustering_model("hclust", num_clusters=num_clusters)
|
| 131 |
+
else:
|
| 132 |
+
best_model = create_clustering_model(selected_model)
|
| 133 |
+
st.session_state['best_model'] = best_model
|
| 134 |
+
st.success(f"Model Created: {selected_model}")
|
| 135 |
+
|
| 136 |
if 'best_model' in st.session_state and st.session_state['best_model'] is not None:
|
| 137 |
+
st.subheader("Model Evaluation and Tuning")
|
| 138 |
+
col1, col2 = st.columns(2)
|
| 139 |
+
with col1:
|
| 140 |
+
if st.button("Evaluate Model"):
|
| 141 |
+
with st.spinner("Evaluating model..."):
|
| 142 |
+
if st.session_state['problem_type'] == "Classification":
|
| 143 |
+
evaluate_classification_model(st.session_state['best_model'])
|
| 144 |
+
elif st.session_state['problem_type'] == "Regression":
|
| 145 |
+
evaluate_regression_model(st.session_state['best_model'])
|
| 146 |
+
elif st.session_state['problem_type'] == "Clustering":
|
| 147 |
+
evaluate_clustering_model(st.session_state['best_model'])
|
| 148 |
+
st.success("Model evaluation complete!")
|
| 149 |
+
with col2:
|
| 150 |
+
if problem_type in ["Classification", "Regression"]:
|
| 151 |
+
if st.button("Tune Model"):
|
| 152 |
+
with st.spinner("Tuning model..."):
|
| 153 |
+
if problem_type == "Classification":
|
| 154 |
+
tuned_model = tune_classification_model(st.session_state['best_model'], fold=folds, optimize=sort_metric)
|
| 155 |
+
else: # Regression
|
| 156 |
+
tuned_model = tune_regression_model(st.session_state['best_model'], fold=folds, optimize=sort_metric)
|
| 157 |
+
st.session_state['best_model'] = tuned_model
|
| 158 |
+
st.success(f"Tuned Model: {tuned_model}")
|
| 159 |
|
| 160 |
if st.button("Save Model"):
|
| 161 |
if st.session_state['problem_type'] == "Classification":
|
|
|
|
| 171 |
elif app_mode == "Validation & Exploration":
|
| 172 |
st.title("🔍 Validation & Exploration")
|
| 173 |
if 'best_model' not in st.session_state or st.session_state['best_model'] is None:
|
| 174 |
+
st.warning("Please train a model first.")
|
| 175 |
st.stop()
|
| 176 |
|
| 177 |
st.subheader("Model Performance")
|