Gowthamvemula's picture
Update app.py
9461fb0 verified
import streamlit as st
import pandas as pd
import numpy as np
import pickle
# Set page configuration
st.set_page_config(page_title="🎯 Direct Message Influence Predictor", layout="wide")
# Custom background and styling
st.markdown("""
<style>
body {
background-color: #080808;
}
.stApp {
background-color: #080808;
}
.title-text {
color: #00FFFF;
font-size: 32px;
font-weight: bold;
}
.sidebar-title {
font-size: 18px;
font-weight: bold;
}
.feature-box {
background-color: #080808 ;
padding: 15px;
border-radius: 10px;
box-shadow: 0px 2px 6px rgba(0,0,0,0.1);
}
</style>
""", unsafe_allow_html=True)
# Load the model
try:
with open("best_random_forest_model (1).pkl", "rb") as file:
model = pickle.load(file)
except FileNotFoundError:
st.error("❌ Model file 'best_random_forest_model (1).pkl' not found. Please make sure it is in the same directory as this app.")
st.stop()
# Title
st.markdown('<div class="title-text">🎯 Coupon Influence Prediction</div>', unsafe_allow_html=True)
st.write("Fill in the customer data below to predict if the direct mail coupon was effective.")
# Two-column layout
col1, col2 = st.columns([1, 1])
with col1:
st.markdown("### 🧾 Customer Input")
with st.form(key="predict_form"):
used_coupon = st.selectbox("Used Coupon After DM?", [0, 1])
after_spend = st.number_input("Spend After DM ($)", min_value=0.0, format="%.2f")
after_units = st.number_input("Units After DM", min_value=0)
before_spend = st.number_input("Spend Before DM ($)", min_value=0.0, format="%.2f")
submit_button = st.form_submit_button("πŸ” Predict")
if submit_button:
input_data = pd.DataFrame([[used_coupon, after_spend, after_units, before_spend]],
columns=["used_coupon_after_dm", "after_spend", "after_units", "before_spend"])
prediction = model.predict(input_data)[0]
proba = model.predict_proba(input_data)[0][prediction]
label = "βœ… Influenced by DM" if prediction == 1 else "❌ Not Influenced"
st.success(f"**Prediction:** {label}")
st.info(f"**Confidence:** {proba * 100:.2f}%")
with col2:
st.markdown("### πŸ“˜ Feature Descriptions")
st.markdown("""
<div class='feature-box'>
<ul>
<li><b>Used Coupon After DM?</b><br>Whether the customer used the direct mail (DM) coupon. <br><code>1 = Yes</code>, <code>0 = No</code></li><br>
<li><b>Spend After DM ($)</b><br>Total money the customer spent <b>after</b> receiving the direct mail coupon.</li><br>
<li><b>Units After DM</b><br>Number of items purchased <b>after</b> receiving the coupon.</li><br>
<li><b>Spend Before DM ($)</b><br>Total money spent <b>before</b> receiving the direct mail coupon.</li>
</ul>
</div>
""", unsafe_allow_html=True)