brendabor commited on
Commit
3f56ca4
1 Parent(s): a5615a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -46
app.py CHANGED
@@ -6,67 +6,57 @@ import pandas as pd
6
  import numpy as np
7
  from sklearn.metrics.pairwise import cosine_similarity
8
 
9
- # Check if scikit-learn is installed
10
- try:
11
- import sklearn
12
- st.write("scikit-learn is installed.")
13
- except ImportError:
14
- st.error("scikit-learn is not installed.")
15
-
16
- # Load your emotion prediction model
17
  emotion_model = load_model('lstm_model.h5')
18
 
19
  # Load the KNN recommender model
20
- try:
21
- recommender_model = joblib.load('knn_model.pkl')
22
- except Exception as e:
23
- st.error(f"Error loading KNN model: {e}")
24
 
25
  # Load the tokenizer (ensure it's the one used during training)
26
- tokenizer = joblib.load('tokenizer.pkl') # Correct the path
27
 
28
  # Load the dataset and preprocess
29
- df = pd.read_csv('df1.csv')
30
  df = df.drop(['Unnamed: 0', 'lyrics_filename', 'analysis_url', 'track_href', "type", "id", "uri", 'mood'], axis=1)
31
 
32
- # Set up the title of the app
33
- st.title('Emotion and Audio Feature-based Song Recommendation System')
 
 
 
34
 
35
- # Input field for lyrics
36
- st.header('Enter Song Lyrics')
37
- lyrics = st.text_area("Input the lyrics of the song here:")
38
 
39
- # Input fields for audio features
40
- st.header('Enter Audio Features')
41
- audio_features = []
42
 
43
- # Display only relevant columns for audio features
44
- audio_feature_columns = ['danceability', 'energy', 'key', 'loudness', 'mode', 'speechiness', 'acousticness',
45
- 'instrumentalness', 'liveness', 'valence', 'tempo']
46
 
47
- for feature_name in audio_feature_columns:
48
- feature = st.number_input(f"Enter value for {feature_name}:", step=0.01)
49
- audio_features.append(feature)
 
 
50
 
51
- # Predict and Recommend button
52
- if st.button('Predict Emotion and Recommend Songs'):
53
- if lyrics and all(audio_features):
54
- # Process the lyrics
55
- sequence = tokenizer.texts_to_sequences([lyrics])
56
- padded_sequence = pad_sequences(sequence, maxlen=50) # Adjust the maxlen to match the expected input size
57
- emotion = emotion_model.predict(padded_sequence).flatten()
58
 
59
- # Combine emotion and audio features for recommendation
60
- combined_features = np.concatenate([emotion, audio_features])
 
 
61
 
62
- # Generate recommendations using the KNN model
63
- knn_distances, knn_indices = recommender_model.kneighbors([combined_features], n_neighbors=5)
64
- knn_recommended_songs = df.iloc[knn_indices.flatten()]
65
 
66
- st.write("Emotion Detected:", emotion[0])
67
- st.header('Recommended Songs (KNN)')
68
- for _, song in knn_recommended_songs.iterrows():
69
- st.write(song)
70
 
71
- else:
72
- st.error("Please fill in all the fields.")
 
 
6
  import numpy as np
7
  from sklearn.metrics.pairwise import cosine_similarity
8
 
9
+ # Load the emotion prediction model
 
 
 
 
 
 
 
10
  emotion_model = load_model('lstm_model.h5')
11
 
12
  # Load the KNN recommender model
13
+ # try:
14
+ # recommender_model = joblib.load('knn_model.pkl')
15
+ # except Exception as e:
16
+ # st.error(f"Error loading KNN model: {e}")
17
 
18
  # Load the tokenizer (ensure it's the one used during training)
19
+ tokenizer = joblib.load('tokenizer.pkl')
20
 
21
  # Load the dataset and preprocess
22
+ df = pd.read_csv('df1.csv')
23
  df = df.drop(['Unnamed: 0', 'lyrics_filename', 'analysis_url', 'track_href', "type", "id", "uri", 'mood'], axis=1)
24
 
25
+ # Load the similarity matrix
26
+ similarity_matrix = np.load('similarity_matrix.npy')
27
+
28
+ # Load the content-based recommendation function
29
+ recommend_cont = joblib.load('recommendation_function.joblib')
30
 
31
+ # Load the hybrid recommendation function
32
+ hybrid_recommendation = joblib.load('hybrid_recommendation_function.joblib')
 
33
 
34
+ # Load the content-based recommendation function
35
+ recommend_cont = joblib.load('recommendation_cont_function.joblib')
 
36
 
37
+ # Load the KNN model
38
+ knn = joblib.load('knn_model.joblib')
 
39
 
40
+ # Load the KNN recommendation function
41
+ recommend_knn = joblib.load('recommendation_knn_function.joblib')
42
+
43
+ # Set up the title of the app
44
+ st.title('Emotion and Audio Feature-based Song Recommendation System')
45
 
46
+ # Get data from index 0
47
+ query_data = df.iloc[0]
 
 
 
 
 
48
 
49
+ # Process the lyrics
50
+ sequence = tokenizer.texts_to_sequences([query_data['lyrics']])
51
+ padded_sequence = pad_sequences(sequence, maxlen=50) # Adjust the maxlen to match the expected input size
52
+ emotion = emotion_model.predict(padded_sequence).flatten()
53
 
54
+ # Combine emotion and audio features for recommendation
55
+ combined_features = np.concatenate([emotion, query_data[audio_feature_columns].values])
 
56
 
57
+ # Generate recommendations using the hybrid model
58
+ hybrid_recs = hybrid_recommendation(song_index=0)
 
 
59
 
60
+ st.write("Emotion Detected:", emotion[0])
61
+ st.header('Recommended Songs (Hybrid)')
62
+ st.write(hybrid_recs)