Zemedkun commited on
Commit
cb021cb
1 Parent(s): 6da0bdf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -109
app.py CHANGED
@@ -2,127 +2,86 @@ import streamlit as st
2
  import numpy as np
3
  import onnxruntime as rt
4
  import soundfile as sf
5
- import sounddevice as sd
6
- from scipy.io.wavfile import write
7
 
8
  # Load the ONNX model
9
- model_path = 'model.onnx'
10
  model_inference = rt.InferenceSession(model_path)
11
 
12
- # Function to preprocess audio data
13
- def preprocess_audio(audio_data):
14
- mu = np.nanmean(audio_data)
15
- std = np.nanstd(audio_data)
16
- audio_data = (audio_data - mu) / std
17
- audio_data = np.pad(audio_data, (0, 22050 - len(audio_data)), 'constant').reshape(1, -1, 1).astype(np.float32)
18
- return audio_data
19
 
20
- # Function to preprocess clinical data
21
- 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):
22
- sex_Female = 1 if sex == 'Female' else 0
23
- sex_Male = 1 if sex == 'Male' else 0
24
- tb_prior_No = 1 if tb_prior == 'No' else 0
25
- tb_prior_Not_sure = 1 if tb_prior == 'Not sure' else 0
26
- tb_prior_Yes = 1 if tb_prior == 'Yes' else 0
27
- tb_prior_Pul_No = 1 if tb_prior_Pul == 'No' else 0
28
- tb_prior_Pul_Yes = 1 if tb_prior_Pul == 'Yes' else 0
29
- tb_prior_Extrapul_No = 1 if tb_prior_Extrapul == 'No' else 0
30
- tb_prior_Extrapul_Yes = 1 if tb_prior_Extrapul == 'Yes' else 0
31
- tb_prior_Unknown_No = 1 if tb_prior_Unknown == 'No' else 0
32
- tb_prior_Unknown_Yes = 1 if tb_prior_Unknown == 'Yes' else 0
33
- hemoptysis_No = 1 if hemoptysis == 'No' else 0
34
- hemoptysis_Yes = 1 if hemoptysis == 'Yes' else 0
35
- weight_loss_No = 1 if weight_loss == 'No' else 0
36
- weight_loss_Yes = 1 if weight_loss == 'Yes' else 0
37
- smoke_lweek_No = 1 if smoke_lweek == 'No' else 0
38
- smoke_lweek_Yes = 1 if smoke_lweek == 'Yes' else 0
39
- fever_No = 1 if fever == 'No' else 0
40
- fever_Yes = 1 if fever == 'Yes' else 0
41
- night_sweats_No = 1 if night_sweats == 'No' else 0
42
- night_sweats_Yes = 1 if night_sweats == 'Yes' else 0
43
 
44
- clinical_data = [age, height, weight, reported_cough_dur, heart_rate, temperature,
45
- sex_Female, sex_Male, tb_prior_No, tb_prior_Not_sure, tb_prior_Yes,
46
- tb_prior_Pul_No, tb_prior_Pul_Yes, tb_prior_Extrapul_No, tb_prior_Extrapul_Yes,
47
- tb_prior_Unknown_No, tb_prior_Unknown_Yes, hemoptysis_No, hemoptysis_Yes,
48
- weight_loss_No, weight_loss_Yes, smoke_lweek_No, smoke_lweek_Yes,
49
- fever_No, fever_Yes, night_sweats_No, night_sweats_Yes]
50
- clinical_data = np.array(clinical_data).reshape(1, -1).astype(np.float32)
51
- return clinical_data
52
 
53
- # Main function to run the app
54
- def main():
55
- st.title('TB Cough Sound Analysis')
 
 
 
 
 
 
 
56
 
57
- # Create tabs
58
- tabs = ["Record Cough Sound", "Upload Cough Sound"]
59
- choice = st.sidebar.selectbox("Choose Option", tabs)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
- if choice == "Record Cough Sound":
62
- st.write("**Record Cough Sound**")
63
- st.write("Press the button below to start recording:")
64
- start_recording = st.button("Start Recording")
 
 
 
 
65
 
66
- if start_recording:
67
- duration = 5 # Set the default recording duration to 5 seconds
68
- st.write("Recording started...")
69
- sample_rate = 22050
70
- audio_data = sd.rec(int(duration * sample_rate), samplerate=sample_rate, channels=1)
71
- sd.wait()
72
- write("audio_file.wav", sample_rate, audio_data)
73
- st.success("Recording saved as 'audio_file.wav'.")
74
- st.info("Please proceed to the next step.")
75
-
76
- elif choice == "Upload Cough Sound":
77
- st.write("**Upload Cough Sound**")
78
- uploaded_file = st.file_uploader("Upload Cough Sound (WAV file)", type=["wav"])
79
- if uploaded_file is not None:
80
- with open("audio_file.wav", "wb") as f:
81
- f.write(uploaded_file.getvalue())
82
- st.success("File uploaded successfully.")
83
- st.info("Please proceed to the next step.")
84
-
85
- st.write('**Step 2: Enter Clinical Information**')
86
- age = st.slider('Age', 1, 100, 30)
87
- height = st.slider('Height (cm)', 100, 300, 170)
88
- weight = st.slider('Weight (kg)', 20, 200, 70)
89
- reported_cough_dur = st.slider('Reported Cough Duration (days)', 1, 100, 10)
90
- heart_rate = st.slider('Heart Rate (bpm)', 50, 200, 80)
91
- temperature = st.slider('Body Temperature (°C)', 35.0, 40.0, 37.0)
92
- sex = st.radio('Sex', ('Male', 'Female'))
93
- tb_prior = st.radio('TB Prior', ('No', 'Not sure', 'Yes'))
94
- tb_prior_Pul = st.radio('TB Prior Pul', ('No', 'Yes'))
95
- tb_prior_Extrapul = st.radio('TB Prior Extrapul', ('No', 'Yes'))
96
- tb_prior_Unknown = st.radio('TB Prior Unknown', ('No', 'Yes'))
97
- hemoptysis = st.radio('Hemoptysis', ('No', 'Yes'))
98
- weight_loss = st.radio('Weight Loss', ('No', 'Yes'))
99
- smoke_lweek = st.radio('Smoke Lweek', ('No', 'Yes'))
100
- fever = st.radio('Fever', ('No', 'Yes'))
101
- night_sweats = st.radio('Night Sweats', ('No', 'Yes'))
102
-
103
- if st.button('Predict'):
104
- if choice == "Record Cough Sound":
105
- audio_file_path = "audio_file.wav"
106
- elif choice == "Upload Cough Sound":
107
- audio_file_path = "audio_file.wav"
108
-
109
- raw_values, rate = sf.read(audio_file_path)
110
- audio_data = preprocess_audio(raw_values)
111
- clinical_data = preprocess_clinical_data(age, height, weight, reported_cough_dur, heart_rate, temperature,
112
- sex, tb_prior, tb_prior_Pul, tb_prior_Extrapul, tb_prior_Unknown,
113
- hemoptysis, weight_loss, smoke_lweek, fever, night_sweats)
114
  input_name = model_inference.get_inputs()[0].name
115
  input_name2 = model_inference.get_inputs()[1].name
116
  label_name = model_inference.get_outputs()[0].name
117
  onnx_pred = model_inference.run([label_name], {input_name: audio_data, input_name2: clinical_data})
118
  result = onnx_pred[0]
119
-
120
- st.write(f"**Prediction:** {result[0][0]}")
121
- if result[0][0] >= 0.5:
122
- st.write("Tuberculosis (TB) is found based on the audio data and clinical information.")
123
- else:
124
- st.write("No Tuberculosis (TB) is found based on the audio data and clinical information.")
125
-
126
- if __name__ == "__main__":
127
- main()
128
-
 
2
  import numpy as np
3
  import onnxruntime as rt
4
  import soundfile as sf
 
 
5
 
6
  # Load the ONNX model
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.')