sok99 commited on
Commit
8ecd465
·
verified ·
1 Parent(s): c0949a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -80
app.py CHANGED
@@ -1,80 +1,85 @@
1
- import streamlit as st
2
- import pandas as pd
3
- from scipy.sparse import csr_matrix, coo_matrix
4
- from sklearn.metrics.pairwise import cosine_similarity
5
-
6
- #loading the song_dataset [in cache form to minimize resource usage]
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(index="user", columns="song", values="play_count", fill_value=0)
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)[:top_n]
48
- return [song for song, score in recommended_songs]
49
-
50
-
51
- # Streamlit Interface
52
- st.title("Song Recommendation Engine[Proj_charlie]")
53
- st.write("**Please select as many songs as you've listened to.**")
54
-
55
- # Geting User inputs
56
- all_songs = song_titles_series.index.tolist()
57
- selected_songs = st.multiselect(
58
- "Select Song(s):", options=all_songs, format_func=lambda x: song_titles_series[x],
59
- )
60
-
61
-
62
- # Recommendation Magic
63
-
64
- if st.button("Get Recommendations"):
65
- if not selected_songs:
66
- st.warning("Please select song(s) you have listened to!")
67
- else:
68
- recommendations = recommend_similar_items_sparse(selected_songs, top_n=10)
69
- if recommendations:
70
- # displaying selected songs:
71
- st.subheader("Great! You selected:")
72
- for idx, song in enumerate(selected_songs, start=1):
73
- st.write(f"{idx}. {song_titles_series.get(song, song)}")
74
-
75
- # displaying recommended songs
76
- st.subheader("Top Recommended songs:")
77
- for idx, song in enumerate(recommendations, start=1):
78
- st.write(f"{idx}. {song_titles_series.get(song, song)}")
79
- else:
80
- st.info("Oops! No recommendations available for selected songs.")
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ from scipy.sparse import csr_matrix, coo_matrix
4
+ from sklearn.metrics.pairwise import cosine_similarity
5
+
6
+ #loading the song_dataset [in cache form to minimize resource usage]
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(index="user", columns="song", values="play_count", fill_value=0)
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)[:top_n]
48
+ return [song for song, score in recommended_songs]
49
+
50
+
51
+ # Streamlit Interface
52
+ st.title("Song Recommendation Engine[Proj_charlie]")
53
+
54
+ # Geting User inputs
55
+ st.write("**Please select a user.**")
56
+ selected_user_id = st.selectbox("Select a User ID:", options=all_users)
57
+
58
+
59
+ st.write("**Please select as many songs as user has listened to.**")
60
+ all_songs = song_titles_series.index.tolist()
61
+ selected_songs = st.multiselect(
62
+ "Select Song(s):",
63
+ options=all_songs,
64
+ format_func=lambda x: song_titles_series[x],
65
+ )
66
+
67
+ # Recommendation Magic
68
+
69
+ if st.button("Get Recommendations"):
70
+ if not selected_songs:
71
+ st.warning("Please select song(s) you have listened to!")
72
+ else:
73
+ recommendations = recommend_similar_items_sparse(selected_songs, top_n=10)
74
+ if recommendations:
75
+ # displaying selected songs:
76
+ st.subheader(f"Great! {selected_user_id} has listened to:")
77
+ for idx, song in enumerate(selected_songs, start=1):
78
+ st.write(f"{idx}. {song_titles_series.get(song, song)}")
79
+
80
+ # displaying recommended songs
81
+ st.subheader(f"Top Recommended songs for {selected_user_id}:")
82
+ for idx, song in enumerate(recommendations, start=1):
83
+ st.write(f"{idx}. {song_titles_series.get(song, song)}")
84
+ else:
85
+ st.info("Oops! No recommendations available for selected songs.")