brendabor commited on
Commit
946af09
1 Parent(s): ff3aa08

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -58
app.py CHANGED
@@ -1,77 +1,78 @@
1
  import streamlit as st
 
2
  from tensorflow.keras.models import load_model
3
  from tensorflow.keras.preprocessing.sequence import pad_sequences
4
  import joblib
5
  import pandas as pd
6
- import numpy as np
7
  from sklearn.preprocessing import StandardScaler
 
8
 
9
- # Load the dataset
10
- df = pd.read_csv('df1.csv')
11
- df = df.drop(['Unnamed: 0', 'lyrics_filename', 'analysis_url', 'track_href', "type", "id", "uri"], axis=1)
12
-
13
- # Load the tokenizer (ensure it's the one used during training)
14
- tokenizer = joblib.load('tokenizer.pkl')
15
-
16
- # Load the emotion prediction model
17
  emotion_model = load_model('lstm_model.h5')
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 = 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 = 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
- # Call the function from the module
56
- hybrid_recs = hybrid_recommendation(song_index=0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
  # Set up the title of the app
59
- st.title('Emotion and Audio Feature-based Song Recommendation System')
60
-
61
- # Get data from index 0
62
- query_data = df.iloc[0]
63
-
64
- # Process the lyrics
65
- sequence = tokenizer.texts_to_sequences([query_data['lyrics']])
66
- padded_sequence = pad_sequences(sequence, maxlen=50)
67
- emotion = emotion_model.predict(padded_sequence).flatten()
68
 
69
- # Combine emotion and audio features for recommendation
70
- combined_features_hybrid = np.concatenate([emotion, query_data[audio_features.columns].values])
71
 
72
- # Generate recommendations using the hybrid model
73
- hybrid_recs = hybrid_recommendation(song_index=0)
74
 
75
- st.write("Emotion Detected:", emotion[0])
76
- st.header('Recommended Songs (Hybrid)')
77
- st.write(hybrid_recs)
 
 
1
  import streamlit as st
2
+ import numpy as np
3
  from tensorflow.keras.models import load_model
4
  from tensorflow.keras.preprocessing.sequence import pad_sequences
5
  import joblib
6
  import pandas as pd
7
+ from sklearn.neighbors import NearestNeighbors
8
  from sklearn.preprocessing import StandardScaler
9
+ from sklearn.metrics.pairwise import cosine_similarity
10
 
11
+ # Load the LSTM model for emotion prediction
 
 
 
 
 
 
 
12
  emotion_model = load_model('lstm_model.h5')
13
 
14
+ # Load the KNN model
15
+ knn_model = joblib.load('knn_model.joblib')
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
+ # Load the tokenizer
18
+ tokenizer = joblib.load('tokenizer.pkl')
19
 
20
+ # Load the dataset
21
+ df = pd.read_csv('df1.csv')
22
 
23
+ # Load the scaler for KNN
24
  scaler_knn = StandardScaler()
 
 
 
 
 
 
25
 
26
+ # Function for hybrid recommendation
27
+ def hybrid_recommendation(song_index):
28
+ # Get data for the query song
29
+ query_data = df.iloc[song_index]
30
+
31
+ # Process the lyrics for emotion prediction using LSTM
32
+ sequence = tokenizer.texts_to_sequences([query_data['lyrics']])
33
+ padded_sequence = pad_sequences(sequence, maxlen=50)
34
+ predicted_emotion = emotion_model.predict(padded_sequence).flatten()
35
+
36
+ # Preprocess for KNN
37
+ audio_features_knn = query_data[['danceability', 'energy', 'key', 'loudness', 'mode', 'speechiness',
38
+ 'acousticness', 'instrumentalness', 'liveness', 'valence', 'tempo',
39
+ 'duration_ms', 'time_signature']].values.reshape(1, -1)
40
+ mood_cats = query_data[['mood_cats']]
41
+ mood_cats_df = pd.DataFrame(mood_cats)
42
+ audio_features_scaled_knn = scaler_knn.fit_transform(audio_features_knn)
43
+ combined_features = pd.concat([mood_cats_df, pd.DataFrame(audio_features_scaled_knn, columns=audio_features_knn.columns)], axis=1)
44
+
45
+ # Predict using the KNN model
46
+ knn_recommendations = knn_model.kneighbors(combined_features, n_neighbors=5, return_distance=False)[0]
47
+
48
+ # Mapping emotion predictions to encoded categories
49
+ emotion_mapping = {0: 'happy', 1: 'sad', 2: 'calm', 3: 'anger'}
50
+ encoded_emotion = np.argmax(predicted_emotion)
51
+ emotion_category = emotion_mapping[encoded_emotion]
52
+
53
+ # Compute cosine similarity for content-based recommendation
54
+ features_for_similarity = df[['danceability', 'energy', 'key', 'loudness', 'mode', 'speechiness',
55
+ 'acousticness', 'instrumentalness', 'liveness', 'valence', 'tempo',
56
+ 'duration_ms', 'time_signature']].values
57
+
58
+ cosine_similarities = cosine_similarity([emotion_category], features_for_similarity).flatten()
59
+
60
+ # Combine recommendations from both models
61
+ combined_indices = np.argsort(-np.concatenate([knn_recommendations, cosine_similarities]))
62
+ hybrid_recs_sorted = combined_indices[:5] # Select top 5 recommendations
63
+
64
+ return hybrid_recs_sorted
65
 
66
  # Set up the title of the app
67
+ st.title('Hybrid Recommender App')
 
 
 
 
 
 
 
 
68
 
69
+ # Get song index from user input
70
+ song_index_to_recommend = st.number_input('Enter song index:', min_value=0, max_value=len(df)-1, value=0)
71
 
72
+ # Get hybrid recommendations
73
+ hybrid_recs = hybrid_recommendation(song_index_to_recommend)
74
 
75
+ # Display the recommendations
76
+ st.write("Hybrid Recommendations:")
77
+ for index in hybrid_recs:
78
+ st.write(f"Song Index: {index}, Title: {df.iloc[index]['title']}, Artist: {df.iloc[index]['artist']}")