Update app.py
Browse files
app.py
CHANGED
@@ -7,81 +7,140 @@ import soundfile as sf
|
|
7 |
model_path = 'model.onnx' # Replace with the actual path to your model
|
8 |
model_inference = rt.InferenceSession(model_path)
|
9 |
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
reported_cough_dur
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
tb_prior_Unknown_Yes
|
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 |
-
st.
|
86 |
-
|
87 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
model_path = 'model.onnx' # Replace with the actual path to your model
|
8 |
model_inference = rt.InferenceSession(model_path)
|
9 |
|
10 |
+
# Function to preprocess audio data
|
11 |
+
def preprocess_audio(audio_file):
|
12 |
+
raw_values, rate = sf.read(audio_file)
|
13 |
+
mu = np.nanmean(raw_values)
|
14 |
+
std = np.nanstd(raw_values)
|
15 |
+
audio_data = (raw_values - mu) / std
|
16 |
+
audio_data = np.pad(audio_data, (0, 22050 - len(audio_data)), 'constant').reshape(1, -1, 1).astype(np.float32)
|
17 |
+
return audio_data
|
18 |
+
|
19 |
+
# Function to preprocess clinical data
|
20 |
+
def preprocess_clinical_data(age, height, weight, reported_cough_dur, heart_rate, temperature, sex, tb_prior, tb_prior_Pul, tb_prior_Extrapul, tb_prior_Unknown, hemoptysis, weight_loss, smoke_lweek, fever, night_sweats):
|
21 |
+
sex_Female = 1 if sex == 'Female' else 0
|
22 |
+
sex_Male = 1 if sex == 'Male' else 0
|
23 |
+
tb_prior_No = 1 if tb_prior == 'No' else 0
|
24 |
+
tb_prior_Not_sure = 1 if tb_prior == 'Not sure' else 0
|
25 |
+
tb_prior_Yes = 1 if tb_prior == 'Yes' else 0
|
26 |
+
tb_prior_Pul_No = 1 if tb_prior_Pul == 'No' else 0
|
27 |
+
tb_prior_Pul_Yes = 1 if tb_prior_Pul == 'Yes' else 0
|
28 |
+
tb_prior_Extrapul_No = 1 if tb_prior_Extrapul == 'No' else 0
|
29 |
+
tb_prior_Extrapul_Yes = 1 if tb_prior_Extrapul == 'Yes' else 0
|
30 |
+
tb_prior_Unknown_No = 1 if tb_prior_Unknown == 'No' else 0
|
31 |
+
tb_prior_Unknown_Yes = 1 if tb_prior_Unknown == 'Yes' else 0
|
32 |
+
hemoptysis_No = 1 if hemoptysis == 'No' else 0
|
33 |
+
hemoptysis_Yes = 1 if hemoptysis == 'Yes' else 0
|
34 |
+
weight_loss_No = 1 if weight_loss == 'No' else 0
|
35 |
+
weight_loss_Yes = 1 if weight_loss == 'Yes' else 0
|
36 |
+
smoke_lweek_No = 1 if smoke_lweek == 'No' else 0
|
37 |
+
smoke_lweek_Yes = 1 if smoke_lweek == 'Yes' else 0
|
38 |
+
fever_No = 1 if fever == 'No' else 0
|
39 |
+
fever_Yes = 1 if fever == 'Yes' else 0
|
40 |
+
night_sweats_No = 1 if night_sweats == 'No' else 0
|
41 |
+
night_sweats_Yes = 1 if night_sweats == 'Yes' else 0
|
42 |
+
|
43 |
+
clinical_data = [age, height, weight, reported_cough_dur, heart_rate, temperature,
|
44 |
+
sex_Female, sex_Male, tb_prior_No, tb_prior_Not_sure, tb_prior_Yes,
|
45 |
+
tb_prior_Pul_No, tb_prior_Pul_Yes, tb_prior_Extrapul_No, tb_prior_Extrapul_Yes,
|
46 |
+
tb_prior_Unknown_No, tb_prior_Unknown_Yes, hemoptysis_No, hemoptysis_Yes,
|
47 |
+
weight_loss_No, weight_loss_Yes, smoke_lweek_No, smoke_lweek_Yes,
|
48 |
+
fever_No, fever_Yes, night_sweats_No, night_sweats_Yes]
|
49 |
+
clinical_data = np.array(clinical_data).reshape(1, -1).astype(np.float32)
|
50 |
+
return clinical_data
|
51 |
+
|
52 |
+
# Main function to run the app
|
53 |
+
def main():
|
54 |
+
st.title('TB Cough Sound Analysis')
|
55 |
+
|
56 |
+
# Initialize state variables
|
57 |
+
page = st.session_state.get('page', 0)
|
58 |
+
st.session_state['page'] = page
|
59 |
+
|
60 |
+
# Create multi-step form
|
61 |
+
if page == 0:
|
62 |
+
st.write('**Step 1: Upload Cough Sound Recording**')
|
63 |
+
audio_file = st.file_uploader("Upload a cough sound recording", type=['wav', 'mp3'])
|
64 |
+
if audio_file is not None:
|
65 |
+
st.audio(audio_file)
|
66 |
+
st.session_state['audio_file'] = audio_file
|
67 |
+
if st.button('Next'):
|
68 |
+
st.session_state['page'] = 1
|
69 |
+
|
70 |
+
elif page == 1:
|
71 |
+
st.write('**Step 2: Enter Clinical Data**')
|
72 |
+
age = st.number_input('Age', min_value=0, step=1, format='%d')
|
73 |
+
height = st.number_input('Height (in cm)', min_value=0.0, step=0.1, format='%f')
|
74 |
+
weight = st.number_input('Weight (in kg)', min_value=0.0, step=0.1, format='%f')
|
75 |
+
reported_cough_dur = st.number_input('Reported Cough Duration (in days)', min_value=0, step=1, format='%d')
|
76 |
+
heart_rate = st.number_input('Heart Rate (beats per minute)', min_value=0, step=1, format='%d')
|
77 |
+
temperature = st.number_input('Body Temperature (in °C)', min_value=0.0, step=0.1, format='%f')
|
78 |
+
sex = st.radio('Sex', ['Male', 'Female'])
|
79 |
+
tb_prior = st.selectbox('TB Prior', ['No', 'Not sure', 'Yes'])
|
80 |
+
tb_prior_Pul = st.selectbox('TB Prior - Pulmonary', ['No', 'Yes'])
|
81 |
+
tb_prior_Extrapul = st.selectbox('TB Prior - Extrapulmonary', ['No', 'Yes'])
|
82 |
+
tb_prior_Unknown = st.selectbox('TB Prior - Unknown', ['No', 'Yes'])
|
83 |
+
hemoptysis = st.selectbox('Hemoptysis', ['No', 'Yes'])
|
84 |
+
weight_loss = st.selectbox('Weight Loss', ['No', 'Yes'])
|
85 |
+
smoke_lweek = st.selectbox('Smoking Last Week', ['No', 'Yes'])
|
86 |
+
fever = st.selectbox('Fever', ['No', 'Yes'])
|
87 |
+
night_sweats = st.selectbox('Night Sweats', ['No', 'Yes'])
|
88 |
+
|
89 |
+
if st.button('Next'):
|
90 |
+
st.session_state['page'] = 2
|
91 |
+
|
92 |
+
elif page == 2:
|
93 |
+
st.write('**Step 3: Review Input Data**')
|
94 |
+
st.write('**Clinical Data:**')
|
95 |
+
st.write(f"- Age: {age}")
|
96 |
+
st.write(f"- Height: {height} cm")
|
97 |
+
st.write(f"- Weight: {weight} kg")
|
98 |
+
st.write(f"- Reported Cough Duration: {reported_cough_dur} days")
|
99 |
+
st.write(f"- Heart Rate: {heart_rate} bpm")
|
100 |
+
st.write(f"- Temperature: {temperature} °C")
|
101 |
+
st.write(f"- Sex: {sex}")
|
102 |
+
st.write(f"- TB Prior: {tb_prior}")
|
103 |
+
st.write(f"- TB Prior - Pulmonary: {tb_prior_Pul}")
|
104 |
+
st.write(f"- TB Prior - Extrapulmonary: {tb_prior_Extrapul}")
|
105 |
+
st.write(f"- TB Prior - Unknown: {tb_prior_Unknown}")
|
106 |
+
st.write(f"- Hemoptysis: {hemoptysis}")
|
107 |
+
st.write(f"- Weight Loss: {weight_loss}")
|
108 |
+
st.write(f"- Smoking Last Week: {smoke_lweek}")
|
109 |
+
st.write(f"- Fever: {fever}")
|
110 |
+
st.write(f"- Night Sweats: {night_sweats}")
|
111 |
+
|
112 |
+
if st.button('Next'):
|
113 |
+
st.session_state['page'] = 3
|
114 |
+
|
115 |
+
elif page == 3:
|
116 |
+
st.write('**Step 4: Prediction**')
|
117 |
+
if 'audio_file' in st.session_state:
|
118 |
+
audio_data = preprocess_audio(st.session_state['audio_file'])
|
119 |
+
clinical_data = preprocess_clinical_data(age, height, weight, reported_cough_dur, heart_rate, temperature,
|
120 |
+
sex, tb_prior, tb_prior_Pul, tb_prior_Extrapul, tb_prior_Unknown, hemoptysis, weight_loss, smoke_lweek, fever,
|
121 |
+
night_sweats)
|
122 |
+
input_name = model_inference.get_inputs()[0].name
|
123 |
+
input_name2 = model_inference.get_inputs()[1].name
|
124 |
+
label_name = model_inference.get_outputs()[0].name
|
125 |
+
onnx_pred = model_inference.run([label_name], {input_name: audio_data, input_name2: clinical_data})
|
126 |
+
result = onnx_pred[0]
|
127 |
+
|
128 |
+
# Interpret prediction
|
129 |
+
if result >= 0.5:
|
130 |
+
prediction_text = "Tuberculosis is found based on the audio data and clinical information."
|
131 |
+
probability_label = "Positive Probability"
|
132 |
+
else:
|
133 |
+
prediction_text = "No Tuberculosis is found based on the audio data and clinical information."
|
134 |
+
probability_label = "Negative Probability"
|
135 |
+
|
136 |
+
st.write('**Prediction:**')
|
137 |
+
st.write(f"{prediction_text}")
|
138 |
+
st.write(f"{probability_label}: {result}")
|
139 |
+
else:
|
140 |
+
st.error('Please upload an audio file.')
|
141 |
+
|
142 |
+
if st.button('Previous'):
|
143 |
+
st.session_state['page'] = 2
|
144 |
+
|
145 |
+
if __name__ == '__main__':
|
146 |
+
main()
|