-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
208 lines (168 loc) · 10.4 KB
/
app.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
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
import streamlit as st
from streamlit_option_menu import option_menu
import streamlit.components.v1 as components
import spotify_music_recommender as smr
if "song_init" not in st.session_state:
st.session_state.song_init = False
def song_page(name, year):
"""
Displays the Spotify song with the given name and year using an iframe.
Args:
name (str): The name of the song.
year (str): The year of the song.
"""
song_uri = smr.find_song_uri(name, year)
formatted_song_uri = song_uri.split(':')[-1]
uri_link = f'https://open.spotify.com/embed/track/{formatted_song_uri}?utm_source=generator'
components.iframe(uri_link, height=100)
def spr_sidebar():
"""
Renders the sidebar for the app and sets the app mode based on the selected option.
"""
menu = option_menu(
menu_title=None,
options=['Home', 'Results', 'About'],
icons=['house', 'book', 'info-square'],
menu_icon='cast',
default_index=0,
orientation='horizontal'
)
if menu == 'Home':
st.session_state.app_mode = 'Home'
elif menu == 'Results':
st.session_state.app_mode = 'Results'
elif menu == 'About':
st.session_state.app_mode = 'About'
def home_page():
"""
Renders the Home page of the app, including input fields for the song, year, and song review.
"""
st.title("Spotify Music Recommender")
col1, col2 = st.columns(2)
song_input = col1.text_input("Enter a song:")
year_input = col2.text_input("Enter the year:")
col3, col4 = st.columns(2)
find_song_button = col3.button("Find Song")
find_random_song_button = col4.button("Random Song")
st.subheader("Song Review")
critic_input = st.text_input("")
predict_button = st.button("Start Prediction")
if find_song_button:
song_page(song_input, year_input)
elif find_random_song_button:
find_random_song()
elif not st.session_state.song_init:
st.session_state.song_init = True
find_random_song()
if predict_button:
with st.spinner('Getting Recommendations...'):
try:
data_path = "data/data.csv"
file_path = "data/pipeline.pkl"
cluster_labels = "data/cluster_labels.csv"
song_cluster_pipeline, data, number_cols = smr.get_model_values(
data_path, file_path, cluster_labels)
user_critic_text = critic_input
raw_recommendation_array = smr.get_recommendation_array(
song_input, year_input, number_cols, user_critic_text)
result = smr.format_chatgpt_recommendations(
raw_recommendation_array, data, song_cluster_pipeline, 15)
st.session_state.song_uris = smr.get_recommendation_song_uri(
result)
st.write("You can access recommended song at result page")
except:
st.write("An error occurred please try again")
def text_field(label, columns=None, **input_params):
"""
Helper function to display a labeled text input field with the given label.
Args:
label (str): The label for the text input field.
columns (Optional[List[int]]): A list of column widths for layout purposes.
**input_params: Additional parameters to pass to the text_input function.
Returns:
str: The text entered in the text input field.
"""
c1, c2 = st.columns(columns or [1, 4])
# Display field name with some alignment
c1.markdown("##")
c1.markdown(label)
# Sets a default key parameter to avoid duplicate key errors
input_params.setdefault("key", label)
# Forward text input parameters
return c2.text_input("", **input_params)
def find_random_song():
"""
Finds and displays a random song using the Spotify API.
"""
try:
song_input, year_input = smr.get_random_song()
song_page(song_input, year_input)
except:
song_input, year_input = "Heat Waves", "2020"
song_page(song_input, year_input)
def result_page():
"""
Renders the Results page of the app, displaying the recommended songs.
"""
if "song_uris" in st.session_state:
for uri in st.session_state.song_uris:
uri = uri.split(":")[-1]
uri_link = "https://open.spotify.com/embed/track/" + \
uri + "?utm_source=generator&theme=0"
components.iframe(uri_link, height=80)
else:
st.subheader(
"Please enter song information and review, then click Start Prediction")
def examples_page():
"""
Placeholder function for the Examples page (to be implemented if needed).
"""
pass
def About_page():
"""
Renders the About page of the app, providing information about the development and credits.
"""
st.header('Development')
"""
Have you ever listened to a song and liked it overall, but felt that certain features could be improved? Maybe the chorus was too loud, the energy level wasn't quite right, or there were either too many or too few words. I've had those experiences too, and that's what inspired me to create a song recommender based on user reviews.
The process is straightforward: simply type in the name of a song or choose a random one, and then enter your review. The recommender will analyze your review using ChatGPT and utilize the Spotify API to generate personalized song recommendations. It's an exciting way to enhance your music discovery and tailor the recommendations to your specific preferences.
Although it's important to note that the dataset used for training the model was relatively small (170k), the recommender still aims to provide valuable suggestions. While it may not reach its full potential due to the limited data, it serves as a starting point for exploring new songs that align with your individual tastes.
So, if you're looking to fine-tune your music experience and discover songs that better match your preferences, give this song recommender a try. Enter your review, and let the algorithm work its magic to recommend songs that you're more likely to enjoy.\n
Github: [alanahmet](https://github.com/alanahmet) \n
Mail: cs.ahmetyusufalan@gmail.com
"""
st.subheader('Audio Features Explanation')
"""
| Variable | Description |
| :----: | :---: |
| Acousticness | A confidence measure from 0.0 to 1.0 of whether the track is acoustic. 1.0 represents high confidence the track is acoustic. |
| Danceability | Danceability describes how suitable a track is for dancing based on a combination of musical elements including tempo, rhythm stability, beat strength, and overall regularity. A value of 0.0 is least danceable and 1.0 is most danceable. |
| Energy | Energy is a measure from 0.0 to 1.0 and represents a perceptual measure of intensity and activity. Typically, energetic tracks feel fast, loud, and noisy. For example, death metal has high energy, while a Bach prelude scores low on the scale. Perceptual features contributing to this attribute include dynamic range, perceived loudness, timbre, onset rate, and general entropy. |
| Instrumentalness | Predicts whether a track contains no vocals. "Ooh" and "aah" sounds are treated as instrumental in this context. Rap or spoken word tracks are clearly "vocal". The closer the instrumentalness value is to 1.0, the greater likelihood the track contains no vocal content. Values above 0.5 are intended to represent instrumental tracks, but confidence is higher as the value approaches 1.0. |
| Key | The key the track is in. Integers map to pitches using standard Pitch Class notation. E.g. 0 = C, 1 = C♯/D♭, 2 = D, and so on. If no key was detected, the value is -1. |
| Liveness | Detects the presence of an audience in the recording. Higher liveness values represent an increased probability that the track was performed live. A value above 0.8 provides strong likelihood that the track is live. |
| Loudness | The overall loudness of a track in decibels (dB). Loudness values are averaged across the entire track and are useful for comparing relative loudness of tracks. Loudness is the quality of a sound that is the primary psychological correlate of physical strength (amplitude). Values typically range between -60 and 0 dB. |
| Mode | Mode indicates the modality (major or minor) of a track, the type of scale from which its melodic content is derived. Major is represented by 1 and minor is 0. |
| Speechiness | Speechiness detects the presence of spoken words in a track. The more exclusively speech-like the recording (e.g. talk show, audio book, poetry), the closer to 1.0 the attribute value. Values above 0.66 describe tracks that are probably made entirely of spoken words. Values between 0.33 and 0.66 describe tracks that may contain both music and speech, either in sections or layered, including such cases as rap music. Values below 0.33 most likely represent music and other non-speech-like tracks. |
| Tempo | The overall estimated tempo of a track in beats per minute (BPM). In musical terminology, tempo is the speed or pace of a given piece and derives directly from the average beat duration. |
| Time Signature | An estimated time signature. The time signature (meter) is a notational convention to specify how many beats are in each bar (or measure). The time signature ranges from 3 to 7 indicating time signatures of "3/4", to "7/4". |
| Valence | A measure from 0.0 to 1.0 describing the musical positiveness conveyed by a track. Tracks with high valence sound more positive (e.g. happy, cheerful, euphoric), while tracks with low valence sound more negative (e.g. sad, depressed, angry). |
Information about features: [here](https://developer.spotify.com/documentation/web-api/reference/#/operations/get-audio-features)
"""
st.subheader('Credit')
"""
Thanks for the base of the streamlit application to [abdelrhmanelruby](https://github.com/abdelrhmanelruby/Spotify-Recommendation-System), and the dataset can be found [here](https://www.kaggle.com/datasets/vatsalmavani/spotify-dataset)
"""
def main():
"""
Main function to run the app. Renders different pages based on the app mode set in the sidebar.
"""
spr_sidebar()
if st.session_state.app_mode == 'Home':
home_page()
if st.session_state.app_mode == 'Results':
result_page()
if st.session_state.app_mode == 'About':
About_page()
if __name__ == '__main__':
main()