import streamlit as st import numpy as np from tensorflow.keras.models import load_model from tensorflow.keras.preprocessing.sequence import pad_sequences import joblib import pandas as pd from sklearn.neighbors import NearestNeighbors from sklearn.preprocessing import StandardScaler # Load the LSTM model for emotion prediction emotion_model = load_model('lstm_model.h5') # Load the KNN model knn_model = joblib.load('knn_model.joblib') # Load the tokenizer tokenizer = joblib.load('tokenizer.pkl') # Load the dataset df = pd.read_csv('df1.csv') # Preprocess for KNN audio_feature_columns = ['danceability', 'energy', 'key', 'loudness', 'mode', 'speechiness', 'acousticness', 'instrumentalness', 'liveness', 'valence', 'tempo', 'duration_ms', 'time_signature'] audio_features = df[audio_feature_columns] mood_cats = df[['mood_cats']] mood_cats_df = pd.DataFrame(mood_cats) # Normalize audio features for KNN scaler_knn = StandardScaler() audio_features_scaled_knn = scaler_knn.fit_transform(audio_features) audio_features_df_knn = pd.DataFrame(audio_features_scaled_knn, columns=audio_feature_columns) combined_features_knn = pd.concat([mood_cats_df, audio_features_df_knn], axis=1) # Function for KNN-based recommendation def recommend_knn(query_index, n_recommendations=5): distances, indices = knn_model.kneighbors(combined_features_knn.iloc[query_index].values.reshape(1, -1), n_neighbors=n_recommendations) recommended_songs = df.iloc[indices.flatten()].copy() # Convert distances to scores recommended_songs['score'] = 1 / (1 + distances.flatten()) # Inverse of distance return recommended_songs # Set up the title of the app st.title('KNN Recommender App') # Get song index from user input song_index_to_recommend = st.number_input('Enter song index:', min_value=0, max_value=len(df)-1, value=0) # Get lyrics for emotion prediction lyrics = df.iloc[song_index_to_recommend]['lyrics'] # Process the lyrics sequence = tokenizer.texts_to_sequences([lyrics]) padded_sequence = pad_sequences(sequence, maxlen=50) # Adjust the maxlen to match the expected input size emotion = emotion_model.predict(padded_sequence).flatten() # Combine emotion and audio features for recommendation combined_features = np.concatenate([emotion, audio_features_scaled_knn[song_index_to_recommend]]) # Get KNN-based recommendations knn_recs = recommend_knn(song_index_to_recommend) # Display the predicted emotion and recommendations st.write(f"Predicted Emotion: {emotion}") if not knn_recs.empty: st.write("KNN Recommendations:") for index, row in knn_recs.iterrows(): st.write(f"Song Index: {index}, Title: {row['title']}, Artist: {row['artist']}, Score: {row['score']}") else: st.write("No KNN Recommendations found."