BQGamer commited on
Commit
851a67d
·
verified ·
1 Parent(s): 871f141

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py CHANGED
@@ -108,6 +108,44 @@ def search_youtube_music(mood_value, tempo_value):
108
 
109
  return iframe_html + "<br>" + download_link_html
110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  # Streamlit UI
112
  st.set_page_config(page_title="Emotion Detector & Music Finder", layout="centered")
113
 
@@ -147,3 +185,11 @@ with tab2:
147
  if st.button("Find Music"):
148
  youtube_embed = search_youtube_music(mood_slider, tempo_slider)
149
  st.markdown(youtube_embed, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
108
 
109
  return iframe_html + "<br>" + download_link_html
110
 
111
+
112
+ # Spotify Authentication Function
113
+ SPOTIFY_CLIENT_ID = "d4925860a6894ce68c72a6ff4b69f542"
114
+ SPOTIFY_CLIENT_SECRET = "787194348cb544ad889c397cf4963cc8"
115
+
116
+ def get_spotify_token():
117
+ auth_url = "https://accounts.spotify.com/api/token"
118
+ credentials = f"{SPOTIFY_CLIENT_ID}:{SPOTIFY_CLIENT_SECRET}"
119
+ headers = {
120
+ "Authorization": f"Basic {base64.b64encode(credentials.encode()).decode()}",
121
+ "Content-Type": "application/x-www-form-urlencoded"
122
+ }
123
+ data = {"grant_type": "client_credentials"}
124
+
125
+ response = requests.post(auth_url, headers=headers, data=data)
126
+ response.raise_for_status()
127
+ return response.json().get("access_token")
128
+
129
+ # Function to get song recommendations from Spotify
130
+ def get_spotify_recommendations(mood):
131
+ genre = "pop" if mood not in ["sad", "happy"] else mood
132
+ access_token = get_spotify_token()
133
+
134
+ search_url = "https://api.spotify.com/v1/search"
135
+ headers = {"Authorization": f"Bearer {access_token}"}
136
+ params = {
137
+ "q": genre,
138
+ "type": "track",
139
+ "limit": 3
140
+ }
141
+
142
+ response = requests.get(search_url, headers=headers, params=params)
143
+ response.raise_for_status()
144
+
145
+ tracks = response.json().get("tracks", {}).get("items", [])
146
+ return [f"{track['name']} by {track['artists'][0]['name']} - [🎵 Listen on Spotify]({track['external_urls']['spotify']})"
147
+ for track in tracks]
148
+
149
  # Streamlit UI
150
  st.set_page_config(page_title="Emotion Detector & Music Finder", layout="centered")
151
 
 
185
  if st.button("Find Music"):
186
  youtube_embed = search_youtube_music(mood_slider, tempo_slider)
187
  st.markdown(youtube_embed, unsafe_allow_html=True)
188
+
189
+ # Fetch Spotify song recommendations based on mood
190
+ mood_description = get_mood_from_slider(mood_slider)
191
+ spotify_recommendations = get_spotify_recommendations(mood_description)
192
+
193
+ st.subheader("Spotify Song Recommendations")
194
+ for song in spotify_recommendations:
195
+ st.markdown(song, unsafe_allow_html=True)