-
Notifications
You must be signed in to change notification settings - Fork 0
/
recommendations.html
351 lines (320 loc) · 16.9 KB
/
recommendations.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Song Recommender helps you find new music based on your preferences.">
<meta name="keywords" content="music, recommendations, song recommender, playlist">
<meta name="author" content="Your Name">
<title>Song Recommender</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.0/chart.min.js"></script>
</head>
<body class="bg-gray-100">
<div class="container mx-auto px-4 py-8">
<header class="flex justify-between items-center mb-8">
<h1 class="text-3xl font-bold text-gray-800">Song Recommender</h1>
<nav>
<a href="/profile" class="text-blue-500 hover:text-blue-700 mr-4" aria-label="Profile">Profile</a>
<a href="/logout" class="text-blue-500 hover:text-blue-700" aria-label="Logout">Logout</a>
</nav>
</header>
<main class="bg-white rounded-lg shadow-lg p-6 mb-8">
<section id="input-section" class="mb-8">
<div class="flex justify-center space-x-4 mb-4">
<button id="singleTrackBtn" class="bg-blue-500 text-white px-4 py-2 rounded">Single Track</button>
<button id="playlistBtn" class="bg-blue-500 text-white px-4 py-2 rounded">Playlist</button>
<button id="recentlyPlayedBtn" class="bg-blue-500 text-white px-4 py-2 rounded">Recently Played</button>
</div>
<div id="inputField" class="mb-4">
<input type="text" id="trackInput" placeholder="Enter track ID" class="w-full p-2 border rounded">
</div>
<div id="playlistDropdown" class="mb-4 hidden">
<select id="playlistSelect" class="w-full p-2 border rounded">
<option value="">Select a playlist</option>
</select>
</div>
<div class="flex items-center justify-center mb-4">
<input type="checkbox" id="useDeepLearning" class="mr-2">
<label for="useDeepLearning">Use Deep Learning(Might take too long!)</label>
</div>
<div class="mb-4">
<label for="numRecommendations" class="block text-gray-700 text-sm font-bold mb-2">Number of Recommendations:</label>
<input type="range" id="numRecommendations" min="1" max="50" value="10" class="w-full">
<span id="numRecommendationsValue" class="block text-gray-700 text-sm">10</span>
</div>
<button id="getRecommendationsBtn" class="bg-green-500 text-white px-4 py-2 rounded w-full">Get Recommendations</button>
</section>
<section id="mood-selector" class="mb-8">
<h2 class="text-2xl font-semibold mb-4">Select Mood</h2>
<div class="flex space-x-4">
<button class="mood-btn bg-yellow-400 hover:bg-yellow-500 text-white font-bold py-2 px-4 rounded" data-mood="happy">Happy</button>
<button class="mood-btn bg-blue-400 hover:bg-blue-500 text-white font-bold py-2 px-4 rounded" data-mood="sad">Sad</button>
<button class="mood-btn bg-red-400 hover:bg-red-500 text-white font-bold py-2 px-4 rounded" data-mood="energetic">Energetic</button>
<button class="mood-btn bg-green-400 hover:bg-green-500 text-white font-bold py-2 px-4 rounded" data-mood="relaxed">Relaxed</button>
</div>
</section>
<section id="feature-weights" class="mb-8">
<h2 class="text-2xl font-semibold mb-4">Adjust Feature Weights</h2>
<div class="feature-slider mb-2">
<label for="danceability" class="block text-gray-700 text-sm font-bold mb-2">Danceability: <span id="danceability-value">0.5</span></label>
<input type="range" id="danceability" min="0" max="1" step="0.1" value="0.5" class="w-full">
</div>
<div class="feature-slider mb-2">
<label for="energy" class="block text-gray-700 text-sm font-bold mb-2">Energy: <span id="energy-value">0.5</span></label>
<input type="range" id="energy" min="0" max="1" step="0.1" value="0.5" class="w-full">
</div>
<div class="feature-slider mb-2">
<label for="valence" class="block text-gray-700 text-sm font-bold mb-2">Valence: <span id="valence-value">0.5</span></label>
<input type="range" id="valence" min="0" max="1" step="0.1" value="0.5" class="w-full">
</div>
<div class="feature-slider mb-2">
<label for="liveness" class="block text-gray-700 text-sm font-bold mb-2">Liveness: <span id="liveness-value">0.5</span></label>
<input type="range" id="liveness" min="0" max="1" step="0.1" value="0.5" class="w-full">
</div>
<div class="feature-slider mb-2">
<label for="acousticness" class="block text-gray-700 text-sm font-bold mb-2">Acousticness: <span id="acousticness-value">0.5</span></label>
<input type="range" id="acousticness" min="0" max="1" step="0.1" value="0.5" class="w-full">
</div>
<div class="feature-slider mb-2">
<label for="instrumentalness" class="block text-gray-700 text-sm font-bold mb-2">Instrumentalness: <span id="instrumentalness-value">0.5</span></label>
<input type="range" id="instrumentalness" min="0" max="1" step="0.1" value="0.5" class="w-full">
</div>
</section>
<section id="exclusions" class="mb-8">
<h2 class="text-2xl font-semibold mb-4">Exclusions</h2>
<div class="mb-4">
<label for="excludeArtists" class="block text-gray-700 text-sm font-bold mb-2">Exclude Artists:</label>
<input type="text" id="excludeArtists" placeholder="Enter artist names, separated by commas" class="w-full p-2 border rounded">
</div>
<div class="mb-4">
<label for="excludeGenres" class="block text-gray-700 text-sm font-bold mb-2">Exclude Genres:</label>
<input type="text" id="excludeGenres" placeholder="Enter genres, separated by commas" class="w-full p-2 border rounded">
</div>
</section>
</main>
<div id="loader" class="loader hidden ease-linear rounded-full border-8 border-t-8 border-gray-200 h-32 w-32 mx-auto"></div>
<section id="audioFeaturesChart" class="mb-8 hidden">
<h2 class="text-2xl font-semibold mb-4">Audio Features Comparison</h2>
<canvas id="radarChart" aria-label="Audio Features Chart" role="img"></canvas>
</section>
<div id="recommendations" class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4"></div>
<section id="createPlaylistSection" class="mt-8 hidden">
<h2 class="text-2xl font-semibold mb-4">Create Playlist</h2>
<input type="text" id="playlistName" placeholder="Enter playlist name" class="w-full p-2 border rounded mb-4" aria-label="Playlist Name">
<button id="createPlaylistBtn" class="bg-green-500 text-white px-4 py-2 rounded w-full" aria-label="Create Playlist">Create Playlist</button>
</section>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const singleTrackBtn = document.getElementById('singleTrackBtn');
const playlistBtn = document.getElementById('playlistBtn');
const recentlyPlayedBtn = document.getElementById('recentlyPlayedBtn');
const inputField = document.getElementById('inputField');
const playlistDropdown = document.getElementById('playlistDropdown');
const playlistSelect = document.getElementById('playlistSelect');
const getRecommendationsBtn = document.getElementById('getRecommendationsBtn');
const recommendationsContainer = document.getElementById('recommendations');
const createPlaylistSection = document.getElementById('createPlaylistSection');
const createPlaylistBtn = document.getElementById('createPlaylistBtn');
const audioFeaturesChart = document.getElementById('audioFeaturesChart');
let radarChart;
let currentInputType = 'track';
let recommendedTracks = [];
let selectedMood = '';
document.querySelectorAll('.mood-btn').forEach(btn => {
btn.addEventListener('click', function() {
selectedMood = this.dataset.mood;
document.querySelectorAll('.mood-btn').forEach(b => b.classList.remove('ring-2', 'ring-offset-2'));
this.classList.add('ring-2', 'ring-offset-2');
});
});
singleTrackBtn.addEventListener('click', () => setInputType('track'));
playlistBtn.addEventListener('click', () => setInputType('playlist'));
recentlyPlayedBtn.addEventListener('click', () => setInputType('recent'));
function setInputType(type) {
currentInputType = type;
inputField.classList.toggle('hidden', type !== 'track');
playlistDropdown.classList.toggle('hidden', type !== 'playlist');
if (type === 'playlist') {
fetchPlaylists();
}
}
function fetchPlaylists() {
fetch('/user-playlists')
.then(response => response.json())
.then(data => {
playlistSelect.innerHTML = '<option value="">Select a playlist</option>';
data.items.forEach(playlist => {
const option = document.createElement('option');
option.value = playlist.id;
option.textContent = playlist.name;
playlistSelect.appendChild(option);
});
})
.catch(error => console.error('Error fetching playlists:', error));
}
getRecommendationsBtn.addEventListener('click', getRecommendations);
function getRecommendations() {
const loader = document.getElementById('loader');
loader.classList.remove('hidden');
recommendationsContainer.innerHTML = '';
audioFeaturesChart.classList.add('hidden');
const inputId = currentInputType === 'track' ? document.getElementById('trackInput').value :
currentInputType === 'playlist' ? playlistSelect.value : '';
const requestBody = {
input_type: currentInputType,
input_id: inputId,
weights: {
danceability: parseFloat(document.getElementById('danceability').value),
energy: parseFloat(document.getElementById('energy').value),
valence: parseFloat(document.getElementById('valence').value),
liveness: parseFloat(document.getElementById('liveness').value),
acousticness: parseFloat(document.getElementById('acousticness').value),
instrumentalness: parseFloat(document.getElementById('instrumentalness').value)
},
exclude_artists: document.getElementById('excludeArtists').value.split(',').map(artist => artist.trim()),
exclude_genres: document.getElementById('excludeGenres').value.split(',').map(genre => genre.trim()),
num_recommendations: parseInt(document.getElementById('numRecommendations').value),
use_deep_learning: document.getElementById('useDeepLearning').checked,
mood: selectedMood
};
fetch('/recommend', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(requestBody),
})
.then(response => response.json())
.then(data => {
loader.classList.add('hidden');
recommendedTracks = data;
displayRecommendations(data);
createPlaylistSection.classList.remove('hidden');
updateAudioFeaturesChart(data);
})
.catch(error => {
console.error('Error:', error);
loader.classList.add('hidden');
});
}
function displayRecommendations(tracks) {
recommendationsContainer.innerHTML = '';
tracks.forEach(track => {
const trackElement = document.createElement('div');
trackElement.className = 'bg-white rounded-lg shadow-md p-4 flex items-center';
const albumImage = track.album && track.album.images && track.album.images.length > 0
? track.album.images[1].url // Use the second image (usually 300x300)
: 'path/to/default/image.jpg'; // Provide a default image path
trackElement.innerHTML = `
<img src="${albumImage}" alt="${track.name} album cover" class="w-20 h-20 object-cover mr-4">
<div>
<h3 class="font-bold">${track.name}</h3>
<p>${track.artist}</p>
<p>Genres: ${track.genres}</p>
<audio controls class="w-full mt-2">
<source src="${track.preview_url}" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
</div>
`;
recommendationsContainer.appendChild(trackElement);
});
}
function updateAudioFeaturesChart(tracks) {
const features = ['danceability', 'energy', 'valence', 'acousticness', 'instrumentalness', 'liveness'];
const averageFeatures = features.reduce((acc, feature) => {
acc[feature] = tracks.reduce((sum, track) => sum + (track[feature] || 0), 0) / tracks.length;
return acc;
}, {});
const chartData = {
labels: features,
datasets: [{
label: 'Average Audio Features',
data: features.map(feature => averageFeatures[feature]),
fill: true,
backgroundColor: 'rgba(54, 162, 235, 0.2)',
borderColor: 'rgb(54, 162, 235)',
pointBackgroundColor: 'rgb(54, 162, 235)',
pointBorderColor: '#fff',
pointHoverBackgroundColor: '#fff',
pointHoverBorderColor: 'rgb(54, 162, 235)'
}]
};
if (radarChart) {
radarChart.destroy();
}
const ctx = document.getElementById('radarChart').getContext('2d');
radarChart = new Chart(ctx, {
type: 'radar',
data: chartData,
options: {
elements: {
line: {
borderWidth: 3
}
},
scales: {
r: {
angleLines: {
display: false
},
suggestedMin: 0,
suggestedMax: 1
}
}
}
});
audioFeaturesChart.classList.remove('hidden');
}
createPlaylistBtn.addEventListener('click', createPlaylist);
function createPlaylist() {
const playlistName = document.getElementById('playlistName').value;
const trackIds = recommendedTracks.map(track => track.id);
fetch('/create-playlist', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
name: playlistName,
tracks: trackIds
}),
})
.then(response => response.json())
.then(data => {
alert(`Playlist "${playlistName}" created successfully!`);
})
.catch(error => {
console.error('Error:', error);
alert('An error occurred while creating the playlist.');
});
}
// Update slider values
const sliders = document.querySelectorAll('input[type="range"]');
sliders.forEach(slider => {
const output = document.getElementById(`${slider.id}-value`);
slider.addEventListener('input', function() {
output.textContent = this.value;
});
});
// Number of recommendations slider
const numRecommendationsSlider = document.getElementById('numRecommendations');
const numRecommendationsValue = document.getElementById('numRecommendationsValue');
numRecommendationsSlider.addEventListener('input', function() {
numRecommendationsValue.textContent = this.value;
});
// Check authentication status on page load
fetch('/check-auth')
.then(response => response.json())
.then(data => {
if (!data.authenticated) {
window.location.href = '/login';
}
})
.catch(error => console.error('Error checking authentication:', error));
});
</script>
</body>
</html>