brendabor commited on
Commit
bed56fe
·
1 Parent(s): c18e3ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -53
app.py CHANGED
@@ -6,74 +6,85 @@ import pandas as pd
6
  import numpy as np
7
  from sklearn.preprocessing import StandardScaler
8
 
9
- # Load the tokenizer (ensure it's the one used during training)
10
- tokenizer = joblib.load('tokenizer.pkl')
 
11
 
12
- # Load the emotion prediction model
13
- emotion_model = load_model('lstm_model.h5')
14
 
15
- # Load the dataset
16
- df = pd.read_csv('df1.csv')
17
- df = df.drop(['Unnamed: 0', 'lyrics_filename', 'analysis_url', 'track_href', "type", "id", "uri"], axis=1)
18
 
19
- # Preprocess for content-based
20
- audio_feature_columns = ['danceability', 'energy', 'key', 'loudness', 'mode', 'speechiness',
21
- 'acousticness', 'instrumentalness', 'liveness', 'valence', 'tempo',
22
- 'duration_ms', 'time_signature']
23
 
24
- audio_features = df[audio_feature_columns]
25
- mood_cats = df[['mood_cats']]
26
- mood_cats_df = pd.DataFrame(mood_cats)
27
 
28
- # Normalize audio features for content-based
29
- scaler_cb = StandardScaler()
30
- audio_features_scaled_cb = scaler_cb.fit_transform(audio_features)
31
- audio_features_df_cb = pd.DataFrame(audio_features_scaled_cb, columns=audio_feature_columns)
32
- combined_features_cb = pd.concat([mood_cats, audio_features_df_cb], axis=1)
33
 
34
- # Load the similarity matrix for content-based
35
- similarity_matrix = np.load('similarity_matrix.npy')
36
 
37
- # Load the content-based recommendation function
38
- recommend_cont = joblib.load('recommendation_cont_function.joblib')
39
 
40
- # Preprocessing for KNN
41
- scaler_knn = StandardScaler()
42
- audio_features_scaled_knn = scaler_knn.fit_transform(audio_features)
43
- audio_features_df_knn = pd.DataFrame(audio_features_scaled_knn, columns=audio_feature_columns)
44
- combined_features_knn = pd.concat([mood_cats_df, audio_features_df_knn], axis=1)
45
 
46
- # Load the KNN model
47
- knn = joblib.load('knn_model.joblib')
48
 
49
- # Load the KNN recommendation function
50
- recommend_knn = joblib.load('recommendation_knn_function.joblib')
51
 
52
- # Load the hybrid recommendation function
53
- hybrid_recommendation = joblib.load('hybrid_recommendation_function.joblib')
54
 
55
- # Set up the title of the app
56
- st.title('Emotion and Audio Feature-based Song Recommendation System')
57
 
58
- # Get data from index 0
59
- query_data = df.iloc[0]
 
60
 
61
- # Process the lyrics
62
- sequence = tokenizer.texts_to_sequences([query_data['lyrics']])
63
- padded_sequence = pad_sequences(sequence, maxlen=50)
64
- emotion = emotion_model.predict(padded_sequence).flatten()
65
 
66
- # Mapping emotion predictions to encoded categories
67
- emotion_mapping = {0: 'happy', 1: 'sad', 2: 'calm', 3: 'anger'}
68
- encoded_emotion = np.argmax(emotion)
69
- emotion_category = emotion_mapping[encoded_emotion]
70
 
71
- # Combine emotion and audio features for recommendation
72
- combined_features_hybrid = np.concatenate([encoded_emotion, query_data[audio_feature_columns].values])
 
 
73
 
74
- # Generate recommendations using the hybrid model
75
- hybrid_recs = hybrid_recommendation(song_index=0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
- st.write("Emotion Detected:", emotion[0])
78
- st.header('Recommended Songs (Hybrid)')
79
- st.write(hybrid_recs)
 
6
  import numpy as np
7
  from sklearn.preprocessing import StandardScaler
8
 
9
+ def load_models():
10
+ # Load the tokenizer (ensure it's the one used during training)
11
+ tokenizer = joblib.load('tokenizer.pkl')
12
 
13
+ # Load the emotion prediction model
14
+ emotion_model = load_model('lstm_model.h5')
15
 
16
+ # Load the dataset
17
+ df = pd.read_csv('df1.csv')
18
+ df = df.drop(['Unnamed: 0', 'lyrics_filename', 'analysis_url', 'track_href', "type", "id", "uri"], axis=1)
19
 
20
+ # Preprocess for content-based
21
+ audio_feature_columns = ['danceability', 'energy', 'key', 'loudness', 'mode', 'speechiness',
22
+ 'acousticness', 'instrumentalness', 'liveness', 'valence', 'tempo',
23
+ 'duration_ms', 'time_signature']
24
 
25
+ audio_features = df[audio_feature_columns]
26
+ mood_cats = df[['mood_cats']]
27
+ mood_cats_df = pd.DataFrame(mood_cats)
28
 
29
+ # Normalize audio features for content-based
30
+ scaler_cb = StandardScaler()
31
+ audio_features_scaled_cb = scaler_cb.fit_transform(audio_features)
32
+ audio_features_df_cb = pd.DataFrame(audio_features_scaled_cb, columns=audio_feature_columns)
33
+ combined_features_cb = pd.concat([mood_cats, audio_features_df_cb], axis=1)
34
 
35
+ # Load the similarity matrix for content-based
36
+ similarity_matrix = np.load('similarity_matrix.npy')
37
 
38
+ # Load the content-based recommendation function
39
+ recommend_cont = joblib.load('recommendation_cont_function.joblib')
40
 
41
+ # Preprocessing for KNN
42
+ scaler_knn = StandardScaler()
43
+ audio_features_scaled_knn = scaler_knn.fit_transform(audio_features)
44
+ audio_features_df_knn = pd.DataFrame(audio_features_scaled_knn, columns=audio_feature_columns)
45
+ combined_features_knn = pd.concat([mood_cats_df, audio_features_df_knn], axis=1)
46
 
47
+ # Load the KNN model
48
+ knn = joblib.load('knn_model.joblib')
49
 
50
+ # Load the KNN recommendation function
51
+ recommend_knn = joblib.load('recommendation_knn_function.joblib')
52
 
53
+ # Load the hybrid recommendation function
54
+ hybrid_recommendation = joblib.load('hybrid_recommendation_function.joblib')
55
 
56
+ 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
 
57
 
58
+ def main():
59
+ # Set up the title of the app
60
+ st.title('Emotion and Audio Feature-based Song Recommendation System')
61
 
62
+ # Load models and data
63
+ 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()
 
 
64
 
65
+ # Get data from index 0
66
+ query_data = df.iloc[0]
 
 
67
 
68
+ # Process the lyrics
69
+ sequence = tokenizer.texts_to_sequences([query_data['lyrics']])
70
+ padded_sequence = pad_sequences(sequence, maxlen=50)
71
+ emotion = emotion_model.predict(padded_sequence).flatten()
72
 
73
+ # Mapping emotion predictions to encoded categories
74
+ emotion_mapping = {0: 'happy', 1: 'sad', 2: 'calm', 3: 'anger'}
75
+ encoded_emotion = np.argmax(emotion)
76
+ emotion_category = emotion_mapping[encoded_emotion]
77
+
78
+ # Combine emotion and audio features for recommendation
79
+ combined_features_hybrid = np.concatenate([encoded_emotion, query_data[audio_feature_columns].values])
80
+
81
+ # Generate recommendations using the hybrid model
82
+ hybrid_recs = hybrid_recommendation(song_index=0)
83
+
84
+ st.write("Emotion Detected:", emotion[0])
85
+ st.header('Recommended Songs (Hybrid)')
86
+ st.write(hybrid_recs)
87
+
88
+ if __name__ == '__main__':
89
+ main()
90