nurindahpratiwi commited on
Commit
673f4ca
·
1 Parent(s): faf60d7

update file

Browse files
Files changed (3) hide show
  1. app.py +150 -156
  2. app_2.py +4 -17
  3. requirements.txt +11 -6
app.py CHANGED
@@ -1,172 +1,166 @@
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 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")
16
  )
17
 
18
- cat_imputer = joblib.load(
19
- hf_hub_download(repo_id=REPO_ID, filename="categorical_imputer.joblib")
20
- )
21
-
22
- encoder = joblib.load(
23
- hf_hub_download(repo_id=REPO_ID, filename="encoder.joblib")
24
- )
25
-
26
  scaler = joblib.load(
27
  hf_hub_download(repo_id=REPO_ID, filename="scaler.joblib")
28
  )
29
 
30
- dt_model = joblib.load(
31
  hf_hub_download(repo_id=REPO_ID, filename="Final_model.joblib")
32
  )
33
 
34
- # Add a title and subtitle
35
- st.write("<center><h1>Sales Prediction App</h1></center>", unsafe_allow_html=True)
36
-
37
- # Set up the layout
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
-
44
- st.subheader("Enter the details to predict sales")
45
-
46
- # Add some text
47
- #st.write("Enter some data for Prediction.")
48
-
49
- # Create the input fields
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
75
- # Define the custom CSS
76
- predict_button_css = """
77
- <style>
78
- .predict-button {
79
- background-color: #C4C4C4;
80
- color: gray;
81
- padding: 0.75rem 2rem;
82
- border-radius: 0.5rem;
83
- border: none;
84
- font-size: 1.1rem;
85
- font-weight: bold;
86
- text-align: center;
87
- margin-top: 2rem;
88
- }
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
113
-
114
- if st.button("Predict", key="predict_button", help="Click to make a prediction."):
115
- # Convert the input data to a pandas DataFrame
116
- input_df = pd.DataFrame([input_data])
117
-
118
-
119
- # Selecting categorical and numerical columns separately
120
- cat_columns = [col for col in input_df.columns if input_df[col].dtype == 'object']
121
- num_columns = [col for col in input_df.columns if input_df[col].dtype != 'object']
122
-
123
-
124
- # Apply the imputers
125
- input_df_imputed_cat = cat_imputer.transform(input_df[cat_columns])
126
- input_df_imputed_num = num_imputer.transform(input_df[num_columns])
127
-
128
-
129
- # Encode the categorical columns
130
- input_encoded_df = pd.DataFrame(encoder.transform(input_df_imputed_cat).toarray(),
131
- columns=encoder.get_feature_names(cat_columns))
132
-
133
- # Scale the numerical columns
134
- input_df_scaled = scaler.transform(input_df_imputed_num)
135
- input_scaled_df = pd.DataFrame(input_df_scaled , columns = num_columns)
136
-
137
- #joining the cat encoded and num scaled
138
- final_df = pd.concat([input_encoded_df, input_scaled_df], axis=1)
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
- )
 
 
 
 
 
1
  import streamlit as st
2
+ import pandas as pd
 
 
 
3
  import joblib
4
+ import matplotlib.pyplot as plt
 
5
  from huggingface_hub import hf_hub_download
6
+ import time
7
+ import base64
8
+
9
+ # Load the pre-trained numerical imputer, scaler, and model using joblib
10
 
11
+ REPO_ID = "Abubakari/Sepsis-prediction-streamlit-app"
12
 
13
  num_imputer = joblib.load(
14
  hf_hub_download(repo_id=REPO_ID, filename="numerical_imputer.joblib")
15
  )
16
 
 
 
 
 
 
 
 
 
17
  scaler = joblib.load(
18
  hf_hub_download(repo_id=REPO_ID, filename="scaler.joblib")
19
  )
20
 
21
+ model = joblib.load(
22
  hf_hub_download(repo_id=REPO_ID, filename="Final_model.joblib")
23
  )
24
 
25
+ # Define a function to preprocess the input data
26
+ def preprocess_input_data(input_data):
27
+ input_data_df = pd.DataFrame(input_data, columns=['PRG', 'PL', 'PR', 'SK', 'TS', 'M11', 'BD2', 'Age', 'Insurance'])
28
+ num_columns = input_data_df.select_dtypes(include='number').columns
29
+
30
+ input_data_imputed_num = num_imputer.transform(input_data_df[num_columns])
31
+ input_scaled_df = pd.DataFrame(scaler.transform(input_data_imputed_num), columns=num_columns)
32
+
33
+ return input_scaled_df
34
+
35
+
36
+ # Define a function to make the sepsis prediction
37
+ def predict_sepsis(input_data):
38
+ input_scaled_df = preprocess_input_data(input_data)
39
+ prediction = model.predict(input_scaled_df)[0]
40
+ probabilities = model.predict_proba(input_scaled_df)[0]
41
+ sepsis_status = "Positive" if prediction == 1 else "Negative"
42
+
43
+ status_icon = "✔" if prediction == 1 else "✘" # Red 'X' icon for positive sepsis prediction, green checkmark icon for negative sepsis prediction
44
+ sepsis_explanation = "Sepsis is a life-threatening condition caused by an infection. A positive prediction suggests that the patient might be exhibiting sepsis symptoms and requires immediate medical attention." if prediction == 1 else "Sepsis is a life-threatening condition caused by an infection. A negative prediction suggests that the patient is not currently exhibiting sepsis symptoms."
45
+
46
+ output_df = pd.DataFrame(input_data, columns=['PRG', 'PL', 'PR', 'SK', 'TS', 'M11', 'BD2', 'Age', 'Insurance'])
47
+ output_df['Prediction'] = sepsis_status
48
+ output_df['Negative Probability'] = probabilities[0]
49
+ output_df['Positive Probability'] = probabilities[1]
50
+
51
+ return output_df, probabilities, status_icon, sepsis_explanation
52
+
53
+ # Create a Streamlit app
54
+ def main():
55
+ st.title('Sepsis Prediction App')
56
+
57
+ st.image("Strealit_.jpg")
58
+
59
+ # How to use
60
+ st.sidebar.title('How to Use')
61
+ st.sidebar.markdown('1. Adjust the input parameters on the left sidebar.')
62
+ st.sidebar.markdown('2. Click the "Predict" button to initiate the prediction.')
63
+ st.sidebar.markdown('3. The app will simulate a prediction process with a progress bar.')
64
+ st.sidebar.markdown('4. Once the prediction is complete, the results will be displayed below.')
65
+
66
+
67
+ st.sidebar.title('Input Parameters')
68
+
69
+ # Input parameter explanations
70
+ st.sidebar.markdown('**PRG:** Plasma Glucose')
71
+ PRG = st.sidebar.number_input('PRG', value=0.0)
72
+
73
+ st.sidebar.markdown('**PL:** Blood Work Result 1')
74
+ PL = st.sidebar.number_input('PL', value=0.0)
75
+
76
+ st.sidebar.markdown('**PR:** Blood Pressure Measured')
77
+ PR = st.sidebar.number_input('PR', value=0.0)
78
+
79
+ st.sidebar.markdown('**SK:** Blood Work Result 2')
80
+ SK = st.sidebar.number_input('SK', value=0.0)
81
+
82
+ st.sidebar.markdown('**TS:** Blood Work Result 3')
83
+ TS = st.sidebar.number_input('TS', value=0.0)
84
+
85
+ st.sidebar.markdown('**M11:** BMI')
86
+ M11 = st.sidebar.number_input('M11', value=0.0)
87
+
88
+ st.sidebar.markdown('**BD2:** Blood Work Result 4')
89
+ BD2 = st.sidebar.number_input('BD2', value=0.0)
90
+
91
+ st.sidebar.markdown('**Age:** What is the Age of the Patient: ')
92
+ Age = st.sidebar.number_input('Age', value=0.0)
93
+
94
+ st.sidebar.markdown('**Insurance:** Does the patient have Insurance?')
95
+ insurance_options = {0: 'NO', 1: 'YES'}
96
+ Insurance = st.sidebar.radio('Insurance', list(insurance_options.keys()), format_func=lambda x: insurance_options[x])
97
+
98
+
99
+ input_data = [[PRG, PL, PR, SK, TS, M11, BD2, Age, Insurance]]
100
+
101
+ if st.sidebar.button('Predict'):
102
+ with st.spinner("Predicting..."):
103
+ # Simulate a long-running process
104
+ progress_bar = st.progress(0)
105
+ step = 20 # A big step will reduce the execution time
106
+ for i in range(0, 100, step):
107
+ time.sleep(0.1)
108
+ progress_bar.progress(i + step)
109
+
110
+ output_df, probabilities, status_icon, sepsis_explanation = predict_sepsis(input_data)
111
+
112
+ st.subheader('Prediction Result')
113
+ prediction_text = "Positive" if status_icon == "✔" else "Negative"
114
+ st.markdown(f"Prediction: **{prediction_text}**")
115
+ st.markdown(f"{status_icon} {sepsis_explanation}")
116
+ st.write(output_df)
117
+
118
+ # Add a download button for output_df
119
+ csv = output_df.to_csv(index=False)
120
+ b64 = base64.b64encode(csv.encode()).decode()
121
+ href = f'<a href="data:file/csv;base64,{b64}" download="output.csv">Download Output CSV</a>'
122
+ st.markdown(href, unsafe_allow_html=True)
123
+
124
+
125
+ # Plot the probabilities
126
+ fig, ax = plt.subplots()
127
+ ax.bar(['Negative', 'Positive'], probabilities)
128
+ ax.set_xlabel('Sepsis Status')
129
+ ax.set_ylabel('Probability')
130
+ ax.set_title('Sepsis Prediction Probabilities')
131
+ st.pyplot(fig)
132
+
133
+ # Print feature importance
134
+ if hasattr(model, 'coef_'):
135
+ feature_importances = model.coef_[0]
136
+ feature_names = ['PRG', 'PL', 'PR', 'SK', 'TS', 'M11', 'BD2', 'Age', 'Insurance']
137
+
138
+ importance_df = pd.DataFrame({'Feature': feature_names, 'Importance': feature_importances})
139
+ importance_df = importance_df.sort_values('Importance', ascending=False)
140
+
141
+ st.subheader('Feature Importance')
142
+ fig, ax = plt.subplots()
143
+ bars = ax.bar(importance_df['Feature'], importance_df['Importance'])
144
+ ax.set_xlabel('Feature')
145
+ ax.set_ylabel('Importance')
146
+ ax.set_title('Feature Importance')
147
+ ax.tick_params(axis='x', rotation=45)
148
+
149
+ # Add data labels to the bars
150
+ for bar in bars:
151
+ height = bar.get_height()
152
+ ax.annotate(f'{height:.2f}', xy=(bar.get_x() + bar.get_width() / 2, height),
153
+ xytext=(0, 3), # 3 points vertical offset
154
+ textcoords="offset points",
155
+ ha='center', va='bottom')
156
+ st.pyplot(fig)
157
+
158
+ else:
159
+ st.write('Feature importance is not available for this model.')
160
+
161
+ #st.subheader('Sepsis Explanation')
162
+ #st.markdown(f"{status_icon} {sepsis_explanation}")
163
+
164
+
165
+ if __name__ == '__main__':
166
+ main()
app_2.py CHANGED
@@ -11,27 +11,14 @@ 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)
 
11
  import datetime
12
  from huggingface_hub import hf_hub_download
13
 
14
+ REPO_ID = "rajistics/churn-model"
15
+ FILENAME = "churn.pkl"
16
 
 
 
 
 
 
 
 
17
 
18
+ model = joblib.load(
19
+ hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
20
  )
21
 
 
 
 
 
 
 
 
22
 
23
  # Add a title and subtitle
24
  st.write("<center><h1>Sales Prediction App</h1></center>", unsafe_allow_html=True)
requirements.txt CHANGED
@@ -1,7 +1,12 @@
1
- transformers==4.22.2
2
- streamlit
3
- huggingface_hub==0.9.1
 
 
4
  scikit-learn==1.2.2
5
- joblib
6
- torch
7
- pandas
 
 
 
 
1
+ joblib==1.2.0
2
+ matplotlib==3.7.1
3
+ matplotlib-inline==0.1.6
4
+ numpy==1.24.2
5
+ pandas==1.5.3
6
  scikit-learn==1.2.2
7
+ scipy==1.10.0
8
+ seaborn==0.12.2
9
+ streamlit==1.20.0
10
+ fastapi==0.95.1
11
+ uvicorn==0.22.0
12
+ pydantic==1.10.7