Zemedkun commited on
Commit
7963704
1 Parent(s): 97cdff5

Upload 4 files

Browse files
Files changed (4) hide show
  1. READ THIS .txt +0 -0
  2. app.py +127 -0
  3. model.onnx +3 -0
  4. requirements.txt +6 -0
READ THIS .txt ADDED
File without changes
app.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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' # Replace with the actual path to your model
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()
model.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:076ae7c17d22fa1ac1be93f26c12ae5c14962f6c97743433329ced2d36c22030
3
+ size 1852490
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ streamlit
2
+ numpy
3
+ onnxruntime
4
+ soundfile
5
+ sounddevice
6
+ scipy