Spaces:
Runtime error
Runtime error
nurindahpratiwi
commited on
Commit
·
673f4ca
1
Parent(s):
faf60d7
update file
Browse files- app.py +150 -156
- app_2.py +4 -17
- requirements.txt +11 -6
app.py
CHANGED
@@ -1,172 +1,166 @@
|
|
1 |
-
import pandas as pd
|
2 |
import streamlit as st
|
3 |
-
import
|
4 |
-
from matplotlib import pyplot as plt
|
5 |
-
import pickle
|
6 |
-
import sklearn
|
7 |
import joblib
|
8 |
-
|
9 |
-
import base64
|
10 |
from huggingface_hub import hf_hub_download
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
REPO_ID = "Abubakari/
|
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 |
-
|
31 |
hf_hub_download(repo_id=REPO_ID, filename="Final_model.joblib")
|
32 |
)
|
33 |
|
34 |
-
#
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
.
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
}
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
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 = "
|
|
|
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 |
-
|
25 |
-
hf_hub_download(repo_id=REPO_ID, filename=
|
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 |
-
|
2 |
-
|
3 |
-
|
|
|
|
|
4 |
scikit-learn==1.2.2
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
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
|