-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusic-match.py
136 lines (122 loc) · 3.42 KB
/
music-match.py
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
import streamlit as st
from streamlit_gsheets import GSheetsConnection
import pandas as pd
from fuzzywuzzy import fuzz
#css
page_bg_image = """
<style>
[data-testid="stAppViewContainer"]{
background-color: rgb(30 215 96)
}
</style>
"""
st.markdown(page_bg_image,unsafe_allow_html=True)
#title and description
st.title("Music-Match")
st.markdown("Enter your details and music preference below.")
#establishing connection with google sheets
conn = st.connection("gsheets", type=GSheetsConnection)
#fetch data
existing_data = conn.read(worksheet="info", usecols=list(range(4)), ttl=60)
existing_data = existing_data.dropna(how="all")
#creating a panda dataframe with existing data
df = pd.DataFrame(existing_data)
#creating a pandas dataframe with just the "genres" column
col_data = df["genres"]
#st.dataframe(df)
#list of genres
genre_options = [
"Pop",
"Rock",
"Hip Hop",
"R&B",
"Country",
"Electronic",
"Jazz",
"Blues",
"Classical",
"Reggae",
"Indie",
"Metal",
"Folk",
"Punk",
"Soul",
"Funk",
"Disco",
"EDM",
"Rap",
"Alternative",
"Gospel",
"Ska",
"Dubstep",
"House",
"Techno",
"K-pop",
"Latin",
"J-Pop",
"Bollywood",
"Ambient",
"Trance",
"Grime",
"Chillout",
"Salsa",
"Bluegrass",
"Swing",
"Acoustic",
"New Age",
"Dancehall",
"Hard Rock",
"Trap",
"Psychedelic",
"World",
"Reggaeton",
"Instrumental",
"Garage",
"Samba",
"Rock and Roll"
]
#creating the form
with st.form(key="user_form"):
name = st.text_input(label="Create an username*")
spotify = st.text_input(label="your Spotify username")
usergenres = st.multiselect("Select your favorite genres", options=genre_options)
hours = st.slider("How many hours do you listen to music per day",0,24,1)
#mandatory fields
st.markdown("**required field*")
#button
submit = st.form_submit_button(label="Submit")
#button click action
if submit:
if not name:
st.warning("Ensure that the mandatory fields are filled")
st.stop()
elif existing_data["username"].str.contains(name).any():
st.warning("username already exists, please choose another username")
st.stop()
else:
#calculation of scores
reference_string = usergenres
similarity_scores = df['genres'].apply(lambda x: fuzz.ratio(reference_string, x))
# st.write(similarity_scores)
index_of_max_value = similarity_scores.idxmax()
name_at_index = df.loc[index_of_max_value, 'username']
#creation of dataframe to be pushed in the form
info = pd.DataFrame(
[
{
"username": name,
"genres": ",".join(usergenres),
"spotify_username": spotify,
"hours_listened": hours,
}
]
)
updated_info = pd.concat([existing_data, info], ignore_index=True)
#push to google sheets
conn.update(worksheet="info", data=updated_info)
# st.success("calculating")
st.write('Hey,',name,'your music-match is',name_at_index)
#finding score
#reference_string = usergenres
#similarity_scores = df['genres'].apply(lambda x: fuzz.ratio(reference_string, x))
#st.write(similarity_scores)