ggirishg commited on
Commit
317082e
·
verified ·
1 Parent(s): 74882e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +377 -5
app.py CHANGED
@@ -1,3 +1,358 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import subprocess
2
  import streamlit as st
3
  import os
@@ -28,8 +383,8 @@ except Exception as e:
28
  m = hub.KerasLayer('https://tfhub.dev/google/nonsemantic-speech-benchmark/trillsson4/1')
29
 
30
  class TransformerEncoder(tf.keras.layers.Layer):
31
- def _init_(self, embed_dim, num_heads, ff_dim, rate=0.01, **kwargs):
32
- super(TransformerEncoder, self)._init_(**kwargs)
33
  self.embed_dim = embed_dim
34
  self.num_heads = num_heads
35
  self.ff_dim = ff_dim
@@ -87,7 +442,7 @@ def extract_features(path):
87
 
88
  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)
89
 
90
- option = st.radio("*Choose an option:*", ["Upload an audio file", "Record audio"])
91
 
92
  def run_prediction(features):
93
  try:
@@ -194,14 +549,17 @@ else: # Option is "Record audio"
194
  align-items: center;
195
  height: 100vh;
196
  }
 
197
  .container {
198
  text-align: center;
199
  background-color: #ffffff;
200
  border-radius: 0%;
201
  }
 
202
  h1 {
203
  color: #000000;
204
  }
 
205
  button {
206
  background-color: #40826D;
207
  color: rgb(0, 0, 0);
@@ -215,13 +573,16 @@ else: # Option is "Record audio"
215
  cursor: pointer;
216
  border-radius: 5px;
217
  }
 
218
  button:hover {
219
  background-color: #40826D;
220
  }
 
221
  button:disabled {
222
  background-color: #df5e5e;
223
  cursor: not-allowed;
224
  }
 
225
  #timer {
226
  font-size: 20px;
227
  margin-top: 20px;
@@ -236,28 +597,34 @@ else: # Option is "Record audio"
236
  <button id="stopRecording" disabled>Stop Recording</button>
237
  <div id="timer">00:00</div>
238
  </div>
 
239
  <script>
240
  let recorder;
241
  let audioChunks = [];
242
  let startTime;
243
  let timerInterval;
 
244
  function updateTime() {
245
  const elapsedTime = Math.floor((Date.now() - startTime) / 1000);
246
  const minutes = Math.floor(elapsedTime / 60);
247
  const seconds = elapsedTime % 60;
248
- const formattedTime = ${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')};
249
  document.getElementById('timer').textContent = formattedTime;
250
  }
 
251
  navigator.mediaDevices.getUserMedia({ audio: true })
252
  .then(stream => {
253
  recorder = new MediaRecorder(stream);
 
254
  recorder.ondataavailable = e => {
255
  audioChunks.push(e.data);
256
  };
 
257
  recorder.onstart = () => {
258
  startTime = Date.now();
259
  timerInterval = setInterval(updateTime, 1000);
260
  };
 
261
  recorder.onstop = () => {
262
  const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
263
  const audioUrl = URL.createObjectURL(audioBlob);
@@ -266,6 +633,7 @@ else: # Option is "Record audio"
266
  a.download = 'recorded_audio.wav';
267
  document.body.appendChild(a);
268
  a.click();
 
269
  // Reset
270
  audioChunks = [];
271
  clearInterval(timerInterval);
@@ -274,6 +642,7 @@ else: # Option is "Record audio"
274
  .catch(err => {
275
  console.error('Permission to access microphone denied:', err);
276
  });
 
277
  document.getElementById('startRecording').addEventListener('click', () => {
278
  recorder.start();
279
  document.getElementById('startRecording').disabled = true;
@@ -284,6 +653,7 @@ else: # Option is "Record audio"
284
  document.getElementById('stopRecording').disabled = true;
285
  }, 15000); // 15 seconds
286
  });
 
287
  document.getElementById('stopRecording').addEventListener('click', () => {
288
  recorder.stop();
289
  document.getElementById('startRecording').disabled = false;
@@ -323,4 +693,6 @@ else: # Option is "Record audio"
323
  except Exception as e:
324
  print(f"Error deleting 'recorded_audio2.wav': {e}")
325
  except Exception as e:
326
- st.error(f"An error occurred: {e}")
 
 
 
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 subprocess
10
+ st.markdown(
11
+ """
12
+ <style>
13
+ body {
14
+ background-color: white;
15
+ }
16
+ .stApp {
17
+ background-color: white;
18
+ }
19
+ .main {
20
+ background-color: white;
21
+ }
22
+ </style>
23
+ """,
24
+ unsafe_allow_html=True
25
+ )
26
+
27
+ # Attempt to set GPU memory growth
28
+ try:
29
+ from tensorflow.compat.v1 import ConfigProto
30
+ from tensorflow.compat.v1 import InteractiveSession
31
+
32
+ config = ConfigProto()
33
+ config.gpu_options.allow_growth = True
34
+ session = InteractiveSession(config=config)
35
+ except Exception as e:
36
+ st.warning(f"Could not set GPU memory growth: {e}")
37
+
38
+ model_path = 'TrillsonFeature_model'
39
+ m = hub.load(model_path)
40
+
41
+ class TransformerEncoder(tf.keras.layers.Layer):
42
+ def __init__(self, embed_dim, num_heads, ff_dim, rate=0.01, **kwargs):
43
+ super(TransformerEncoder, self).__init__(**kwargs)
44
+ self.embed_dim = embed_dim
45
+ self.num_heads = num_heads
46
+ self.ff_dim = ff_dim
47
+ self.rate = rate
48
+ self.att = tf.keras.layers.MultiHeadAttention(num_heads=num_heads, key_dim=embed_dim)
49
+ self.ffn = tf.keras.Sequential([tf.keras.layers.Dense(ff_dim, activation="relu"), tf.keras.layers.Dense(embed_dim)])
50
+ self.layernorm1 = tf.keras.layers.LayerNormalization(epsilon=1e-6)
51
+ self.layernorm2 = tf.keras.layers.LayerNormalization(epsilon=1e-6)
52
+ self.dropout1 = tf.keras.layers.Dropout(rate)
53
+ self.dropout2 = tf.keras.layers.Dropout(rate)
54
+
55
+ def call(self, inputs, training=False):
56
+ attn_output = self.att(inputs, inputs)
57
+ attn_output = self.dropout1(attn_output, training=training)
58
+ out1 = self.layernorm1(inputs + attn_output)
59
+ ffn_output = self.ffn(out1)
60
+ ffn_output = self.dropout2(ffn_output, training=training)
61
+ return self.layernorm2(out1 + ffn_output)
62
+
63
+ def get_config(self):
64
+ config = super(TransformerEncoder, self).get_config()
65
+ config.update({
66
+ 'embed_dim': self.embed_dim,
67
+ 'num_heads': self.num_heads,
68
+ 'ff_dim': self.ff_dim,
69
+ 'rate': self.rate
70
+ })
71
+ return config
72
+
73
+ def load_autism_model():
74
+ try:
75
+ return load_model('autism_detection_model3.h5', custom_objects={'TransformerEncoder': TransformerEncoder})
76
+ except Exception as e:
77
+ st.error(f"Error loading model: {e}")
78
+ return None
79
+
80
+ model = load_autism_model()
81
+
82
+ def extract_features(path):
83
+ sample_rate = 16000
84
+ array, fs = torchaudio.load(path)
85
+
86
+ array = np.array(array)
87
+ if array.shape[0] > 1:
88
+ array = np.mean(array, axis=0, keepdims=True)
89
+
90
+ # Truncate the audio to 10 seconds for reducing memory usage
91
+ array = array[:, :sample_rate * 10]
92
+
93
+ embeddings = m(array)['embedding']
94
+ embeddings.shape.assert_is_compatible_with([None, 1024])
95
+ embeddings = np.squeeze(np.array(embeddings), axis=0)
96
+
97
+ return embeddings
98
+
99
+ 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)
100
+
101
+ option = st.radio("**Choose an option:**", ["Upload an audio file", "Record audio"])
102
+
103
+ def run_prediction(features):
104
+ try:
105
+ prediction = model.predict(np.expand_dims(features, axis=0))
106
+ autism_probability = prediction[0][1]
107
+ normal_probability = prediction[0][0]
108
+
109
+ st.subheader("Prediction Probabilities:")
110
+
111
+ if autism_probability > normal_probability:
112
+ st.markdown(
113
+ f'<div style="background-color:#658EA9;padding:20px;border-radius:10px;margin-bottom:40px;">'
114
+ f'<h3 style="color:black;">Autism: {autism_probability}</h3>'
115
+ '</div>',
116
+ unsafe_allow_html=True
117
+ )
118
+ st.markdown(
119
+ f'<div style="background-color:#ADD8E6;padding:20px;border-radius:10px;margin-bottom:40px;">'
120
+ f'<h3 style="color:black;">Normal: {normal_probability}</h3>'
121
+ '</div>',
122
+ unsafe_allow_html=True
123
+ )
124
+ else:
125
+ st.markdown(
126
+ f'<div style="background-color:#658EA9;padding:20px;border-radius:10px;margin-bottom:40px;">'
127
+ f'<h3 style="color:black;">Normal: {normal_probability}</h3>'
128
+ '</div>',
129
+ unsafe_allow_html=True
130
+ )
131
+ st.markdown(
132
+ f'<div style="background-color:#ADD8E6;padding:20px;border-radius:10px;margin-bottom:40px;">'
133
+ f'<h3 style="color:black;">Autism: {autism_probability}</h3>'
134
+ '</div>',
135
+ unsafe_allow_html=True
136
+ )
137
+
138
+ except tf.errors.ResourceExhaustedError as e:
139
+ st.error("Resource exhausted error: switching to CPU.")
140
+ with tf.device('/cpu:0'):
141
+ prediction = model.predict(np.expand_dims(features, axis=0))
142
+ autism_probability = prediction[0][1]
143
+ normal_probability = prediction[0][0]
144
+
145
+ st.subheader("Prediction Probabilities:")
146
+
147
+ if autism_probability > normal_probability:
148
+ st.markdown(
149
+ f'<div style="background-color:#658EA9;padding:20px;border-radius:10px;margin-bottom:40px;">'
150
+ f'<h3 style="color:black;">Autism: {autism_probability}</h3>'
151
+ '</div>',
152
+ unsafe_allow_html=True
153
+ )
154
+ st.markdown(
155
+ f'<div style="background-color:#ADD8E6;padding:20px;border-radius:10px;margin-bottom:40px;">'
156
+ f'<h3 style="color:black;">Normal: {normal_probability}</h3>'
157
+ '</div>',
158
+ unsafe_allow_html=True
159
+ )
160
+ else:
161
+ st.markdown(
162
+ f'<div style="background-color:#658EA9;padding:20px;border-radius:10px;margin-bottom:40px;">'
163
+ f'<h3 style="color:black;">Normal: {normal_probability}</h3>'
164
+ '</div>',
165
+ unsafe_allow_html=True
166
+ )
167
+ st.markdown(
168
+ f'<div style="background-color:#ADD8E6;padding:20px;border-radius:10px;margin-bottom:40px;">'
169
+ f'<h3 style="color:black;">Autism: {autism_probability}</h3>'
170
+ '</div>',
171
+ unsafe_allow_html=True
172
+ )
173
+
174
+ if option == "Upload an audio file":
175
+ uploaded_file = st.file_uploader("Upload an audio file (.wav)", type=["wav"])
176
+ if uploaded_file is not None:
177
+ start_time = time.time() # Record start time
178
+ with st.spinner('Extracting features...'):
179
+ # Process the uploaded file
180
+ with open("temp_audio.wav", "wb") as f:
181
+ f.write(uploaded_file.getbuffer())
182
+ features = extract_features("temp_audio.wav")
183
+ os.remove("temp_audio.wav")
184
+ run_prediction(features)
185
+ elapsed_time = round(time.time() - start_time, 2)
186
+ st.write(f"Elapsed Time: {elapsed_time} seconds")
187
+
188
+ else: # Option is "Record audio"
189
+ audio_recorder_html = '''
190
+ <!DOCTYPE html>
191
+ <html lang="en">
192
+ <head>
193
+ <meta charset="UTF-8">
194
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
195
+ <title>Audio Recorder</title>
196
+ <style>
197
+ body {
198
+ font-family: Arial, sans-serif;
199
+ background-color: #ffffff;
200
+ margin: 0;
201
+ padding: 0;
202
+ display: flex;
203
+ justify-content: center;
204
+ align-items: center;
205
+ height: 100vh;
206
+ }
207
+
208
+ .container {
209
+ text-align: center;
210
+ background-color: #ffffff;
211
+ border-radius: 0%;
212
+ }
213
+
214
+ h1 {
215
+ color: #000000;
216
+ }
217
+
218
+ button {
219
+ background-color: #40826D;
220
+ color: rgb(0, 0, 0);
221
+ border: none;
222
+ padding: 10px 20px;
223
+ text-align: center;
224
+ text-decoration: none;
225
+ display: inline-block;
226
+ font-size: 16px;
227
+ margin: 10px;
228
+ cursor: pointer;
229
+ border-radius: 5px;
230
+ }
231
+
232
+ button:hover {
233
+ background-color: #40826D;
234
+ }
235
+
236
+ button:disabled {
237
+ background-color: #df5e5e;
238
+ cursor: not-allowed;
239
+ }
240
+
241
+ #timer {
242
+ font-size: 20px;
243
+ margin-top: 20px;
244
+ color: #000000;
245
+ }
246
+ </style>
247
+ </head>
248
+ <body>
249
+ <div class="container">
250
+ <h1>Audio Recorder</h1>
251
+ <button id="startRecording">Start Recording</button>
252
+ <button id="stopRecording" disabled>Stop Recording</button>
253
+ <div id="timer">00:00</div>
254
+ </div>
255
+
256
+ <script>
257
+ let recorder;
258
+ let audioChunks = [];
259
+ let startTime;
260
+ let timerInterval;
261
+
262
+ function updateTime() {
263
+ const elapsedTime = Math.floor((Date.now() - startTime) / 1000);
264
+ const minutes = Math.floor(elapsedTime / 60);
265
+ const seconds = elapsedTime % 60;
266
+ const formattedTime = `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
267
+ document.getElementById('timer').textContent = formattedTime;
268
+ }
269
+
270
+ navigator.mediaDevices.getUserMedia({ audio: true })
271
+ .then(stream => {
272
+ recorder = new MediaRecorder(stream);
273
+
274
+ recorder.ondataavailable = e => {
275
+ audioChunks.push(e.data);
276
+ };
277
+
278
+ recorder.onstart = () => {
279
+ startTime = Date.now();
280
+ timerInterval = setInterval(updateTime, 1000);
281
+ };
282
+
283
+ recorder.onstop = () => {
284
+ const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
285
+ const audioUrl = URL.createObjectURL(audioBlob);
286
+ const a = document.createElement('a');
287
+ a.href = audioUrl;
288
+ a.download = 'recorded_audio.wav';
289
+ document.body.appendChild(a);
290
+ a.click();
291
+
292
+ // Reset
293
+ audioChunks = [];
294
+ clearInterval(timerInterval);
295
+ };
296
+ })
297
+ .catch(err => {
298
+ console.error('Permission to access microphone denied:', err);
299
+ });
300
+
301
+ document.getElementById('startRecording').addEventListener('click', () => {
302
+ recorder.start();
303
+ document.getElementById('startRecording').disabled = true;
304
+ document.getElementById('stopRecording').disabled = false;
305
+ setTimeout(() => {
306
+ recorder.stop();
307
+ document.getElementById('startRecording').disabled = false;
308
+ document.getElementById('stopRecording').disabled = true;
309
+ }, 15000); // 15 seconds
310
+ });
311
+
312
+ document.getElementById('stopRecording').addEventListener('click', () => {
313
+ recorder.stop();
314
+ document.getElementById('startRecording').disabled = false;
315
+ document.getElementById('stopRecording').disabled = true;
316
+ });
317
+ </script>
318
+ </body>
319
+ </html>
320
+ '''
321
+ st.components.v1.html(audio_recorder_html, height=600)
322
+
323
+ if st.button("Click to Predict"):
324
+ try:
325
+ # Run the ffmpeg command to convert the recorded audio
326
+ command = 'ffmpeg -i C:/Users/giris/Downloads/recorded_audio.wav -acodec pcm_s16le -ar 16000 -ac 1 C:/Users/giris/Downloads/recorded_audio2.wav'
327
+ result = subprocess.run(command, shell=True, capture_output=True, text=True)
328
+ if result.returncode != 0:
329
+ st.error(f"Error running ffmpeg: {result.stderr}")
330
+ else:
331
+ # Check if the file exists
332
+ if not os.path.exists("C:/Users/giris/Downloads/recorded_audio2.wav"):
333
+ st.error("The converted audio file was not created.")
334
+ else:
335
+ # Process the converted audio file
336
+ features = extract_features("C:/Users/giris/Downloads/recorded_audio2.wav")
337
+ run_prediction(features)
338
+
339
+ # Try to delete the first audio file
340
+ try:
341
+ os.remove("recorded_audio.wav")
342
+ except Exception as e:
343
+ print(f"Error deleting 'recorded_audio.wav': {e}")
344
+
345
+ # Try to delete the second audio file
346
+ try:
347
+ os.remove("recorded_audio2.wav")
348
+ except Exception as e:
349
+ print(f"Error deleting 'recorded_audio2.wav': {e}")
350
+ except Exception as e:
351
+ st.error(f"An error occurred: {e}")
352
+
353
+
354
+
355
+ """
356
  import subprocess
357
  import streamlit as st
358
  import os
 
383
  m = hub.KerasLayer('https://tfhub.dev/google/nonsemantic-speech-benchmark/trillsson4/1')
384
 
385
  class TransformerEncoder(tf.keras.layers.Layer):
386
+ def __init__(self, embed_dim, num_heads, ff_dim, rate=0.01, **kwargs):
387
+ super(TransformerEncoder, self).__init__(**kwargs)
388
  self.embed_dim = embed_dim
389
  self.num_heads = num_heads
390
  self.ff_dim = ff_dim
 
442
 
443
  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)
444
 
445
+ option = st.radio("**Choose an option:**", ["Upload an audio file", "Record audio"])
446
 
447
  def run_prediction(features):
448
  try:
 
549
  align-items: center;
550
  height: 100vh;
551
  }
552
+
553
  .container {
554
  text-align: center;
555
  background-color: #ffffff;
556
  border-radius: 0%;
557
  }
558
+
559
  h1 {
560
  color: #000000;
561
  }
562
+
563
  button {
564
  background-color: #40826D;
565
  color: rgb(0, 0, 0);
 
573
  cursor: pointer;
574
  border-radius: 5px;
575
  }
576
+
577
  button:hover {
578
  background-color: #40826D;
579
  }
580
+
581
  button:disabled {
582
  background-color: #df5e5e;
583
  cursor: not-allowed;
584
  }
585
+
586
  #timer {
587
  font-size: 20px;
588
  margin-top: 20px;
 
597
  <button id="stopRecording" disabled>Stop Recording</button>
598
  <div id="timer">00:00</div>
599
  </div>
600
+
601
  <script>
602
  let recorder;
603
  let audioChunks = [];
604
  let startTime;
605
  let timerInterval;
606
+
607
  function updateTime() {
608
  const elapsedTime = Math.floor((Date.now() - startTime) / 1000);
609
  const minutes = Math.floor(elapsedTime / 60);
610
  const seconds = elapsedTime % 60;
611
+ const formattedTime = `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
612
  document.getElementById('timer').textContent = formattedTime;
613
  }
614
+
615
  navigator.mediaDevices.getUserMedia({ audio: true })
616
  .then(stream => {
617
  recorder = new MediaRecorder(stream);
618
+
619
  recorder.ondataavailable = e => {
620
  audioChunks.push(e.data);
621
  };
622
+
623
  recorder.onstart = () => {
624
  startTime = Date.now();
625
  timerInterval = setInterval(updateTime, 1000);
626
  };
627
+
628
  recorder.onstop = () => {
629
  const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
630
  const audioUrl = URL.createObjectURL(audioBlob);
 
633
  a.download = 'recorded_audio.wav';
634
  document.body.appendChild(a);
635
  a.click();
636
+
637
  // Reset
638
  audioChunks = [];
639
  clearInterval(timerInterval);
 
642
  .catch(err => {
643
  console.error('Permission to access microphone denied:', err);
644
  });
645
+
646
  document.getElementById('startRecording').addEventListener('click', () => {
647
  recorder.start();
648
  document.getElementById('startRecording').disabled = true;
 
653
  document.getElementById('stopRecording').disabled = true;
654
  }, 15000); // 15 seconds
655
  });
656
+
657
  document.getElementById('stopRecording').addEventListener('click', () => {
658
  recorder.stop();
659
  document.getElementById('startRecording').disabled = false;
 
693
  except Exception as e:
694
  print(f"Error deleting 'recorded_audio2.wav': {e}")
695
  except Exception as e:
696
+ st.error(f"An error occurred: {e}")
697
+
698
+ """