nurindahpratiwi commited on
Commit
933390a
·
1 Parent(s): 17b69a9
Files changed (2) hide show
  1. app.py +63 -29
  2. app_2.py +138 -0
app.py CHANGED
@@ -7,11 +7,9 @@ import sklearn
7
  import joblib
8
  from PIL import Image
9
  import base64
10
- from transformers import pipeline
11
- import datetime
12
  from huggingface_hub import hf_hub_download
13
 
14
- REPO_ID = "AlbieCofie/predict-customer-churn"
15
 
16
  num_imputer = joblib.load(
17
  hf_hub_download(repo_id=REPO_ID, filename="numerical_imputer.joblib")
@@ -40,7 +38,6 @@ st.write("<center><h1>Sales Prediction App</h1></center>", unsafe_allow_html=Tru
40
  col1, col2, col3 = st.columns([1, 3, 3])
41
 
42
 
43
- #st.image("https://www.example.com/logo.png", width=200)
44
  # Add a subtitle or description
45
  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!")
46
 
@@ -53,28 +50,25 @@ st.subheader("Enter the details to predict sales")
53
  input_data = {}
54
  col1,col2 = st.columns(2)
55
  with col1:
56
- input_data["gender"] = st.radio('Select your gender', ('male', 'female'))
57
- input_data["SeniorCitizen"] = st.radio("Are you a Seniorcitizen; No=0 and Yes=1", ('0', '1'))
58
- input_data["Partner"] = st.radio('Do you have Partner', ('Yes', 'No'))
59
- input_data["Dependents"] = st.selectbox('Do you have any Dependents?', ('No', 'Yes'))
60
- input_data["tenure"] = st.number_input('Lenght of tenure (no. of months with Telco)', min_value=0, max_value=90, value=1, step=1)
61
- input_data["PhoneService"] = st.radio('Do you have PhoneService? ', ('No', 'Yes'))
62
- input_data["MultipleLines"] = st.radio('Do you have MultipleLines', ('No', 'Yes'))
63
- input_data["InternetService"] = st.radio('Do you have InternetService', ('DSL', 'Fiber optic', 'No'))
64
- input_data["OnlineSecurity"] = st.radio('Do you have OnlineSecurity?', ('No', 'Yes'))
 
 
65
 
66
  with col2:
67
- input_data["OnlineBackup"] = st.radio('Do you have OnlineBackup?', ('No', 'Yes'))
68
- input_data["DeviceProtection"] = st.radio('Do you have DeviceProtection?', ('No', 'Yes'))
69
- input_data["TechSupport"] = st.radio('Do you have TechSupport?', ('No', 'Yes'))
70
- input_data["StreamingTV"] = st.radio('Do you have StreamingTV?', ('No', 'Yes'))
71
- input_data["StreamingMovies"] = st.radio('Do you have StreamingMovies?', ('No', 'Yes'))
72
- input_data["Contract"] = st.selectbox('which Contract do you use?', ('Month-to-month', 'One year', 'Two year'))
73
- input_data["PaperlessBilling"] = st.radio('Do you prefer PaperlessBilling?', ('Yes', 'No'))
74
- input_data["PaymentMethod"] = st.selectbox('Which PaymentMethod do you prefer?', ('Electronic check', 'Mailed check', 'Bank transfer (automatic)',
75
- 'Credit card (automatic)'))
76
- input_data["MonthlyCharges"] = st.number_input("Enter monthly charges (the range should between 0-120)")
77
- input_data["TotalCharges"] = st.number_input("Enter total charges (the range should between 0-10.000)")
78
 
79
 
80
  # Define CSS style for the download button
@@ -95,8 +89,24 @@ predict_button_css = """
95
  </style>
96
  """
97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  # Display the custom CSS
99
- st.markdown(predict_button_css, unsafe_allow_html=True)
100
 
101
 
102
  # Create a button to make a prediction
@@ -129,10 +139,34 @@ if st.button("Predict", key="predict_button", help="Click to make a prediction."
129
 
130
  # Make a prediction
131
  prediction = dt_model.predict(final_df)[0]
132
- prediction_label = "Beware!!! This customer is likely to Churn" if prediction.item() == "Yes" else "This customer is Not likely churn"
133
- prediction_label
134
 
135
 
136
  # Display the prediction
137
- st.write(f"The predicted sales are: {prediction_label}.")
138
- st.table(input_df)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  import joblib
8
  from PIL import Image
9
  import base64
 
 
10
  from huggingface_hub import hf_hub_download
11
 
12
+ REPO_ID = "Abubakari/Sales_Prediction"
13
 
14
  num_imputer = joblib.load(
15
  hf_hub_download(repo_id=REPO_ID, filename="numerical_imputer.joblib")
 
38
  col1, col2, col3 = st.columns([1, 3, 3])
39
 
40
 
 
41
  # Add a subtitle or description
42
  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!")
43
 
 
50
  input_data = {}
51
  col1,col2 = st.columns(2)
52
  with col1:
53
+ input_data['store_nbr'] = st.slider("store_nbr",0,54)
54
+ input_data['products'] = st.selectbox("products", ['AUTOMOTIVE', 'CLEANING', 'BEAUTY', 'FOODS', 'STATIONERY',
55
+ 'CELEBRATION', 'GROCERY', 'HARDWARE', 'HOME', 'LADIESWEAR',
56
+ 'LAWN AND GARDEN', 'CLOTHING', 'LIQUOR,WINE,BEER', 'PET SUPPLIES'])
57
+ input_data['onpromotion'] =st.number_input("onpromotion",step=1)
58
+ input_data['state'] = st.selectbox("state", ['Pichincha', 'Cotopaxi', 'Chimborazo', 'Imbabura',
59
+ 'Santo Domingo de los Tsachilas', 'Bolivar', 'Pastaza',
60
+ 'Tungurahua', 'Guayas', 'Santa Elena', 'Los Rios', 'Azuay', 'Loja',
61
+ 'El Oro', 'Esmeraldas', 'Manabi'])
62
+ input_data['store_type'] = st.selectbox("store_type",['D', 'C', 'B', 'E', 'A'])
63
+ input_data['cluster'] = st.number_input("cluster",step=1)
64
 
65
  with col2:
66
+ input_data['dcoilwtico'] = st.number_input("dcoilwtico",step=1)
67
+ input_data['year'] = st.number_input("year",step=1)
68
+ input_data['month'] = st.slider("month",1,12)
69
+ input_data['day'] = st.slider("day",1,31)
70
+ input_data['dayofweek'] = st.number_input("dayofweek,0=Sun and 6=Sat",step=1)
71
+ input_data['end_month'] = st.selectbox("end_month",['True','False'])
 
 
 
 
 
72
 
73
 
74
  # Define CSS style for the download button
 
89
  </style>
90
  """
91
 
92
+ download_button_css = """
93
+ <style>
94
+ .download-button {
95
+ background-color: #C4C4C4;
96
+ color: white;
97
+ padding: 0.75rem 2rem;
98
+ border-radius: 0.5rem;
99
+ border: none;
100
+ font-size: 1.1rem;
101
+ font-weight: bold;
102
+ text-align: center;
103
+ margin-top: 1rem;
104
+ }
105
+ </style>
106
+ """
107
+
108
  # Display the custom CSS
109
+ st.markdown(predict_button_css + download_button_css, unsafe_allow_html=True)
110
 
111
 
112
  # Create a button to make a prediction
 
139
 
140
  # Make a prediction
141
  prediction = dt_model.predict(final_df)[0]
 
 
142
 
143
 
144
  # Display the prediction
145
+ st.write(f"The predicted sales are: {prediction}.")
146
+ input_df.to_csv("data.csv", index=False)
147
+ st.table(input_df)
148
+
149
+ # Define custom CSS
150
+ css = """
151
+ table {
152
+ background-color: #f2f2f2;
153
+ color: #333333;
154
+ }
155
+ """
156
+
157
+ # Set custom CSS
158
+ st.write(f'<style>{css}</style>', unsafe_allow_html=True)
159
+
160
+
161
+ # Add the download button
162
+ def download_csv():
163
+ with open("data.csv", "r") as f:
164
+ csv = f.read()
165
+ b64 = base64.b64encode(csv.encode()).decode()
166
+ button = f'<button class="download-button"><a href="data:file/csv;base64,{b64}" download="data.csv">Download Data CSV</a></button>'
167
+ return button
168
+
169
+ st.markdown(
170
+ f'<div style="text-align: center">{download_csv()}</div>',
171
+ unsafe_allow_html=True
172
+ )
app_2.py ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import streamlit as st
3
+ import numpy as np
4
+ from matplotlib import pyplot as plt
5
+ import pickle
6
+ import sklearn
7
+ import joblib
8
+ from PIL import Image
9
+ import base64
10
+ from transformers import pipeline
11
+ import datetime
12
+ from huggingface_hub import hf_hub_download
13
+
14
+ REPO_ID = "AlbieCofie/predict-customer-churn"
15
+
16
+ num_imputer = joblib.load(
17
+ hf_hub_download(repo_id=REPO_ID, filename="numerical_imputer.joblib")
18
+ )
19
+
20
+ cat_imputer = joblib.load(
21
+ hf_hub_download(repo_id=REPO_ID, filename="categorical_imputer.joblib")
22
+ )
23
+
24
+ encoder = joblib.load(
25
+ hf_hub_download(repo_id=REPO_ID, filename="encoder.joblib")
26
+ )
27
+
28
+ scaler = joblib.load(
29
+ hf_hub_download(repo_id=REPO_ID, filename="scaler.joblib")
30
+ )
31
+
32
+ dt_model = joblib.load(
33
+ hf_hub_download(repo_id=REPO_ID, filename="Final_model.joblib")
34
+ )
35
+
36
+ # Add a title and subtitle
37
+ st.write("<center><h1>Sales Prediction App</h1></center>", unsafe_allow_html=True)
38
+
39
+ # Set up the layout
40
+ col1, col2, col3 = st.columns([1, 3, 3])
41
+
42
+
43
+ #st.image("https://www.example.com/logo.png", width=200)
44
+ # Add a subtitle or description
45
+ 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!")
46
+
47
+ st.subheader("Enter the details to predict sales")
48
+
49
+ # Add some text
50
+ #st.write("Enter some data for Prediction.")
51
+
52
+ # Create the input fields
53
+ input_data = {}
54
+ col1,col2 = st.columns(2)
55
+ with col1:
56
+ input_data["gender"] = st.radio('Select your gender', ('male', 'female'))
57
+ input_data["SeniorCitizen"] = st.radio("Are you a Seniorcitizen; No=0 and Yes=1", ('0', '1'))
58
+ input_data["Partner"] = st.radio('Do you have Partner', ('Yes', 'No'))
59
+ input_data["Dependents"] = st.selectbox('Do you have any Dependents?', ('No', 'Yes'))
60
+ input_data["tenure"] = st.number_input('Lenght of tenure (no. of months with Telco)', min_value=0, max_value=90, value=1, step=1)
61
+ input_data["PhoneService"] = st.radio('Do you have PhoneService? ', ('No', 'Yes'))
62
+ input_data["MultipleLines"] = st.radio('Do you have MultipleLines', ('No', 'Yes'))
63
+ input_data["InternetService"] = st.radio('Do you have InternetService', ('DSL', 'Fiber optic', 'No'))
64
+ input_data["OnlineSecurity"] = st.radio('Do you have OnlineSecurity?', ('No', 'Yes'))
65
+
66
+ with col2:
67
+ input_data["OnlineBackup"] = st.radio('Do you have OnlineBackup?', ('No', 'Yes'))
68
+ input_data["DeviceProtection"] = st.radio('Do you have DeviceProtection?', ('No', 'Yes'))
69
+ input_data["TechSupport"] = st.radio('Do you have TechSupport?', ('No', 'Yes'))
70
+ input_data["StreamingTV"] = st.radio('Do you have StreamingTV?', ('No', 'Yes'))
71
+ input_data["StreamingMovies"] = st.radio('Do you have StreamingMovies?', ('No', 'Yes'))
72
+ input_data["Contract"] = st.selectbox('which Contract do you use?', ('Month-to-month', 'One year', 'Two year'))
73
+ input_data["PaperlessBilling"] = st.radio('Do you prefer PaperlessBilling?', ('Yes', 'No'))
74
+ input_data["PaymentMethod"] = st.selectbox('Which PaymentMethod do you prefer?', ('Electronic check', 'Mailed check', 'Bank transfer (automatic)',
75
+ 'Credit card (automatic)'))
76
+ input_data["MonthlyCharges"] = st.number_input("Enter monthly charges (the range should between 0-120)")
77
+ input_data["TotalCharges"] = st.number_input("Enter total charges (the range should between 0-10.000)")
78
+
79
+
80
+ # Define CSS style for the download button
81
+ # Define the custom CSS
82
+ predict_button_css = """
83
+ <style>
84
+ .predict-button {
85
+ background-color: #C4C4C4;
86
+ color: gray;
87
+ padding: 0.75rem 2rem;
88
+ border-radius: 0.5rem;
89
+ border: none;
90
+ font-size: 1.1rem;
91
+ font-weight: bold;
92
+ text-align: center;
93
+ margin-top: 2rem;
94
+ }
95
+ </style>
96
+ """
97
+
98
+ # Display the custom CSS
99
+ st.markdown(predict_button_css, unsafe_allow_html=True)
100
+
101
+
102
+ # Create a button to make a prediction
103
+
104
+ if st.button("Predict", key="predict_button", help="Click to make a prediction."):
105
+ # Convert the input data to a pandas DataFrame
106
+ input_df = pd.DataFrame([input_data])
107
+
108
+
109
+ # Selecting categorical and numerical columns separately
110
+ cat_columns = [col for col in input_df.columns if input_df[col].dtype == 'object']
111
+ num_columns = [col for col in input_df.columns if input_df[col].dtype != 'object']
112
+
113
+
114
+ # Apply the imputers
115
+ input_df_imputed_cat = cat_imputer.transform(input_df[cat_columns])
116
+ input_df_imputed_num = num_imputer.transform(input_df[num_columns])
117
+
118
+
119
+ # Encode the categorical columns
120
+ input_encoded_df = pd.DataFrame(encoder.transform(input_df_imputed_cat).toarray(),
121
+ columns=encoder.get_feature_names(cat_columns))
122
+
123
+ # Scale the numerical columns
124
+ input_df_scaled = scaler.transform(input_df_imputed_num)
125
+ input_scaled_df = pd.DataFrame(input_df_scaled , columns = num_columns)
126
+
127
+ #joining the cat encoded and num scaled
128
+ final_df = pd.concat([input_encoded_df, input_scaled_df], axis=1)
129
+
130
+ # Make a prediction
131
+ prediction = dt_model.predict(final_df)[0]
132
+ prediction_label = "Beware!!! This customer is likely to Churn" if prediction.item() == "Yes" else "This customer is Not likely churn"
133
+ prediction_label
134
+
135
+
136
+ # Display the prediction
137
+ st.write(f"The predicted sales are: {prediction_label}.")
138
+ st.table(input_df)