Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
from tensorflow.keras.models import load_model
|
4 |
+
from tensorflow.keras.preprocessing.text import Tokenizer
|
5 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
6 |
+
import pickle
|
7 |
+
import joblib
|
8 |
+
import pandas as pd
|
9 |
+
from sklearn.neighbors import NearestNeighbors
|
10 |
+
from sklearn.preprocessing import StandardScaler
|
11 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
12 |
+
|
13 |
+
# Load the LSTM model
|
14 |
+
lstm_model = load_model('lstm_model.h5')
|
15 |
+
|
16 |
+
# Load the Tokenizer used during training
|
17 |
+
with open('tokenizer.pkl', 'rb') as tokenizer_file:
|
18 |
+
tokenizer = pickle.load(tokenizer_file)
|
19 |
+
|
20 |
+
class_mapping = {"Happy": 0, "Sad": 1, "Calm": 2, "Anger": 3}
|
21 |
+
numerical_to_label = {v: k for k, v in class_mapping.items()}
|
22 |
+
|
23 |
+
# Load the KNN model
|
24 |
+
knn_model = joblib.load('knn_model.joblib')
|
25 |
+
|
26 |
+
# Load the dataset
|
27 |
+
df = pd.read_csv('df1.csv')
|
28 |
+
df = df.dropna()
|
29 |
+
|
30 |
+
# Preprocess for KNN
|
31 |
+
audio_feature_columns = ['danceability', 'energy', 'key', 'loudness', 'mode', 'speechiness',
|
32 |
+
'acousticness', 'instrumentalness', 'liveness', 'valence', 'tempo',
|
33 |
+
'duration_ms', 'time_signature']
|
34 |
+
|
35 |
+
audio_features = df[audio_feature_columns]
|
36 |
+
mood_cats = df[['mood_cats']]
|
37 |
+
audio_features_scaled = StandardScaler().fit_transform(audio_features)
|
38 |
+
audio_features_df = pd.DataFrame(audio_features_scaled, columns=audio_feature_columns)
|
39 |
+
combined_features = pd.concat([mood_cats, audio_features_df], axis=1)
|
40 |
+
|
41 |
+
# Calculate similarity matrix for content-based
|
42 |
+
audio_features_scaled_content = StandardScaler().fit_transform(audio_features)
|
43 |
+
combined_features_content = pd.concat([mood_cats, pd.DataFrame(audio_features_scaled_content)], axis=1)
|
44 |
+
similarity_matrix = cosine_similarity(combined_features_content)
|
45 |
+
|
46 |
+
def recommend_cont(song_index, num_recommendations=5):
|
47 |
+
song_similarity = similarity_matrix[song_index]
|
48 |
+
similar_songs = sorted(list(enumerate(song_similarity)), key=lambda x: x[1], reverse=True)[1:num_recommendations+1]
|
49 |
+
recommended_song_indices = [idx for idx, similarity in similar_songs]
|
50 |
+
recommended_songs = df.iloc[recommended_song_indices].copy()
|
51 |
+
recommended_songs['score'] = [similarity for idx, similarity in similar_songs]
|
52 |
+
return recommended_songs
|
53 |
+
|
54 |
+
def recommend_knn(query_index, n_recommendations=5):
|
55 |
+
distances, indices = knn_model.kneighbors(combined_features.iloc[query_index].values.reshape(1, -1), n_neighbors=n_recommendations)
|
56 |
+
recommended_songs = df.iloc[indices.flatten()].copy()
|
57 |
+
recommended_songs['score'] = 1 / (1 + distances.flatten())
|
58 |
+
return recommended_songs
|
59 |
+
|
60 |
+
def hybrid_recommendation(song_index, top_n=10):
|
61 |
+
content_based_recs = recommend_cont(song_index, top_n)
|
62 |
+
knn_based_recs = recommend_knn(song_index, top_n)
|
63 |
+
combined_recs = pd.concat([content_based_recs, knn_based_recs])
|
64 |
+
hybrid_recs = combined_recs.groupby(combined_recs.index).mean().sort_values(by='score', ascending=False).head(top_n)
|
65 |
+
return hybrid_recs
|
66 |
+
|
67 |
+
# Streamlit app
|
68 |
+
st.title('Music Recommendation and Emotion Detection')
|
69 |
+
|
70 |
+
# Emotion Detection
|
71 |
+
st.subheader('Emotion Detection from Song Lyrics')
|
72 |
+
user_input = st.text_input('Enter a Text:')
|
73 |
+
prediction = None
|
74 |
+
|
75 |
+
if st.button('Predict Emotion'):
|
76 |
+
sequence = tokenizer.texts_to_sequences([user_input])
|
77 |
+
padded_sequence = pad_sequences(sequence, maxlen=50)
|
78 |
+
prediction = lstm_model.predict(padded_sequence)
|
79 |
+
|
80 |
+
if prediction is not None:
|
81 |
+
for i in range(len(prediction[0])):
|
82 |
+
label = numerical_to_label[i]
|
83 |
+
probability = prediction[0][i]
|
84 |
+
threshold = 0.5
|
85 |
+
if probability > threshold:
|
86 |
+
st.write(f'Predicted Emotion: {label}')
|
87 |
+
|
88 |
+
# Music Recommendation
|
89 |
+
st.subheader('Music Recommendation')
|
90 |
+
song_index_to_recommend = st.number_input('Enter song index:', min_value=0, max_value=len(df)-1, value=0)
|
91 |
+
hybrid_recs = hybrid_recommendation(song_index_to_recommend)
|
92 |
+
|
93 |
+
st.write("Hybrid Recommendations:")
|
94 |
+
if not hybrid_recs.empty:
|
95 |
+
for index in hybrid_recs.index:
|
96 |
+
st.write(f"Song Index: {index}, Title: {df.iloc[index]['track_name']}, Artist: {df.iloc[index]['track_artist']}, Score: {hybrid_recs.loc[index, 'score']}")
|
97 |
+
else:
|
98 |
+
st.write("No recommendations found.")
|