Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
#
|
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')
|
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 |
-
#
|
33 |
-
|
|
|
|
|
|
|
34 |
|
35 |
-
#
|
36 |
-
|
37 |
-
lyrics = st.text_area("Input the lyrics of the song here:")
|
38 |
|
39 |
-
#
|
40 |
-
|
41 |
-
audio_features = []
|
42 |
|
43 |
-
#
|
44 |
-
|
45 |
-
'instrumentalness', 'liveness', 'valence', 'tempo']
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
50 |
|
51 |
-
#
|
52 |
-
|
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 |
-
|
60 |
-
|
|
|
|
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
knn_recommended_songs = df.iloc[knn_indices.flatten()]
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
for _, song in knn_recommended_songs.iterrows():
|
69 |
-
st.write(song)
|
70 |
|
71 |
-
|
72 |
-
|
|
|
|
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)
|