customer_churn / app.py
louiecerv's picture
sync to remote
6ba565a
import streamlit as st
import pandas as pd
import numpy as np
import pickle
import os
from huggingface_hub import hf_hub_download
from sklearn.preprocessing import StandardScaler, LabelEncoder
# Hugging Face repo details
HF_REPO_ID = "wvsu-dti-aidev-team/customer_churn_logres_model"
MODEL_FILENAME = "customer_churn_logres_model.pkl"
# Download and load the trained model
st.write("## Telco Customer Churn Prediction")
hf_token = os.getenv("HF_TOKEN")
if not hf_token:
st.error("HF_TOKEN environment variable not set. Please configure it before proceeding.")
else:
with st.spinner("Downloading the model from Hugging Face..."):
model_path = hf_hub_download(repo_id=HF_REPO_ID, filename=MODEL_FILENAME, token=hf_token)
# Load the model
with open(model_path, "rb") as f:
model = pickle.load(f)
st.success("Model loaded successfully!")
# Define feature names (from dataset)
feature_names = [
"gender", "SeniorCitizen", "Partner", "Dependents", "tenure",
"PhoneService", "MultipleLines", "InternetService", "OnlineSecurity",
"OnlineBackup", "DeviceProtection", "TechSupport", "StreamingTV",
"StreamingMovies", "Contract", "PaperlessBilling", "PaymentMethod",
"MonthlyCharges", "TotalCharges"
]
# Define categorical features for encoding
categorical_features = ["gender", "InternetService", "Contract", "PaymentMethod"]
# Create input fields for each feature
st.write("### Enter Customer Details")
user_input = {}
for feature in feature_names:
if feature in categorical_features:
user_input[feature] = st.selectbox(f"{feature}:", ["DSL", "Fiber optic", "No"])
elif feature in ["SeniorCitizen", "Partner", "Dependents", "PhoneService", "MultipleLines",
"OnlineSecurity", "OnlineBackup", "DeviceProtection", "TechSupport",
"StreamingTV", "StreamingMovies", "PaperlessBilling"]:
user_input[feature] = st.radio(f"{feature}:", [0, 1])
else:
user_input[feature] = st.number_input(f"{feature}:", min_value=0.0, step=0.1)
# Convert input to DataFrame
input_df = pd.DataFrame([user_input])
# Encode categorical features using LabelEncoder
label_encoders = {}
for feature in categorical_features:
le = LabelEncoder()
input_df[feature] = le.fit_transform(input_df[feature])
label_encoders[feature] = le
# Preprocess input: Apply scaling
scaler = StandardScaler()
input_scaled = scaler.fit_transform(input_df)
# Predict churn
if st.button("Predict Customer Churn"):
prediction = model.predict(input_scaled)[0]
st.write("## Prediction:")
if prediction == 1:
st.error("⚠️ This customer is likely to churn!")
else:
st.success("✅ This customer is likely to stay.")