import streamlit as st import pandas as pd import numpy as np import joblib import matplotlib.pyplot as plt import seaborn as sns from sklearn.model_selection import train_test_split from sklearn.pipeline import Pipeline from sklearn.compose import ColumnTransformer from sklearn.preprocessing import OneHotEncoder, StandardScaler from sklearn.ensemble import RandomForestClassifier from sklearn.metrics import accuracy_score, confusion_matrix st.set_page_config(page_title="Lead Conversion Predictor", layout="wide") st.title("📊 ExtraaLearn Lead Conversion Predictor") # Upload dataset uploaded_file = st.file_uploader("Upload CSV Dataset", type=["csv"]) if uploaded_file: data = pd.read_csv(uploaded_file) st.subheader("Dataset Preview") st.dataframe(data.head()) # Drop ID if exists if "ID" in data.columns: data = data.drop("ID", axis=1) # Target target = "status" # Split features X = data.drop(target, axis=1) y = data[target] # Identify columns cat_cols = X.select_dtypes(include="object").columns.tolist() num_cols = X.select_dtypes(exclude="object").columns.tolist() # Preprocessing preprocessor = ColumnTransformer([ ("num", StandardScaler(), num_cols), ("cat", OneHotEncoder(handle_unknown="ignore"), cat_cols) ]) # Model model = Pipeline([ ("preprocessor", preprocessor), ("classifier", RandomForestClassifier(random_state=42)) ]) # Train-test split X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=42 ) model.fit(X_train, y_train) preds = model.predict(X_test) acc = accuracy_score(y_test, preds) st.success(f"Model Accuracy: {acc:.2f}") # Confusion Matrix st.subheader("Confusion Matrix") cm = confusion_matrix(y_test, preds) fig, ax = plt.subplots() sns.heatmap(cm, annot=True, fmt="d", ax=ax) st.pyplot(fig) # --------------------------- # EDA SECTION # --------------------------- st.subheader("📈 Exploratory Data Analysis") col1, col2 = st.columns(2) with col1: st.write("### Age Distribution") fig, ax = plt.subplots() sns.histplot(data["age"], kde=True, ax=ax) st.pyplot(fig) with col2: st.write("### Website Visits") fig, ax = plt.subplots() sns.histplot(data["website_visits"], kde=True, ax=ax) st.pyplot(fig) st.write("### Conversion by Occupation") fig, ax = plt.subplots() sns.countplot(data=data, x="current_occupation", hue="status", ax=ax) st.pyplot(fig) # --------------------------- # PREDICTION UI # --------------------------- st.subheader("🎯 Predict New Lead") input_data = {} for col in X.columns: if col in cat_cols: input_data[col] = st.selectbox(col, data[col].unique()) else: input_data[col] = st.number_input(col, float(data[col].min()), float(data[col].max())) if st.button("Predict"): input_df = pd.DataFrame([input_data]) prediction = model.predict(input_df)[0] prob = model.predict_proba(input_df)[0][1] st.success(f"Prediction: {'Converted' if prediction == 1 else 'Not Converted'}") st.info(f"Conversion Probability: {prob:.2f}")