File size: 1,151 Bytes
ade8067
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import joblib
import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder

model = joblib.load("mood_prediction_model.pkl")
label_encoder = joblib.load("mood_label_encoder.pkl")

genre_mapping = {
    "Hip-Hop": 0, "Pop": 1, "Electronic": 2, "Folk": 3, "Classical": 4,
    "Jazz": 5, "Country": 6, "R&B": 7, "Reggae": 8
}

def predict_mood(data):
    genre = data['genre']
    popularity = data['popularity']
    stream = data['stream']
    duration = data['duration']
    explicit_content = data['explicit_content']

    genre_encoded = genre_mapping.get(genre, -1)
    explicit_content_encoded = 1 if explicit_content.lower() == "yes" else 0
    
    input_data = pd.DataFrame([{
        'genre_encoded': genre_encoded,
        'popularity': popularity,
        'stream': stream,
        'duration': duration,
        'explicit_content_encoded': explicit_content_encoded
    }])

    mood_encoded = model.predict(input_data)[0]
    mood = label_encoder.inverse_transform([mood_encoded])[0]
    
    return {"predicted_mood": mood}

def predict(inputs):
    return predict_mood(inputs)