Chanjeans commited on
Commit
27f77b6
Β·
verified Β·
1 Parent(s): 9c45e5d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -46
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
- depression_score, depression_label = predict_depression(req.user_input)
1256
- counseling_response = ""
1257
- if depression_label == "상담 ꢌμž₯":
1258
- counseling_response = (
1259
- "μž…λ ₯ν•˜μ‹  λ©”μ‹œμ§€μ—μ„œ μ‹¬κ°ν•œ 우울 μ‹ ν˜Έκ°€ κ°μ§€λ˜μ—ˆμŠ΅λ‹ˆλ‹€."
1260
- "μ „λ¬Έ 상담을 λ°›μœΌμ‹€ 것을 κ°•λ ₯히 ꢌμž₯λ“œλ¦½λ‹ˆλ‹€."
1261
- "λΉ λ₯Έ μ‹œμΌ 내에 전문가와 μƒλ‹΄ν•˜μ‹œκΈΈ λ°”λžλ‹ˆλ‹€."
1262
- )
1263
-
1264
- is_recommend_query = any(keyword in req.user_input for keyword in RECOMMEND_KEYWORDS)
1265
-
1266
-
1267
- # 항상 챗봇 λ©”μ‹œμ§€ 생성 (emotion λ˜λŠ” rational λͺ¨λ“œ)
1268
- mode = req.mode.lower()
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
- recommendation_msg = "당신을 μœ„ν•œ 맞좀 μΆ”μ²œμ„ κ°€μ Έμ™”μ–΄μš”! ☺️"
 
 
1288
  for i, (item, score) in enumerate(top_items, start=1):
1289
- recommendations_list.append({
 
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
- clean_desc = re.sub(r"\(.*?\)", "", item["desc"]).strip()
1297
- recommendation_msg += f"{i}. **{item['title']}** - {clean_desc}"
1298
- recommendation_msg += "이 μ€‘μ—μ„œ μ–΄λ–€ ν™œλ™μ΄ κ°€μž₯ λŒλ¦¬μ‹œλ‚˜μš”? 🌟"
1299
-
1300
- # μ΅œμ’… 응닡 κ²°ν•©
1301
-
1302
- final_response = counseling_response + chatbot_msg
1303
- if recommendation_msg:
1304
- final_response += "" + recommendation_msg
1305
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1306
 
1307
- response_dict = {
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)