Spaces:
Sleeping
Sleeping
| # β app.py - InsightForge AI with Full Preprocessing Control | |
| import streamlit as st | |
| import pandas as pd | |
| from utils import ( | |
| load_data, preprocess_data, train_models, train_regressors, | |
| visualize_results, explain_model, detect_problem_type | |
| ) | |
| from pdf_report import generate_pdf | |
| st.set_page_config(page_title="InsightForge AI", layout="wide") | |
| st.title("π InsightForge AI - Business Intelligence AutoML Tool") | |
| with st.sidebar: | |
| st.header("π Upload Your CSV") | |
| uploaded_file = st.file_uploader("Choose a file", type=["csv"]) | |
| explain_mode = st.checkbox("π§ Explain Like I'm 5 Mode (ELI5)") | |
| user_problem_type = st.radio("π Select Problem Type", ["Auto Detect", "Regression", "Classification"]) | |
| encoding_strategy = st.selectbox("π€ Select Encoding Strategy", ["label", "onehot", "ordinal"]) | |
| if uploaded_file: | |
| df, error = load_data(uploaded_file) | |
| if error: | |
| st.error(f"β {error}") | |
| else: | |
| st.subheader("π 1. Preview of Raw Data") | |
| st.dataframe(df.head()) | |
| if st.checkbox("π Show Raw vs Clean Toggle"): | |
| st.write("π Raw Data:") | |
| st.dataframe(df.head()) | |
| st.subheader("π οΈ 2. Preprocessing Summary") | |
| detected_type = detect_problem_type(df) | |
| problem_type = user_problem_type.lower() if user_problem_type != "Auto Detect" else detected_type | |
| df_clean, preprocess_summary = preprocess_data(df, explain_mode=explain_mode, problem_type=problem_type, encoding_strategy=encoding_strategy) | |
| st.dataframe(preprocess_summary) | |
| if st.checkbox("β Show Processed Data"): | |
| st.write("π Processed Data:") | |
| st.dataframe(df_clean.head()) | |
| st.subheader("π‘οΈ 3. Correlation Heatmap") | |
| visualize_results(df_clean, stage="correlation") | |
| st.subheader("π€ 4. Modeling Results") | |
| X = df_clean.drop(columns=[df_clean.columns[-1]]) | |
| y = df_clean[df_clean.columns[-1]] | |
| if problem_type == "regression": | |
| model_results = train_regressors(X, y) | |
| metric_key = "R2 Score" | |
| else: | |
| model_results = train_models(X, y) | |
| metric_key = "Accuracy" | |
| best_model_name = max(model_results, key=lambda x: model_results[x]["metrics"].get(metric_key, 0)) | |
| best_model = model_results[best_model_name]["model"] | |
| st.dataframe(pd.DataFrame({k: v["metrics"] for k, v in model_results.items()}).T.round(3)) | |
| st.success(f"π Best Model: {best_model_name}") | |
| visualize_results(model_results, stage="model_eval", problem_type=problem_type) | |
| st.subheader("π§ 5. Explainable AI - Feature Importance") | |
| if explain_mode: | |
| if hasattr(best_model, 'feature_importances_'): | |
| explain_model(best_model, df_clean, problem_type) | |
| else: | |
| st.warning( | |
| f"β The selected best model '{best_model_name}' does not support feature importance.\n" | |
| f"Please choose a supported model (like Decision Tree)." | |
| ) | |
| explain_choice = st.selectbox("π Choose a model to explain:", options=list(model_results.keys())) | |
| chosen_model = model_results[explain_choice]["model"] | |
| if hasattr(chosen_model, 'feature_importances_'): | |
| explain_model(chosen_model, df_clean, problem_type) | |
| else: | |
| st.error(f"β οΈ The chosen model '{explain_choice}' also does not support feature importance.") | |
| else: | |
| st.info("π‘ Enable 'Explain Like I'm 5 Mode (ELI5)' from the sidebar to view feature importance.") | |
| if st.button("π₯ Download PDF Report"): | |
| report_df = pd.DataFrame({k: v["metrics"] for k, v in model_results.items()}).T.round(3) | |
| generate_pdf(df, report_df, best_model_name, problem_type) | |
| st.success("β PDF Report generated and downloaded successfully.") | |
| else: | |
| st.info("π Please upload a CSV file to begin.") |