soctopus2327
commited on
Commit
•
ade8067
1
Parent(s):
98376d4
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import joblib
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
from sklearn.preprocessing import LabelEncoder
|
5 |
+
|
6 |
+
model = joblib.load("mood_prediction_model.pkl")
|
7 |
+
label_encoder = joblib.load("mood_label_encoder.pkl")
|
8 |
+
|
9 |
+
genre_mapping = {
|
10 |
+
"Hip-Hop": 0, "Pop": 1, "Electronic": 2, "Folk": 3, "Classical": 4,
|
11 |
+
"Jazz": 5, "Country": 6, "R&B": 7, "Reggae": 8
|
12 |
+
}
|
13 |
+
|
14 |
+
def predict_mood(data):
|
15 |
+
genre = data['genre']
|
16 |
+
popularity = data['popularity']
|
17 |
+
stream = data['stream']
|
18 |
+
duration = data['duration']
|
19 |
+
explicit_content = data['explicit_content']
|
20 |
+
|
21 |
+
genre_encoded = genre_mapping.get(genre, -1)
|
22 |
+
explicit_content_encoded = 1 if explicit_content.lower() == "yes" else 0
|
23 |
+
|
24 |
+
input_data = pd.DataFrame([{
|
25 |
+
'genre_encoded': genre_encoded,
|
26 |
+
'popularity': popularity,
|
27 |
+
'stream': stream,
|
28 |
+
'duration': duration,
|
29 |
+
'explicit_content_encoded': explicit_content_encoded
|
30 |
+
}])
|
31 |
+
|
32 |
+
mood_encoded = model.predict(input_data)[0]
|
33 |
+
mood = label_encoder.inverse_transform([mood_encoded])[0]
|
34 |
+
|
35 |
+
return {"predicted_mood": mood}
|
36 |
+
|
37 |
+
def predict(inputs):
|
38 |
+
return predict_mood(inputs)
|