현진_챗봇 로직 변경
Browse files
app.py
CHANGED
|
@@ -1194,7 +1194,7 @@ def home():
|
|
| 1194 |
return {"message": "안녕하세요! 추천 & 챗봇 FastAPI 서버입니다."}
|
| 1195 |
|
| 1196 |
|
| 1197 |
-
# (2) 추천용 모델
|
| 1198 |
class UserProfile(BaseModel):
|
| 1199 |
extroversion: str
|
| 1200 |
feeling_thinking: str
|
|
@@ -1215,7 +1215,6 @@ def recommend_api(profile: UserProfile):
|
|
| 1215 |
})
|
| 1216 |
return {"recommendations": results}
|
| 1217 |
|
| 1218 |
-
|
| 1219 |
# (3) 챗봇용 모델 + 다중 취미/세부취미 대응
|
| 1220 |
class ChatRequest(BaseModel):
|
| 1221 |
user_input: str
|
|
@@ -1239,7 +1238,6 @@ class ChatOrRecommendRequest(BaseModel):
|
|
| 1239 |
# (5) 자동 분기 엔드포인트
|
| 1240 |
@app.post("/chat_or_recommend")
|
| 1241 |
def chat_or_recommend(req: ChatOrRecommendRequest):
|
| 1242 |
-
# 우울도 예측 및 상담 권장 메시지 준비
|
| 1243 |
depression_score, depression_label = predict_depression(req.user_input)
|
| 1244 |
counseling_response = ""
|
| 1245 |
if depression_label == "상담 권장":
|
|
@@ -1249,32 +1247,29 @@ def chat_or_recommend(req: ChatOrRecommendRequest):
|
|
| 1249 |
"빠른 시일 내에 전문가와 상담하시길 바랍니다.\n\n"
|
| 1250 |
)
|
| 1251 |
|
| 1252 |
-
#
|
| 1253 |
-
base_response = ""
|
| 1254 |
-
mode_used = ""
|
| 1255 |
-
additional_data = {}
|
| 1256 |
-
user_text = req.user_input
|
| 1257 |
mode = req.mode.lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1258 |
|
| 1259 |
-
#
|
| 1260 |
-
|
| 1261 |
-
|
| 1262 |
-
|
| 1263 |
-
raise HTTPException(
|
| 1264 |
-
status_code=400,
|
| 1265 |
-
detail="추천을 위해 [hobby, detail_hobby, extroversion, feeling_thinking] 정보가 필요합니다."
|
| 1266 |
-
)
|
| 1267 |
user_profile = {
|
| 1268 |
"extroversion": req.extroversion or "",
|
| 1269 |
"feeling_thinking": req.feeling_thinking or "",
|
| 1270 |
-
"hobby": req.hobby,
|
| 1271 |
"detail_hobby": req.detail_hobby or [],
|
| 1272 |
}
|
| 1273 |
top_items = recommend_content_based(user_profile, top_n=5)
|
| 1274 |
-
|
| 1275 |
-
response_text = "당신을 위한 맞춤 추천을 가져왔어요! ☺️\n"
|
| 1276 |
for i, (item, score) in enumerate(top_items, start=1):
|
| 1277 |
-
|
| 1278 |
"item_id": item["item_id"],
|
| 1279 |
"title": item["title"],
|
| 1280 |
"desc": item["desc"],
|
|
@@ -1282,28 +1277,20 @@ def chat_or_recommend(req: ChatOrRecommendRequest):
|
|
| 1282 |
"score": round(score, 4)
|
| 1283 |
})
|
| 1284 |
clean_desc = re.sub(r"\(.*?\)", "", item["desc"]).strip()
|
| 1285 |
-
|
| 1286 |
-
|
| 1287 |
-
base_response = response_text
|
| 1288 |
-
mode_used = "recommend"
|
| 1289 |
-
additional_data["recommendations"] = results
|
| 1290 |
-
|
| 1291 |
-
# ▶ 2) 추천 키워드가 없는 경우 → 기본 챗봇 로직 수행
|
| 1292 |
-
else:
|
| 1293 |
-
if mode not in ["emotion", "rational"]:
|
| 1294 |
-
raise HTTPException(
|
| 1295 |
-
status_code=400,
|
| 1296 |
-
detail="mode는 'emotion' 또는 'rational'이어야 합니다."
|
| 1297 |
-
)
|
| 1298 |
-
base_response = chat_response(user_text, mode=mode)
|
| 1299 |
-
mode_used = "chat"
|
| 1300 |
|
| 1301 |
-
#
|
| 1302 |
-
final_response = counseling_response +
|
|
|
|
|
|
|
|
|
|
| 1303 |
response_dict = {
|
| 1304 |
-
"mode":
|
| 1305 |
"response": final_response,
|
| 1306 |
"depression_label": depression_label
|
| 1307 |
}
|
| 1308 |
-
|
|
|
|
|
|
|
| 1309 |
return response_dict
|
|
|
|
| 1194 |
return {"message": "안녕하세요! 추천 & 챗봇 FastAPI 서버입니다."}
|
| 1195 |
|
| 1196 |
|
| 1197 |
+
# (2) 추천용 모델
|
| 1198 |
class UserProfile(BaseModel):
|
| 1199 |
extroversion: str
|
| 1200 |
feeling_thinking: str
|
|
|
|
| 1215 |
})
|
| 1216 |
return {"recommendations": results}
|
| 1217 |
|
|
|
|
| 1218 |
# (3) 챗봇용 모델 + 다중 취미/세부취미 대응
|
| 1219 |
class ChatRequest(BaseModel):
|
| 1220 |
user_input: str
|
|
|
|
| 1238 |
# (5) 자동 분기 엔드포인트
|
| 1239 |
@app.post("/chat_or_recommend")
|
| 1240 |
def chat_or_recommend(req: ChatOrRecommendRequest):
|
|
|
|
| 1241 |
depression_score, depression_label = predict_depression(req.user_input)
|
| 1242 |
counseling_response = ""
|
| 1243 |
if depression_label == "상담 권장":
|
|
|
|
| 1247 |
"빠른 시일 내에 전문가와 상담하시길 바랍니다.\n\n"
|
| 1248 |
)
|
| 1249 |
|
| 1250 |
+
# ② 항상 챗봇 메시지를 생성 (emotion 또는 rational 모드)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1251 |
mode = req.mode.lower()
|
| 1252 |
+
if mode not in ["emotion", "rational"]:
|
| 1253 |
+
raise HTTPException(
|
| 1254 |
+
status_code=400,
|
| 1255 |
+
detail="mode는 'emotion' 또는 'rational'이어야 합니다."
|
| 1256 |
+
)
|
| 1257 |
+
chatbot_msg = chat_response(req.user_input, mode=mode)
|
| 1258 |
|
| 1259 |
+
# ③ 프로필 정보가 제공된 경우 추천 메시지 생성
|
| 1260 |
+
recommendation_msg = ""
|
| 1261 |
+
recommendations_list = []
|
| 1262 |
+
if req.hobby:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1263 |
user_profile = {
|
| 1264 |
"extroversion": req.extroversion or "",
|
| 1265 |
"feeling_thinking": req.feeling_thinking or "",
|
| 1266 |
+
"hobby": req.hobby,
|
| 1267 |
"detail_hobby": req.detail_hobby or [],
|
| 1268 |
}
|
| 1269 |
top_items = recommend_content_based(user_profile, top_n=5)
|
| 1270 |
+
recommendation_msg = "당신을 위한 맞춤 추천을 가져왔어요! ☺️\n"
|
|
|
|
| 1271 |
for i, (item, score) in enumerate(top_items, start=1):
|
| 1272 |
+
recommendations_list.append({
|
| 1273 |
"item_id": item["item_id"],
|
| 1274 |
"title": item["title"],
|
| 1275 |
"desc": item["desc"],
|
|
|
|
| 1277 |
"score": round(score, 4)
|
| 1278 |
})
|
| 1279 |
clean_desc = re.sub(r"\(.*?\)", "", item["desc"]).strip()
|
| 1280 |
+
recommendation_msg += f"{i}. **{item['title']}** - {clean_desc}\n"
|
| 1281 |
+
recommendation_msg += "이 중에서 어떤 활동이 가장 끌리시나요? 🌟"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1282 |
|
| 1283 |
+
# ④ 최종 응답
|
| 1284 |
+
final_response = counseling_response + chatbot_msg
|
| 1285 |
+
if recommendation_msg:
|
| 1286 |
+
final_response += "\n\n" + recommendation_msg
|
| 1287 |
+
|
| 1288 |
response_dict = {
|
| 1289 |
+
"mode": "chat+recommend" if recommendation_msg else "chat",
|
| 1290 |
"response": final_response,
|
| 1291 |
"depression_label": depression_label
|
| 1292 |
}
|
| 1293 |
+
if recommendation_msg:
|
| 1294 |
+
response_dict["recommendations"] = recommendations_list
|
| 1295 |
+
|
| 1296 |
return response_dict
|