replica-m1-p2 / prediction.py
mukhlishr's picture
Update prediction.py
f6df849
import streamlit as st
import pandas as pd
import numpy as np
import tensorflow
from tensorflow.keras.models import load_model
import datetime
import pickle
# Load All Files
with open('final_pipeline.pkl', 'rb') as file_1:
model_pipeline = pickle.load(file_1)
model_ann = load_model('churn_model.h5')
# bikin fungsi
def run():
with st.form(key='churn_data'):
user_id = st.text_input('User ID', value='')
age = st.number_input('Age', min_value=10, max_value=70, value=25, help='Customer Age')
gender = st.selectbox('Gender', ('F','M'), index=1, help='M = Male F= Female')
region_category = st.selectbox('Region category', ('Town','Village','City'), index=1)
membership_category = st.selectbox('Membership', ('No Membership','Basic Membership','Silver Membership', 'Gold Membership','Platinum Membership','Premium Membership'), index=1)
joining_date = st.date_input('Joining date',datetime.date(2019, 7, 6))
joined_through_referral = st.selectbox('Join using referral', ('Yes','No'), index=1)
preferred_offer_types = st.selectbox('preferred offer', ('Gift Vouchers/Coupons','Without Offers','Credit/Debit Card Offers'), index=1)
medium_of_operation = st.selectbox('device ', ('Desktop','Smartphone','Both'), index=1)
internet_option= st.selectbox('Internet', ('Mobile_data','Fiber_Optic','Wi-Fi'), index=1)
days_since_last_login = st.number_input('How many days since last login', min_value=0, max_value=30, value=5)
avg_time_spent = st.number_input('Avg time login', min_value=0, max_value=3000, value=5)
avg_transaction_value = st.number_input('Avg transaction value', min_value=0, max_value=100000, value=1000)
avg_frequency_login_days= st.number_input('Avg freq login', min_value=0, max_value=100, value=5)
points_in_wallet= st.number_input('Avg time login', min_value=0, max_value=3000, value=50)
used_special_discount = st.selectbox('Spesial discount', ('Yes','No'), index=1)
offer_application_preference = st.selectbox('app preference', ('Yes','No'), index=1)
past_complaint = st.selectbox('past complaint', ('Yes','No'), index=1)
complaint_status = st.selectbox('Complain status', ('No Information Available','Not Applicable','Solved','Solved in Follow-up','Unsolved'), index=1)
feedback = st.selectbox('Feedback customer', ('User Friendly Website','Too many ads','Reasonable Price','Quality Customer Care','Products always in Stock','Poor Website','Poor Product Quality','Poor Customer Service'), index=1)
st.markdown('---')
submitted = st.form_submit_button('Predict')
data_inf = {
'user_id':user_id,
'age': age,
'gender':gender,
'region_category': region_category,
'membership_category': membership_category,
'joining_date': joining_date,
'joined_through_referral':joined_through_referral,
'preferred_offer_types': preferred_offer_types,
'medium_of_operation': medium_of_operation,
'internet_option': internet_option,
'days_since_last_login': days_since_last_login,
'avg_time_spent': avg_time_spent,
'avg_transaction_value': avg_transaction_value,
'avg_frequency_login_days': avg_frequency_login_days,
'points_in_wallet': points_in_wallet,
'used_special_discount': used_special_discount,
'offer_application_preference': offer_application_preference,
'past_complaint': past_complaint,
'complaint_status': complaint_status,
'feedback': feedback
}
data_inf = pd.DataFrame([data_inf])
# Create Binning frequency login
bins = [-1, 10, 20, 30, 40, 50, 100]
labels =[1,2,3,4,6,7]
data_inf['binned_frequency_login'] = pd.cut(data_inf['avg_frequency_login_days'], bins,labels=labels).astype(float)
st.dataframe(data_inf)
if submitted:
# transform data inference
data_inf_transform = model_pipeline.transform(data_inf)
# Predict using model ann
y_pred_inf = model_ann.predict(data_inf_transform)
y_pred_inf = np.where(y_pred_inf >= 0.5, 1, 0)
if y_pred_inf.any() == 1:
st.write('## The Customer will CHURN')
else:
st.write('## The Customer will NOT Churn')
if __name__ == '__main__':
run()