Gregory2041 commited on
Commit
7b8298a
1 Parent(s): d4c90c2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -38
app.py CHANGED
@@ -1,22 +1,14 @@
1
  # Importing the required libraries
2
- import os
3
- os.system('apt-get update && apt-get install -y portaudio19-dev')
4
-
5
  import streamlit as st
6
- import sounddevice as sd
7
  import numpy as np
8
- from scipy.io.wavfile import write
9
  import librosa
10
  import matplotlib.pyplot as plt
11
  from tensorflow.keras.models import Sequential, load_model
12
  from tensorflow.keras.layers import Dense, Dropout
13
- import io
14
-
15
- print(sd.query_devices())
16
-
17
- # Set the device ID (replace 'device_id' with the actual ID)
18
- device_id = 1 # Example: choose the correct device ID from the list
19
- sd.default.device = device_id
20
 
21
  # Define the target speakers in a list.
22
  # ALSO THESE ARE FILES THAT WERE NOT USED TO TEST NOR TRAIN THE MODEL
@@ -150,49 +142,45 @@ if uploaded_file is not None:
150
  speaker, probability, _ = classify_speaker("uploaded_audio.wav")
151
  gender, gen_probability = classify_gender("uploaded_audio.wav")
152
 
153
- # Whats the gender of speaker?
154
  st.write(f"Predicted Gender: {gender}")
155
 
156
- # Whats the shot of speaker being a male or female
157
  st.write(f"Gender Probability: {gen_probability}")
158
 
159
  # Which speaker is it?
160
  st.write(f"Predicted Speaker: {speaker}")
161
 
162
- # Whats the chances of being spealer?
163
  st.write(f"Speaker Probability: {probability}")
164
 
165
  except Exception as e:
166
  st.error(f"Error occurred: {e}")
167
 
168
- # Record audio with sounddevice
169
- def record_audio(duration=8, samplerate=22050):
170
- st.write("Recording...")
171
- audio_data = sd.rec(int(duration * samplerate), samplerate=samplerate, channels=1, dtype='int16')
172
- sd.wait() # Wait until recording is finished
173
- write("recorded_audio.wav", samplerate, audio_data) # Save as WAV file
174
- return "recorded_audio.wav"
175
-
176
- if st.button("Record Audio"):
177
- wav_file_path = record_audio(duration=8)
178
- st.write(f"Audio recorded and saved to {wav_file_path}") # save auido
179
- st.audio(wav_file_path) # show audio
180
- audio_to_spectrogram(wav_file_path) # melspectogram of recorded audio
181
  st.image("spectrogram.png", caption="Mel Spectrogram of the recorded audio file", use_column_width="auto", width=200)
182
- speaker, probability, wav_file = classify_speaker(wav_file_path)
183
- gender, gen_probability = classify_gender(wav_file_path)
 
 
184
 
185
- # who do u sound like the most?
186
  st.write(f"Predicted Gender: {gender}")
187
-
188
- # how much do u sound like this speaker?
189
  st.write(f"Gender Probability: {gen_probability}")
190
-
191
- # who do u sound like the most?
192
  st.write(f"Predicted Speaker: {speaker}")
193
-
194
- # how much do u sound like this speaker?
195
  st.write(f"Speaker Probability: {probability}")
196
 
197
- # speaker audio
198
  st.audio(wav_file)
 
1
  # Importing the required libraries
 
 
 
2
  import streamlit as st
 
3
  import numpy as np
 
4
  import librosa
5
  import matplotlib.pyplot as plt
6
  from tensorflow.keras.models import Sequential, load_model
7
  from tensorflow.keras.layers import Dense, Dropout
8
+ import io
9
+ import soundfile as sf
10
+ from streamlit_audio_recorder import st_audiorec
11
+ from scipy.io.wavfile import write, read as wav_read
 
 
 
12
 
13
  # Define the target speakers in a list.
14
  # ALSO THESE ARE FILES THAT WERE NOT USED TO TEST NOR TRAIN THE MODEL
 
142
  speaker, probability, _ = classify_speaker("uploaded_audio.wav")
143
  gender, gen_probability = classify_gender("uploaded_audio.wav")
144
 
145
+ # What's the gender of speaker?
146
  st.write(f"Predicted Gender: {gender}")
147
 
148
+ # What's the shot of speaker being a male or female
149
  st.write(f"Gender Probability: {gen_probability}")
150
 
151
  # Which speaker is it?
152
  st.write(f"Predicted Speaker: {speaker}")
153
 
154
+ # What's the chances of being the speaker?
155
  st.write(f"Speaker Probability: {probability}")
156
 
157
  except Exception as e:
158
  st.error(f"Error occurred: {e}")
159
 
160
+ # Record audio with streamlit_audio_recorder
161
+ recorded_audio = st_audiorec()
162
+
163
+ if recorded_audio:
164
+ # Save the audio as a .wav file
165
+ with open("recorded_audio.wav", "wb") as f:
166
+ f.write(recorded_audio)
167
+
168
+ st.write(f"Audio recorded and saved to recorded_audio.wav") # Show message
169
+ st.audio("recorded_audio.wav") # Show the audio file
170
+
171
+ # Process the recorded audio
172
+ audio_to_spectrogram("recorded_audio.wav")
173
  st.image("spectrogram.png", caption="Mel Spectrogram of the recorded audio file", use_column_width="auto", width=200)
174
+
175
+ # Classify the speaker and gender
176
+ speaker, probability, wav_file = classify_speaker("recorded_audio.wav")
177
+ gender, gen_probability = classify_gender("recorded_audio.wav")
178
 
179
+ # Display results
180
  st.write(f"Predicted Gender: {gender}")
 
 
181
  st.write(f"Gender Probability: {gen_probability}")
 
 
182
  st.write(f"Predicted Speaker: {speaker}")
 
 
183
  st.write(f"Speaker Probability: {probability}")
184
 
185
+ # Display the wav file of the predicted speaker
186
  st.audio(wav_file)