samithcs commited on
Commit
883ae7c
·
verified ·
1 Parent(s): 5cc9a8f

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +74 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,76 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
1
  import streamlit as st
2
+ import joblib
3
+ import pandas as pd
4
+
5
+ def load_model():
6
+ model_path = "model/model.joblib"
7
+ model_dict = joblib.load(model_path)
8
+ return model_dict['model']
9
+
10
+ def main():
11
+ st.set_page_config(page_title="Customer Churn Prediction App",layout="wide")
12
+
13
+ st.title(" Customer Churn Prediction")
14
+ st.write("Predict whether a customer is likely to **churn or stay**, "
15
+ "based on behavioral and transaction-level features."
16
+ )
17
+ st.header("Input Customer Features")
18
+
19
+ Frequency = st.number_input("Frequency", min_value=0.0, value=10.0)
20
+ Monetary = st.number_input("Monetary", min_value=0.0, value=10000.0)
21
+ Total_Products_Purchased = st.number_input("Total Products Purchased", min_value=0, value=20)
22
+ Unique_Products_Purchased = st.number_input("Unique Products Purchased", min_value=0, value=10)
23
+ Avg_Transaction_Value = st.number_input("Average Transaction Value", min_value=0.0, value=50.0)
24
+ Customer_Tenure_Days = st.number_input("Customer Tenure (Days)", min_value=0, value=365)
25
+ Revenue_Per_Product = st.number_input("Revenue Per Product", min_value=0.0, value=25.0)
26
+ Avg_Days_Between_Purchases = st.number_input("Avg Days Between Purchases", min_value=0.0, value=15.0)
27
+ Purchase_Regularity = st.number_input("Purchase Regularity", min_value=0.0, value=0.5)
28
+ Top_Product_Concentration = st.number_input("Top Product Concentration", min_value=0.0, max_value=1.0, value=0.4)
29
+ Category_Diversity = st.number_input("Category Diversity", min_value=0.0, value=3.0)
30
+ Quarterly_Spending_Trend = st.number_input("Quarterly Spending Trend", value=0.1)
31
+ Price_Sensitivity = st.number_input("Price Sensitivity", min_value=0.0, value=0.3)
32
+ Spending_Trend = st.number_input("Spending Trend", value=0.05)
33
+ Cancellation_Rate = st.number_input("Cancellation Rate", min_value=0.0, max_value=1.0, value=0.1)
34
+ Is_UK = st.selectbox("Is UK Customer?", [0, 1])
35
+
36
+ input_data = pd.DataFrame(
37
+ [{
38
+ "Frequency": Frequency,
39
+ "Monetary": Monetary,
40
+ "Total_Products_Purchased": Total_Products_Purchased,
41
+ "Unique_Products_Purchased": Unique_Products_Purchased,
42
+ "Avg_Transaction_Value": Avg_Transaction_Value,
43
+ "Customer_Tenure_Days": Customer_Tenure_Days,
44
+ "Revenue_Per_Product": Revenue_Per_Product,
45
+ "Avg_Days_Between_Purchases": Avg_Days_Between_Purchases,
46
+ "Purchase_Regularity": Purchase_Regularity,
47
+ "Top_Product_Concentration": Top_Product_Concentration,
48
+ "Category_Diversity": Category_Diversity,
49
+ "Quarterly_Spending_Trend": Quarterly_Spending_Trend,
50
+ "Price_Sensitivity": Price_Sensitivity,
51
+ "Spending_Trend": Spending_Trend,
52
+ "Cancellation_Rate": Cancellation_Rate,
53
+ "Is_UK": Is_UK
54
+ }]
55
+ )
56
+
57
+
58
+ if st.button("Predict Churn"):
59
+ model = load_model()
60
+
61
+ prediction = model.predict(input_data)[0]
62
+
63
+ churn_label = "Churn" if prediction == 1 else "Not Churn"
64
+
65
+ st.subheader("Prediction Result")
66
+ st.success(f"**Customer Status:** {churn_label}")
67
+
68
+ if hasattr(model, "predict_proba"):
69
+ prob = model.predict_proba(input_data)[0][1]
70
+ st.info(f"Churn Probability: **{prob:.2%}**")
71
+
72
+ else:
73
+ st.info("Enter feature values and click **Predict Churn**")
74
 
75
+ if __name__ == "__main__":
76
+ main()