Spaces:
Runtime error
Runtime error
File size: 3,732 Bytes
edc4276 01e4bba c4d5407 6868cdb 401d1a7 bed56fe c4d5407 bed56fe a4492f7 bed56fe c4d5407 bed56fe 3f56ca4 bed56fe edc4276 bed56fe a9ac4db edc4276 bed56fe 6868cdb bed56fe a4492f7 bed56fe a9ac4db a4492f7 bed56fe a4492f7 bed56fe a4492f7 bed56fe 6868cdb bed56fe 577a126 bed56fe 577a126 bed56fe 577a126 bed56fe c18e3ad bed56fe 577a126 bed56fe 1f9b2ed |
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 |
import streamlit as st
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.sequence import pad_sequences
import joblib
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
def load_models():
# Load the tokenizer (ensure it's the one used during training)
tokenizer = joblib.load('tokenizer.pkl')
# Load the emotion prediction model
emotion_model = load_model('lstm_model.h5')
# Load the dataset
df = pd.read_csv('df1.csv')
df = df.drop(['Unnamed: 0', 'lyrics_filename', 'analysis_url', 'track_href', "type", "id", "uri"], axis=1)
# Preprocess for content-based
audio_feature_columns = ['danceability', 'energy', 'key', 'loudness', 'mode', 'speechiness',
'acousticness', 'instrumentalness', 'liveness', 'valence', 'tempo',
'duration_ms', 'time_signature']
audio_features = df[audio_feature_columns]
mood_cats = df[['mood_cats']]
mood_cats_df = pd.DataFrame(mood_cats)
# Normalize audio features for content-based
scaler_cb = StandardScaler()
audio_features_scaled_cb = scaler_cb.fit_transform(audio_features)
audio_features_df_cb = pd.DataFrame(audio_features_scaled_cb, columns=audio_feature_columns)
combined_features = pd.concat([mood_cats, audio_features_df_cb], axis=1)
# Load the similarity matrix for content-based
similarity_matrix = np.load('similarity_matrix.npy')
# Load the content-based recommendation function
recommend_cont = joblib.load('recommendation_cont_function.joblib')
# Preprocessing for KNN
scaler_knn = StandardScaler()
audio_features_scaled_knn = scaler_knn.fit_transform(audio_features)
audio_features_df_knn = pd.DataFrame(audio_features_scaled_knn, columns=audio_feature_columns)
combined_features = pd.concat([mood_cats_df, audio_features_df_knn], axis=1)
# Load the KNN model
knn = joblib.load('knn_model.joblib')
# Load the KNN recommendation function
recommend_knn = joblib.load('recommendation_knn_function.joblib')
# Load the hybrid recommendation function
hybrid_recommendation = joblib.load('hybrid_recommendation_function.joblib')
return tokenizer, emotion_model, df, audio_feature_columns, combined_features_cb, similarity_matrix, recommend_cont, mood_cats_df, audio_features_df_knn, combined_features_knn, knn, recommend_knn, hybrid_recommendation
def main():
# Set up the title of the app
st.title('Emotion and Audio Feature-based Song Recommendation System')
# Load models and data
tokenizer, emotion_model, df, audio_feature_columns, combined_features_cb, similarity_matrix, recommend_cont, mood_cats_df, audio_features_df_knn, combined_features_knn, knn, recommend_knn, hybrid_recommendation = load_models()
# Get data from index 0
query_data = df.iloc[0]
# Process the lyrics
sequence = tokenizer.texts_to_sequences([query_data['lyrics']])
padded_sequence = pad_sequences(sequence, maxlen=50)
emotion = emotion_model.predict(padded_sequence).flatten()
# Mapping emotion predictions to encoded categories
emotion_mapping = {0: 'happy', 1: 'sad', 2: 'calm', 3: 'anger'}
encoded_emotion = np.argmax(emotion)
emotion_category = emotion_mapping[encoded_emotion]
# Combine emotion and audio features for recommendation
combined_features_hybrid = np.concatenate([encoded_emotion, query_data[audio_feature_columns].values])
# Generate recommendations using the hybrid model
hybrid_recs = hybrid_recommendation(song_index=0)
st.write("Emotion Detected:", emotion[0])
st.header('Recommended Songs (Hybrid)')
st.write(hybrid_recs)
if __name__ == '__main__':
main()
|