SpotifyProject / app.py
brendabor's picture
Update app.py
58ad677
raw
history blame
No virus
2.65 kB
import streamlit as st
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.sequence import pad_sequences
import joblib
import pandas as pd
import numpy as np
try:
import sklearn
st.write("scikit-learn is installed.")
except ImportError:
st.error("scikit-learn is not installed.")
# Load your emotion prediction model
emotion_model = load_model('lstm_model.h5')
# Load the KNN recommender model
#recommender_model = joblib.load('knn_model.pkl')
# Load the KNN recommender model
try:
recommender_model = joblib.load('knn_model.pkl')
except Exception as e:
st.error(f"Error loading model: {e}")
# Load the tokenizer (ensure it's the one used during training)
# tokenizer = joblib.load('tokenizer.pkl') # Update this to the correct path
# Load the dataset
df = pd.read_csv('df1.csv')
#remove null values
df = df.dropna()
# Drop unwanted columns
#dropping irrelevant features
df = df.drop(['Unnamed: 0', 'lyrics_filename', 'analysis_url', 'track_href',"type","id","uri"], axis = 1)
# Set up the title of the app
st.title('Emotion and Audio Feature-based Song Recommendation System')
# Input field for lyrics
st.header('Enter Song Lyrics')
lyrics = st.text_area("Input the lyrics of the song here:")
# Input fields for audio features
st.header('Enter Audio Features')
audio_features = []
for feature_name in df.columns: # Ensure this matches your DataFrame's structure
feature = st.number_input(f"Enter value for {feature_name}:", step=0.01)
audio_features.append(feature)
# Predict and Recommend button
if st.button('Predict Emotion and Recommend Songs'):
if lyrics and all(audio_features):
# Process the lyrics
sequence = tokenizer.texts_to_sequences([lyrics])
padded_sequence = pad_sequences(sequence, maxlen=128)
emotion = emotion_model.predict(padded_sequence).flatten()
# Combine emotion and audio features for recommendation
combined_features = np.concatenate([emotion, audio_features]) # Ensure the concatenation logic matches your model's expectation
# Generate recommendations using the KNN model
distances, indices = recommender_model.kneighbors([combined_features], n_neighbors=5)
recommended_songs = df.iloc[indices.flatten()]
# Display emotion and recommendations
st.write("Emotion Detected:", emotion[0]) # Adjust as per your model's output
st.header('Recommended Songs')
for _, song in recommended_songs.iterrows():
st.write(song) # Customize this to display relevant song info
else:
st.error("Please fill in all the fields.")