Update app.py
Browse files
app.py
CHANGED
|
@@ -1251,68 +1251,113 @@ class ChatOrRecommendRequest(BaseModel):
|
|
| 1251 |
# (5) μλ λΆκΈ° μλν¬μΈνΈ
|
| 1252 |
@app.post("/chat_or_recommend")
|
| 1253 |
def chat_or_recommend(req: ChatOrRecommendRequest):
|
| 1254 |
-
|
| 1255 |
-
|
| 1256 |
-
|
| 1257 |
-
|
| 1258 |
-
|
| 1259 |
-
|
| 1260 |
-
|
| 1261 |
-
|
| 1262 |
-
|
| 1263 |
-
|
| 1264 |
-
is_recommend_query = any(keyword in
|
| 1265 |
-
|
| 1266 |
-
|
| 1267 |
-
|
| 1268 |
-
|
| 1269 |
-
if mode not in ["emotion", "rational"]:
|
| 1270 |
-
raise HTTPException(
|
| 1271 |
-
status_code=400,
|
| 1272 |
-
detail="modeλ 'emotion' λλ 'rational'μ΄μ΄μΌ ν©λλ€."
|
| 1273 |
-
)
|
| 1274 |
-
chatbot_msg = chat_response(req.user_input, mode=mode)
|
| 1275 |
-
|
| 1276 |
-
# μ°μΈλκ° "μλ΄ κΆμ₯"μ΄ μλ κ²½μ°μλ§ μΆμ² λ©μμ§ μμ±
|
| 1277 |
-
recommendation_msg = ""
|
| 1278 |
-
recommendations_list = []
|
| 1279 |
-
if depression_label != "μλ΄ κΆμ₯" and req.hobby and is_recommend_query:
|
| 1280 |
user_profile = {
|
| 1281 |
"extroversion": req.extroversion or "",
|
| 1282 |
"feeling_thinking": req.feeling_thinking or "",
|
| 1283 |
-
"hobby": req.hobby,
|
| 1284 |
"detail_hobby": req.detail_hobby or [],
|
| 1285 |
}
|
|
|
|
| 1286 |
top_items = recommend_content_based(user_profile, top_n=5)
|
| 1287 |
-
|
|
|
|
|
|
|
| 1288 |
for i, (item, score) in enumerate(top_items, start=1):
|
| 1289 |
-
|
|
|
|
| 1290 |
"item_id": item["item_id"],
|
| 1291 |
"title": item["title"],
|
| 1292 |
"desc": item["desc"],
|
| 1293 |
"personality": item["personality"],
|
| 1294 |
"score": round(score, 4)
|
| 1295 |
})
|
| 1296 |
-
|
| 1297 |
-
|
| 1298 |
-
|
| 1299 |
-
|
| 1300 |
-
|
| 1301 |
-
|
| 1302 |
-
|
| 1303 |
-
if recommendation_msg:
|
| 1304 |
-
final_response += "" + recommendation_msg
|
| 1305 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1306 |
|
| 1307 |
-
|
| 1308 |
-
"mode": "chat+recommend" if recommendation_msg else "chat",
|
| 1309 |
-
"response": final_response,
|
| 1310 |
-
"depression_label": depression_label
|
| 1311 |
-
}
|
| 1312 |
-
if recommendation_msg:
|
| 1313 |
-
response_dict["recommendations"] = recommendations_list
|
| 1314 |
|
| 1315 |
-
return response_dict
|
| 1316 |
|
| 1317 |
def run_fastapi():
|
| 1318 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1251 |
# (5) μλ λΆκΈ° μλν¬μΈνΈ
|
| 1252 |
@app.post("/chat_or_recommend")
|
| 1253 |
def chat_or_recommend(req: ChatOrRecommendRequest):
|
| 1254 |
+
"""
|
| 1255 |
+
1) λ§μ½ 'μΆμ²' ν€μλκ° ν¬ν¨λμ΄ μλ€λ©΄ β μΆμ²λ§(return)
|
| 1256 |
+
2) μλ€λ©΄ β μ°μΈλΆμ + μ±λ΄ μλ΅
|
| 1257 |
+
2-1) μ°μΈλκ° 'μλ΄ κΆμ₯'μ΄λ©΄ β μλ΄μλ΄ + μ±λ΄λ§
|
| 1258 |
+
2-2) μ°μΈλκ° μλ΄ κΆμ₯μ΄ μλλ©΄ β μλ΄μλ΄(νμ μ) + μ±λ΄ + (μ·¨λ―Έ μ 보 μμΌλ©΄) μΆμ²
|
| 1259 |
+
"""
|
| 1260 |
+
user_input = req.user_input
|
| 1261 |
+
mode = req.mode.lower() # "emotion" or "rational"
|
| 1262 |
+
|
| 1263 |
+
# (A) 'μΆμ²' ν€μλ ν¬ν¨ μ¬λΆ νμΈ
|
| 1264 |
+
is_recommend_query = any(keyword in user_input for keyword in RECOMMEND_KEYWORDS)
|
| 1265 |
+
|
| 1266 |
+
# (B) 'μΆμ²' ν€μλκ° μμΌλ©΄ β μΆμ²λ§ μν (μ±λ΄/μ°μΈλΆμ X)
|
| 1267 |
+
if is_recommend_query:
|
| 1268 |
+
# 1) μ¬μ©μ νλ‘ν μμ±
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1269 |
user_profile = {
|
| 1270 |
"extroversion": req.extroversion or "",
|
| 1271 |
"feeling_thinking": req.feeling_thinking or "",
|
| 1272 |
+
"hobby": req.hobby or [],
|
| 1273 |
"detail_hobby": req.detail_hobby or [],
|
| 1274 |
}
|
| 1275 |
+
# 2) μΆμ² λ‘μ§
|
| 1276 |
top_items = recommend_content_based(user_profile, top_n=5)
|
| 1277 |
+
|
| 1278 |
+
rec_results = []
|
| 1279 |
+
recommendation_msg = "μμ²νμ μΆμ² λͺ©λ‘μ
λλ€.\n\n"
|
| 1280 |
for i, (item, score) in enumerate(top_items, start=1):
|
| 1281 |
+
clean_desc = re.sub(r"\(.*?\)", "", item["desc"]).strip()
|
| 1282 |
+
rec_results.append({
|
| 1283 |
"item_id": item["item_id"],
|
| 1284 |
"title": item["title"],
|
| 1285 |
"desc": item["desc"],
|
| 1286 |
"personality": item["personality"],
|
| 1287 |
"score": round(score, 4)
|
| 1288 |
})
|
| 1289 |
+
recommendation_msg += f"{i}. **{item['title']}** - {clean_desc}\n"
|
| 1290 |
+
|
| 1291 |
+
return {
|
| 1292 |
+
"mode": "recommend_only",
|
| 1293 |
+
"response": recommendation_msg,
|
| 1294 |
+
"recommendations": rec_results
|
| 1295 |
+
}
|
|
|
|
|
|
|
| 1296 |
|
| 1297 |
+
# (C) 'μΆμ²' ν€μλκ° μλ κ²½μ°
|
| 1298 |
+
else:
|
| 1299 |
+
# 1) μ°μΈ λΆμ
|
| 1300 |
+
depression_score, depression_label = predict_depression(user_input)
|
| 1301 |
+
|
| 1302 |
+
# 2) μλ΄ μλ΄ λ©μμ§ μ€λΉ
|
| 1303 |
+
counseling_response = ""
|
| 1304 |
+
if depression_label == "μλ΄ κΆμ₯":
|
| 1305 |
+
counseling_response = (
|
| 1306 |
+
"μ
λ ₯νμ λ©μμ§μμ μ¬κ°ν μ°μΈ μ νΈκ° κ°μ§λμμ΅λλ€. "
|
| 1307 |
+
"μ λ¬Έ μλ΄μ λ°μΌμ€ κ²μ κΆμ₯λ립λλ€.\n\n"
|
| 1308 |
+
)
|
| 1309 |
+
|
| 1310 |
+
# 3) μ±λ΄ μλ΅
|
| 1311 |
+
if mode not in ["emotion", "rational"]:
|
| 1312 |
+
raise HTTPException(
|
| 1313 |
+
status_code=400,
|
| 1314 |
+
detail="modeλ 'emotion' λλ 'rational'μ΄μ΄μΌ ν©λλ€."
|
| 1315 |
+
)
|
| 1316 |
+
chatbot_msg = chat_response(user_input, mode=mode)
|
| 1317 |
+
|
| 1318 |
+
# 4) μΆμ² λ©μμ§ μ€λΉ
|
| 1319 |
+
recommendation_msg = ""
|
| 1320 |
+
recommendations_list = []
|
| 1321 |
+
|
| 1322 |
+
# μ°μΈλκ° 'μλ΄ κΆμ₯'μ΄ μλκ³ , μ¬μ©μκ° hobby μ λ³΄κ° μμΌλ©΄ β μΆμ²
|
| 1323 |
+
if depression_label != "μλ΄ κΆμ₯" and req.hobby:
|
| 1324 |
+
user_profile = {
|
| 1325 |
+
"extroversion": req.extroversion or "",
|
| 1326 |
+
"feeling_thinking": req.feeling_thinking or "",
|
| 1327 |
+
"hobby": req.hobby,
|
| 1328 |
+
"detail_hobby": req.detail_hobby or [],
|
| 1329 |
+
}
|
| 1330 |
+
top_items = recommend_content_based(user_profile, top_n=5)
|
| 1331 |
+
|
| 1332 |
+
recommendation_msg = "\n\nλΉμ μ μν λ§μΆ€ μΆμ²μ κ°μ Έμμ΄μ! βΊοΈ\n"
|
| 1333 |
+
for i, (item, score) in enumerate(top_items, start=1):
|
| 1334 |
+
clean_desc = re.sub(r"\(.*?\)", "", item["desc"]).strip()
|
| 1335 |
+
recommendations_list.append({
|
| 1336 |
+
"item_id": item["item_id"],
|
| 1337 |
+
"title": item["title"],
|
| 1338 |
+
"desc": item["desc"],
|
| 1339 |
+
"personality": item["personality"],
|
| 1340 |
+
"score": round(score, 4)
|
| 1341 |
+
})
|
| 1342 |
+
recommendation_msg += f"{i}. **{item['title']}** - {clean_desc}\n"
|
| 1343 |
+
recommendation_msg += "\nμ΄ μ€μμ μ΄λ€ νλμ΄ κ°μ₯ λ리μλμ? π"
|
| 1344 |
+
|
| 1345 |
+
# 5) μ΅μ’
μλ΅ κ²°ν©
|
| 1346 |
+
final_response = counseling_response + chatbot_msg
|
| 1347 |
+
if recommendation_msg:
|
| 1348 |
+
final_response += recommendation_msg
|
| 1349 |
+
|
| 1350 |
+
# 6) μλ΅ JSON ꡬμ±
|
| 1351 |
+
response_dict = {
|
| 1352 |
+
"mode": "chat+recommend" if recommendation_msg else "chat",
|
| 1353 |
+
"response": final_response,
|
| 1354 |
+
"depression_label": depression_label
|
| 1355 |
+
}
|
| 1356 |
+
if recommendation_msg:
|
| 1357 |
+
response_dict["recommendations"] = recommendations_list
|
| 1358 |
|
| 1359 |
+
return response_dict
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1360 |
|
|
|
|
| 1361 |
|
| 1362 |
def run_fastapi():
|
| 1363 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|