val2e commited on
Commit
44867fc
1 Parent(s): b49dddb

Upload predict.py

Browse files
Files changed (1) hide show
  1. predict.py +108 -0
predict.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import requests
4
+ import os
5
+ from io import StringIO
6
+ import datetime
7
+
8
+ # Set page configuration
9
+ st.set_page_config(page_title="Predict", page_icon="🔮", layout="wide")
10
+
11
+
12
+ st.title("Predict Sepsis")
13
+
14
+
15
+ def select_model():
16
+ col1, col2 = st.columns(2)
17
+ with col1:
18
+ choice = st.selectbox('Select a model', options=['xgboost', 'random_forest'], key='select_model')
19
+ with col2:
20
+ pass
21
+
22
+ # if st.session_state['selected_model'] == 'Xgboost':
23
+ # pipeline = xgboost_pipeline()
24
+ # else:
25
+ # pipeline = load_random_forest_pipeline()
26
+
27
+ # encoder = joblib.load('models/encoder.joblib')
28
+ return choice
29
+
30
+
31
+ def make_prediction():
32
+ selected_model = st.session_state['select_model']
33
+ age = st.session_state['age']
34
+ insurance = 1 if st.session_state['insurance'] == 'Yes' else 0
35
+ m11 = st.session_state['m11']
36
+ pr = st.session_state['pr']
37
+ prg = st.session_state['prg']
38
+ ts = st.session_state['ts']
39
+ pl = st.session_state['pl']
40
+ sk = st.session_state['sk']
41
+ bd2 = st.session_state['bd2']
42
+
43
+ base_url = 'https://gabcares-team-curium.hf.space/'
44
+ url = base_url + f"{ 'xgboost_prediction' if selected_model=='xgboost' else 'random_forest_prediction'}"
45
+
46
+
47
+ data = {'PRG': prg, 'PL': pl, 'PR': pr,'SK': sk, 'TS': ts, 'M11': m11, 'BD2': bd2, 'Age': age, 'Insurance': insurance}
48
+ # Send POST request with JSON data using the json parameter
49
+ response_status = requests.get(base_url)
50
+
51
+ if (response_status.status_code == 200):
52
+ response = requests.post(url, json=data,timeout=30)
53
+ pred_prob = (response.json()['result'])
54
+ prediction = pred_prob['prediction']
55
+ probability = pred_prob['probability']
56
+
57
+ st.session_state['prediction'] = prediction
58
+ st.session_state['probability'] = probability
59
+ else:
60
+ st.write('Unable to connect to the server.')
61
+
62
+
63
+ # Creating the form
64
+ def display_form():
65
+ select_model()
66
+
67
+ with st.form('input_features'):
68
+
69
+ col1, col2 = st.columns(2)
70
+ with col1:
71
+ st.write('### Patient Demographics')
72
+ age = st.number_input('Age', min_value=0, max_value=100, step=1, key = 'age')
73
+ insurance = st.selectbox('Insurance', options = ['Yes', 'No'], key = 'insurance')
74
+
75
+ st.write('### Vital Signs')
76
+ m11 = st.number_input('BMI', min_value=10.0, format="%.2f",step = 1.00, key = 'm11')
77
+ pr = st.number_input('Blood Pressure', min_value=10.0, format="%.2f",step = 1.00, key = 'pr')
78
+ prg = st.number_input('PRG(plasma glucose)', min_value=10.0, format="%.2f",step = 1.00, key = 'prg')
79
+
80
+ with col2:
81
+ st.write('### Blood Work')
82
+ pl = st.number_input('PL(Blood Work Result 1)', min_value=10.0, format="%.2f",step = 1.00, key = 'pl')
83
+ sk = st.number_input('SK(Blood Work Result 2)', min_value=10.0, format="%.2f",step = 1.00, key = 'sk')
84
+ ts = st.number_input('TS(Blood Work Result 3)', min_value=10.0, format="%.2f",step = 1.00, key = 'ts')
85
+ bd2 = st.number_input('BD2(Blood Work Result 4)', min_value=10.0, format="%.2f",step = 1.00, key = 'bd2')
86
+
87
+ st.form_submit_button('Submit', on_click=make_prediction)
88
+
89
+
90
+ if __name__ == '__main__':
91
+
92
+ display_form()
93
+
94
+ final_prediction = st.session_state.get('prediction')
95
+ final_probability = st.session_state.get('probability')
96
+
97
+ if final_prediction is None:
98
+ st.write('Predictions show here!')
99
+ st.divider()
100
+ else:
101
+ if final_prediction.lower() == 'positive':
102
+ st.markdown(f'### Patient is likely to develop sepsis😞.')
103
+ st.markdown(f'## Probability: {final_probability:.2f}%')
104
+
105
+ else:
106
+ # st.markdown(f'## Sepsis: {final_prediction}')
107
+ st.markdown(f'### Patient is unlikely to develop sepsis😊.')
108
+ st.markdown(f'## Probability: {final_probability:.2f}%')