Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""
|
3 |
+
Created on Sat Nov 16 22:21:29 2024
|
4 |
+
@author: User
|
5 |
+
"""
|
6 |
+
|
7 |
+
import gradio as gr
|
8 |
+
import pandas as pd
|
9 |
+
import pickle
|
10 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
11 |
+
import heapq
|
12 |
+
|
13 |
+
# Load data and model
|
14 |
+
df = pd.read_csv('./DATA/spotify_millsongdata.csv')
|
15 |
+
|
16 |
+
# Load saved embeddings
|
17 |
+
with open("./DATA/lyrics_embeddings.pkl", "rb") as f:
|
18 |
+
lyrics_embeddings = pickle.load(f)
|
19 |
+
|
20 |
+
# List of artists and songs
|
21 |
+
artists = df['artist'].unique()
|
22 |
+
song_titles = df['song']
|
23 |
+
|
24 |
+
# Recommendation logic
|
25 |
+
def recommend_songs(song_index, top_n=5, batch_size=100):
|
26 |
+
top_sim_scores = []
|
27 |
+
num_batches = len(df) // batch_size + 1
|
28 |
+
|
29 |
+
for i in range(num_batches):
|
30 |
+
start_idx = i * batch_size
|
31 |
+
end_idx = min((i + 1) * batch_size, len(df))
|
32 |
+
|
33 |
+
# Compute cosine similarity for the current batch
|
34 |
+
cosine_sim_batch = cosine_similarity(
|
35 |
+
lyrics_embeddings[start_idx:end_idx],
|
36 |
+
[lyrics_embeddings[song_index]]
|
37 |
+
)
|
38 |
+
|
39 |
+
# Select the top N most similar songs
|
40 |
+
for j, sim_score in enumerate(cosine_sim_batch):
|
41 |
+
global_idx = start_idx + j
|
42 |
+
heapq.heappush(top_sim_scores, (sim_score[0], global_idx))
|
43 |
+
if len(top_sim_scores) > top_n + 1:
|
44 |
+
heapq.heappop(top_sim_scores)
|
45 |
+
|
46 |
+
# Exclude the selected song itself and return the most similar songs with their similarity scores
|
47 |
+
top_sim_scores = sorted(top_sim_scores, key=lambda x: x[0], reverse=True)[1:top_n+1]
|
48 |
+
recommended_songs = [(song_titles[i[1]], df['link'][i[1]], round(i[0], 2)) for i in top_sim_scores]
|
49 |
+
|
50 |
+
return recommended_songs
|
51 |
+
|
52 |
+
# Interface logic function
|
53 |
+
def get_songs_by_artist(artist_name):
|
54 |
+
filtered_songs = df[df['artist'] == artist_name]['song'].tolist()
|
55 |
+
return gr.update(choices=filtered_songs, value=filtered_songs[0] if filtered_songs else None)
|
56 |
+
|
57 |
+
|
58 |
+
def gradio_recommend(song_title):
|
59 |
+
try:
|
60 |
+
# Find the index of the selected song
|
61 |
+
song_index = song_titles[song_titles == song_title].index[0]
|
62 |
+
# Get recommended songs
|
63 |
+
recommendations = recommend_songs(song_index)
|
64 |
+
|
65 |
+
# Format the output, making song links clickable
|
66 |
+
result = "<div style='text-align: left;'>"
|
67 |
+
for song, link, sim_score in recommendations:
|
68 |
+
result += f"<b>Song Name:</b> {song}<br>"
|
69 |
+
result += f"<b>Search Link:</b> <a href='https://www.google.com/search?q={link}' target='_blank'>{link}</a><br>"
|
70 |
+
result += f"<b>Lyrics Similarity:</b> {sim_score:.2f}<br><br>"
|
71 |
+
result += "</div>"
|
72 |
+
return result
|
73 |
+
except IndexError:
|
74 |
+
return "Song not found."
|
75 |
+
|
76 |
+
# Create Gradio multi-page interface
|
77 |
+
with gr.Blocks(css="""
|
78 |
+
@media (max-width: 768px) {
|
79 |
+
.gr-container {
|
80 |
+
width: 100%;
|
81 |
+
padding: 10px;
|
82 |
+
box-sizing: border-box;
|
83 |
+
}
|
84 |
+
.gr-dropdown select {
|
85 |
+
width: 100%;
|
86 |
+
height: 40px; /* Limit height */
|
87 |
+
font-size: 16px;
|
88 |
+
padding: 5px;
|
89 |
+
box-sizing: border-box;
|
90 |
+
}
|
91 |
+
.gr-button {
|
92 |
+
width: 100%;
|
93 |
+
font-size: 16px;
|
94 |
+
margin-top: 10px;
|
95 |
+
}
|
96 |
+
.gr-html, .gr-row {
|
97 |
+
width: 100%;
|
98 |
+
font-size: 16px;
|
99 |
+
margin: 10px 0;
|
100 |
+
}
|
101 |
+
h1 {
|
102 |
+
font-size: 24px;
|
103 |
+
}
|
104 |
+
p {
|
105 |
+
font-size: 14px;
|
106 |
+
}
|
107 |
+
.gr-dropdown::after {
|
108 |
+
content: '';
|
109 |
+
width: 12px;
|
110 |
+
height: 12px;
|
111 |
+
border: solid black;
|
112 |
+
border-width: 0 2px 2px 0;
|
113 |
+
display: inline-block;
|
114 |
+
transform: rotate(45deg);
|
115 |
+
margin-left: 10px;
|
116 |
+
}
|
117 |
+
}
|
118 |
+
""") as demo:
|
119 |
+
|
120 |
+
gr.Markdown(
|
121 |
+
"""
|
122 |
+
<div style="text-align: center; padding: 20px;">
|
123 |
+
<h1 style="color: #1DB954;">Music Recommendation System</h1>
|
124 |
+
<p style="font-size: 18px;">Get the most relevant song recommendations based on lyrics similarity</p>
|
125 |
+
</div>
|
126 |
+
"""
|
127 |
+
)
|
128 |
+
|
129 |
+
# Page 1: Select artist
|
130 |
+
with gr.Row():
|
131 |
+
with gr.Column():
|
132 |
+
artist_dropdown = gr.Dropdown(choices=list(artists), label="Select Artist")
|
133 |
+
next_button = gr.Button("Next")
|
134 |
+
|
135 |
+
# Page 2: Select song and get recommendations
|
136 |
+
with gr.Row(visible=False) as song_selection_row:
|
137 |
+
song_dropdown = gr.Dropdown(label="Select Song")
|
138 |
+
recommend_button = gr.Button("Get Recommendations")
|
139 |
+
output = gr.HTML(label="Recommended Similar Songs")
|
140 |
+
|
141 |
+
# Event bindings
|
142 |
+
artist_dropdown.change(get_songs_by_artist, inputs=artist_dropdown, outputs=song_dropdown)
|
143 |
+
next_button.click(lambda: gr.update(visible=True), None, song_selection_row)
|
144 |
+
recommend_button.click(gradio_recommend, inputs=song_dropdown, outputs=output)
|
145 |
+
|
146 |
+
if __name__ == "__main__":
|
147 |
+
demo.launch()
|