Spaces:
Runtime error
Runtime error
import streamlit as st | |
import pandas as pd | |
from sklearn.preprocessing import StandardScaler | |
from sklearn.metrics.pairwise import cosine_similarity | |
# Load your dataset | |
df1 = pd.read_csv('df1.csv') | |
df1 = df1.dropna()#drop null values | |
# Copy the content-based recommendation code | |
audio_features = df1[['danceability', 'energy', 'key', 'loudness', 'mode', 'speechiness', | |
'acousticness', 'instrumentalness', 'liveness', 'valence', 'tempo', | |
'duration_ms', 'time_signature']] | |
mood_cats = df1[['mood_cats']] | |
# Normalize audio features | |
scaler = StandardScaler() | |
audio_features_scaled = scaler.fit_transform(audio_features) | |
# Combine mood and audio features | |
combined_features = pd.concat([mood_cats, pd.DataFrame(audio_features_scaled)], axis=1) | |
# Calculate similarity matrix | |
similarity_matrix = cosine_similarity(combined_features) | |
def recommend_cont(song_index, num_recommendations=5): | |
song_similarity = similarity_matrix[song_index] | |
# Get indices and similarity scores of top similar songs | |
similar_songs = sorted(list(enumerate(song_similarity)), key=lambda x: x[1], reverse=True)[1:num_recommendations+1] | |
recommended_song_indices = [idx for idx, similarity in similar_songs] | |
recommended_songs = df1.iloc[recommended_song_indices].copy() | |
recommended_songs['score'] = [similarity for idx, similarity in similar_songs] | |
return recommended_songs | |
# Streamlit app | |
st.title('Content-Based Recommender System') | |
# Select a song index | |
selected_index = st.slider('Select a song index', 0, len(df1)-1, 0) | |
# Get recommendations | |
recommended_songs = recommend_cont(selected_index) | |
# Display recommended songs using st.write | |
st.subheader('Recommended Songs:') | |
for index in recommended_songs.index: | |
st.write(f"Song Index: {index}, Title: {recommended_songs.loc[index, 'track_name']}, Artist: {recommended_songs.loc[index, 'track_artist']}, Score: {recommended_songs.loc[index, 'score']}") | |