Zemedkun commited on
Commit
ffca332
1 Parent(s): 46bdbe6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +137 -78
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
- # Streamlit UI
11
- st.title('TB Cough Sound Analysis')
12
-
13
- # Audio file uploader
14
- audio_file = st.file_uploader("Upload a cough sound recording", type=['wav', 'mp3'])
15
-
16
- # Clinical data input fields
17
- age = st.number_input('Age', min_value=0, step=1, format='%d')
18
- height = st.number_input('Height (in cm)', min_value=0.0, step=0.1, format='%f')
19
- weight = st.number_input('Weight (in kg)', min_value=0.0, step=0.1, format='%f')
20
- reported_cough_dur = st.number_input('Reported Cough Duration (in days)', min_value=0, step=1, format='%d')
21
- heart_rate = st.number_input('Heart Rate (beats per minute)', min_value=0, step=1, format='%d')
22
- temperature = st.number_input('Temperature (in Celsius)', min_value=0.0, step=0.1, format='%f')
23
-
24
- sex = st.radio('Sex', ['Male', 'Female'])
25
- tb_prior = st.radio('Previous TB', ['No', 'Not sure', 'Yes'])
26
- tb_prior_Pul = st.radio('Previous Pulmonary TB', ['No', 'Yes'])
27
- tb_prior_Extrapul = st.radio('Previous Extrapulmonary TB', ['No', 'Yes'])
28
- tb_prior_Unknown = st.radio('Previous TB Unknown', ['No', 'Yes'])
29
- hemoptysis = st.radio('Hemoptysis', ['No', 'Yes'])
30
- weight_loss = st.radio('Weight Loss', ['No', 'Yes'])
31
- smoke_lweek = st.radio('Smoking Last Week', ['No', 'Yes'])
32
- fever = st.radio('Fever', ['No', 'Yes'])
33
- night_sweats = st.radio('Night Sweats', ['No', 'Yes'])
34
-
35
- # Convert categorical inputs to binary
36
- sex_Female = 1 if sex == 'Female' else 0
37
- sex_Male = 1 if sex == 'Male' else 0
38
- tb_prior_No = 1 if tb_prior == 'No' else 0
39
- tb_prior_Not_sure = 1 if tb_prior == 'Not sure' else 0
40
- tb_prior_Yes = 1 if tb_prior == 'Yes' else 0
41
- tb_prior_Pul_No = 1 if tb_prior_Pul == 'No' else 0
42
- tb_prior_Pul_Yes = 1 if tb_prior_Pul == 'Yes' else 0
43
- tb_prior_Extrapul_No = 1 if tb_prior_Extrapul == 'No' else 0
44
- tb_prior_Extrapul_Yes = 1 if tb_prior_Extrapul == 'Yes' else 0
45
- tb_prior_Unknown_No = 1 if tb_prior_Unknown == 'No' else 0
46
- tb_prior_Unknown_Yes = 1 if tb_prior_Unknown == 'Yes' else 0
47
- hemoptysis_No = 1 if hemoptysis == 'No' else 0
48
- hemoptysis_Yes = 1 if hemoptysis == 'Yes' else 0
49
- weight_loss_No = 1 if weight_loss == 'No' else 0
50
- weight_loss_Yes = 1 if weight_loss == 'Yes' else 0
51
- smoke_lweek_No = 1 if smoke_lweek == 'No' else 0
52
- smoke_lweek_Yes = 1 if smoke_lweek == 'Yes' else 0
53
- fever_No = 1 if fever == 'No' else 0
54
- fever_Yes = 1 if fever == 'Yes' else 0
55
- night_sweats_No = 1 if night_sweats == 'No' else 0
56
- night_sweats_Yes = 1 if night_sweats == 'Yes' else 0
57
-
58
- # Combine all inputs
59
- clinical_data = [age, height, weight, reported_cough_dur, heart_rate, temperature,
60
- sex_Female, sex_Male, tb_prior_No, tb_prior_Not_sure, tb_prior_Yes,
61
- tb_prior_Pul_No, tb_prior_Pul_Yes, tb_prior_Extrapul_No, tb_prior_Extrapul_Yes,
62
- tb_prior_Unknown_No, tb_prior_Unknown_Yes, hemoptysis_No, hemoptysis_Yes,
63
- weight_loss_No, weight_loss_Yes, smoke_lweek_No, smoke_lweek_Yes,
64
- fever_No, fever_Yes, night_sweats_No, night_sweats_Yes]
65
- clinical_data = np.array(clinical_data).reshape(1, -1).astype(np.float32)
66
-
67
- # Button to make prediction
68
- if st.button('Predict'):
69
- if audio_file is not None:
70
- # Process audio file
71
- raw_values, rate = sf.read(audio_file)
72
- mu = np.nanmean(raw_values)
73
- std = np.nanstd(raw_values)
74
- audio_data = (raw_values - mu) / std
75
- audio_data = np.pad(audio_data, (0, 22050 - len(audio_data)), 'constant').reshape(1, -1, 1).astype(np.float32)
76
-
77
- # Make prediction
78
- input_name = model_inference.get_inputs()[0].name
79
- input_name2 = model_inference.get_inputs()[1].name
80
- label_name = model_inference.get_outputs()[0].name
81
- onnx_pred = model_inference.run([label_name], {input_name: audio_data, input_name2: clinical_data})
82
- result = onnx_pred[0]
83
-
84
- # Display result
85
- st.write('Prediction:', result)
86
- else:
87
- st.error('Please upload an audio file.')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()