nurindahpratiwi commited on
Commit
f31f205
1 Parent(s): faa8739

change name

Browse files
Files changed (3) hide show
  1. app.py +61 -129
  2. app_2.py +139 -0
  3. app_3.py +0 -71
app.py CHANGED
@@ -1,139 +1,71 @@
 
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
- access_token = st.secrets["HF_TOKEN"]
16
-
17
- cat_imputer = joblib.load(
18
- hf_hub_download(repo_id=REPO_ID, filename="categorical_imputer.joblib", token=access_token, repo_type="model")
19
- )
20
-
21
- encoder = joblib.load(
22
- hf_hub_download(repo_id=REPO_ID, filename="encoder.joblib", token=access_token, repo_type="model")
23
- )
24
-
25
- num_imputer = joblib.load(
26
- hf_hub_download(repo_id=REPO_ID, filename="numerical_imputer.joblib", token=access_token, repo_type="model")
27
- )
28
 
29
- scaler = joblib.load(
30
- hf_hub_download(repo_id=REPO_ID, filename="scaler.joblib", token=access_token, repo_type="model")
31
- )
32
 
33
  model = joblib.load(
34
- hf_hub_download(repo_id=REPO_ID, filename="Final_model.joblib", token=access_token, repo_type="model")
35
  )
36
 
37
- # Add a title and subtitle
38
- st.write("<center><h1>Sales Prediction App</h1></center>", unsafe_allow_html=True)
39
-
40
- # Set up the layout
41
- col1, col2, col3 = st.columns([1, 3, 3])
42
-
43
-
44
- #st.image("https://www.example.com/logo.png", width=200)
45
- # Add a subtitle or description
46
- 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!")
47
-
48
- st.subheader("Enter the details to predict sales")
49
-
50
- # Add some text
51
- #st.write("Enter some data for Prediction.")
52
-
53
- # Create the input fields
54
- input_data = {}
55
- col1,col2 = st.columns(2)
56
- with col1:
57
- input_data["gender"] = st.radio('Select your gender', ('male', 'female'))
58
- input_data["SeniorCitizen"] = st.radio("Are you a Seniorcitizen; No=0 and Yes=1", ('0', '1'))
59
- input_data["Partner"] = st.radio('Do you have Partner', ('Yes', 'No'))
60
- input_data["Dependents"] = st.selectbox('Do you have any Dependents?', ('No', 'Yes'))
61
- input_data["tenure"] = st.number_input('Lenght of tenure (no. of months with Telco)', min_value=0, max_value=90, value=1, step=1)
62
- input_data["PhoneService"] = st.radio('Do you have PhoneService? ', ('No', 'Yes'))
63
- input_data["MultipleLines"] = st.radio('Do you have MultipleLines', ('No', 'Yes'))
64
- input_data["InternetService"] = st.radio('Do you have InternetService', ('DSL', 'Fiber optic', 'No'))
65
- input_data["OnlineSecurity"] = st.radio('Do you have OnlineSecurity?', ('No', 'Yes'))
66
-
67
- with col2:
68
- input_data["OnlineBackup"] = st.radio('Do you have OnlineBackup?', ('No', 'Yes'))
69
- input_data["DeviceProtection"] = st.radio('Do you have DeviceProtection?', ('No', 'Yes'))
70
- input_data["TechSupport"] = st.radio('Do you have TechSupport?', ('No', 'Yes'))
71
- input_data["StreamingTV"] = st.radio('Do you have StreamingTV?', ('No', 'Yes'))
72
- input_data["StreamingMovies"] = st.radio('Do you have StreamingMovies?', ('No', 'Yes'))
73
- input_data["Contract"] = st.selectbox('which Contract do you use?', ('Month-to-month', 'One year', 'Two year'))
74
- input_data["PaperlessBilling"] = st.radio('Do you prefer PaperlessBilling?', ('Yes', 'No'))
75
- input_data["PaymentMethod"] = st.selectbox('Which PaymentMethod do you prefer?', ('Electronic check', 'Mailed check', 'Bank transfer (automatic)',
76
- 'Credit card (automatic)'))
77
- input_data["MonthlyCharges"] = st.number_input("Enter monthly charges (the range should between 0-120)")
78
- input_data["TotalCharges"] = st.number_input("Enter total charges (the range should between 0-10.000)")
79
-
80
-
81
- # Define CSS style for the download button
82
- # Define the custom CSS
83
- predict_button_css = """
84
- <style>
85
- .predict-button {
86
- background-color: #C4C4C4;
87
- color: gray;
88
- padding: 0.75rem 2rem;
89
- border-radius: 0.5rem;
90
- border: none;
91
- font-size: 1.1rem;
92
- font-weight: bold;
93
- text-align: center;
94
- margin-top: 2rem;
95
- }
96
- </style>
97
- """
98
-
99
- # Display the custom CSS
100
- st.markdown(predict_button_css, unsafe_allow_html=True)
101
-
102
-
103
- # Create a button to make a prediction
104
-
105
- if st.button("Predict", key="predict_button", help="Click to make a prediction."):
106
- # Convert the input data to a pandas DataFrame
107
- input_df = pd.DataFrame([input_data])
108
-
109
-
110
- # Selecting categorical and numerical columns separately
111
- cat_columns = [col for col in input_df.columns if input_df[col].dtype == 'object']
112
- num_columns = [col for col in input_df.columns if input_df[col].dtype != 'object']
113
-
114
-
115
- # Apply the imputers
116
- input_df_imputed_cat = cat_imputer.transform(input_df[cat_columns])
117
- input_df_imputed_num = num_imputer.transform(input_df[num_columns])
118
-
119
-
120
- # Encode the categorical columns
121
- input_encoded_df = pd.DataFrame(encoder.transform(input_df_imputed_cat).toarray(),
122
- columns=encoder.get_feature_names(cat_columns))
123
-
124
- # Scale the numerical columns
125
- input_df_scaled = scaler.transform(input_df_imputed_num)
126
- input_scaled_df = pd.DataFrame(input_df_scaled , columns = num_columns)
127
-
128
- #joining the cat encoded and num scaled
129
- final_df = pd.concat([input_encoded_df, input_scaled_df], axis=1)
130
-
131
- # Make a prediction
132
- prediction = model.predict(final_df)[0]
133
- prediction_label = "Beware!!! This customer is likely to Churn" if prediction.item() == "Yes" else "This customer is Not likely churn"
134
- prediction_label
135
-
136
 
137
- # Display the prediction
138
- st.write(f"The predicted sales are: {prediction_label}.")
139
- st.table(input_df)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import joblib
2
  import pandas as pd
3
  import streamlit as st
 
 
 
 
 
 
 
 
 
4
  from huggingface_hub import hf_hub_download
5
 
6
+ REPO_ID = "chanyaphas/creditc"
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
+ access_token = st.secrets["HF_TOKEN"]
 
 
9
 
10
  model = joblib.load(
11
+ hf_hub_download(repo_id=REPO_ID, filename='model.joblib', token=access_token, repo_type="space")
12
  )
13
 
14
+ unique_values = joblib.load(
15
+ hf_hub_download(repo_id=REPO_ID, filename='unique_values.joblib', token=access_token, repo_type="space")
16
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
+ EDU_DICT = {'Lower secondary': 1,
19
+ 'Secondary / secondary special': 2,
20
+ 'Academic degree': 3,
21
+ 'Incomplete higher': 4,
22
+ 'Higher education' : 5
23
+ }
24
+
25
+ def main():
26
+ st.title("Credit Card Approval Prediction")
27
+
28
+ with st.form("questionaire"):
29
+
30
+ Gender = st.selectbox('Gender', unique_values['CODE_GENDER'])
31
+ Own_car = st.selectbox('Own_car', unique_values['FLAG_OWN_CAR'])
32
+ Property = st.selectbox('Property', unique_values['FLAG_OWN_REALTY'])
33
+ Income_type = st.selectbox('Income_type', unique_values['NAME_INCOME_TYPE'])
34
+ Marital_status = st.selectbox('Marital_status', unique_values['NAME_FAMILY_STATUS'])
35
+ Housing_type = st.selectbox('Housing_type', unique_values['NAME_HOUSING_TYPE'])
36
+ Education = st.selectbox('Education', unique_values['NAME_EDUCATION_TYPE'])
37
+
38
+ Income = st.slider('Income', min_value=27000, max_value=1575000)
39
+ Children = st.slider('Children', min_value=0, max_value=19)
40
+ Day_Employed = st.slider('Day_Employed', min_value=0, max_value=3)
41
+ Flag_Mobile = st.slider('Flag_Mobile', min_value=0, max_value=1)
42
+ Flag_work_phone = st.slider('Flag_work_phone', min_value=0, max_value=1)
43
+ Flag_Phone = st.slider('Flag_Phone', min_value=0, max_value=1)
44
+ Flag_Email = st.slider('Flag_Email', min_value=0, max_value=1)
45
+ Family_mem = st.slider('Family_mem', min_value=1, max_value=20)
46
+
47
+ clicked = st.form_submit_button("Result")
48
+ if clicked:
49
+ result = model.predict(pd.DataFrame({
50
+ "CODE_GENDER": [Gender],
51
+ "FLAG_OWN_CAR": [Own_car],
52
+ "FLAG_OWN_REALTY": [Property],
53
+ "CNT_CHILDREN": [Children],
54
+ "AMT_INCOME_TOTAL": [Income],
55
+ "NAME_INCOME_TYPE": [Income_type],
56
+ "NAME_EDUCATION_TYPE": [EDU_DICT[Education]],
57
+ "NAME_FAMILY_STATUS": [Marital_status],
58
+ "NAME_HOUSING_TYPE": [Housing_type],
59
+ "DAYS_EMPLOYED": [Day_Employed],
60
+ "FLAG_MOBIL": [Flag_Mobile],
61
+ "FLAG_WORK_PHONE": [Flag_work_phone],
62
+ "FLAG_PHONE": [Flag_Phone],
63
+ "FLAG_EMAIL": [Flag_Email],
64
+ "CNT_FAM_MEMBERS": [Family_mem]}))
65
+
66
+ result = 'Pass' if result[0] == 1 else 'Did not Pass'
67
+
68
+ st.success('Credit Card approval prediction results is {}'.format(result))
69
+
70
+ if __name__ == '__main__':
71
+ main()
app_2.py ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ access_token = st.secrets["HF_TOKEN"]
16
+
17
+ cat_imputer = joblib.load(
18
+ hf_hub_download(repo_id=REPO_ID, filename="categorical_imputer.joblib", token=access_token, repo_type="model")
19
+ )
20
+
21
+ encoder = joblib.load(
22
+ hf_hub_download(repo_id=REPO_ID, filename="encoder.joblib", token=access_token, repo_type="model")
23
+ )
24
+
25
+ num_imputer = joblib.load(
26
+ hf_hub_download(repo_id=REPO_ID, filename="numerical_imputer.joblib", token=access_token, repo_type="model")
27
+ )
28
+
29
+ scaler = joblib.load(
30
+ hf_hub_download(repo_id=REPO_ID, filename="scaler.joblib", token=access_token, repo_type="model")
31
+ )
32
+
33
+ model = joblib.load(
34
+ hf_hub_download(repo_id=REPO_ID, filename="Final_model.joblib", token=access_token, repo_type="model")
35
+ )
36
+
37
+ # Add a title and subtitle
38
+ st.write("<center><h1>Sales Prediction App</h1></center>", unsafe_allow_html=True)
39
+
40
+ # Set up the layout
41
+ col1, col2, col3 = st.columns([1, 3, 3])
42
+
43
+
44
+ #st.image("https://www.example.com/logo.png", width=200)
45
+ # Add a subtitle or description
46
+ 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!")
47
+
48
+ st.subheader("Enter the details to predict sales")
49
+
50
+ # Add some text
51
+ #st.write("Enter some data for Prediction.")
52
+
53
+ # Create the input fields
54
+ input_data = {}
55
+ col1,col2 = st.columns(2)
56
+ with col1:
57
+ input_data["gender"] = st.radio('Select your gender', ('male', 'female'))
58
+ input_data["SeniorCitizen"] = st.radio("Are you a Seniorcitizen; No=0 and Yes=1", ('0', '1'))
59
+ input_data["Partner"] = st.radio('Do you have Partner', ('Yes', 'No'))
60
+ input_data["Dependents"] = st.selectbox('Do you have any Dependents?', ('No', 'Yes'))
61
+ input_data["tenure"] = st.number_input('Lenght of tenure (no. of months with Telco)', min_value=0, max_value=90, value=1, step=1)
62
+ input_data["PhoneService"] = st.radio('Do you have PhoneService? ', ('No', 'Yes'))
63
+ input_data["MultipleLines"] = st.radio('Do you have MultipleLines', ('No', 'Yes'))
64
+ input_data["InternetService"] = st.radio('Do you have InternetService', ('DSL', 'Fiber optic', 'No'))
65
+ input_data["OnlineSecurity"] = st.radio('Do you have OnlineSecurity?', ('No', 'Yes'))
66
+
67
+ with col2:
68
+ input_data["OnlineBackup"] = st.radio('Do you have OnlineBackup?', ('No', 'Yes'))
69
+ input_data["DeviceProtection"] = st.radio('Do you have DeviceProtection?', ('No', 'Yes'))
70
+ input_data["TechSupport"] = st.radio('Do you have TechSupport?', ('No', 'Yes'))
71
+ input_data["StreamingTV"] = st.radio('Do you have StreamingTV?', ('No', 'Yes'))
72
+ input_data["StreamingMovies"] = st.radio('Do you have StreamingMovies?', ('No', 'Yes'))
73
+ input_data["Contract"] = st.selectbox('which Contract do you use?', ('Month-to-month', 'One year', 'Two year'))
74
+ input_data["PaperlessBilling"] = st.radio('Do you prefer PaperlessBilling?', ('Yes', 'No'))
75
+ input_data["PaymentMethod"] = st.selectbox('Which PaymentMethod do you prefer?', ('Electronic check', 'Mailed check', 'Bank transfer (automatic)',
76
+ 'Credit card (automatic)'))
77
+ input_data["MonthlyCharges"] = st.number_input("Enter monthly charges (the range should between 0-120)")
78
+ input_data["TotalCharges"] = st.number_input("Enter total charges (the range should between 0-10.000)")
79
+
80
+
81
+ # Define CSS style for the download button
82
+ # Define the custom CSS
83
+ predict_button_css = """
84
+ <style>
85
+ .predict-button {
86
+ background-color: #C4C4C4;
87
+ color: gray;
88
+ padding: 0.75rem 2rem;
89
+ border-radius: 0.5rem;
90
+ border: none;
91
+ font-size: 1.1rem;
92
+ font-weight: bold;
93
+ text-align: center;
94
+ margin-top: 2rem;
95
+ }
96
+ </style>
97
+ """
98
+
99
+ # Display the custom CSS
100
+ st.markdown(predict_button_css, unsafe_allow_html=True)
101
+
102
+
103
+ # Create a button to make a prediction
104
+
105
+ if st.button("Predict", key="predict_button", help="Click to make a prediction."):
106
+ # Convert the input data to a pandas DataFrame
107
+ input_df = pd.DataFrame([input_data])
108
+
109
+
110
+ # Selecting categorical and numerical columns separately
111
+ cat_columns = [col for col in input_df.columns if input_df[col].dtype == 'object']
112
+ num_columns = [col for col in input_df.columns if input_df[col].dtype != 'object']
113
+
114
+
115
+ # Apply the imputers
116
+ input_df_imputed_cat = cat_imputer.transform(input_df[cat_columns])
117
+ input_df_imputed_num = num_imputer.transform(input_df[num_columns])
118
+
119
+
120
+ # Encode the categorical columns
121
+ input_encoded_df = pd.DataFrame(encoder.transform(input_df_imputed_cat).toarray(),
122
+ columns=encoder.get_feature_names(cat_columns))
123
+
124
+ # Scale the numerical columns
125
+ input_df_scaled = scaler.transform(input_df_imputed_num)
126
+ input_scaled_df = pd.DataFrame(input_df_scaled , columns = num_columns)
127
+
128
+ #joining the cat encoded and num scaled
129
+ final_df = pd.concat([input_encoded_df, input_scaled_df], axis=1)
130
+
131
+ # Make a prediction
132
+ prediction = model.predict(final_df)[0]
133
+ prediction_label = "Beware!!! This customer is likely to Churn" if prediction.item() == "Yes" else "This customer is Not likely churn"
134
+ prediction_label
135
+
136
+
137
+ # Display the prediction
138
+ st.write(f"The predicted sales are: {prediction_label}.")
139
+ st.table(input_df)
app_3.py DELETED
@@ -1,71 +0,0 @@
1
- import joblib
2
- import pandas as pd
3
- import streamlit as st
4
- from huggingface_hub import hf_hub_download
5
-
6
- REPO_ID = "chanyaphas/creditc"
7
-
8
- access_token = st.secrets["HF_TOKEN"]
9
-
10
- model = joblib.load(
11
- hf_hub_download(repo_id=REPO_ID, filename='model.joblib', token=access_token, repo_type="space")
12
- )
13
-
14
- unique_values = joblib.load(
15
- hf_hub_download(repo_id=REPO_ID, filename='unique_values.joblib', token=access_token, repo_type="space")
16
- )
17
-
18
- EDU_DICT = {'Lower secondary': 1,
19
- 'Secondary / secondary special': 2,
20
- 'Academic degree': 3,
21
- 'Incomplete higher': 4,
22
- 'Higher education' : 5
23
- }
24
-
25
- def main():
26
- st.title("Credit Card Approval Prediction")
27
-
28
- with st.form("questionaire"):
29
-
30
- Gender = st.selectbox('Gender', unique_values['CODE_GENDER'])
31
- Own_car = st.selectbox('Own_car', unique_values['FLAG_OWN_CAR'])
32
- Property = st.selectbox('Property', unique_values['FLAG_OWN_REALTY'])
33
- Income_type = st.selectbox('Income_type', unique_values['NAME_INCOME_TYPE'])
34
- Marital_status = st.selectbox('Marital_status', unique_values['NAME_FAMILY_STATUS'])
35
- Housing_type = st.selectbox('Housing_type', unique_values['NAME_HOUSING_TYPE'])
36
- Education = st.selectbox('Education', unique_values['NAME_EDUCATION_TYPE'])
37
-
38
- Income = st.slider('Income', min_value=27000, max_value=1575000)
39
- Children = st.slider('Children', min_value=0, max_value=19)
40
- Day_Employed = st.slider('Day_Employed', min_value=0, max_value=3)
41
- Flag_Mobile = st.slider('Flag_Mobile', min_value=0, max_value=1)
42
- Flag_work_phone = st.slider('Flag_work_phone', min_value=0, max_value=1)
43
- Flag_Phone = st.slider('Flag_Phone', min_value=0, max_value=1)
44
- Flag_Email = st.slider('Flag_Email', min_value=0, max_value=1)
45
- Family_mem = st.slider('Family_mem', min_value=1, max_value=20)
46
-
47
- clicked = st.form_submit_button("Result")
48
- if clicked:
49
- result = model.predict(pd.DataFrame({
50
- "CODE_GENDER": [Gender],
51
- "FLAG_OWN_CAR": [Own_car],
52
- "FLAG_OWN_REALTY": [Property],
53
- "CNT_CHILDREN": [Children],
54
- "AMT_INCOME_TOTAL": [Income],
55
- "NAME_INCOME_TYPE": [Income_type],
56
- "NAME_EDUCATION_TYPE": [EDU_DICT[Education]],
57
- "NAME_FAMILY_STATUS": [Marital_status],
58
- "NAME_HOUSING_TYPE": [Housing_type],
59
- "DAYS_EMPLOYED": [Day_Employed],
60
- "FLAG_MOBIL": [Flag_Mobile],
61
- "FLAG_WORK_PHONE": [Flag_work_phone],
62
- "FLAG_PHONE": [Flag_Phone],
63
- "FLAG_EMAIL": [Flag_Email],
64
- "CNT_FAM_MEMBERS": [Family_mem]}))
65
-
66
- result = 'Pass' if result[0] == 1 else 'Did not Pass'
67
-
68
- st.success('Credit Card approval prediction results is {}'.format(result))
69
-
70
- if __name__ == '__main__':
71
- main()