ggirishg commited on
Commit
31efbb5
·
verified ·
1 Parent(s): bc1a503

Upload 2 files

Browse files
Files changed (2) hide show
  1. app2.py +204 -0
  2. index.html +133 -0
app2.py ADDED
@@ -0,0 +1,204 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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}")
index.html ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Audio Recorder</title>
7
+ <style>
8
+ body {
9
+ font-family: Arial, sans-serif;
10
+ background-color: #ffffff;
11
+ margin: 0;
12
+ padding: 0;
13
+ display: flex;
14
+ justify-content: center;
15
+ align-items: center;
16
+ height: 100vh;
17
+ }
18
+
19
+ .container {
20
+ text-align: center;
21
+ background-color: #ffffff;
22
+ border-radius: 0%;
23
+ }
24
+
25
+ h1 {
26
+ color: #000000;
27
+ }
28
+
29
+ button {
30
+ background-color: #40826D;
31
+ color: rgb(0, 0, 0);
32
+ border: none;
33
+ padding: 10px 20px;
34
+ text-align: center;
35
+ text-decoration: none;
36
+ display: inline-block;
37
+ font-size: 16px;
38
+ margin: 10px;
39
+ cursor: pointer;
40
+ border-radius: 5px;
41
+ }
42
+
43
+ button:hover {
44
+ background-color: #40826D;
45
+ }
46
+
47
+ button:disabled {
48
+ background-color: #df5e5e;
49
+ cursor: not-allowed;
50
+ }
51
+
52
+ #timer {
53
+ font-size: 20px;
54
+ margin-top: 20px;
55
+ color: #000000;
56
+ }
57
+ </style>
58
+ </head>
59
+ <body>
60
+ <div class="container">
61
+ <h1>Audio Recorder</h1>
62
+ <button id="startRecording">Start Recording</button>
63
+ <button id="stopRecording" disabled>Stop Recording</button>
64
+ <div id="timer">00:00</div>
65
+ </div>
66
+
67
+ <script>
68
+ let recorder;
69
+ let audioChunks = [];
70
+ let startTime;
71
+ let timerInterval;
72
+
73
+ function updateTime() {
74
+ const elapsedTime = Math.floor((Date.now() - startTime) / 1000);
75
+ const minutes = Math.floor(elapsedTime / 60);
76
+ const seconds = elapsedTime % 60;
77
+ const formattedTime = `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
78
+ document.getElementById('timer').textContent = formattedTime;
79
+ }
80
+
81
+ navigator.mediaDevices.getUserMedia({ audio: true })
82
+ .then(stream => {
83
+ recorder = new MediaRecorder(stream);
84
+
85
+ recorder.ondataavailable = e => {
86
+ audioChunks.push(e.data);
87
+ };
88
+
89
+ recorder.onstart = () => {
90
+ startTime = Date.now();
91
+ timerInterval = setInterval(updateTime, 1000);
92
+ };
93
+
94
+ recorder.onstop = () => {
95
+ const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
96
+ const audioUrl = URL.createObjectURL(audioBlob);
97
+ const a = document.createElement('a');
98
+ a.href = audioUrl;
99
+ a.download = 'recorded_audio.wav';
100
+ document.body.appendChild(a);
101
+ a.click();
102
+
103
+ // Reset
104
+ audioChunks = [];
105
+ clearInterval(timerInterval);
106
+ };
107
+ })
108
+ .catch(err => {
109
+ console.error('Permission to access microphone denied:', err);
110
+ });
111
+
112
+ document.getElementById('startRecording').addEventListener('click', () => {
113
+ recorder.start();
114
+ document.getElementById('startRecording').disabled = true;
115
+ document.getElementById('stopRecording').disabled = false;
116
+ setTimeout(() => {
117
+ recorder.stop();
118
+ document.getElementById('startRecording').disabled = false;
119
+ document.getElementById('stopRecording').disabled = true;
120
+ }, 15000); // 15 seconds
121
+ });
122
+
123
+ document.getElementById('stopRecording').addEventListener('click', () => {
124
+ recorder.stop();
125
+ document.getElementById('startRecording').disabled = false;
126
+ document.getElementById('stopRecording').disabled = true;
127
+ });
128
+ </script>
129
+ </body>
130
+ </html>
131
+ <!--
132
+ (tf) C:\Users\giris>ffmpeg -i C:\Users\giris\Downloads\recorded_audio.wav -acodec pcm_s16le -ar 16000 -ac 1 C:\Users\giris\Downloads\recorded_audio2.wav
133
+ -->