brendabor commited on
Commit
577a126
1 Parent(s): 55d4823

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -22
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import streamlit as st
2
  from tensorflow.keras.models import load_model
3
- from tensorflow.keras.preprocessing.text import Tokenizer
4
  from tensorflow.keras.preprocessing.sequence import pad_sequences
5
  import joblib
6
  import pandas as pd
@@ -8,31 +7,46 @@ import numpy as np
8
 
9
  # Load your models
10
  emotion_model = load_model('lstm_model.h5')
11
- recommender_model = joblib.load('knn_model.npy')
12
 
13
- #load the dataset
14
- # df = pd.read_csv('path_to_your_dataframe.csv')
15
 
 
 
16
 
17
- st.title("Emotion-based Song Recommender")
 
18
 
19
- # User input for lyrics
20
- lyrics = st.text_area("Enter lyrics here:")
 
21
 
22
- if st.button("Recommend Songs"):
23
-
24
- if lyrics:
25
- # Predict emotion
26
- # Here, ensure that the input shape and preprocessing of lyrics
27
- # match the requirements of your LSTM model
 
 
 
 
28
  sequence = tokenizer.texts_to_sequences([lyrics])
29
  padded_sequence = pad_sequences(sequence, maxlen=128)
30
- emotion = emotion_model.predict(padded_sequence) # Adjust this as per your model's requirement
31
-
32
- # Get song recommendations
33
- # The recommend method should be defined as part of your KNN model
34
- # or as a separate function that uses the KNN model
35
- recommendations = recommender_model.recommend(emotion, ...)
36
-
37
- st.write("Emotion Detected:", emotion)
38
- st.write("Recommended Songs:", recommendations)
 
 
 
 
 
 
 
 
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
 
7
 
8
  # Load your models
9
  emotion_model = load_model('lstm_model.h5')
10
+ recommender_model = joblib.load('knn_model.pkl') # Assuming it's a .pkl file
11
 
12
+ # Load the tokenizer (if used during training)
13
+ # tokenizer = joblib.load('tokenizer.pkl') # Update with actual file name
14
 
15
+ # Load the dataset
16
+ df = pd.read_csv('df1.csv') # Make sure this is the correct DataFrame
17
 
18
+ # Set up the title of the app
19
+ st.title('Emotion and Audio Feature-based Song Recommendation System')
20
 
21
+ # Input field for lyrics
22
+ st.header('Enter Song Lyrics')
23
+ lyrics = st.text_area("Input the lyrics of the song here:")
24
 
25
+ # Input fields for audio features
26
+ st.header('Enter Audio Features')
27
+ audio_features = []
28
+ for feature_name in df.columns: # Make sure this matches your DataFrame
29
+ feature = st.number_input(f"Enter value for {feature_name}:", step=0.01)
30
+ audio_features.append(feature)
31
+
32
+ # Predict and Recommend button
33
+ if st.button('Predict Emotion and Recommend Songs'):
34
+ if lyrics and all(audio_features):
35
  sequence = tokenizer.texts_to_sequences([lyrics])
36
  padded_sequence = pad_sequences(sequence, maxlen=128)
37
+ emotion = emotion_model.predict(padded_sequence).flatten() # Flatten if needed
38
+
39
+ # Combine emotion and audio features for recommendation
40
+ combined_features = np.concatenate([[emotion], audio_features])
41
+
42
+ # Generate recommendations using the KNN model
43
+ distances, indices = recommender_model.kneighbors([combined_features], n_neighbors=5)
44
+ recommended_songs = df.iloc[indices.flatten()]
45
+
46
+ # Display emotion and recommendations
47
+ st.write("Emotion Detected:", emotion[0]) # Adjust as per your model's output
48
+ st.header('Recommended Songs')
49
+ for _, song in recommended_songs.iterrows():
50
+ st.write(song) # Adjust based on your dataset
51
+ else:
52
+ st.error("Please fill in all the fields.")