predict_genre function
Browse files- app.py +39 -0
- genre_classifier.joblib +3 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import joblib
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
# Load your saved joblib model, scaler, label_encoder, feature order
|
| 6 |
+
d = joblib.load("genre_classifier.joblib")
|
| 7 |
+
clf = d["model"]
|
| 8 |
+
scaler = d["scaler"]
|
| 9 |
+
le = d["label_encoder"]
|
| 10 |
+
feature_order = d["features"] # Should match what your web UI sends
|
| 11 |
+
|
| 12 |
+
def predict_genre(
|
| 13 |
+
danceability, energy, key, loudness, mode, speechiness, acousticness,
|
| 14 |
+
instrumentalness, liveness, valence, tempo, time_signature
|
| 15 |
+
):
|
| 16 |
+
# Pack input as expected by model
|
| 17 |
+
X = np.array([[
|
| 18 |
+
danceability, energy, key, loudness, mode, speechiness, acousticness,
|
| 19 |
+
instrumentalness, liveness, valence, tempo, time_signature
|
| 20 |
+
]])
|
| 21 |
+
X_scaled = scaler.transform(X)
|
| 22 |
+
pred = clf.predict(X_scaled)
|
| 23 |
+
label = le.inverse_transform(pred)[0]
|
| 24 |
+
return label
|
| 25 |
+
|
| 26 |
+
# For API: single call with all features
|
| 27 |
+
iface = gr.Interface(
|
| 28 |
+
fn=predict_genre,
|
| 29 |
+
inputs=[
|
| 30 |
+
gr.Number(label=f) for f in feature_order
|
| 31 |
+
],
|
| 32 |
+
outputs=gr.Text(label="Predicted Genre"),
|
| 33 |
+
title="Billboard Genre Classifier",
|
| 34 |
+
description="Predicts the genre from audio features. For API usage, send a POST to /run/predict.",
|
| 35 |
+
allow_flagging="never"
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
if __name__ == "__main__":
|
| 39 |
+
iface.launch()
|
genre_classifier.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:cc0e0fd86f4b93ca401d555e2e366528886bcb7ac76d1e4065ddfb9fc7bd4f89
|
| 3 |
+
size 61085604
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
scikit-learn
|
| 2 |
+
joblib
|
| 3 |
+
gradio
|