File size: 4,039 Bytes
cf24096
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# βœ… 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.")