# ✅ 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.")