Spaces:
Runtime error
Runtime error
import pandas as pd | |
import streamlit as st | |
import numpy as np | |
from matplotlib import pyplot as plt | |
import pickle | |
import sklearn | |
import joblib | |
from PIL import Image | |
import base64 | |
from transformers import pipeline | |
import datetime | |
from huggingface_hub import hf_hub_download | |
REPO_ID = "rajistics/churn-model" | |
FILENAME = "churn.pkl" | |
model = joblib.load( | |
hf_hub_download(repo_id=REPO_ID, filename=FILENAME) | |
) | |
cat_imputer = joblib.load( | |
hf_hub_download(repo_id=REPO_ID, filename="categorical_imputer.joblib") | |
) | |
encoder = joblib.load( | |
hf_hub_download(repo_id=REPO_ID, filename="encoder.joblib") | |
) | |
scaler = joblib.load( | |
hf_hub_download(repo_id=REPO_ID, filename="scaler.joblib") | |
) | |
dt_model = joblib.load( | |
hf_hub_download(repo_id=REPO_ID, filename="Final_model.joblib") | |
) | |
# Add a title and subtitle | |
st.write("<center><h1>Sales Prediction App</h1></center>", unsafe_allow_html=True) | |
# Set up the layout | |
col1, col2, col3 = st.columns([1, 3, 3]) | |
#st.image("https://www.example.com/logo.png", width=200) | |
# Add a subtitle or description | |
st.write("This app uses machine learning to predict sales based on certain input parameters. Simply enter the required information and click 'Predict' to get a sales prediction!") | |
st.subheader("Enter the details to predict sales") | |
# Add some text | |
#st.write("Enter some data for Prediction.") | |
# Create the input fields | |
input_data = {} | |
col1,col2 = st.columns(2) | |
with col1: | |
input_data["gender"] = st.radio('Select your gender', ('male', 'female')) | |
input_data["SeniorCitizen"] = st.radio("Are you a Seniorcitizen; No=0 and Yes=1", ('0', '1')) | |
input_data["Partner"] = st.radio('Do you have Partner', ('Yes', 'No')) | |
input_data["Dependents"] = st.selectbox('Do you have any Dependents?', ('No', 'Yes')) | |
input_data["tenure"] = st.number_input('Lenght of tenure (no. of months with Telco)', min_value=0, max_value=90, value=1, step=1) | |
input_data["PhoneService"] = st.radio('Do you have PhoneService? ', ('No', 'Yes')) | |
input_data["MultipleLines"] = st.radio('Do you have MultipleLines', ('No', 'Yes')) | |
input_data["InternetService"] = st.radio('Do you have InternetService', ('DSL', 'Fiber optic', 'No')) | |
input_data["OnlineSecurity"] = st.radio('Do you have OnlineSecurity?', ('No', 'Yes')) | |
with col2: | |
input_data["OnlineBackup"] = st.radio('Do you have OnlineBackup?', ('No', 'Yes')) | |
input_data["DeviceProtection"] = st.radio('Do you have DeviceProtection?', ('No', 'Yes')) | |
input_data["TechSupport"] = st.radio('Do you have TechSupport?', ('No', 'Yes')) | |
input_data["StreamingTV"] = st.radio('Do you have StreamingTV?', ('No', 'Yes')) | |
input_data["StreamingMovies"] = st.radio('Do you have StreamingMovies?', ('No', 'Yes')) | |
input_data["Contract"] = st.selectbox('which Contract do you use?', ('Month-to-month', 'One year', 'Two year')) | |
input_data["PaperlessBilling"] = st.radio('Do you prefer PaperlessBilling?', ('Yes', 'No')) | |
input_data["PaymentMethod"] = st.selectbox('Which PaymentMethod do you prefer?', ('Electronic check', 'Mailed check', 'Bank transfer (automatic)', | |
'Credit card (automatic)')) | |
input_data["MonthlyCharges"] = st.number_input("Enter monthly charges (the range should between 0-120)") | |
input_data["TotalCharges"] = st.number_input("Enter total charges (the range should between 0-10.000)") | |
# Define CSS style for the download button | |
# Define the custom CSS | |
predict_button_css = """ | |
<style> | |
.predict-button { | |
background-color: #C4C4C4; | |
color: gray; | |
padding: 0.75rem 2rem; | |
border-radius: 0.5rem; | |
border: none; | |
font-size: 1.1rem; | |
font-weight: bold; | |
text-align: center; | |
margin-top: 2rem; | |
} | |
</style> | |
""" | |
# Display the custom CSS | |
st.markdown(predict_button_css, unsafe_allow_html=True) | |
# Create a button to make a prediction | |
if st.button("Predict", key="predict_button", help="Click to make a prediction."): | |
# Convert the input data to a pandas DataFrame | |
input_df = pd.DataFrame([input_data]) | |
# Selecting categorical and numerical columns separately | |
cat_columns = [col for col in input_df.columns if input_df[col].dtype == 'object'] | |
num_columns = [col for col in input_df.columns if input_df[col].dtype != 'object'] | |
# Apply the imputers | |
input_df_imputed_cat = cat_imputer.transform(input_df[cat_columns]) | |
input_df_imputed_num = num_imputer.transform(input_df[num_columns]) | |
# Encode the categorical columns | |
input_encoded_df = pd.DataFrame(encoder.transform(input_df_imputed_cat).toarray(), | |
columns=encoder.get_feature_names(cat_columns)) | |
# Scale the numerical columns | |
input_df_scaled = scaler.transform(input_df_imputed_num) | |
input_scaled_df = pd.DataFrame(input_df_scaled , columns = num_columns) | |
#joining the cat encoded and num scaled | |
final_df = pd.concat([input_encoded_df, input_scaled_df], axis=1) | |
# Make a prediction | |
prediction = dt_model.predict(final_df)[0] | |
prediction_label = "Beware!!! This customer is likely to Churn" if prediction.item() == "Yes" else "This customer is Not likely churn" | |
prediction_label | |
# Display the prediction | |
st.write(f"The predicted sales are: {prediction_label}.") | |
st.table(input_df) |