Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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.
|
12 |
|
13 |
-
#
|
14 |
-
#
|
15 |
|
|
|
|
|
16 |
|
17 |
-
|
|
|
18 |
|
19 |
-
#
|
20 |
-
|
|
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
28 |
sequence = tokenizer.texts_to_sequences([lyrics])
|
29 |
padded_sequence = pad_sequences(sequence, maxlen=128)
|
30 |
-
emotion = emotion_model.predict(padded_sequence) #
|
31 |
-
|
32 |
-
#
|
33 |
-
|
34 |
-
|
35 |
-
recommendations
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.")
|