Spaces:
Build error
Build error
sarala
commited on
Commit
·
34580ab
1
Parent(s):
408f61c
firstpush
Browse files- .DS_Store +0 -0
- Main_Page.py → Main_Page copy.py +0 -0
- app.py +384 -0
.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
Main_Page.py → Main_Page copy.py
RENAMED
File without changes
|
app.py
ADDED
@@ -0,0 +1,384 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import math
|
3 |
+
import numpy as np
|
4 |
+
import psycopg2
|
5 |
+
from sklearn.neighbors import KNeighborsClassifier
|
6 |
+
from sklearn.model_selection import train_test_split
|
7 |
+
import pandas as pd
|
8 |
+
from scipy.spatial import distance
|
9 |
+
import random
|
10 |
+
|
11 |
+
# Initialize connection.
|
12 |
+
# Uses st.experimental_singleton to only run once.
|
13 |
+
|
14 |
+
|
15 |
+
@st.experimental_singleton
|
16 |
+
def init_connection():
|
17 |
+
return psycopg2.connect(**st.secrets["postgres"])
|
18 |
+
|
19 |
+
|
20 |
+
list_selected_songs = []
|
21 |
+
list_allsongs = []
|
22 |
+
emotion = []
|
23 |
+
q1_song = []
|
24 |
+
q2_song = []
|
25 |
+
q3_song = []
|
26 |
+
q4_song = []
|
27 |
+
|
28 |
+
conn = init_connection()
|
29 |
+
# number of recommendation of the song to be made
|
30 |
+
num_recommendation = 10
|
31 |
+
|
32 |
+
# Perform query.
|
33 |
+
# Uses st.experimental_memo to only rerun when the query changes or after 10 min.
|
34 |
+
|
35 |
+
|
36 |
+
@st.experimental_memo(ttl=600)
|
37 |
+
def run_query(query):
|
38 |
+
with conn.cursor() as cur:
|
39 |
+
cur.execute(query)
|
40 |
+
return cur.fetchall()
|
41 |
+
|
42 |
+
|
43 |
+
def count_emotion(list_emotion):
|
44 |
+
s1 = 'q1'
|
45 |
+
s2 = 'q2'
|
46 |
+
s3 = 'q3'
|
47 |
+
s4 = 'q4'
|
48 |
+
global count_q1
|
49 |
+
global count_q2
|
50 |
+
global count_q3
|
51 |
+
global count_q4
|
52 |
+
count_q1 = list_emotion.count(s1)
|
53 |
+
count_q2 = list_emotion.count(s2)
|
54 |
+
count_q3 = list_emotion.count(s3)
|
55 |
+
count_q4 = list_emotion.count(s4)
|
56 |
+
|
57 |
+
|
58 |
+
def recommended_songs_q1_q2_q3_q4(num_recommendation_q1, num_recommendation_q2, num_recommendation_q3, num_recommendation_q4):
|
59 |
+
global q1
|
60 |
+
global q2
|
61 |
+
global q3
|
62 |
+
global g4
|
63 |
+
q1 = np.array(q1_song[0:int(num_recommendation_q1)])
|
64 |
+
q2 = np.array(q2_song[0:int(num_recommendation_q2)])
|
65 |
+
q3 = np.array(q3_song[0:int(num_recommendation_q3)])
|
66 |
+
q4 = np.array(q4_song[0:int(num_recommendation_q4)])
|
67 |
+
return q1, q2, q3, q4
|
68 |
+
|
69 |
+
|
70 |
+
def get_number_quadrant_recommendation():
|
71 |
+
total = count_q1+count_q2+count_q3+count_q4
|
72 |
+
percentage_q1 = count_q1/total
|
73 |
+
percentage_q2 = count_q2/total
|
74 |
+
percentage_q3 = count_q3/total
|
75 |
+
percentage_q4 = count_q4/total
|
76 |
+
global num_recommendation_q1
|
77 |
+
global num_recommendation_q2
|
78 |
+
global num_recommendation_q3
|
79 |
+
global num_recommendation_q4
|
80 |
+
num_recommendation_q1 = (percentage_q1*num_recommendation)
|
81 |
+
num_recommendation_q2 = (percentage_q2*num_recommendation)
|
82 |
+
num_recommendation_q3 = (percentage_q3*num_recommendation)
|
83 |
+
num_recommendation_q4 = (percentage_q4*num_recommendation)
|
84 |
+
|
85 |
+
|
86 |
+
def get_distance(e):
|
87 |
+
# index 6 consists of euclidian distance between two songs, seleted song and song in the database
|
88 |
+
return e[7]
|
89 |
+
|
90 |
+
|
91 |
+
def get_distance1(e):
|
92 |
+
# index 0 consists of euclidian distance between two songs in the case of the repeated song
|
93 |
+
return e[0]
|
94 |
+
|
95 |
+
|
96 |
+
|
97 |
+
rows = run_query("SELECT song_title, artist from nepali_songs order by song_title")
|
98 |
+
st.title('Emotion Based Music Recommendation System')
|
99 |
+
st.image('music.jpg')
|
100 |
+
|
101 |
+
with st.form("my_form"):
|
102 |
+
|
103 |
+
options = st.multiselect('Choose the song that you want to listen', [
|
104 |
+
':'.join(map(str, x)) for x in rows])
|
105 |
+
submitted = st.form_submit_button("Recommend song to me")
|
106 |
+
|
107 |
+
# splitting artist and song title of the song that user have selected
|
108 |
+
if submitted:
|
109 |
+
artist_song_list = []
|
110 |
+
for index, element in enumerate(options):
|
111 |
+
artist_list = element.split(':')
|
112 |
+
|
113 |
+
artist_song_list.append(artist_list)
|
114 |
+
|
115 |
+
# storing the information of the audio features of the songs that the user have selected by retrieving through the database
|
116 |
+
|
117 |
+
for x in artist_song_list:
|
118 |
+
with conn.cursor() as cur:
|
119 |
+
cur.execute(
|
120 |
+
"select * from nepali_songs where song_title=%s and artist=%s", [x[0], x[1]])
|
121 |
+
row = cur.fetchall()
|
122 |
+
list_selected_songs.append(row)
|
123 |
+
print("selected songs")
|
124 |
+
happy_songs=[]
|
125 |
+
tensed_songs=[]
|
126 |
+
sad_songs=[]
|
127 |
+
calm_songs=[]
|
128 |
+
|
129 |
+
|
130 |
+
|
131 |
+
# collecting the information about the emotion quadrant of the each song that the user have selected and storing in the list emotion
|
132 |
+
for i in list_selected_songs:
|
133 |
+
print(i[0][12],i[0][13])
|
134 |
+
emotion.append(i[0][14])
|
135 |
+
#categorizing the songs into 4 different emotions
|
136 |
+
if(i[0][14]=='q1'):
|
137 |
+
happy_songs.append(i[0][12])
|
138 |
+
elif(i[0][14]=='q2'):
|
139 |
+
tensed_songs.append(i[0][12])
|
140 |
+
elif(i[0][14]=='q3'):
|
141 |
+
sad_songs.append(i[0][12])
|
142 |
+
else:
|
143 |
+
calm_songs.append(i[0][12])
|
144 |
+
|
145 |
+
|
146 |
+
# calculating the total number of emotion quadrant of the song that the user have selected
|
147 |
+
count_emotion(emotion)
|
148 |
+
total = count_q1+count_q2+count_q3+count_q4
|
149 |
+
st.write("You have selected ", count_q1, " happy songs")
|
150 |
+
st.write(happy_songs)
|
151 |
+
st.write("You have selected ", count_q2, " tensed songs")
|
152 |
+
st.write(tensed_songs)
|
153 |
+
st.write("You have selected ", count_q3, " sad songs")
|
154 |
+
st.write(sad_songs)
|
155 |
+
st.write("You have selected ", count_q4, " calm songs")
|
156 |
+
st.write(calm_songs)
|
157 |
+
# retriving information of all songs in the database and storing in the list list_allsongs
|
158 |
+
|
159 |
+
distance_info_song_all = []
|
160 |
+
with conn.cursor() as cur:
|
161 |
+
cur.execute("select * from nepali_songs")
|
162 |
+
row = cur.fetchall()
|
163 |
+
# print(type(row[0]))
|
164 |
+
list_allsongs.append(row)
|
165 |
+
|
166 |
+
# calculating the euclidean distance of selected songs with all songs in the database
|
167 |
+
for i in list_selected_songs:
|
168 |
+
valence_selected_songs = float(i[0][11])
|
169 |
+
energy_selected_songs = float(i[0][2])
|
170 |
+
list1 = [valence_selected_songs, energy_selected_songs]
|
171 |
+
|
172 |
+
for lists in list_allsongs:
|
173 |
+
# print("printing the list of songs\n")
|
174 |
+
distance_info = []
|
175 |
+
for tuples in lists:
|
176 |
+
valence_allsongs = float(tuples[11])
|
177 |
+
energy_allsongs = float(tuples[2])
|
178 |
+
list2 = [valence_allsongs, energy_allsongs]
|
179 |
+
dist = distance.euclidean(list1, list2)
|
180 |
+
# i[0][0] is the spotifyid of the selected song and tuples[0] is the spotifyid
|
181 |
+
# of the song in the database
|
182 |
+
distance_info.append(
|
183 |
+
[i[0][12], i[0][14], i[0][0], tuples[12], tuples[14], tuples[13], tuples[0], dist])
|
184 |
+
distance_info_song_all.append(distance_info)
|
185 |
+
# st.write("distance info")
|
186 |
+
# st.write(distance_info_song_all)
|
187 |
+
nearest_neighbour = []
|
188 |
+
# distance_info_song_all consists of the euclidian distance of the selected song with all songs in the database
|
189 |
+
# recommend_list consist of the top 10 songs having minimum distance with all the selected song
|
190 |
+
for i in distance_info_song_all:
|
191 |
+
i.sort(key=get_distance, reverse=False)
|
192 |
+
#sorting the 10 nearest neighbour of the each selected song selected song
|
193 |
+
for k in range(0,10):
|
194 |
+
nearest_neighbour.append(i[k])
|
195 |
+
# st.write(nearest_neighbour)
|
196 |
+
|
197 |
+
# calculating the percentange of song associated with q1, q2, q3 and q4 that the user have selected
|
198 |
+
|
199 |
+
# st.write("q1 recommendation number", num_recommendation_q1)
|
200 |
+
# st.write("q2 recommendation number", num_recommendation_q2)
|
201 |
+
# st.write("q3 recommendation number", num_recommendation_q3)
|
202 |
+
# st.write("q4 recommendation numner", num_recommendation_q4)
|
203 |
+
# st.write(num_recommendation)
|
204 |
+
|
205 |
+
# selecting the song of the particular quadrant from the nearest neighbour and storing in the list
|
206 |
+
|
207 |
+
for t in nearest_neighbour:
|
208 |
+
|
209 |
+
if (t[4] == 'q2' and t[7]!=0.0):
|
210 |
+
q2_song.append(t)
|
211 |
+
|
212 |
+
elif (t[4] == 'q1' and t[7]!=0.0):
|
213 |
+
q1_song.append(t)
|
214 |
+
elif (t[4] == 'q3' and t[7]!=0.0):
|
215 |
+
q3_song.append(t)
|
216 |
+
elif (t[4] == 'q4' and t[7]!=0.0):
|
217 |
+
q4_song.append(t)
|
218 |
+
else:
|
219 |
+
pass
|
220 |
+
|
221 |
+
q1_song.sort(key=get_distance,reverse=False)
|
222 |
+
q2_song.sort(key=get_distance,reverse=False)
|
223 |
+
q3_song.sort(key=get_distance,reverse=False)
|
224 |
+
q4_song.sort(key=get_distance,reverse=False)
|
225 |
+
|
226 |
+
|
227 |
+
|
228 |
+
|
229 |
+
|
230 |
+
get_number_quadrant_recommendation()
|
231 |
+
q1, q2, q3, q4 = recommended_songs_q1_q2_q3_q4(
|
232 |
+
num_recommendation_q1, num_recommendation_q2, num_recommendation_q3, num_recommendation_q4)
|
233 |
+
# st.write("q1")
|
234 |
+
# st.write(q1)
|
235 |
+
# st.write("q2")
|
236 |
+
# st.write(q2)
|
237 |
+
# st.write("q3")
|
238 |
+
# st.write(q3)
|
239 |
+
# st.write("q4")
|
240 |
+
# st.write(q4)
|
241 |
+
|
242 |
+
w = set()
|
243 |
+
x = set()
|
244 |
+
y = set()
|
245 |
+
z = set()
|
246 |
+
|
247 |
+
for i in q1:
|
248 |
+
w.add(i[6])
|
249 |
+
for j in q2:
|
250 |
+
x.add(j[6])
|
251 |
+
for k in q3:
|
252 |
+
y.add(k[6])
|
253 |
+
for l in q4:
|
254 |
+
z.add(l[6])
|
255 |
+
|
256 |
+
# Checking whether the set w,x,z which consists of the unique spotifyid of the song in the q1, q2, q3, q4 consists of at least one element
|
257 |
+
if len(w) != 0:
|
258 |
+
# a set that consists of the spotify id of the repeated song
|
259 |
+
spotifyid=set()
|
260 |
+
#list consist of one copy of the repeated song with minimum distance
|
261 |
+
q1_=[]
|
262 |
+
|
263 |
+
for i in w:
|
264 |
+
lst1=[]
|
265 |
+
solutions=np.argwhere(q1==i)
|
266 |
+
#if the spotify id ofthat songs consists in the q1 more than one time
|
267 |
+
if(len(solutions)>1):
|
268 |
+
|
269 |
+
for i in range(0,len(solutions)):
|
270 |
+
# print(q1[solutions[i][0]][7])
|
271 |
+
# since that song is repeated more than once, collecing the info of the euclidian distance of that repeated songs in the lst1
|
272 |
+
lst1.append([q1[solutions[i][0]][7],q1[solutions[i][0]][0],q1[solutions[i][0]][1],q1[solutions[i][0]][3],q1[solutions[i][0]][2],q1[solutions[i][0]][5],q1[solutions[i][0]][4],q1[solutions[i][0]][6]])
|
273 |
+
spotifyid.add(q1[solutions[i][0]][6])
|
274 |
+
lst1.sort(key=get_distance1,reverse=False)
|
275 |
+
q1_.append(lst1[0])
|
276 |
+
#excluding all the repeated songs
|
277 |
+
for i in spotifyid:
|
278 |
+
q1 = [item for item in q1 if item[6]!= i]
|
279 |
+
#appending the only one copy of repeated song with minimum distance
|
280 |
+
for i in q1_:
|
281 |
+
q1.append(i)
|
282 |
+
|
283 |
+
|
284 |
+
if len(x) != 0:
|
285 |
+
# a set that consists of the spotify id of the repeated song
|
286 |
+
spotifyid=set()
|
287 |
+
q2_=[]
|
288 |
+
|
289 |
+
for i in x:
|
290 |
+
lst2=[]
|
291 |
+
solutions=np.argwhere(q2==i)
|
292 |
+
#if the spotify id ofthat songs consists in the q1 more than one time
|
293 |
+
if(len(solutions)>1):
|
294 |
+
|
295 |
+
for i in range(0,len(solutions)):
|
296 |
+
# print(q1[solutions[i][0]][7])
|
297 |
+
# since that song is repeated more than once, collecing the info of the euclidian distance of that repeated songs in the lst1
|
298 |
+
lst2.append([q2[solutions[i][0]][7],q2[solutions[i][0]][0],q2[solutions[i][0]][1],q2[solutions[i][0]][3],q2[solutions[i][0]][2],q2[solutions[i][0]][5],q2[solutions[i][0]][4],q2[solutions[i][0]][6]])
|
299 |
+
spotifyid.add(q2[solutions[i][0]][6])
|
300 |
+
lst2.sort(key=get_distance1,reverse=False)
|
301 |
+
q2_.append(lst2[0])
|
302 |
+
for i in spotifyid:
|
303 |
+
q2 = [item for item in q1 if item[6]!= i]
|
304 |
+
|
305 |
+
for i in q2_:
|
306 |
+
q2.append(i)
|
307 |
+
|
308 |
+
if len(y) != 0:
|
309 |
+
# a set that consists of the spotify id of the repeated song
|
310 |
+
spotifyid=set()
|
311 |
+
q3_=[]
|
312 |
+
|
313 |
+
for i in y:
|
314 |
+
lst3=[]
|
315 |
+
solutions=np.argwhere(q3==i)
|
316 |
+
#if the spotify id ofthat songs consists in the q1 more than one time
|
317 |
+
if(len(solutions)>1):
|
318 |
+
|
319 |
+
for i in range(0,len(solutions)):
|
320 |
+
# print(q1[solutions[i][0]][7])
|
321 |
+
# since that song is repeated more than once, collecing the info of the euclidian distance of that repeated songs in the lst1
|
322 |
+
lst3.append([q3[solutions[i][0]][7],q3[solutions[i][0]][0],q3[solutions[i][0]][1],q3[solutions[i][0]][3],q3[solutions[i][0]][2],q3[solutions[i][0]][5],q3[solutions[i][0]][4],q3[solutions[i][0]][6]])
|
323 |
+
spotifyid.add(q3[solutions[i][0]][6])
|
324 |
+
lst3.sort(key=get_distance1,reverse=False)
|
325 |
+
q3_.append(lst3[0])
|
326 |
+
for i in spotifyid:
|
327 |
+
q3 = [item for item in q3 if item[6]!= i]
|
328 |
+
|
329 |
+
for i in q3_:
|
330 |
+
q3.append(i)
|
331 |
+
|
332 |
+
|
333 |
+
if len(z) != 0:
|
334 |
+
# a set that consists of the spotify id of the repeated song
|
335 |
+
spotifyid=set()
|
336 |
+
q4_=[]
|
337 |
+
|
338 |
+
for i in z:
|
339 |
+
lst4=[]
|
340 |
+
solutions=np.argwhere(q4==i)
|
341 |
+
#if the spotify id ofthat songs consists in the q1 more than one time
|
342 |
+
if(len(solutions)>1):
|
343 |
+
|
344 |
+
for i in range(0,len(solutions)):
|
345 |
+
# print(q1[solutions[i][0]][7])
|
346 |
+
# since that song is repeated more than once, collecing the info of the euclidian distance of that repeated songs in the lst1
|
347 |
+
lst4.append([q4[solutions[i][0]][7],q4[solutions[i][0]][0],q4[solutions[i][0]][1],q4[solutions[i][0]][3],q4[solutions[i][0]][2],q4[solutions[i][0]][5],q4[solutions[i][0]][4],q4[solutions[i][0]][6]])
|
348 |
+
spotifyid.add(q4[solutions[i][0]][6])
|
349 |
+
lst4.sort(key=get_distance1,reverse=False)
|
350 |
+
q4_.append(lst4[0])
|
351 |
+
for i in spotifyid:
|
352 |
+
q4 = [item for item in q4 if item[6]!= i]
|
353 |
+
for i in q4_:
|
354 |
+
q4.append(i)
|
355 |
+
|
356 |
+
|
357 |
+
|
358 |
+
st.markdown(f'<h1 style="font-size:24px;">{"Recommened list of songs"}</h1>', unsafe_allow_html=True)
|
359 |
+
print("Recommended list of songs")
|
360 |
+
if(q1!=0):
|
361 |
+
st.write("Happy Songs\n")
|
362 |
+
for i in q1:
|
363 |
+
print(i[3]," ",i[6])
|
364 |
+
st.write(i[3],":",i[5])
|
365 |
+
if(q2!=0):
|
366 |
+
st.write("Tensed Songs\n")
|
367 |
+
for j in q2:
|
368 |
+
print(j[3]," ",j[6])
|
369 |
+
st.write(j[3],":",j[5])
|
370 |
+
|
371 |
+
if(q3!=0):
|
372 |
+
st.write("Sad Songs\n")
|
373 |
+
for k in q3:
|
374 |
+
print(k[3]," ",k[6])
|
375 |
+
st.write(k[3],":",k[5])
|
376 |
+
if(q4!=0):
|
377 |
+
st.write("Calm Songs\n")
|
378 |
+
for l in q4:
|
379 |
+
print(l[3]," ",l[6])
|
380 |
+
st.write(l[3],":",l[5])
|
381 |
+
|
382 |
+
|
383 |
+
|
384 |
+
|