Spaces:
Sleeping
Sleeping
- adjust treshold suggestion song
Browse files- backend/app.py +2 -9
backend/app.py
CHANGED
|
@@ -29,14 +29,12 @@ app.add_middleware(
|
|
| 29 |
)
|
| 30 |
|
| 31 |
|
| 32 |
-
# ๐น Load Emotion Detection Model
|
| 33 |
emotion_classifier = pipeline(
|
| 34 |
"zero-shot-classification",
|
| 35 |
model="MarfinF/marfin_emotion",
|
| 36 |
framework="pt"
|
| 37 |
)
|
| 38 |
|
| 39 |
-
# ๐น Emotion-to-Mood Mapping
|
| 40 |
emotion_to_mood = {
|
| 41 |
"senang": "happy",
|
| 42 |
"sedih": "sad",
|
|
@@ -45,7 +43,6 @@ emotion_to_mood = {
|
|
| 45 |
"cinta": "romantic"
|
| 46 |
}
|
| 47 |
|
| 48 |
-
# ๐น WebSocket Clients
|
| 49 |
clients = {}
|
| 50 |
chat_history = deque(maxlen=4)
|
| 51 |
|
|
@@ -67,7 +64,6 @@ genre_to_song = {
|
|
| 67 |
"chill": ["https://sounds.pond5.com/fall-chill-music-088328584_nw_prev.m4a"]
|
| 68 |
}
|
| 69 |
|
| 70 |
-
# ๐น Detect Emotion
|
| 71 |
def detect_emotion(text):
|
| 72 |
labels = ["takut", "marah", "sedih", "senang", "cinta"]
|
| 73 |
result = emotion_classifier(text, candidate_labels=labels)
|
|
@@ -75,7 +71,6 @@ def detect_emotion(text):
|
|
| 75 |
top_scores = result['scores'][0]
|
| 76 |
return top_emotion, top_scores
|
| 77 |
|
| 78 |
-
# ๐น Get Music Recommendations
|
| 79 |
def get_recommendations_by_mood(mood):
|
| 80 |
genre_folder = mood_to_genre.get(mood, "chill")
|
| 81 |
songs = genre_to_song.get(genre_folder, [])
|
|
@@ -104,7 +99,7 @@ async def periodic_recommendation():
|
|
| 104 |
while True:
|
| 105 |
user_list = list(clients.keys())
|
| 106 |
if len(user_list) >= 2:
|
| 107 |
-
await asyncio.sleep(
|
| 108 |
if clients: # Only run if someone is connected
|
| 109 |
if len(chat_history) > 0:
|
| 110 |
# 1. Detect emotion dan ambil (label, score)
|
|
@@ -146,12 +141,10 @@ async def periodic_recommendation():
|
|
| 146 |
await asyncio.sleep(2)
|
| 147 |
await broadcast_user_list()
|
| 148 |
|
| 149 |
-
# ๐น Start periodic task
|
| 150 |
@app.on_event("startup")
|
| 151 |
async def start_recommender():
|
| 152 |
asyncio.create_task(periodic_recommendation())
|
| 153 |
|
| 154 |
-
# ๐น WebSocket Endpoint
|
| 155 |
@app.websocket("/chat/{username}")
|
| 156 |
async def chat_endpoint(websocket: WebSocket, username: str):
|
| 157 |
await websocket.accept()
|
|
@@ -180,7 +173,7 @@ async def chat_endpoint(websocket: WebSocket, username: str):
|
|
| 180 |
del clients[username]
|
| 181 |
await broadcast_user_list()
|
| 182 |
|
| 183 |
-
@app.get("/
|
| 184 |
def read_root():
|
| 185 |
print("frontend")
|
| 186 |
return FileResponse("frontend/index.html")
|
|
|
|
| 29 |
)
|
| 30 |
|
| 31 |
|
|
|
|
| 32 |
emotion_classifier = pipeline(
|
| 33 |
"zero-shot-classification",
|
| 34 |
model="MarfinF/marfin_emotion",
|
| 35 |
framework="pt"
|
| 36 |
)
|
| 37 |
|
|
|
|
| 38 |
emotion_to_mood = {
|
| 39 |
"senang": "happy",
|
| 40 |
"sedih": "sad",
|
|
|
|
| 43 |
"cinta": "romantic"
|
| 44 |
}
|
| 45 |
|
|
|
|
| 46 |
clients = {}
|
| 47 |
chat_history = deque(maxlen=4)
|
| 48 |
|
|
|
|
| 64 |
"chill": ["https://sounds.pond5.com/fall-chill-music-088328584_nw_prev.m4a"]
|
| 65 |
}
|
| 66 |
|
|
|
|
| 67 |
def detect_emotion(text):
|
| 68 |
labels = ["takut", "marah", "sedih", "senang", "cinta"]
|
| 69 |
result = emotion_classifier(text, candidate_labels=labels)
|
|
|
|
| 71 |
top_scores = result['scores'][0]
|
| 72 |
return top_emotion, top_scores
|
| 73 |
|
|
|
|
| 74 |
def get_recommendations_by_mood(mood):
|
| 75 |
genre_folder = mood_to_genre.get(mood, "chill")
|
| 76 |
songs = genre_to_song.get(genre_folder, [])
|
|
|
|
| 99 |
while True:
|
| 100 |
user_list = list(clients.keys())
|
| 101 |
if len(user_list) >= 2:
|
| 102 |
+
await asyncio.sleep(60)
|
| 103 |
if clients: # Only run if someone is connected
|
| 104 |
if len(chat_history) > 0:
|
| 105 |
# 1. Detect emotion dan ambil (label, score)
|
|
|
|
| 141 |
await asyncio.sleep(2)
|
| 142 |
await broadcast_user_list()
|
| 143 |
|
|
|
|
| 144 |
@app.on_event("startup")
|
| 145 |
async def start_recommender():
|
| 146 |
asyncio.create_task(periodic_recommendation())
|
| 147 |
|
|
|
|
| 148 |
@app.websocket("/chat/{username}")
|
| 149 |
async def chat_endpoint(websocket: WebSocket, username: str):
|
| 150 |
await websocket.accept()
|
|
|
|
| 173 |
del clients[username]
|
| 174 |
await broadcast_user_list()
|
| 175 |
|
| 176 |
+
@app.get("/mp")
|
| 177 |
def read_root():
|
| 178 |
print("frontend")
|
| 179 |
return FileResponse("frontend/index.html")
|