brendabor commited on
Commit
021fc49
1 Parent(s): cf6a913

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -18
app.py CHANGED
@@ -5,6 +5,7 @@ import joblib
5
  import pandas as pd
6
  import numpy as np
7
 
 
8
  try:
9
  import sklearn
10
  st.write("scikit-learn is installed.")
@@ -14,9 +15,6 @@ except ImportError:
14
  # Load your emotion prediction model
15
  emotion_model = load_model('lstm_model.h5')
16
 
17
- # Load the KNN recommender model
18
- #recommender_model = joblib.load('knn_model.pkl')
19
-
20
  # Load the KNN recommender model
21
  try:
22
  recommender_model = joblib.load('knn_model.pkl')
@@ -24,16 +22,13 @@ except Exception as e:
24
  st.error(f"Error loading model: {e}")
25
 
26
  # Load the tokenizer (ensure it's the one used during training)
27
- tokenizer = joblib.load('tokenizer.pkl') # Update this to the correct path
28
 
29
- # Load the dataset
30
  df = pd.read_csv('df1.csv')
31
- #remove null values
32
  df = df.dropna()
33
-
34
- # Drop unwanted columns
35
- #dropping irrelevant features
36
- df = df.drop(['Unnamed: 0', 'lyrics_filename', 'analysis_url', 'track_href',"type","id","uri"], axis = 1)
37
 
38
  # Set up the title of the app
39
  st.title('Emotion and Audio Feature-based Song Recommendation System')
@@ -45,29 +40,25 @@ lyrics = st.text_area("Input the lyrics of the song here:")
45
  # Input fields for audio features
46
  st.header('Enter Audio Features')
47
  audio_features = []
48
- for feature_name in df.columns: # Ensure this matches your DataFrame's structure
49
  feature = st.number_input(f"Enter value for {feature_name}:", step=0.01)
50
  audio_features.append(feature)
51
 
52
  # Predict and Recommend button
53
  if st.button('Predict Emotion and Recommend Songs'):
54
  if lyrics and all(audio_features):
55
- # Process the lyrics
56
  sequence = tokenizer.texts_to_sequences([lyrics])
57
  padded_sequence = pad_sequences(sequence, maxlen=128)
58
  emotion = emotion_model.predict(padded_sequence).flatten()
59
 
60
- # Combine emotion and audio features for recommendation
61
- combined_features = np.concatenate([emotion, audio_features]) # Ensure the concatenation logic matches your model's expectation
62
 
63
- # Generate recommendations using the KNN model
64
  distances, indices = recommender_model.kneighbors([combined_features], n_neighbors=5)
65
  recommended_songs = df.iloc[indices.flatten()]
66
 
67
- # Display emotion and recommendations
68
- st.write("Emotion Detected:", emotion[0]) # Adjust as per your model's output
69
  st.header('Recommended Songs')
70
  for _, song in recommended_songs.iterrows():
71
- st.write(song) # Customize this to display relevant song info
72
  else:
73
  st.error("Please fill in all the fields.")
 
5
  import pandas as pd
6
  import numpy as np
7
 
8
+ # Check if scikit-learn is installed
9
  try:
10
  import sklearn
11
  st.write("scikit-learn is installed.")
 
15
  # Load your emotion prediction model
16
  emotion_model = load_model('lstm_model.h5')
17
 
 
 
 
18
  # Load the KNN recommender model
19
  try:
20
  recommender_model = joblib.load('knn_model.pkl')
 
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
+ # Optionally, remove null values and drop unwanted columns
30
  df = df.dropna()
31
+ df = df.drop(['Unnamed: 0', 'lyrics_filename', 'analysis_url', 'track_href', "type", "id", "uri"], axis=1)
 
 
 
32
 
33
  # Set up the title of the app
34
  st.title('Emotion and Audio Feature-based Song Recommendation System')
 
40
  # Input fields for audio features
41
  st.header('Enter Audio Features')
42
  audio_features = []
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.")