File size: 4,689 Bytes
74569a1 4815911 74569a1 9100bbc 74569a1 9100bbc 74569a1 6bcf173 9100bbc 74569a1 9100bbc 74569a1 9100bbc 74569a1 9100bbc 74569a1 9100bbc 74569a1 9100bbc 74569a1 9100bbc |
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
import streamlit as st
import joblib
import pandas as pd
import numpy as np
import xgboost
# Local Imports
import feature_extraction
import audio_splitting
st.set_page_config(layout="wide")
# Vars
fields_df = ['Chromagram Short-Time Fourier Transform (Chroma-STFT)',
'Root Mean Square Energy (RMS)',
'Spectral Centroid',
'Spectral Bandwidth',
'Spectral Rolloff',
'Zero Crossing Rate',
'Harmony',
'Percussion',
'Tempo',
'Mel-Frequency Cepstral Coefficients (MFCC-1)',
'MFCC-2',
'MFCC-3',
'MFCC-4',
'MFCC-5',
'MFCC-6',
'MFCC-7',
'MFCC-8',
'MFCC-9',
'MFCC-10',
'MFCC-11',
'MFCC-12',
'MFCC-13',
'MFCC-14',
'MFCC-15',
'MFCC-16',
'MFCC-17',
'MFCC-18',
'MFCC-19',
'MFCC-20', ]
st.title("Music Genre Classifier")
st.write("A single-label music genre classifier based and trained on the GTZAN Dataset available for use on "
"Kaggle. All the models have been trained on that dataset.")
st.write("Prediction of following genres")
class_names = ["Blues", "Classical", "Country", "Disco", "HipHop",
"Jazz", "Metal", "Pop", "Reggae", "Rock"]
class_indices = {i: class_name for i, class_name in enumerate(class_names)}
col1, col2 = st.columns(2)
s = ''
with col1:
for i in class_names[:5]:
s += "- " + i + "\n"
st.markdown(s)
s = ''
with col2:
for i in class_names[5:]:
s += "- " + i + "\n"
st.markdown(s)
st.divider()
# Upload music file
st.subheader("Upload a music file")
uploaded_file = st.file_uploader("Upload a music file", type=["mp3", "wav", "ogg"], label_visibility="collapsed")
st.divider()
if uploaded_file is not None:
# User selects a model
all_models = ["K-Nearest Neighbors",
"Logistic Regression",
"Support Vector Machines",
"Neural Network",
"XGB Classifier"]
features_list, val_list = audio_splitting.split_audio(uploaded_file)
features = feature_extraction.scale(features_list)
feature_copy = features_list
feature_copy.insert(19, "-")
st.header("Feature Extraction")
st.write("The given audio sample is processed using the librosa library to get the features extracted used by the "
"models for genre prediction. Following is the dataframe with each of the feature extracted and "
"corresponding mean and variance of the feature")
col3, col4 = st.columns([0.6,0.4])
with col3:
# Features Dataframe
df = pd.DataFrame({
"name": fields_df,
"Mean": feature_copy[2::2],
"Variance": feature_copy[3::2]
})
st.dataframe(
df,
column_config={
"name": "Features",
"Mean": "Mean of Feature",
"Variance": "Variance of Feature"
},
use_container_width=True
)
with col4:
col1, col2 = st.columns([0.55, 0.45])
col1.subheader("Select a model")
with col1:
model_name = st.selectbox("Select a model", all_models, label_visibility="collapsed")
# Load the selected model
if model_name == "K-Nearest Neighbors":
model = joblib.load("./models/knn.pkl")
elif model_name == "Logistic Regression":
model = joblib.load("./models/logistic.pkl")
elif model_name == "Support Vector Machines":
model = joblib.load("./models/svm.pkl")
elif model_name == "Neural Network":
model = joblib.load("./models/nn.pkl")
elif model_name == "XGB Classifier":
model = joblib.load("./models/xgb.pkl")
col2.subheader("Predicted genre")
# Reshape the features to match the expected shape for prediction
reshaped_features = features.reshape(1, -1)
if model_name == "XGB Classifier":
predicted_indices = model.predict(reshaped_features)
predicted_labels = [class_indices[i] for i in predicted_indices]
with col2:
st.metric("Predicted Genre:", str(predicted_labels[0]), label_visibility="collapsed")
else:
predicted_label = model.predict(features)[0]
with col2:
st.metric("Predicted Genre:", str(predicted_label).capitalize(), label_visibility="collapsed")
|