ggirishg commited on
Commit
2167d97
·
verified ·
1 Parent(s): 4a84f3f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +210 -204
app.py CHANGED
@@ -1,204 +1,210 @@
1
- import streamlit as st
2
- import os
3
- import numpy as np
4
- import torchaudio
5
- import tensorflow as tf
6
- from tensorflow.keras.models import load_model
7
- import tensorflow_hub as hub
8
- import time
9
- import psutil
10
- import streamlit.components.v1 as components
11
- import random
12
-
13
- # model_path = 'C:/Users/giris/Downloads/AutismUI/TrillsonFeature_model'
14
- # m = hub.load(model_path)
15
- m = hub.KerasLayer('https://tfhub.dev/google/nonsemantic-speech-benchmark/trillsson4/1')
16
- class TransformerEncoder(tf.keras.layers.Layer):
17
- def __init__(self, embed_dim, num_heads, ff_dim, rate=0.01, **kwargs):
18
- super(TransformerEncoder, self).__init__(**kwargs)
19
- self.embed_dim = embed_dim
20
- self.num_heads = num_heads
21
- self.ff_dim = ff_dim
22
- self.rate = rate
23
- self.att = tf.keras.layers.MultiHeadAttention(num_heads=num_heads, key_dim=embed_dim)
24
- self.ffn = tf.keras.Sequential([tf.keras.layers.Dense(ff_dim, activation="relu"), tf.keras.layers.Dense(embed_dim)])
25
- self.layernorm1 = tf.keras.layers.LayerNormalization(epsilon=1e-6)
26
- self.layernorm2 = tf.keras.layers.LayerNormalization(epsilon=1e-6)
27
- self.dropout1 = tf.keras.layers.Dropout(rate)
28
- self.dropout2 = tf.keras.layers.Dropout(rate)
29
-
30
- def call(self, inputs, training=False):
31
- attn_output = self.att(inputs, inputs)
32
- attn_output = self.dropout1(attn_output, training=training)
33
- out1 = self.layernorm1(inputs + attn_output)
34
- ffn_output = self.ffn(out1)
35
- ffn_output = self.dropout2(ffn_output, training=training)
36
- return self.layernorm2(out1 + ffn_output)
37
-
38
- def get_config(self):
39
- config = super(TransformerEncoder, self).get_config()
40
- config.update({
41
- 'embed_dim': self.embed_dim,
42
- 'num_heads': self.num_heads,
43
- 'ff_dim': self.ff_dim,
44
- 'rate': self.rate
45
- })
46
- return config
47
-
48
- model = load_model('C:/Users/giris/Downloads/AutismUI/autism_detection_model3.h5', custom_objects={'TransformerEncoder': TransformerEncoder})
49
-
50
- def extract_features(path):
51
- sample_rate = 16000
52
- array, fs = torchaudio.load(path)
53
-
54
- array = np.array(array)
55
- if array.shape[0] > 1:
56
- array = np.mean(array, axis=0, keepdims=True)
57
-
58
- embeddings = m(array)['embedding']
59
- embeddings.shape.assert_is_compatible_with([None, 1024])
60
- embeddings = np.squeeze(np.array(embeddings), axis=0)
61
-
62
- return embeddings
63
- # st.markdown('Streamlit is **_really_ cool**.')
64
- st.markdown('<span style="color:black; font-size: 48px; font-weight: bold;">Neu</span> <span style="color:black; font-size: 48px; font-weight: bold;">RO:</span> <span style="color:black; font-size: 48px; font-weight: bold;">An Application for Code-Switched Autism Detection in Children</span>', unsafe_allow_html=True)
65
- # def set_background():
66
- # # HTML code with CSS styling for background image
67
- # page_bg_img = '''
68
- # <style>
69
- # body {
70
- # background-image: url("https://example.com/background_image.jpg");
71
- # background-size: cover;
72
- # }
73
- # </style>
74
- # '''
75
- # st.markdown(page_bg_img, unsafe_allow_html=True)
76
-
77
- # # Call the function to set background image
78
- # set_background()
79
-
80
- # def random_color():
81
- # r = random.randint(0, 255)
82
- # g = random.randint(0, 255)
83
- # b = random.randint(0, 255)
84
- # return f'rgb({r}, {g}, {b})'
85
-
86
- # text = "NeuRO: An Application for Code-Switched Autism Detection in Children"
87
- # words = text.split()
88
-
89
- # styled_text = ""
90
- # for word in words:
91
- # color = random_color()
92
- # styled_text += f'<span style="color:{color}; font-size: 48px; font-weight: bold;">{word}</span> '
93
-
94
- # st.markdown(styled_text, unsafe_allow_html=True)
95
-
96
- #st.title('NeuRO: An Application for Code-Switched Autism Detection in Children')
97
-
98
- # option = st.radio("Choose an option:", ("Upload an audio file", "Record audio"))
99
- option = st.radio("**Choose an option:**", ["Upload an audio file", "Record audio"])
100
-
101
- if option == "Upload an audio file":
102
- uploaded_file = st.file_uploader("Upload an audio file (.wav)", type=["wav"])
103
- if uploaded_file is not None:
104
- start_time = time.time() # Record start time
105
- with st.spinner('Extracting features...'):
106
- # Process the uploaded file
107
- with open("temp_audio.wav", "wb") as f:
108
- f.write(uploaded_file.getbuffer())
109
- features = extract_features("temp_audio.wav")
110
- os.remove("temp_audio.wav")
111
-
112
- # Display prediction probabilities
113
- prediction = model.predict(np.expand_dims(features, axis=0))
114
- autism_probability = prediction[0][1]
115
- normal_probability = prediction[0][0]
116
-
117
- st.subheader("Prediction Probabilities:")
118
-
119
- if autism_probability > normal_probability:
120
- st.markdown(
121
- f'<div style="background-color:#658EA9;padding:20px;border-radius:10px;margin-bottom:40px;">'
122
- f'<h3 style="color:black;">Autism: {autism_probability}</h3>'
123
- '</div>',
124
- unsafe_allow_html=True
125
- )
126
- st.markdown(
127
- f'<div style="background-color:#ADD8E6;padding:20px;border-radius:10px;margin-bottom:40px;">'
128
- f'<h3 style="color:black;">Normal: {normal_probability}</h3>'
129
- '</div>',
130
- unsafe_allow_html=True
131
- )
132
- else:
133
- st.markdown(
134
- f'<div style="background-color:#658EA9;padding:20px;border-radius:10px;margin-bottom:40px;">'
135
- f'<h3 style="color:black;">Normal: {normal_probability}</h3>'
136
- '</div>',
137
- unsafe_allow_html=True
138
- )
139
- st.markdown(
140
- f'<div style="background-color:#ADD8E6;padding:20px;border-radius:10px;margin-bottom:40px;">'
141
- f'<h3 style="color:black;">Autism: {autism_probability}</h3>'
142
- '</div>',
143
- unsafe_allow_html=True
144
- )
145
-
146
- elapsed_time = round(time.time() - start_time, 2)
147
- st.write(f"Elapsed Time: {elapsed_time} seconds")
148
-
149
- else: # Option is "Record audio"
150
- #st.write('<iframe src="http://localhost:8000/index.html" width="300" height="250"></iframe>', unsafe_allow_html=True)
151
- st.components.v1.iframe("http://localhost:8000/index.html", width=500, height=250)
152
-
153
- if st.button("Click to Predict"):
154
- # Run the ffmpeg command to convert the recorded audio
155
- os.system('ffmpeg -i C:/Users/giris/Downloads/recorded_audio.wav -acodec pcm_s16le -ar 16000 -ac 1 C:/Users/giris/Downloads/recorded_audio2.wav')
156
-
157
- # Process the converted audio file
158
- features = extract_features("C:/Users/giris/Downloads/recorded_audio2.wav")
159
-
160
- # Display prediction probabilities
161
- prediction = model.predict(np.expand_dims(features, axis=0))
162
- autism_probability = prediction[0][1]
163
- normal_probability = prediction[0][0]
164
-
165
- st.subheader("Prediction Probabilities:")
166
-
167
- if autism_probability > normal_probability:
168
- st.markdown(
169
- f'<div style="background-color:#658EA9;padding:20px;border-radius:10px;margin-bottom:40px;">'
170
- f'<h3 style="color:black;">Autism: {autism_probability}</h3>'
171
- '</div>',
172
- unsafe_allow_html=True
173
- )
174
- st.markdown(
175
- f'<div style="background-color:#ADD8E6;padding:20px;border-radius:10px;margin-bottom:40px;">'
176
- f'<h3 style="color:black;">Normal: {normal_probability}</h3>'
177
- '</div>',
178
- unsafe_allow_html=True
179
- )
180
- else:
181
- st.markdown(
182
- f'<div style="background-color:#658EA9;padding:20px;border-radius:10px;margin-bottom:40px;">'
183
- f'<h3 style="color:black;">Normal: {normal_probability}</h3>'
184
- '</div>',
185
- unsafe_allow_html=True
186
- )
187
- st.markdown(
188
- f'<div style="background-color:#ADD8E6;padding:20px;border-radius:10px;margin-bottom:40px;">'
189
- f'<h3 style="color:black;">Autism: {autism_probability}</h3>'
190
- '</div>',
191
- unsafe_allow_html=True
192
- )
193
-
194
- # Try to delete the first audio file
195
- try:
196
- os.remove("C:/Users/giris/Downloads/recorded_audio.wav")
197
- except Exception as e:
198
- print(f"Error deleting 'recorded_audio.wav': {e}")
199
-
200
- # Try to delete the second audio file
201
- try:
202
- os.remove("C:/Users/giris/Downloads/recorded_audio2.wav")
203
- except Exception as e:
204
- print(f"Error deleting 'recorded_audio2.wav': {e}")
 
 
 
 
 
 
 
1
+ import subprocess
2
+
3
+ # Ensure setup.sh is executable and then run it using bash
4
+ subprocess.run(['chmod', '+x', 'setup.sh'])
5
+ subprocess.run(['bash', 'setup.sh'], check=True)
6
+
7
+ import streamlit as st
8
+ import os
9
+ import numpy as np
10
+ import torchaudio
11
+ import tensorflow as tf
12
+ from tensorflow.keras.models import load_model
13
+ import tensorflow_hub as hub
14
+ import time
15
+ import psutil
16
+ import streamlit.components.v1 as components
17
+ import random
18
+
19
+ # model_path = 'C:/Users/giris/Downloads/AutismUI/TrillsonFeature_model'
20
+ # m = hub.load(model_path)
21
+ m = hub.KerasLayer('https://tfhub.dev/google/nonsemantic-speech-benchmark/trillsson4/1')
22
+ class TransformerEncoder(tf.keras.layers.Layer):
23
+ def __init__(self, embed_dim, num_heads, ff_dim, rate=0.01, **kwargs):
24
+ super(TransformerEncoder, self).__init__(**kwargs)
25
+ self.embed_dim = embed_dim
26
+ self.num_heads = num_heads
27
+ self.ff_dim = ff_dim
28
+ self.rate = rate
29
+ self.att = tf.keras.layers.MultiHeadAttention(num_heads=num_heads, key_dim=embed_dim)
30
+ self.ffn = tf.keras.Sequential([tf.keras.layers.Dense(ff_dim, activation="relu"), tf.keras.layers.Dense(embed_dim)])
31
+ self.layernorm1 = tf.keras.layers.LayerNormalization(epsilon=1e-6)
32
+ self.layernorm2 = tf.keras.layers.LayerNormalization(epsilon=1e-6)
33
+ self.dropout1 = tf.keras.layers.Dropout(rate)
34
+ self.dropout2 = tf.keras.layers.Dropout(rate)
35
+
36
+ def call(self, inputs, training=False):
37
+ attn_output = self.att(inputs, inputs)
38
+ attn_output = self.dropout1(attn_output, training=training)
39
+ out1 = self.layernorm1(inputs + attn_output)
40
+ ffn_output = self.ffn(out1)
41
+ ffn_output = self.dropout2(ffn_output, training=training)
42
+ return self.layernorm2(out1 + ffn_output)
43
+
44
+ def get_config(self):
45
+ config = super(TransformerEncoder, self).get_config()
46
+ config.update({
47
+ 'embed_dim': self.embed_dim,
48
+ 'num_heads': self.num_heads,
49
+ 'ff_dim': self.ff_dim,
50
+ 'rate': self.rate
51
+ })
52
+ return config
53
+
54
+ model = load_model('C:/Users/giris/Downloads/AutismUI/autism_detection_model3.h5', custom_objects={'TransformerEncoder': TransformerEncoder})
55
+
56
+ def extract_features(path):
57
+ sample_rate = 16000
58
+ array, fs = torchaudio.load(path)
59
+
60
+ array = np.array(array)
61
+ if array.shape[0] > 1:
62
+ array = np.mean(array, axis=0, keepdims=True)
63
+
64
+ embeddings = m(array)['embedding']
65
+ embeddings.shape.assert_is_compatible_with([None, 1024])
66
+ embeddings = np.squeeze(np.array(embeddings), axis=0)
67
+
68
+ return embeddings
69
+ # st.markdown('Streamlit is **_really_ cool**.')
70
+ st.markdown('<span style="color:black; font-size: 48px; font-weight: bold;">Neu</span> <span style="color:black; font-size: 48px; font-weight: bold;">RO:</span> <span style="color:black; font-size: 48px; font-weight: bold;">An Application for Code-Switched Autism Detection in Children</span>', unsafe_allow_html=True)
71
+ # def set_background():
72
+ # # HTML code with CSS styling for background image
73
+ # page_bg_img = '''
74
+ # <style>
75
+ # body {
76
+ # background-image: url("https://example.com/background_image.jpg");
77
+ # background-size: cover;
78
+ # }
79
+ # </style>
80
+ # '''
81
+ # st.markdown(page_bg_img, unsafe_allow_html=True)
82
+
83
+ # # Call the function to set background image
84
+ # set_background()
85
+
86
+ # def random_color():
87
+ # r = random.randint(0, 255)
88
+ # g = random.randint(0, 255)
89
+ # b = random.randint(0, 255)
90
+ # return f'rgb({r}, {g}, {b})'
91
+
92
+ # text = "NeuRO: An Application for Code-Switched Autism Detection in Children"
93
+ # words = text.split()
94
+
95
+ # styled_text = ""
96
+ # for word in words:
97
+ # color = random_color()
98
+ # styled_text += f'<span style="color:{color}; font-size: 48px; font-weight: bold;">{word}</span> '
99
+
100
+ # st.markdown(styled_text, unsafe_allow_html=True)
101
+
102
+ #st.title('NeuRO: An Application for Code-Switched Autism Detection in Children')
103
+
104
+ # option = st.radio("Choose an option:", ("Upload an audio file", "Record audio"))
105
+ option = st.radio("**Choose an option:**", ["Upload an audio file", "Record audio"])
106
+
107
+ if option == "Upload an audio file":
108
+ uploaded_file = st.file_uploader("Upload an audio file (.wav)", type=["wav"])
109
+ if uploaded_file is not None:
110
+ start_time = time.time() # Record start time
111
+ with st.spinner('Extracting features...'):
112
+ # Process the uploaded file
113
+ with open("temp_audio.wav", "wb") as f:
114
+ f.write(uploaded_file.getbuffer())
115
+ features = extract_features("temp_audio.wav")
116
+ os.remove("temp_audio.wav")
117
+
118
+ # Display prediction probabilities
119
+ prediction = model.predict(np.expand_dims(features, axis=0))
120
+ autism_probability = prediction[0][1]
121
+ normal_probability = prediction[0][0]
122
+
123
+ st.subheader("Prediction Probabilities:")
124
+
125
+ if autism_probability > normal_probability:
126
+ st.markdown(
127
+ f'<div style="background-color:#658EA9;padding:20px;border-radius:10px;margin-bottom:40px;">'
128
+ f'<h3 style="color:black;">Autism: {autism_probability}</h3>'
129
+ '</div>',
130
+ unsafe_allow_html=True
131
+ )
132
+ st.markdown(
133
+ f'<div style="background-color:#ADD8E6;padding:20px;border-radius:10px;margin-bottom:40px;">'
134
+ f'<h3 style="color:black;">Normal: {normal_probability}</h3>'
135
+ '</div>',
136
+ unsafe_allow_html=True
137
+ )
138
+ else:
139
+ st.markdown(
140
+ f'<div style="background-color:#658EA9;padding:20px;border-radius:10px;margin-bottom:40px;">'
141
+ f'<h3 style="color:black;">Normal: {normal_probability}</h3>'
142
+ '</div>',
143
+ unsafe_allow_html=True
144
+ )
145
+ st.markdown(
146
+ f'<div style="background-color:#ADD8E6;padding:20px;border-radius:10px;margin-bottom:40px;">'
147
+ f'<h3 style="color:black;">Autism: {autism_probability}</h3>'
148
+ '</div>',
149
+ unsafe_allow_html=True
150
+ )
151
+
152
+ elapsed_time = round(time.time() - start_time, 2)
153
+ st.write(f"Elapsed Time: {elapsed_time} seconds")
154
+
155
+ else: # Option is "Record audio"
156
+ #st.write('<iframe src="http://localhost:8000/index.html" width="300" height="250"></iframe>', unsafe_allow_html=True)
157
+ st.components.v1.iframe("http://localhost:8000/index.html", width=500, height=250)
158
+
159
+ if st.button("Click to Predict"):
160
+ # Run the ffmpeg command to convert the recorded audio
161
+ os.system('ffmpeg -i C:/Users/giris/Downloads/recorded_audio.wav -acodec pcm_s16le -ar 16000 -ac 1 C:/Users/giris/Downloads/recorded_audio2.wav')
162
+
163
+ # Process the converted audio file
164
+ features = extract_features("C:/Users/giris/Downloads/recorded_audio2.wav")
165
+
166
+ # Display prediction probabilities
167
+ prediction = model.predict(np.expand_dims(features, axis=0))
168
+ autism_probability = prediction[0][1]
169
+ normal_probability = prediction[0][0]
170
+
171
+ st.subheader("Prediction Probabilities:")
172
+
173
+ if autism_probability > normal_probability:
174
+ st.markdown(
175
+ f'<div style="background-color:#658EA9;padding:20px;border-radius:10px;margin-bottom:40px;">'
176
+ f'<h3 style="color:black;">Autism: {autism_probability}</h3>'
177
+ '</div>',
178
+ unsafe_allow_html=True
179
+ )
180
+ st.markdown(
181
+ f'<div style="background-color:#ADD8E6;padding:20px;border-radius:10px;margin-bottom:40px;">'
182
+ f'<h3 style="color:black;">Normal: {normal_probability}</h3>'
183
+ '</div>',
184
+ unsafe_allow_html=True
185
+ )
186
+ else:
187
+ st.markdown(
188
+ f'<div style="background-color:#658EA9;padding:20px;border-radius:10px;margin-bottom:40px;">'
189
+ f'<h3 style="color:black;">Normal: {normal_probability}</h3>'
190
+ '</div>',
191
+ unsafe_allow_html=True
192
+ )
193
+ st.markdown(
194
+ f'<div style="background-color:#ADD8E6;padding:20px;border-radius:10px;margin-bottom:40px;">'
195
+ f'<h3 style="color:black;">Autism: {autism_probability}</h3>'
196
+ '</div>',
197
+ unsafe_allow_html=True
198
+ )
199
+
200
+ # Try to delete the first audio file
201
+ try:
202
+ os.remove("C:/Users/giris/Downloads/recorded_audio.wav")
203
+ except Exception as e:
204
+ print(f"Error deleting 'recorded_audio.wav': {e}")
205
+
206
+ # Try to delete the second audio file
207
+ try:
208
+ os.remove("C:/Users/giris/Downloads/recorded_audio2.wav")
209
+ except Exception as e:
210
+ print(f"Error deleting 'recorded_audio2.wav': {e}")