Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,45 @@ import streamlit as st
|
|
| 2 |
import pandas as pd
|
| 3 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 4 |
from sklearn.neighbors import NearestNeighbors
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Page config
|
| 7 |
st.set_page_config(
|
|
@@ -76,6 +115,28 @@ st.markdown("""
|
|
| 76 |
transform: scale(1.05);
|
| 77 |
box-shadow: 0 4px 12px rgba(255, 0, 0, 0.2);
|
| 78 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
</style>
|
| 80 |
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&family=Roboto:wght@400;500&display=swap" rel="stylesheet">
|
| 81 |
""", unsafe_allow_html=True)
|
|
@@ -178,18 +239,40 @@ with st.sidebar:
|
|
| 178 |
if get_recs:
|
| 179 |
st.header("🎵 Your Recommendations")
|
| 180 |
recommendations = hybrid_recommendv2(user_id, song_title, top_n)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
if recommendations.empty:
|
| 182 |
st.error("No recommendations found. Try selecting different songs or users.")
|
| 183 |
else:
|
| 184 |
st.balloons()
|
| 185 |
for idx, row in recommendations.iterrows():
|
| 186 |
youtube_link = f"https://www.youtube.com/results?search_query={row['title']}+{row['artist_name']}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
st.markdown(f"""
|
| 188 |
-
<div class="recommendation-card">
|
| 189 |
-
<
|
| 190 |
-
<
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
|
|
|
|
|
|
|
|
|
| 194 |
</div>
|
| 195 |
""", unsafe_allow_html=True)
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 4 |
from sklearn.neighbors import NearestNeighbors
|
| 5 |
+
import requests
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Function to get Spotify access token
|
| 9 |
+
def get_spotify_token(client_id, client_secret):
|
| 10 |
+
url = "https://accounts.spotify.com/api/token"
|
| 11 |
+
headers = {
|
| 12 |
+
"Content-Type": "application/x-www-form-urlencoded"
|
| 13 |
+
}
|
| 14 |
+
data = {
|
| 15 |
+
"grant_type": "client_credentials"
|
| 16 |
+
}
|
| 17 |
+
response = requests.post(url, headers=headers, data=data, auth=(client_id, client_secret))
|
| 18 |
+
if response.status_code == 200:
|
| 19 |
+
return response.json().get("access_token")
|
| 20 |
+
return None
|
| 21 |
+
|
| 22 |
+
# Function to fetch Spotify track details
|
| 23 |
+
def fetch_spotify_track(query, token):
|
| 24 |
+
url = "https://api.spotify.com/v1/search"
|
| 25 |
+
headers = {
|
| 26 |
+
"Authorization": f"Bearer {token}"
|
| 27 |
+
}
|
| 28 |
+
params = {
|
| 29 |
+
"q": query,
|
| 30 |
+
"type": "track",
|
| 31 |
+
"limit": 1
|
| 32 |
+
}
|
| 33 |
+
response = requests.get(url, headers=headers, params=params)
|
| 34 |
+
if response.status_code == 200:
|
| 35 |
+
data = response.json()
|
| 36 |
+
if data["tracks"]["items"]:
|
| 37 |
+
track = data["tracks"]["items"][0]
|
| 38 |
+
return {
|
| 39 |
+
"thumbnail": track["album"]["images"][0]["url"], # Get the largest available thumbnail
|
| 40 |
+
"spotify_link": track["external_urls"]["spotify"] # Spotify track link
|
| 41 |
+
}
|
| 42 |
+
return {"thumbnail": None, "spotify_link": None}
|
| 43 |
+
|
| 44 |
|
| 45 |
# Page config
|
| 46 |
st.set_page_config(
|
|
|
|
| 115 |
transform: scale(1.05);
|
| 116 |
box-shadow: 0 4px 12px rgba(255, 0, 0, 0.2);
|
| 117 |
}
|
| 118 |
+
.spotify-link {
|
| 119 |
+
background-color: #1db954;
|
| 120 |
+
color: white !important;
|
| 121 |
+
padding: 8px 16px;
|
| 122 |
+
border-radius: 20px;
|
| 123 |
+
text-decoration: none;
|
| 124 |
+
display: inline-flex;
|
| 125 |
+
align-items: center;
|
| 126 |
+
gap: 8px;
|
| 127 |
+
font-family: 'Roboto', sans-serif;
|
| 128 |
+
font-weight: 500;
|
| 129 |
+
transition: all 0.3s ease;
|
| 130 |
+
}
|
| 131 |
+
.spotify-link:before {
|
| 132 |
+
content: "🎧";
|
| 133 |
+
font-size: 0.8em;
|
| 134 |
+
}
|
| 135 |
+
.spotify-link:hover {
|
| 136 |
+
background-color: #cc0000;
|
| 137 |
+
transform: scale(1.05);
|
| 138 |
+
box-shadow: 0 4px 12px rgba(255, 0, 0, 0.2);
|
| 139 |
+
}
|
| 140 |
</style>
|
| 141 |
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&family=Roboto:wght@400;500&display=swap" rel="stylesheet">
|
| 142 |
""", unsafe_allow_html=True)
|
|
|
|
| 239 |
if get_recs:
|
| 240 |
st.header("🎵 Your Recommendations")
|
| 241 |
recommendations = hybrid_recommendv2(user_id, song_title, top_n)
|
| 242 |
+
SPOTIFY_CLIENT_ID = os.getenv("SPOTIFY_CLIENT_ID", None)
|
| 243 |
+
SPOTIFY_CLIENT_SECRET = os.getenv("SPOTIFY_CLIENT_SECRET", None)
|
| 244 |
+
|
| 245 |
+
# Get Spotify token
|
| 246 |
+
spotify_token = get_spotify_token(SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET)
|
| 247 |
if recommendations.empty:
|
| 248 |
st.error("No recommendations found. Try selecting different songs or users.")
|
| 249 |
else:
|
| 250 |
st.balloons()
|
| 251 |
for idx, row in recommendations.iterrows():
|
| 252 |
youtube_link = f"https://www.youtube.com/results?search_query={row['title']}+{row['artist_name']}"
|
| 253 |
+
spotify_data = fetch_spotify_track(f"{row['title']} {row['artist_name']}", spotify_token)
|
| 254 |
+
spotify_link = spotify_data.get("spotify_link", "#")
|
| 255 |
+
thumbnail_url = spotify_data.get("thumbnail")
|
| 256 |
+
# st.markdown(f"""
|
| 257 |
+
# <div class="recommendation-card">
|
| 258 |
+
# <h3>{row['title']}</h3>
|
| 259 |
+
# <p><strong>Artist:</strong> {row['artist_name']}</p>
|
| 260 |
+
# <p><strong>Album:</strong> {row['release']}</p>
|
| 261 |
+
# <a href="{youtube_link}" target="_blank" class="youtube-link">
|
| 262 |
+
# Watch on YouTube
|
| 263 |
+
# </a>
|
| 264 |
+
|
| 265 |
+
# </div>
|
| 266 |
+
# """, unsafe_allow_html=True)
|
| 267 |
st.markdown(f"""
|
| 268 |
+
<div class="recommendation-card" style="display: flex; align-items: center; gap: 20px;">
|
| 269 |
+
{"<img src='" + thumbnail_url + "' alt='Song Thumbnail' style='width:150px; height:150px; border-radius:10px; object-fit: cover;'>" if thumbnail_url else ""}
|
| 270 |
+
<div>
|
| 271 |
+
<h3>{row['title']}</h3>
|
| 272 |
+
<p><strong>Artist:</strong> {row['artist_name']}</p>
|
| 273 |
+
<p><strong>Album:</strong> {row['release']}</p>
|
| 274 |
+
<a href="{youtube_link}" target="_blank" class="youtube-link">Watch on YouTube</a>
|
| 275 |
+
<a href="{spotify_link}" target="_blank" class="spotify-link">Play on Spotify</a>
|
| 276 |
+
</div>
|
| 277 |
</div>
|
| 278 |
""", unsafe_allow_html=True)
|