nurindahpratiwi commited on
Commit
36a6ef0
β€’
1 Parent(s): ec6c55a

initial commit

Browse files
Files changed (2) hide show
  1. app.py +167 -0
  2. requirements.txt +13 -0
app.py ADDED
@@ -0,0 +1,167 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import joblib
4
+ import matplotlib.pyplot as plt
5
+ import time
6
+ import base64
7
+ from huggingface_hub import hf_hub_download
8
+
9
+ REPO_ID = "wiwaaw/Sepsis-prediction-streamlit-app"
10
+
11
+ # Load the pre-trained numerical imputer, scaler, and model using joblib
12
+ num_imputer = joblib.load(
13
+ hf_hub_download(repo_id=REPO_ID, filename="numerical_imputer.joblib")
14
+ )
15
+
16
+ scaler = joblib.load(
17
+ hf_hub_download(repo_id=REPO_ID, filename="scaler.joblib")
18
+ )
19
+
20
+ model = joblib.load(
21
+ hf_hub_download(repo_id=REPO_ID, filename="Final_model.joblib")
22
+ )
23
+
24
+
25
+
26
+ # Define a function to preprocess the input data
27
+ def preprocess_input_data(input_data):
28
+ input_data_df = pd.DataFrame(input_data, columns=['PRG', 'PL', 'PR', 'SK', 'TS', 'M11', 'BD2', 'Age', 'Insurance'])
29
+ num_columns = input_data_df.select_dtypes(include='number').columns
30
+
31
+ input_data_imputed_num = num_imputer.transform(input_data_df[num_columns])
32
+ input_scaled_df = pd.DataFrame(scaler.transform(input_data_imputed_num), columns=num_columns)
33
+
34
+ return input_scaled_df
35
+
36
+
37
+ # Define a function to make the sepsis prediction
38
+ def predict_sepsis(input_data):
39
+ input_scaled_df = preprocess_input_data(input_data)
40
+ prediction = model.predict(input_scaled_df)[0]
41
+ probabilities = model.predict_proba(input_scaled_df)[0]
42
+ sepsis_status = "Positive" if prediction == 1 else "Negative"
43
+
44
+ status_icon = "βœ”" if prediction == 1 else "✘" # Red 'X' icon for positive sepsis prediction, green checkmark icon for negative sepsis prediction
45
+ 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."
46
+
47
+ output_df = pd.DataFrame(input_data, columns=['PRG', 'PL', 'PR', 'SK', 'TS', 'M11', 'BD2', 'Age', 'Insurance'])
48
+ output_df['Prediction'] = sepsis_status
49
+ output_df['Negative Probability'] = probabilities[0]
50
+ output_df['Positive Probability'] = probabilities[1]
51
+
52
+ return output_df, probabilities, status_icon, sepsis_explanation
53
+
54
+ # Create a Streamlit app
55
+ def main():
56
+ st.title('Sepsis Prediction App')
57
+
58
+ st.image("Strealit_.jpg")
59
+
60
+ # How to use
61
+ st.sidebar.title('How to Use')
62
+ st.sidebar.markdown('1. Adjust the input parameters on the left sidebar.')
63
+ st.sidebar.markdown('2. Click the "Predict" button to initiate the prediction.')
64
+ st.sidebar.markdown('3. The app will simulate a prediction process with a progress bar.')
65
+ st.sidebar.markdown('4. Once the prediction is complete, the results will be displayed below.')
66
+
67
+
68
+ st.sidebar.title('Input Parameters')
69
+
70
+ # Input parameter explanations
71
+ st.sidebar.markdown('**PRG:** Plasma Glucose')
72
+ PRG = st.sidebar.number_input('PRG', value=0.0)
73
+
74
+ st.sidebar.markdown('**PL:** Blood Work Result 1')
75
+ PL = st.sidebar.number_input('PL', value=0.0)
76
+
77
+ st.sidebar.markdown('**PR:** Blood Pressure Measured')
78
+ PR = st.sidebar.number_input('PR', value=0.0)
79
+
80
+ st.sidebar.markdown('**SK:** Blood Work Result 2')
81
+ SK = st.sidebar.number_input('SK', value=0.0)
82
+
83
+ st.sidebar.markdown('**TS:** Blood Work Result 3')
84
+ TS = st.sidebar.number_input('TS', value=0.0)
85
+
86
+ st.sidebar.markdown('**M11:** BMI')
87
+ M11 = st.sidebar.number_input('M11', value=0.0)
88
+
89
+ st.sidebar.markdown('**BD2:** Blood Work Result 4')
90
+ BD2 = st.sidebar.number_input('BD2', value=0.0)
91
+
92
+ st.sidebar.markdown('**Age:** What is the Age of the Patient: ')
93
+ Age = st.sidebar.number_input('Age', value=0.0)
94
+
95
+ st.sidebar.markdown('**Insurance:** Does the patient have Insurance?')
96
+ insurance_options = {0: 'NO', 1: 'YES'}
97
+ Insurance = st.sidebar.radio('Insurance', list(insurance_options.keys()), format_func=lambda x: insurance_options[x])
98
+
99
+
100
+ input_data = [[PRG, PL, PR, SK, TS, M11, BD2, Age, Insurance]]
101
+
102
+ if st.sidebar.button('Predict'):
103
+ with st.spinner("Predicting..."):
104
+ # Simulate a long-running process
105
+ progress_bar = st.progress(0)
106
+ step = 20 # A big step will reduce the execution time
107
+ for i in range(0, 100, step):
108
+ time.sleep(0.1)
109
+ progress_bar.progress(i + step)
110
+
111
+ output_df, probabilities, status_icon, sepsis_explanation = predict_sepsis(input_data)
112
+
113
+ st.subheader('Prediction Result')
114
+ prediction_text = "Positive" if status_icon == "βœ”" else "Negative"
115
+ st.markdown(f"Prediction: **{prediction_text}**")
116
+ st.markdown(f"{status_icon} {sepsis_explanation}")
117
+ st.write(output_df)
118
+
119
+ # Add a download button for output_df
120
+ csv = output_df.to_csv(index=False)
121
+ b64 = base64.b64encode(csv.encode()).decode()
122
+ href = f'<a href="data:file/csv;base64,{b64}" download="output.csv">Download Output CSV</a>'
123
+ st.markdown(href, unsafe_allow_html=True)
124
+
125
+
126
+ # Plot the probabilities
127
+ fig, ax = plt.subplots()
128
+ ax.bar(['Negative', 'Positive'], probabilities)
129
+ ax.set_xlabel('Sepsis Status')
130
+ ax.set_ylabel('Probability')
131
+ ax.set_title('Sepsis Prediction Probabilities')
132
+ st.pyplot(fig)
133
+
134
+ # Print feature importance
135
+ if hasattr(model, 'coef_'):
136
+ feature_importances = model.coef_[0]
137
+ feature_names = ['PRG', 'PL', 'PR', 'SK', 'TS', 'M11', 'BD2', 'Age', 'Insurance']
138
+
139
+ importance_df = pd.DataFrame({'Feature': feature_names, 'Importance': feature_importances})
140
+ importance_df = importance_df.sort_values('Importance', ascending=False)
141
+
142
+ st.subheader('Feature Importance')
143
+ fig, ax = plt.subplots()
144
+ bars = ax.bar(importance_df['Feature'], importance_df['Importance'])
145
+ ax.set_xlabel('Feature')
146
+ ax.set_ylabel('Importance')
147
+ ax.set_title('Feature Importance')
148
+ ax.tick_params(axis='x', rotation=45)
149
+
150
+ # Add data labels to the bars
151
+ for bar in bars:
152
+ height = bar.get_height()
153
+ ax.annotate(f'{height:.2f}', xy=(bar.get_x() + bar.get_width() / 2, height),
154
+ xytext=(0, 3), # 3 points vertical offset
155
+ textcoords="offset points",
156
+ ha='center', va='bottom')
157
+ st.pyplot(fig)
158
+
159
+ else:
160
+ st.write('Feature importance is not available for this model.')
161
+
162
+ #st.subheader('Sepsis Explanation')
163
+ #st.markdown(f"{status_icon} {sepsis_explanation}")
164
+
165
+
166
+ if __name__ == '__main__':
167
+ main()
requirements.txt ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
13
+ hf_hub_download