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)