Spaces:
Sleeping
Sleeping
Add User select option
Browse files
app.py
CHANGED
@@ -3,53 +3,64 @@ import pandas as pd
|
|
3 |
from scipy.sparse import csr_matrix, coo_matrix
|
4 |
from sklearn.metrics.pairwise import cosine_similarity
|
5 |
|
6 |
-
|
|
|
7 |
@st.cache_data
|
8 |
def load_songData(path_to_file):
|
9 |
-
df =pd.read_csv(path_to_file)
|
10 |
return df
|
11 |
-
|
12 |
-
#calling load_songData function
|
13 |
-
song_df = load_songData("song_dataset.csv")
|
14 |
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
# create a series with song IDs as index and titles as values
|
17 |
song_titles_series = song_df.drop_duplicates(subset=["song"]).set_index("song")["title"]
|
18 |
|
19 |
# sparse item-item similarity: transpose sparse matrix because we want item-item similarity (songs as rows)
|
20 |
-
interaction_matrix = song_df.pivot_table(
|
|
|
|
|
21 |
sparse_matrix = csr_matrix(interaction_matrix)
|
22 |
|
23 |
item_similarity_sparse = cosine_similarity(sparse_matrix.T, dense_output=False)
|
24 |
coo = coo_matrix(item_similarity_sparse)
|
25 |
|
26 |
-
item_similarity_df = pd.DataFrame(
|
|
|
27 |
"item_1": interaction_matrix.columns[coo.row],
|
28 |
"item_2": interaction_matrix.columns[coo.col],
|
29 |
"similarity": coo.data,
|
30 |
-
}
|
|
|
|
|
|
|
31 |
|
32 |
-
#for song-based recommendation engine call
|
33 |
|
34 |
def recommend_similar_items_sparse(selected_songs, top_n):
|
35 |
scores = {}
|
36 |
for song in selected_songs:
|
37 |
# getting all rows where item_1 is the selected song
|
38 |
similar_items = item_similarity_df[item_similarity_df["item_1"] == song]
|
39 |
-
|
40 |
for _, row in similar_items.iterrows():
|
41 |
similar_song = row["item_2"]
|
42 |
similarity = row["similarity"]
|
43 |
-
#filtering out songs already listened to by user
|
44 |
if similar_song not in selected_songs:
|
45 |
scores[similar_song] = scores.get(similar_song, 0) + similarity
|
46 |
-
|
47 |
-
recommended_songs = sorted(scores.items(), key=lambda x: x[1], reverse=True)[
|
|
|
|
|
48 |
return [song for song, score in recommended_songs]
|
49 |
-
|
50 |
|
51 |
# Streamlit Interface
|
52 |
-
st.
|
53 |
|
54 |
# Geting User inputs
|
55 |
st.write("**Please select a user.**")
|
@@ -64,6 +75,7 @@ selected_songs = st.multiselect(
|
|
64 |
format_func=lambda x: song_titles_series[x],
|
65 |
)
|
66 |
|
|
|
67 |
# Recommendation Magic
|
68 |
|
69 |
if st.button("Get Recommendations"):
|
|
|
3 |
from scipy.sparse import csr_matrix, coo_matrix
|
4 |
from sklearn.metrics.pairwise import cosine_similarity
|
5 |
|
6 |
+
|
7 |
+
# loading the song_dataset [in cache form to minimize resource usage]
|
8 |
@st.cache_data
|
9 |
def load_songData(path_to_file):
|
10 |
+
df = pd.read_csv(path_to_file)
|
11 |
return df
|
|
|
|
|
|
|
12 |
|
13 |
|
14 |
+
# calling load_songData function
|
15 |
+
song_df = load_songData("song_dataset.csv")
|
16 |
+
|
17 |
+
# load all user IDs
|
18 |
+
all_users = song_df["user"].unique()
|
19 |
+
|
20 |
# create a series with song IDs as index and titles as values
|
21 |
song_titles_series = song_df.drop_duplicates(subset=["song"]).set_index("song")["title"]
|
22 |
|
23 |
# sparse item-item similarity: transpose sparse matrix because we want item-item similarity (songs as rows)
|
24 |
+
interaction_matrix = song_df.pivot_table(
|
25 |
+
index="user", columns="song", values="play_count", fill_value=0
|
26 |
+
)
|
27 |
sparse_matrix = csr_matrix(interaction_matrix)
|
28 |
|
29 |
item_similarity_sparse = cosine_similarity(sparse_matrix.T, dense_output=False)
|
30 |
coo = coo_matrix(item_similarity_sparse)
|
31 |
|
32 |
+
item_similarity_df = pd.DataFrame(
|
33 |
+
{
|
34 |
"item_1": interaction_matrix.columns[coo.row],
|
35 |
"item_2": interaction_matrix.columns[coo.col],
|
36 |
"similarity": coo.data,
|
37 |
+
}
|
38 |
+
)
|
39 |
+
|
40 |
+
# for song-based recommendation engine call
|
41 |
|
|
|
42 |
|
43 |
def recommend_similar_items_sparse(selected_songs, top_n):
|
44 |
scores = {}
|
45 |
for song in selected_songs:
|
46 |
# getting all rows where item_1 is the selected song
|
47 |
similar_items = item_similarity_df[item_similarity_df["item_1"] == song]
|
48 |
+
|
49 |
for _, row in similar_items.iterrows():
|
50 |
similar_song = row["item_2"]
|
51 |
similarity = row["similarity"]
|
52 |
+
# filtering out songs already listened to by user
|
53 |
if similar_song not in selected_songs:
|
54 |
scores[similar_song] = scores.get(similar_song, 0) + similarity
|
55 |
+
|
56 |
+
recommended_songs = sorted(scores.items(), key=lambda x: x[1], reverse=True)[
|
57 |
+
:top_n
|
58 |
+
]
|
59 |
return [song for song, score in recommended_songs]
|
60 |
+
|
61 |
|
62 |
# Streamlit Interface
|
63 |
+
st.subheader("Song Recommendation Engine[Proj_charlie]")
|
64 |
|
65 |
# Geting User inputs
|
66 |
st.write("**Please select a user.**")
|
|
|
75 |
format_func=lambda x: song_titles_series[x],
|
76 |
)
|
77 |
|
78 |
+
|
79 |
# Recommendation Magic
|
80 |
|
81 |
if st.button("Get Recommendations"):
|