brendabor commited on
Commit
a4492f7
1 Parent(s): 40c92af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -20
app.py CHANGED
@@ -6,36 +6,51 @@ import pandas as pd
6
  import numpy as np
7
  from sklearn.preprocessing import StandardScaler
8
 
9
- # Load the emotion prediction model
10
- emotion_model = load_model('lstm_model.h5')
11
-
12
  # Load the tokenizer (ensure it's the one used during training)
13
  tokenizer = joblib.load('tokenizer.pkl')
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
- # Load the content-based recommendation module
20
- recommend_cont_module = joblib.load('recommendation_cont_function.joblib')
 
 
21
 
22
- # Call the function from the module
23
- hybrid_recs = recommend_cont_module.recommend_cont(song_index=0)
 
24
 
25
- # Load the hybrid recommendation function
26
- hybrid_recommendation = joblib.load('hybrid_recommendation_function.joblib')
 
 
 
27
 
28
- # Preprocess for content-based
29
- audio_features = df[['danceability', 'energy', 'key', 'loudness', 'mode', 'speechiness',
30
- 'acousticness', 'instrumentalness', 'liveness', 'valence', 'tempo',
31
- 'duration_ms', 'time_signature']]
32
- mood_cats = df[['mood_cats']]
33
 
34
- scaler = StandardScaler()
35
- audio_features_scaled = scaler.fit_transform(audio_features)
36
- audio_features_df = pd.DataFrame(audio_features_scaled, columns=audio_features.columns)
37
- mood_cats_df = pd.DataFrame(mood_cats)
38
- combined_features_content = pd.concat([mood_cats_df, audio_features_df], axis=1)
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
  # Set up the title of the app
41
  st.title('Emotion and Audio Feature-based Song Recommendation System')
@@ -49,7 +64,7 @@ padded_sequence = pad_sequences(sequence, maxlen=50)
49
  emotion = emotion_model.predict(padded_sequence).flatten()
50
 
51
  # Combine emotion and audio features for recommendation
52
- combined_features_hybrid = np.concatenate([emotion, query_data[audio_features.columns].values])
53
 
54
  # Generate recommendations using the hybrid model
55
  hybrid_recs = hybrid_recommendation(song_index=0)
 
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')
 
64
  emotion = emotion_model.predict(padded_sequence).flatten()
65
 
66
  # Combine emotion and audio features for recommendation
67
+ combined_features_hybrid = np.concatenate([emotion, query_data[audio_feature_columns].values])
68
 
69
  # Generate recommendations using the hybrid model
70
  hybrid_recs = hybrid_recommendation(song_index=0)