Spaces:
Sleeping
Sleeping
File size: 3,797 Bytes
b32ba1d ff8e494 b32ba1d ff8e494 b32ba1d ff8e494 b32ba1d ff8e494 b83b539 ff8e494 b83b539 ff8e494 c52b8a8 ff8e494 b32ba1d | 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 | from flask import Flask, render_template, request, jsonify
import joblib
import pandas as pd
import numpy as np
import librosa
from werkzeug.utils import secure_filename
import os
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads'
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # Max 16_mb file
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
model = joblib.load("music_genre_classifier.pkl")
scaler = joblib.load("scaler.pkl")
le = joblib.load("label_encoder.pkl")
# Helper function
def extract_features(file_path):
y, sr = librosa.load(file_path, sr = 22050, duration = 30) # Max 30 sec audio
# Calculate Features
chroma_stft = librosa.feature.chroma_stft(y = y, sr = sr)
rms = librosa.feature.rms(y = y)
spec_cent = librosa.feature.spectral_centroid(y = y, sr = sr)
spec_bw = librosa.feature.spectral_bandwidth(y = y, sr = sr)
rolloff = librosa.feature.spectral_rolloff(y = y, sr = sr)
zcr = librosa.feature.zero_crossing_rate(y)
harmony, perceptr = librosa.effects.hpss(y)
tempo, _ = librosa.beat.beat_track(y = y, sr = sr)
mfcc = librosa.feature.mfcc(y = y, sr = sr, n_mfcc = 20)
features = {
'length': len(y),
'chroma_stft_mean': np.mean(chroma_stft),
'chroma_stft_var': np.var(chroma_stft),
'rms_mean': np.mean(rms),
'rms_var': np.var(rms),
'spectral_centroid_mean': np.mean(spec_cent),
'spectral_centroid_var': np.var(spec_cent),
'spectral_bandwidth_mean': np.mean(spec_bw),
'spectral_bandwidth_var': np.var(spec_bw),
'rolloff_mean': np.mean(rolloff),
'rolloff_var': np.var(rolloff),
'zero_crossing_rate_mean': np.mean(zcr),
'zero_crossing_rate_var': np.var(zcr),
'harmony_mean': np.mean(harmony),
'harmony_var': np.var(harmony),
'perceptr_mean': np.mean(perceptr),
'perceptr_var': np.var(perceptr),
'tempo': tempo[0] if isinstance(tempo, np.ndarray) else tempo,
}
for i in range(1, 21):
features[f'mfcc{i}_mean'] = np.mean(mfcc[i - 1])
features[f'mfcc{i}_var'] = np.var(mfcc[i - 1])
return features
@app.route('/')
def home():
return render_template('index.html')
@app.route('/predict', methods = ['POST'])
def predict():
# Check if a file was uploaded
if 'audio_file' not in request.files:
return jsonify({'error': 'No file uploaded'}), 400
file = request.files['audio_file']
if file.filename == '':
return jsonify({'error': 'No file selected'}), 400
if file:
filename = secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)
try:
features_dict = extract_features(filepath)
input_df = pd.DataFrame([features_dict])
expected_features = scaler.feature_names_in_
input_df = input_df[expected_features]
scaled_data = scaler.transform(input_df)
prediction_idx = model.predict(scaled_data)[0]
probs = model.predict_proba(scaled_data)[0]
confidence = np.max(probs) * 100
genre = le.inverse_transform([prediction_idx])[0]
# Clean up the uploaded file
os.remove(filepath)
return jsonify({
'prediction': genre,
'confidence': confidence
})
except Exception as e:
if os.path.exists(filepath):
os.remove(filepath)
return jsonify({'error': str(e)}), 500
if __name__ == "__main__":
app.run(host = "0.0.0.0", port = 7860) |