Hetan07 commited on
Commit
74569a1
1 Parent(s): f7b675b

Added the application file

Browse files
Files changed (1) hide show
  1. app.py +121 -0
app.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import joblib
3
+ import numpy as np
4
+ import xgboost
5
+ # from sklearn.ensemble import GradientBoostingClassifier
6
+ # from tensorflow.keras.models import load_model
7
+ import tensorflow
8
+ from keras.losses import binary_crossentropy
9
+ from keras.optimizers import Adam
10
+ from tensorflow import keras
11
+ from keras.models import load_model
12
+ # Local Imports
13
+ import feature_extraction
14
+ import audio_splitting
15
+
16
+ # Create a Streamlit web app
17
+ st.title("Music Genre Classifier")
18
+
19
+ # Upload music file
20
+ uploaded_file = st.file_uploader("Upload a music file", type=["mp3", "wav"])
21
+
22
+ if uploaded_file is not None:
23
+ # User selects a model
24
+ all_models = ["K-Nearest Neighbors - (Single Label)", "Logistic Regression - (Single Label)", "Support Vector Machines - (Single Label)",
25
+ "Neural Network - (Single Label)",
26
+ "XGB Classifier - (Single Label)", "Convolutional Recurrent Neural Network - (Multi Label)", "XGB - (Multi Label)",
27
+ "Neural Network - (Multi Label)","Batch Normalization - (Multi Label)"]
28
+ model_name = st.selectbox("Select a model", all_models)
29
+ st.write(f"Predicition of following genres")
30
+ multi_class_names = ["Metal", "Jazz", "Blues", "R&B", "Classical", "Reggae", "Rap & Hip-Hop", "Punk", "Rock",
31
+ "Country", "Bebop", "Pop", "Soul", "Dance & Electronic", "Folk"]
32
+
33
+ st.write(multi_class_names)
34
+
35
+ # Load the selected model
36
+ if model_name == "K-Nearest Neighbors - (Single Label)":
37
+ model = joblib.load("./models/knn.pkl")
38
+ elif model_name == "Logistic Regression - (Single Label)":
39
+ model = joblib.load("./models/logistic.pkl")
40
+ elif model_name == "Support Vector Machines - (Single Label)":
41
+ model = joblib.load("./models/svm.pkl")
42
+ elif model_name == "Neural Network - (Single Label)":
43
+ model = joblib.load("./models/nn.pkl")
44
+ elif model_name == "XGB Classifier - (Single Label)":
45
+ model = joblib.load("./models/xgb.pkl")
46
+ elif model_name == "XGB - (Multi Label)":
47
+ model = joblib.load("./models/xgb_mlb.pkl")
48
+ elif model_name == "Convolutional Recurrent Neural Network - (Multi Label)":
49
+ model = tensorflow.keras.models.load_model("./models/model_crnn1.h5", compile=False)
50
+ model.compile(loss=binary_crossentropy,
51
+ optimizer=Adam(),
52
+ metrics=['accuracy'])
53
+ elif model_name == "Neural Network - (Multi Label)":
54
+ model = tensorflow.keras.models.load_model("./models/model_nn.h5", compile=False)
55
+ model.compile(loss=binary_crossentropy,
56
+ optimizer=Adam(),
57
+ metrics=['accuracy'])
58
+ elif model_name == "Batch Normalization - (Multi Label)":
59
+ model = tensorflow.keras.models.load_model("./models/model_bn.h5", compile=False)
60
+ model.compile(loss=binary_crossentropy,
61
+ optimizer=Adam(),
62
+ metrics=['accuracy'])
63
+ class_names = ["blues", "classical", "country", "disco", "hiphop", "jazz", "metal", "pop", "reggae", "rock"]
64
+
65
+ xgb_multi_class_names = ["Rock", "Rap & Hip-Hop", "Soul", "Classical", "Dance & Electronic", "Blues","Jazz",
66
+ "Country","Bebop","Folk","Reggae","R&B","Punk","Metal","Pop"]
67
+
68
+ xmulti_class_names = ["Metal", "Blues", "Reggae", "Jazz", "Rock", "Folk", "Classical", "Dance & Electronic",
69
+ "Punk","Bebop", "Pop", "R&B", "Country", "Rap & Hip-Hop", "Soul"]
70
+ class_indices = {i: class_name for i, class_name in enumerate(class_names)}
71
+
72
+ features_list = audio_splitting.split_audio(uploaded_file)
73
+ features = feature_extraction.scale(features_list)
74
+
75
+ st.write(features)
76
+ # Reshape the features to match the expected shape for prediction
77
+ reshaped_features = features.reshape(1, -1)
78
+ if model_name == "XGB - (Multi Label)":
79
+ # Predict labels for the input features
80
+ predicted_indices = model.predict(reshaped_features)
81
+ print(predicted_indices)
82
+ predicted_labels = []
83
+ for i in range(0,len(predicted_indices[0])):
84
+ if predicted_indices[0][i]==1.0:
85
+ predicted_labels.append(xgb_multi_class_names[i])
86
+ if predicted_labels:
87
+ st.write(f"Predicted Genres: {', '.join(predicted_labels)}")
88
+ else:
89
+ st.write("No genres predicted for this input.")
90
+ if model_name == "XGB Classifier - (Single Label)":
91
+ predicted_indices = model.predict(reshaped_features)
92
+ predicted_labels = [class_indices[i] for i in predicted_indices]
93
+ st.write(f"Predicted Genre: {predicted_labels[0]}")
94
+ elif model_name == "Convolutional Recurrent Neural Network - (Multi Label)"\
95
+ or model_name == "Neural Network - (Multi Label)"\
96
+ or model_name == "Batch Normalization - (Multi Label)":
97
+ predicted_probabilities = model.predict(reshaped_features)
98
+
99
+ # Set a threshold for class prediction (e.g., 0.5)
100
+ threshold = 0.3
101
+ print(predicted_probabilities)
102
+ probabilities = []
103
+ if model_name == "Convolutional Recurrent Neural Network - (Multi Label)":
104
+ predicted_labels = [class_name for i, class_name in enumerate(multi_class_names) if
105
+ predicted_probabilities[0][i] >= threshold]
106
+ probabilities = [(class_name,predicted_probabilities[0][i]*100) for i, class_name in enumerate(multi_class_names)]
107
+
108
+ else:
109
+ predicted_labels = [class_name for i,class_name in enumerate(xmulti_class_names) if
110
+ predicted_probabilities[0][i] >= threshold]
111
+ probabilities = [(class_name,predicted_probabilities[0][i]*100) for i, class_name in enumerate(xmulti_class_names)]
112
+
113
+ if predicted_labels:
114
+ st.write(f"All probabilities are:")
115
+ st.write(probabilities)
116
+ st.write(f"Predicted Genres: {', '.join(predicted_labels)}")
117
+ else:
118
+ st.write("No genre predicted above the threshold.")
119
+ else:
120
+ predicted_label = model.predict(reshaped_features)[0]
121
+ st.write(f"Predicted Genre: {predicted_label}")