Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,204 +1,210 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
import
|
8 |
-
import
|
9 |
-
import
|
10 |
-
import
|
11 |
-
import
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
self.
|
26 |
-
self.
|
27 |
-
self.
|
28 |
-
self.
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
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 |
-
|
86 |
-
#
|
87 |
-
#
|
88 |
-
|
89 |
-
#
|
90 |
-
#
|
91 |
-
|
92 |
-
#
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
#
|
97 |
-
|
98 |
-
#
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
#
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
st.markdown(
|
127 |
-
f'<div style="background-color:#
|
128 |
-
f'<h3 style="color:black;">
|
129 |
-
'</div>',
|
130 |
-
unsafe_allow_html=True
|
131 |
-
)
|
132 |
-
|
133 |
-
|
134 |
-
f'<
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
st.markdown(
|
140 |
-
f'<div style="background-color:#
|
141 |
-
f'<h3 style="color:black;">
|
142 |
-
'</div>',
|
143 |
-
unsafe_allow_html=True
|
144 |
-
)
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
#
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
st.markdown(
|
175 |
-
f'<div style="background-color:#
|
176 |
-
f'<h3 style="color:black;">
|
177 |
-
'</div>',
|
178 |
-
unsafe_allow_html=True
|
179 |
-
)
|
180 |
-
|
181 |
-
|
182 |
-
f'<
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
st.markdown(
|
188 |
-
f'<div style="background-color:#
|
189 |
-
f'<h3 style="color:black;">
|
190 |
-
'</div>',
|
191 |
-
unsafe_allow_html=True
|
192 |
-
)
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
# Try to delete the
|
201 |
-
try:
|
202 |
-
os.remove("C:/Users/giris/Downloads/
|
203 |
-
except Exception as e:
|
204 |
-
print(f"Error deleting '
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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}")
|