File size: 15,003 Bytes
0ca7265
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import streamlit as st
import math
import numpy as np
import psycopg2
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
import pandas as pd
from scipy.spatial import distance
import random

# Initialize connection.
# Uses st.experimental_singleton to only run once.


@st.experimental_singleton
def init_connection():
    return psycopg2.connect(**st.secrets["postgres"])


list_selected_songs = []
list_allsongs = []
emotion = []
q1_song = []
q2_song = []
q3_song = []
q4_song = []

conn = init_connection()
# number of recommendation of the song to be made
num_recommendation = 10

# Perform query.
# Uses st.experimental_memo to only rerun when the query changes or after 10 min.


@st.experimental_memo(ttl=600)
def run_query(query):
    with conn.cursor() as cur:
        cur.execute(query)
        return cur.fetchall()


def count_emotion(list_emotion):
    s1 = 'q1'
    s2 = 'q2'
    s3 = 'q3'
    s4 = 'q4'
    global count_q1
    global count_q2
    global count_q3
    global count_q4
    count_q1 = list_emotion.count(s1)
    count_q2 = list_emotion.count(s2)
    count_q3 = list_emotion.count(s3)
    count_q4 = list_emotion.count(s4)


def recommended_songs_q1_q2_q3_q4(num_recommendation_q1, num_recommendation_q2, num_recommendation_q3, num_recommendation_q4):
    global q1
    global q2
    global q3
    global g4
    q1 = np.array(q1_song[0:int(num_recommendation_q1)])
    q2 = np.array(q2_song[0:int(num_recommendation_q2)])
    q3 = np.array(q3_song[0:int(num_recommendation_q3)])
    q4 = np.array(q4_song[0:int(num_recommendation_q4)])
    return q1, q2, q3, q4


def get_number_quadrant_recommendation():
    total = count_q1+count_q2+count_q3+count_q4
    percentage_q1 = count_q1/total
    percentage_q2 = count_q2/total
    percentage_q3 = count_q3/total
    percentage_q4 = count_q4/total
    global num_recommendation_q1
    global num_recommendation_q2
    global num_recommendation_q3
    global num_recommendation_q4
    num_recommendation_q1 = (percentage_q1*num_recommendation)
    num_recommendation_q2 = (percentage_q2*num_recommendation)
    num_recommendation_q3 = (percentage_q3*num_recommendation)
    num_recommendation_q4 = (percentage_q4*num_recommendation)


def get_distance(e):
    # index 6 consists of euclidian distance between two songs, seleted song and song in the database
    return e[7]


def get_distance1(e):
    # index 0 consists of euclidian distance between two songs in the case of the repeated song
    return e[0]
   


rows = run_query("SELECT  song_title, artist from nepali_songs order by song_title")
st.title('Emotion Based Music Recommendation System')
st.image('music.jpg')

with st.form("my_form"):

    options = st.multiselect('Choose the song that you want to listen', [
                             ':'.join(map(str, x)) for x in rows])
    submitted = st.form_submit_button("Recommend song to me")

    # splitting artist and song title of the song that user have selected
    if submitted:
        artist_song_list = []
        for index, element in enumerate(options):
            artist_list = element.split(':')

            artist_song_list.append(artist_list)

        # storing the information of the audio features of the songs that the user have selected by retrieving through the database

        for x in artist_song_list:
            with conn.cursor() as cur:
                cur.execute(
                    "select * from nepali_songs where song_title=%s and artist=%s", [x[0], x[1]])
                row = cur.fetchall()
                list_selected_songs.append(row)
        print("selected songs")
        happy_songs=[]
        tensed_songs=[]
        sad_songs=[]
        calm_songs=[]
        
       
            
        # collecting the information about the emotion quadrant of the each song that the user have selected and storing in the list emotion
        for i in list_selected_songs:
            print(i[0][12],i[0][13])
            emotion.append(i[0][14])
            #categorizing the songs into 4 different emotions
            if(i[0][14]=='q1'):
                happy_songs.append(i[0][12])
            elif(i[0][14]=='q2'):
                tensed_songs.append(i[0][12])
            elif(i[0][14]=='q3'):
                sad_songs.append(i[0][12])
            else:
                calm_songs.append(i[0][12])
           

        # calculating the total number of emotion quadrant of the song that the user have selected
        count_emotion(emotion)
        total = count_q1+count_q2+count_q3+count_q4
        st.write("You have selected ", count_q1, " happy songs")
        st.write(happy_songs)
        st.write("You have selected ", count_q2, " tensed songs")
        st.write(tensed_songs)
        st.write("You have selected ", count_q3, " sad songs")
        st.write(sad_songs)
        st.write("You have selected ", count_q4, " calm songs")
        st.write(calm_songs)
        # retriving information of all songs in the database and storing in the list list_allsongs

        distance_info_song_all = []
        with conn.cursor() as cur:
            cur.execute("select * from nepali_songs")
            row = cur.fetchall()
            # print(type(row[0]))
            list_allsongs.append(row)

        # calculating the euclidean distance of selected songs with all songs in the database
        for i in list_selected_songs:
            valence_selected_songs = float(i[0][11])
            energy_selected_songs = float(i[0][2])
            list1 = [valence_selected_songs, energy_selected_songs]

            for lists in list_allsongs:
                # print("printing the list of songs\n")
                distance_info = []
                for tuples in lists:
                    valence_allsongs = float(tuples[11])
                    energy_allsongs = float(tuples[2])
                    list2 = [valence_allsongs, energy_allsongs]
                    dist = distance.euclidean(list1, list2)
                    # i[0][0] is the spotifyid of the selected song and tuples[0] is the spotifyid
                    # of the song in the database
                    distance_info.append(
                        [i[0][12], i[0][14], i[0][0], tuples[12], tuples[14], tuples[13], tuples[0], dist])
                distance_info_song_all.append(distance_info)
        # st.write("distance info")
        # st.write(distance_info_song_all)
        nearest_neighbour = []
        # distance_info_song_all consists of the euclidian distance of the selected song with all songs in the database
        # recommend_list consist of the top 10 songs having minimum distance with all the selected song
        for i in distance_info_song_all:
            i.sort(key=get_distance, reverse=False)
            #sorting the 10 nearest neighbour of the each selected song selected song
            for k in range(0,10):
                nearest_neighbour.append(i[k])
        # st.write(nearest_neighbour)

            # calculating the percentange of song associated with q1, q2, q3 and q4 that the user have selected

            # st.write("q1 recommendation number", num_recommendation_q1)
            # st.write("q2 recommendation number", num_recommendation_q2)
            # st.write("q3 recommendation number", num_recommendation_q3)
            # st.write("q4 recommendation numner", num_recommendation_q4)
            # st.write(num_recommendation)

        # selecting the song of the particular quadrant from the nearest neighbour and storing in the list

        for t in nearest_neighbour:
            
            if (t[4] == 'q2' and t[7]!=0.0):
                q2_song.append(t)

            elif (t[4] == 'q1' and t[7]!=0.0):
                q1_song.append(t)
            elif (t[4] == 'q3' and t[7]!=0.0):
                q3_song.append(t)
            elif (t[4] == 'q4' and t[7]!=0.0):
                q4_song.append(t)
            else:
                pass
       
        q1_song.sort(key=get_distance,reverse=False)
        q2_song.sort(key=get_distance,reverse=False)
        q3_song.sort(key=get_distance,reverse=False)
        q4_song.sort(key=get_distance,reverse=False)





        get_number_quadrant_recommendation()
        q1, q2, q3, q4 = recommended_songs_q1_q2_q3_q4(
            num_recommendation_q1, num_recommendation_q2, num_recommendation_q3, num_recommendation_q4)
        # st.write("q1")
        # st.write(q1)
        # st.write("q2")
        # st.write(q2)
        # st.write("q3")
        # st.write(q3)
        # st.write("q4")
        # st.write(q4)

        w = set()
        x = set()
        y = set()
        z = set()

        for i in q1:
            w.add(i[6])
        for j in q2:
            x.add(j[6])
        for k in q3:
            y.add(k[6])
        for l in q4:
            z.add(l[6])

    # 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
        if len(w) != 0:
            # a set that consists of the spotify id of the repeated song
            spotifyid=set()
            #list consist of one copy of the repeated song with minimum distance
            q1_=[]
           
            for i in w:
                lst1=[]
                solutions=np.argwhere(q1==i)
                #if the spotify id ofthat songs consists in the q1 more than one time
                if(len(solutions)>1):
                    
                    for i in range(0,len(solutions)):
                        # print(q1[solutions[i][0]][7])
                        # since that song is repeated more than once, collecing the info of the euclidian distance of that repeated songs in the lst1
                        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]])
                        spotifyid.add(q1[solutions[i][0]][6])
                    lst1.sort(key=get_distance1,reverse=False)
                    q1_.append(lst1[0])
                    #excluding all the repeated songs   
                    for i in spotifyid: 
                        q1 = [item for item in q1 if item[6]!= i]
                    #appending the only one copy of repeated song with minimum distance
                    for i in q1_:
                        q1.append(i)


        if len(x) != 0:
            # a set that consists of the spotify id of the repeated song
            spotifyid=set()
            q2_=[]
           
            for i in x:
                lst2=[]
                solutions=np.argwhere(q2==i)
                #if the spotify id ofthat songs consists in the q1 more than one time
                if(len(solutions)>1):
                    
                    for i in range(0,len(solutions)):
                        # print(q1[solutions[i][0]][7])
                        # since that song is repeated more than once, collecing the info of the euclidian distance of that repeated songs in the lst1
                        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]])
                        spotifyid.add(q2[solutions[i][0]][6])
                    lst2.sort(key=get_distance1,reverse=False)
                    q2_.append(lst2[0])   
                    for i in spotifyid: 
                        q2 = [item for item in q1 if item[6]!= i]
                    
                    for i in q2_:
                        q2.append(i)

        if len(y) != 0:
             # a set that consists of the spotify id of the repeated song
            spotifyid=set()
            q3_=[]
           
            for i in y:
                lst3=[]
                solutions=np.argwhere(q3==i)
                #if the spotify id ofthat songs consists in the q1 more than one time
                if(len(solutions)>1):
                    
                    for i in range(0,len(solutions)):
                        # print(q1[solutions[i][0]][7])
                        # since that song is repeated more than once, collecing the info of the euclidian distance of that repeated songs in the lst1
                        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]])
                        spotifyid.add(q3[solutions[i][0]][6])
                    lst3.sort(key=get_distance1,reverse=False)
                    q3_.append(lst3[0])   
                    for i in spotifyid: 
                        q3 = [item for item in q3 if item[6]!= i]
                   
                    for i in q3_:
                        q3.append(i)
            

        if len(z) != 0:
             # a set that consists of the spotify id of the repeated song
            spotifyid=set()
            q4_=[]
           
            for i in z:
                lst4=[]
                solutions=np.argwhere(q4==i)
                #if the spotify id ofthat songs consists in the q1 more than one time
                if(len(solutions)>1):
                    
                    for i in range(0,len(solutions)):
                        # print(q1[solutions[i][0]][7])
                        # since that song is repeated more than once, collecing the info of the euclidian distance of that repeated songs in the lst1
                        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]])
                        spotifyid.add(q4[solutions[i][0]][6])
                    lst4.sort(key=get_distance1,reverse=False)
                    q4_.append(lst4[0])   
                    for i in spotifyid: 
                        q4 = [item for item in q4 if item[6]!= i]
                    for i in q4_:
                        q4.append(i)
                    
        

        st.markdown(f'<h1 style="font-size:24px;">{"Recommened list of songs"}</h1>', unsafe_allow_html=True)
        print("Recommended list of songs")
        if(q1!=0):
            st.write("Happy Songs\n")
            for i in q1:
                print(i[3]," ",i[6])
                st.write(i[3],":",i[5])
        if(q2!=0):
            st.write("Tensed Songs\n")
            for j in q2:
                print(j[3]," ",j[6])
                st.write(j[3],":",j[5])

        if(q3!=0):
            st.write("Sad Songs\n")
            for k in q3:
                print(k[3]," ",k[6])
                st.write(k[3],":",k[5])
        if(q4!=0):
            st.write("Calm Songs\n")   
            for l in q4:
                print(l[3]," ",l[6])
                st.write(l[3],":",l[5])