-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathappStreamlit.py
132 lines (112 loc) · 4.34 KB
/
appStreamlit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
127
128
129
130
131
132
import streamlit as st
import keras
import numpy as np
import librosa
import joblib
import random
def process_audio_file(y):
# Zero Crossing Rate
zcr = librosa.feature.zero_crossing_rate(y).mean()
# Root Mean Square Energy
rmse = librosa.feature.rms(y=y).mean()
# Spectral Centroid
spectral_centroid = librosa.feature.spectral_centroid(y=y, sr=sr).mean()
# Spectral Bandwidth
spectral_bandwidth = librosa.feature.spectral_bandwidth(y=y, sr=sr).mean()
# Spectral Rolloff
spectral_rolloff = librosa.feature.spectral_rolloff(y=y, sr=sr, roll_percent=0.85).mean()
# MFCCs
mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13).mean(axis = 1).flatten()
return np.concatenate([np.array([zcr]), np.array([rmse]), np.array([spectral_centroid]), np.array([spectral_bandwidth]), np.array([spectral_rolloff]), mfccs], axis = 0).flatten()
# Sample models dictionary for illustration
modelsNames = {
"model1": "NONE",
"model2": "ANN",
"model3": "CNN",
"model4": "RNN",
"model5": "KNN"
}
speaker_dict = {
0: "Abdul Hamid",
1: "Abdurrafiu",
2: "Abubakar",
3: "Aisha",
4: "Clement",
5: "Clinton",
6: "Comfort",
7: "Faizat",
8: "Faridah",
9: "Faruq",
10: "Feyi",
11: "Gideon",
12: "Happiness",
13: "Ifeoluwa",
14: "Jomiloju",
15: "Kabirat",
16: "Kenny",
17: "Maliqah",
18: "Masturoh",
19: "Michopat",
20: "Naimat",
21: "Nife",
22: "Nonso",
23: "Olumide",
24: "Precious",
25: "Saheed",
26: "Shukrah",
27: "Sunday ",
28: "Tekenah ",
29: "Yessy",
30: "Unknown"
}
modelAnn = keras.models.load_model('ANN MODEL.keras')
modelCnn = keras.models.load_model('CNN MODEL.keras')
modelRnn = keras.models.load_model('RNN MODEL.keras')
modelKnn = joblib.load('KNN MODEL.joblib')
models ={
'ANN': modelAnn,
'CNN': modelCnn,
'RNN': modelRnn,
'KNN': modelKnn
}
# Predict function (mock function to simulate prediction)
def predict_with_model(audio_data, model_name):
X = process_audio_file(audio_data).reshape(1,-1)
if model_name=='NONE':
# preds = []
# preds.append(models['ANN'].predict(X).argmax())
# preds.append(models['CNN'].predict(X).argmax())
# preds.append(models['RNN'].predict(X.reshape(1,1,-1)).argmax())
# preds.append(models['KNN'].predict(X).tolist()[0])
# output = f"{speaker_dict[preds[0]]} by ANN, {speaker_dict[preds[1]]} by CNN, {speaker_dict[preds[2]]} by RNN, {speaker_dict[preds[3]]} by KNN"
model_name = random.choice(list(models.keys()))
if model_name=='ANN' or model_name=='CNN': return f"{speaker_dict[models[model_name].predict(X).argmax()]} by {model_name}"
elif model_name == 'KNN': return f"{speaker_dict[models['KNN'].predict(X).tolist()[0]]} by KNN"
else: return f"{speaker_dict[models['RNN'].predict(X.reshape(1,1,-1)).argmax()]} by RNN"
# return output
else:
if model_name=='ANN' or model_name=='CNN': return models[model_name].predict(X).argmax()
elif model_name == 'KNN': return models['KNN'].predict(X).tolist()[0]
else: return models['RNN'].predict(X.reshape(1,1,-1)).argmax()
# Streamlit App UI
st.title("Audio Classifier")
# File upload section
st.subheader("Upload an Audio File")
audio_file = st.file_uploader("Choose an audio file", type=["wav", "mp3", "m4a"])
# Model selection section
st.subheader("Select a Model")
model_name = st.radio("Choose a model for prediction", ['NONE', 'ANN', 'CNN', 'RNN', 'KNN'])
if audio_file is not None and model_name is not None:
y, sr = librosa.load(audio_file, mono=True, duration=30)
st.audio(y, format=audio_file.name.split('.')[-1], sample_rate = sr, autoplay = True, loop = True)
# Prediction section
if st.button("Classify Audio"):
if audio_file is not None and model_name is not None:
# Run prediction
prediction = predict_with_model(y, model_name)
# Display result
if isinstance(prediction, str): st.success(f"Voice is recognized: {prediction}")
else: st.success(f"Voice is recognized: {speaker_dict[prediction]}")
# Optionally play the audio file
else:
st.error("Please upload a file and select a model.")