brendabor commited on
Commit
1f9b2ed
1 Parent(s): e0b20cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -9
app.py CHANGED
@@ -4,6 +4,7 @@ from tensorflow.keras.preprocessing.sequence import pad_sequences
4
  import joblib
5
  import pandas as pd
6
  import numpy as np
 
7
 
8
  # Check if scikit-learn is installed
9
  try:
@@ -19,15 +20,14 @@ emotion_model = load_model('lstm_model.h5')
19
  try:
20
  recommender_model = joblib.load('knn_model.pkl')
21
  except Exception as e:
22
- st.error(f"Error loading model: {e}")
23
 
24
  # Load the tokenizer (ensure it's the one used during training)
25
  tokenizer = joblib.load('tokenizer.pkl') # Correct the path
26
 
27
  # Load the dataset and preprocess
28
  df = pd.read_csv('df1.csv')
29
- #df = df.dropna()
30
- df = df.drop(['Unnamed: 0', 'lyrics_filename', 'analysis_url', 'track_href', "type", "id", "uri"], axis=1)
31
 
32
  # Set up the title of the app
33
  st.title('Emotion and Audio Feature-based Song Recommendation System')
@@ -39,26 +39,34 @@ lyrics = st.text_area("Input the lyrics of the song here:")
39
  # Input fields for audio features
40
  st.header('Enter Audio Features')
41
  audio_features = []
42
- #df = df.drop([ 'track_artist', 'track_name','Unnamed: 0', 'lyrics_filename', 'analysis_url', 'track_href', "type", "id", "uri"] axis=1)
43
- for feature_name in df.columns:
 
 
 
 
44
  feature = st.number_input(f"Enter value for {feature_name}:", step=0.01)
45
  audio_features.append(feature)
46
 
47
  # Predict and Recommend button
48
  if st.button('Predict Emotion and Recommend Songs'):
49
  if lyrics and all(audio_features):
 
50
  sequence = tokenizer.texts_to_sequences([lyrics])
51
  padded_sequence = pad_sequences(sequence, maxlen=128)
52
  emotion = emotion_model.predict(padded_sequence).flatten()
53
 
 
54
  combined_features = np.concatenate([emotion, audio_features])
55
 
56
- distances, indices = recommender_model.kneighbors([combined_features], n_neighbors=5)
57
- recommended_songs = df.iloc[indices.flatten()]
 
58
 
59
  st.write("Emotion Detected:", emotion[0])
60
- st.header('Recommended Songs')
61
- for _, song in recommended_songs.iterrows():
62
  st.write(song)
 
63
  else:
64
  st.error("Please fill in all the fields.")
 
4
  import joblib
5
  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:
 
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"], axis=1)
 
31
 
32
  # Set up the title of the app
33
  st.title('Emotion and Audio Feature-based Song Recommendation System')
 
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=128)
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.")