RICHARDMENSAH commited on
Commit
1e4aee8
1 Parent(s): 7845ba2

app commit

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