Spaces:
Sleeping
Sleeping
sync to remote
Browse files- app.py +76 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import pickle
|
5 |
+
import os
|
6 |
+
from huggingface_hub import hf_hub_download
|
7 |
+
from sklearn.preprocessing import StandardScaler, LabelEncoder
|
8 |
+
|
9 |
+
# Hugging Face repo details
|
10 |
+
HF_REPO_ID = "wvsu-dti-aidev-team/customer_churn_logres_model"
|
11 |
+
MODEL_FILENAME = "customer_churn_logres_model.pkl"
|
12 |
+
|
13 |
+
# Download and load the trained model
|
14 |
+
st.write("## Telco Customer Churn Prediction")
|
15 |
+
|
16 |
+
hf_token = os.getenv("HF_TOKEN")
|
17 |
+
if not hf_token:
|
18 |
+
st.error("HF_TOKEN environment variable not set. Please configure it before proceeding.")
|
19 |
+
else:
|
20 |
+
with st.spinner("Downloading the model from Hugging Face..."):
|
21 |
+
model_path = hf_hub_download(repo_id=HF_REPO_ID, filename=MODEL_FILENAME, token=hf_token)
|
22 |
+
|
23 |
+
# Load the model
|
24 |
+
with open(model_path, "rb") as f:
|
25 |
+
model = pickle.load(f)
|
26 |
+
|
27 |
+
st.success("Model loaded successfully!")
|
28 |
+
|
29 |
+
# Define feature names (from dataset)
|
30 |
+
feature_names = [
|
31 |
+
"gender", "SeniorCitizen", "Partner", "Dependents", "tenure",
|
32 |
+
"PhoneService", "MultipleLines", "InternetService", "OnlineSecurity",
|
33 |
+
"OnlineBackup", "DeviceProtection", "TechSupport", "StreamingTV",
|
34 |
+
"StreamingMovies", "Contract", "PaperlessBilling", "PaymentMethod",
|
35 |
+
"MonthlyCharges", "TotalCharges"
|
36 |
+
]
|
37 |
+
|
38 |
+
# Define categorical features for encoding
|
39 |
+
categorical_features = ["gender", "InternetService", "Contract", "PaymentMethod"]
|
40 |
+
|
41 |
+
# Create input fields for each feature
|
42 |
+
st.write("### Enter Customer Details")
|
43 |
+
|
44 |
+
user_input = {}
|
45 |
+
for feature in feature_names:
|
46 |
+
if feature in categorical_features:
|
47 |
+
user_input[feature] = st.selectbox(f"{feature}:", ["DSL", "Fiber optic", "No"])
|
48 |
+
elif feature in ["SeniorCitizen", "Partner", "Dependents", "PhoneService", "MultipleLines",
|
49 |
+
"OnlineSecurity", "OnlineBackup", "DeviceProtection", "TechSupport",
|
50 |
+
"StreamingTV", "StreamingMovies", "PaperlessBilling"]:
|
51 |
+
user_input[feature] = st.radio(f"{feature}:", [0, 1])
|
52 |
+
else:
|
53 |
+
user_input[feature] = st.number_input(f"{feature}:", min_value=0.0, step=0.1)
|
54 |
+
|
55 |
+
# Convert input to DataFrame
|
56 |
+
input_df = pd.DataFrame([user_input])
|
57 |
+
|
58 |
+
# Encode categorical features using LabelEncoder
|
59 |
+
label_encoders = {}
|
60 |
+
for feature in categorical_features:
|
61 |
+
le = LabelEncoder()
|
62 |
+
input_df[feature] = le.fit_transform(input_df[feature])
|
63 |
+
label_encoders[feature] = le
|
64 |
+
|
65 |
+
# Preprocess input: Apply scaling
|
66 |
+
scaler = StandardScaler()
|
67 |
+
input_scaled = scaler.fit_transform(input_df)
|
68 |
+
|
69 |
+
# Predict churn
|
70 |
+
if st.button("Predict Customer Churn"):
|
71 |
+
prediction = model.predict(input_scaled)[0]
|
72 |
+
st.write("## Prediction:")
|
73 |
+
if prediction == 1:
|
74 |
+
st.error("⚠️ This customer is likely to churn!")
|
75 |
+
else:
|
76 |
+
st.success("✅ This customer is likely to stay.")
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
pandas
|
3 |
+
numpy
|
4 |
+
scikit-learn
|
5 |
+
huggingface_hub
|