sok99 commited on
Commit
c0949a0
·
verified ·
1 Parent(s): 0906c05

streamlit files

Browse files
Files changed (3) hide show
  1. app.py +80 -0
  2. requirements.txt +4 -0
  3. song_dataset.csv +0 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.")
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ pandas==2.2.3
2
+ scikit_learn==1.5.2
3
+ scipy==1.14.1
4
+ streamlit==1.40.1
song_dataset.csv ADDED
The diff for this file is too large to render. See raw diff