churn_Prediction / predict.py
William14045's picture
commit1
a8914ff
raw
history blame
4.64 kB
import streamlit as st
from tensorflow.keras.models import load_model
import pandas as pd
import numpy as np
import pickle
from datetime import datetime , time
# Load All Files
with open('prep_pipeline.pkl','rb') as file1:
prep = pickle.load(file1)
ann = load_model('churn_model.h5')
tanggal_waktu_sekarang = datetime.now().time()
waktu_sekarang = datetime.now().date()
def run():
with st.form('key=Churn_prediction'):
user_id = st.text_input('user_id:', '')
age = st.number_input("age:" ,min_value=18 , max_value=100,step=1)
gender = st.radio('gender', ('F','M'))
region_category = st.radio('region_category', ('Town','City','Village'))
membership_category = st.selectbox('membership_category', ('No Membership','Basic Membership','Silver Membership','Gold Membership','Platinum Membership','Premium Membership'), index=1)
st.markdown('---')
joining_date = st.date_input('joining_date', waktu_sekarang)
joined_through_referral = st.radio('joined_through_referral',(True,False))
preferred_offer_types = st.selectbox('preferred_offer_types',('Gift Vouchers/Coupons','Credit/Debit Card Offers','Without Offers'))
medium_of_operation = st.selectbox('medium_of_operation',('Desktop','Smartphone','Both'))
internet_option = st.radio('internet_option',('Wi-Fi','Mobile_Data','Fiber_Optic'))
last_visit_time = st.time_input('last_visit_time', tanggal_waktu_sekarang)
days_since_last_login = st.number_input('days_since_last_login', min_value=0 , max_value=1000,step=1)
avg_time_spent = st.number_input('avg_time_spent' , min_value=0 , max_value=3300,step=1)
avg_transaction_value = st.number_input('avg_transaction_value', min_value=500 , max_value=100000,step=5)
avg_frequency_login_days = st.number_input('avg_frequency_login_days', min_value=0 , max_value=75,step=1)
points_in_wallet = st.number_input('points_in_wallet',min_value=0 , max_value=2100,step=5)
used_special_discount = st.radio('used_special_discount', ('Yes','No'))
offer_application_preference = st.radio('offer_application_preference',('Yes','No'))
past_complaint = st.radio('past_complaint',('Yes','No'))
complaint_status = st.selectbox('complaint_status', ('Not Applicable' , 'Unsolved','Solved','Solved in Follow-up','No Information Available'))
feedback = st.selectbox('feedback', ('Poor Product Quality','No reason specified','Too many ads','Poor Website','Poor Customer Service','Reasonable Price','User Friendly Website','Products always in Stock','Quality Customer Care'))
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,
'last_visit_time': last_visit_time,
'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])
positive_words = ['reasonable', 'friendly', 'always', 'stock','care','price','user','products','in']
negative_words = ['poor', 'too', 'many', 'terrible','product quality','ads','service']
def classify_sentiment(text):
words = text.lower().split()
num_positive = sum(1 for word in words if word in positive_words)
num_negative = sum(1 for word in words if word in negative_words)
if num_positive > num_negative:
return 'positif'
elif num_positive < num_negative:
return 'negatif'
else:
return 'netral'
# Menerapkan fungsi klasifikasi sentimen menggunakan apply pada DataFrame
data_inf['sentimen'] = data_inf['feedback'].apply(classify_sentiment)
final_data = prep.transform(data_inf)
if submitted:
y_pred_inf = ann.predict(final_data)
y_pred_inf = np.where(y_pred_inf >= 0.5 , 'Churn' , 'Not Churn')
st.write('Price of the car : ', str(y_pred_inf))
if __name__ == '__main__':
run()