File size: 1,954 Bytes
cf87f54
 
 
 
 
 
15b99ac
 
 
cf87f54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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']}")