Spaces:
Sleeping
Sleeping
- update BE & FE adjust music
Browse files- backend/app.py +28 -14
- frontend/index.html +24 -8
backend/app.py
CHANGED
@@ -9,6 +9,8 @@ from collections import deque
|
|
9 |
from collections import defaultdict
|
10 |
import math
|
11 |
import sys
|
|
|
|
|
12 |
|
13 |
sys.path.append(".")
|
14 |
|
@@ -37,6 +39,15 @@ emotion_to_mood = {
|
|
37 |
clients = {}
|
38 |
chat_history = deque(maxlen=4)
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
# πΉ Detect Emotion
|
41 |
def detect_emotion(text):
|
42 |
labels = ["takut", "marah", "sedih", "senang", "cinta"]
|
@@ -47,16 +58,20 @@ def detect_emotion(text):
|
|
47 |
|
48 |
# πΉ Get Music Recommendations
|
49 |
def get_recommendations_by_mood(mood):
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
60 |
|
61 |
def softmax(scores):
|
62 |
exp_scores = [math.exp(score) for score in scores]
|
@@ -78,7 +93,7 @@ async def periodic_recommendation():
|
|
78 |
while True:
|
79 |
user_list = list(clients.keys())
|
80 |
if len(user_list) >= 2:
|
81 |
-
await asyncio.sleep(
|
82 |
if clients: # Only run if someone is connected
|
83 |
if len(chat_history) > 0:
|
84 |
# 1. Detect emotion dan ambil (label, score)
|
@@ -110,9 +125,8 @@ async def periodic_recommendation():
|
|
110 |
music_recommendations = ["chill"] # default if no chat
|
111 |
|
112 |
recommendation_response = {
|
113 |
-
"
|
114 |
-
"
|
115 |
-
"recommendations": music_recommendations
|
116 |
}
|
117 |
|
118 |
for client in clients.values():
|
|
|
9 |
from collections import defaultdict
|
10 |
import math
|
11 |
import sys
|
12 |
+
import random
|
13 |
+
import os
|
14 |
|
15 |
sys.path.append(".")
|
16 |
|
|
|
39 |
clients = {}
|
40 |
chat_history = deque(maxlen=4)
|
41 |
|
42 |
+
mood_to_genre = {
|
43 |
+
"happy": "pop",
|
44 |
+
"sad": "acoustic",
|
45 |
+
"excited": "rock",
|
46 |
+
"intense": "cinematic",
|
47 |
+
"romantic": "rnb",
|
48 |
+
"chill": "chill"
|
49 |
+
}
|
50 |
+
|
51 |
# πΉ Detect Emotion
|
52 |
def detect_emotion(text):
|
53 |
labels = ["takut", "marah", "sedih", "senang", "cinta"]
|
|
|
58 |
|
59 |
# πΉ Get Music Recommendations
|
60 |
def get_recommendations_by_mood(mood):
|
61 |
+
genre_folder = mood_to_genre.get(mood, "pop")
|
62 |
+
folder_path = f"../music/{genre_folder}"
|
63 |
+
print("folder path")
|
64 |
+
print(folder_path)
|
65 |
+
|
66 |
+
# Check if folder exists
|
67 |
+
if not os.path.exists(folder_path):
|
68 |
+
return []
|
69 |
+
print("folder exist")
|
70 |
+
|
71 |
+
# List and shuffle songs
|
72 |
+
songs = [f"../music/{genre_folder}/{song}" for song in os.listdir(folder_path) if song.endswith(".mp3")]
|
73 |
+
random.shuffle(songs)
|
74 |
+
return songs[:3] # Return top 3 shuffled songs
|
75 |
|
76 |
def softmax(scores):
|
77 |
exp_scores = [math.exp(score) for score in scores]
|
|
|
93 |
while True:
|
94 |
user_list = list(clients.keys())
|
95 |
if len(user_list) >= 2:
|
96 |
+
await asyncio.sleep(10)
|
97 |
if clients: # Only run if someone is connected
|
98 |
if len(chat_history) > 0:
|
99 |
# 1. Detect emotion dan ambil (label, score)
|
|
|
125 |
music_recommendations = ["chill"] # default if no chat
|
126 |
|
127 |
recommendation_response = {
|
128 |
+
"recommendations": music_recommendations,
|
129 |
+
"genre": mood_to_genre.get(mood, "pop")
|
|
|
130 |
}
|
131 |
|
132 |
for client in clients.values():
|
frontend/index.html
CHANGED
@@ -139,7 +139,7 @@
|
|
139 |
</head>
|
140 |
|
141 |
<body>
|
142 |
-
<h1>π΅ Smart
|
143 |
|
144 |
<!-- Chat Box -->
|
145 |
<div id="chat-container">
|
@@ -165,7 +165,10 @@
|
|
165 |
let ws;
|
166 |
let username = "";
|
167 |
let musicStatus = "";
|
168 |
-
let delayedMusic =
|
|
|
|
|
|
|
169 |
|
170 |
function connectChat() {
|
171 |
username = document.getElementById("username").value.trim();
|
@@ -181,7 +184,7 @@
|
|
181 |
});
|
182 |
document.getElementById("music-player").addEventListener("ended", () => {
|
183 |
musicStatus = "stop"
|
184 |
-
if (delayedMusic != "") playCurrentMusic(delayedMusic)
|
185 |
});
|
186 |
|
187 |
ws.onmessage = function (event) {
|
@@ -201,9 +204,17 @@
|
|
201 |
|
202 |
if (data.recommendations) {
|
203 |
if (musicStatus != "playing") {
|
204 |
-
|
|
|
|
|
|
|
|
|
|
|
205 |
} else {
|
206 |
-
delayedMusic =
|
|
|
|
|
|
|
207 |
}
|
208 |
} else {
|
209 |
let messageDiv = document.createElement("div");
|
@@ -234,12 +245,17 @@
|
|
234 |
|
235 |
function playCurrentMusic(recommendationSong) {
|
236 |
const chatBox = document.getElementById("chat-box");
|
237 |
-
delayedMusic =
|
|
|
|
|
|
|
238 |
let recommendationDiv = document.createElement("div");
|
239 |
recommendationDiv.classList.add("recommendation-message");
|
240 |
-
recommendationDiv.innerHTML = `π΅ Recommended Song: "${recommendationSong}" πΆ`;
|
241 |
chatBox.appendChild(recommendationDiv);
|
242 |
-
|
|
|
|
|
243 |
document.getElementById("music-player").play()
|
244 |
}
|
245 |
|
|
|
139 |
</head>
|
140 |
|
141 |
<body>
|
142 |
+
<h1>π΅ Smart Music Rekomend</h1>
|
143 |
|
144 |
<!-- Chat Box -->
|
145 |
<div id="chat-container">
|
|
|
165 |
let ws;
|
166 |
let username = "";
|
167 |
let musicStatus = "";
|
168 |
+
let delayedMusic = {
|
169 |
+
song: "",
|
170 |
+
genre: ""
|
171 |
+
};
|
172 |
|
173 |
function connectChat() {
|
174 |
username = document.getElementById("username").value.trim();
|
|
|
184 |
});
|
185 |
document.getElementById("music-player").addEventListener("ended", () => {
|
186 |
musicStatus = "stop"
|
187 |
+
if (delayedMusic.song != "") playCurrentMusic(delayedMusic)
|
188 |
});
|
189 |
|
190 |
ws.onmessage = function (event) {
|
|
|
204 |
|
205 |
if (data.recommendations) {
|
206 |
if (musicStatus != "playing") {
|
207 |
+
console.log("data")
|
208 |
+
console.log(data)
|
209 |
+
playCurrentMusic({
|
210 |
+
song: data.recommendations[0],
|
211 |
+
genre: data.genre
|
212 |
+
})
|
213 |
} else {
|
214 |
+
delayedMusic = {
|
215 |
+
song: data.recommendations[0],
|
216 |
+
genre: data.genre
|
217 |
+
}
|
218 |
}
|
219 |
} else {
|
220 |
let messageDiv = document.createElement("div");
|
|
|
245 |
|
246 |
function playCurrentMusic(recommendationSong) {
|
247 |
const chatBox = document.getElementById("chat-box");
|
248 |
+
delayedMusic = {
|
249 |
+
song: "",
|
250 |
+
genre: ""
|
251 |
+
};
|
252 |
let recommendationDiv = document.createElement("div");
|
253 |
recommendationDiv.classList.add("recommendation-message");
|
254 |
+
recommendationDiv.innerHTML = `π΅ Recommended Song: "${recommendationSong.genre}" πΆ`;
|
255 |
chatBox.appendChild(recommendationDiv);
|
256 |
+
console.log("recommendation song")
|
257 |
+
console.log(recommendationSong)
|
258 |
+
document.getElementById("music-player").src = recommendationSong.song
|
259 |
document.getElementById("music-player").play()
|
260 |
}
|
261 |
|