MarfinF commited on
Commit
34c4923
Ā·
1 Parent(s): d0d3c43

- adjust with the model marifn_emotion

Browse files
Files changed (2) hide show
  1. backend/app.py +29 -24
  2. frontend/index.html +3 -2
backend/app.py CHANGED
@@ -37,47 +37,50 @@ emotion_classifier = pipeline(
37
  framework="pt"
38
  )
39
 
40
- emotion_to_mood = {
41
- "senang": "happy",
42
- "sedih": "sad",
43
- "marah": "excited",
44
- "takut": "relaxed",
45
- "cinta": "romantic"
46
- }
47
-
48
  clients = {}
49
  chat_history = deque(maxlen=4)
50
 
51
  mood_to_genre = {
52
- "happy": "pop",
53
- "sad": "acoustic",
54
- "excited": "rock",
55
- "intense": "cinematic",
56
- "romantic": "rnb",
57
- "chill": "chill"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  }
59
 
60
  genre_to_song = {
61
  "pop": ["https://sounds.pond5.com/inspirational-motivational-uplifting-acoustic-positive-music-102770115_nw_prev.m4a"],
62
  "acoustic": ["https://sounds.pond5.com/sad-piano-music-063450274_nw_prev.m4a"],
63
  "rock": ["https://sounds.pond5.com/powerful-gritty-action-extreme-rock-music-136693652_nw_prev.m4a"],
64
- "intense": ["https://sounds.pond5.com/epic-action-hybrid-trailer-instrum-music-072727124_nw_prev.m4a"],
65
  "romantic": ["https://sounds.pond5.com/bloom-sweet-tender-delicate-romantic-music-158563013_nw_prev.m4a"],
66
  "chill": ["https://sounds.pond5.com/fall-chill-music-088328584_nw_prev.m4a"]
67
  }
68
 
69
  def detect_emotion(text):
70
- labels = ["takut", "marah", "sedih", "senang", "cinta"]
71
  result = emotion_classifier(text, candidate_labels=labels)
72
  top_emotion = result['labels'][0]
73
  top_scores = result['scores'][0]
74
  return top_emotion, top_scores
75
 
76
- def get_recommendations_by_mood(mood):
77
- genre_folder = mood_to_genre.get(mood, "chill")
78
- songs = genre_to_song.get(genre_folder, [])
79
 
80
- # List and shuffle songs
81
  random.shuffle(songs)
82
  return songs[:3] # Return top 3 shuffled songs
83
 
@@ -138,14 +141,16 @@ async def periodic_recommendation():
138
  # 5. Dominant emotion
139
  most_common_emotion = max(softmax_result, key=lambda x: x[1])[0]
140
  print("Dominant Emotion:", most_common_emotion)
141
- mood = emotion_to_mood.get(most_common_emotion, "chill")
142
- music_recommendations = get_recommendations_by_mood(mood)
 
143
  else:
144
- music_recommendations = ["chill"] # default if no chat
 
145
 
146
  recommendation_response = {
147
  "recommendations": music_recommendations,
148
- "genre": mood_to_genre.get(mood, "pop")
149
  }
150
 
151
  for client in clients.values():
 
37
  framework="pt"
38
  )
39
 
 
 
 
 
 
 
 
 
40
  clients = {}
41
  chat_history = deque(maxlen=4)
42
 
43
  mood_to_genre = {
44
+ "senang": {
45
+ "genre": "pop",
46
+ "word": "Poppin pops!"
47
+ },
48
+ "sedih": {
49
+ "genre": "acoustic",
50
+ "word": "Playing acoustic"
51
+ },
52
+ "marah": {
53
+ "genre": "rock",
54
+ "word": "Rock 'n play!!"
55
+ },
56
+ "cinta": {
57
+ "genre": "romantic",
58
+ "word": "Playing romantic"
59
+ },
60
+ "chill": {
61
+ "genre": "chill",
62
+ "word": "Chill!"
63
+ }
64
  }
65
 
66
  genre_to_song = {
67
  "pop": ["https://sounds.pond5.com/inspirational-motivational-uplifting-acoustic-positive-music-102770115_nw_prev.m4a"],
68
  "acoustic": ["https://sounds.pond5.com/sad-piano-music-063450274_nw_prev.m4a"],
69
  "rock": ["https://sounds.pond5.com/powerful-gritty-action-extreme-rock-music-136693652_nw_prev.m4a"],
 
70
  "romantic": ["https://sounds.pond5.com/bloom-sweet-tender-delicate-romantic-music-158563013_nw_prev.m4a"],
71
  "chill": ["https://sounds.pond5.com/fall-chill-music-088328584_nw_prev.m4a"]
72
  }
73
 
74
  def detect_emotion(text):
75
+ labels = ["marah", "sedih", "senang", "cinta"]
76
  result = emotion_classifier(text, candidate_labels=labels)
77
  top_emotion = result['labels'][0]
78
  top_scores = result['scores'][0]
79
  return top_emotion, top_scores
80
 
81
+ def get_recommendations_by_mood(genre):
82
+ songs = genre_to_song.get(genre, [])
 
83
 
 
84
  random.shuffle(songs)
85
  return songs[:3] # Return top 3 shuffled songs
86
 
 
141
  # 5. Dominant emotion
142
  most_common_emotion = max(softmax_result, key=lambda x: x[1])[0]
143
  print("Dominant Emotion:", most_common_emotion)
144
+ music = mood_to_genre.get(most_common_emotion, mood_to_genre["chill"])
145
+ music_recommendations = get_recommendations_by_mood(music["genre"])
146
+ word = music["word"]
147
  else:
148
+ music_recommendations = get_recommendations_by_mood("chill")
149
+ word = "Chill!"
150
 
151
  recommendation_response = {
152
  "recommendations": music_recommendations,
153
+ "genre": word
154
  }
155
 
156
  for client in clients.values():
frontend/index.html CHANGED
@@ -178,7 +178,8 @@
178
  }
179
  console.log("location host")
180
  console.log(location.host)
181
- ws = new WebSocket(`${location.protocol === 'https:' ? 'wss' : 'ws'}://${location.host}/chat/${username}`);
 
182
 
183
  document.getElementById("music-player").addEventListener("play", () => {
184
  musicStatus = "playing"
@@ -252,7 +253,7 @@
252
  };
253
  let recommendationDiv = document.createElement("div");
254
  recommendationDiv.classList.add("recommendation-message");
255
- recommendationDiv.innerHTML = `šŸŽµ Recommended Song: "${recommendationSong.genre}" šŸŽ¶`;
256
  chatBox.appendChild(recommendationDiv);
257
  console.log("recommendation song")
258
  console.log(recommendationSong)
 
178
  }
179
  console.log("location host")
180
  console.log(location.host)
181
+ // ws = new WebSocket(`${location.protocol === 'https:' ? 'wss' : 'ws'}://${location.host}/chat/${username}`);
182
+ ws = new WebSocket(`ws://localhost:8000/chat/${username}`);
183
 
184
  document.getElementById("music-player").addEventListener("play", () => {
185
  musicStatus = "playing"
 
253
  };
254
  let recommendationDiv = document.createElement("div");
255
  recommendationDiv.classList.add("recommendation-message");
256
+ recommendationDiv.innerHTML = `šŸŽµ ${recommendationSong.genre}`;
257
  chatBox.appendChild(recommendationDiv);
258
  console.log("recommendation song")
259
  console.log(recommendationSong)