Chanjeans commited on
Commit
c245bd7
Β·
verified Β·
1 Parent(s): ce7592f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py CHANGED
@@ -1107,6 +1107,9 @@ def recommend_api(profile: UserProfile):
1107
  class ChatRequest(BaseModel):
1108
  user_input: str
1109
  mode: str # `emotion` λ˜λŠ” `rational`을 λͺ…ν™•ν•˜κ²Œ μž…λ ₯받도둝 μ„€μ •
 
 
 
1110
 
1111
  @app.post("/chat")
1112
  def chat_api(req: ChatRequest):
@@ -1117,3 +1120,73 @@ def chat_api(req: ChatRequest):
1117
 
1118
  reply = chat_response(user_text, mode=mode)
1119
  return {"response": reply}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1107
  class ChatRequest(BaseModel):
1108
  user_input: str
1109
  mode: str # `emotion` λ˜λŠ” `rational`을 λͺ…ν™•ν•˜κ²Œ μž…λ ₯받도둝 μ„€μ •
1110
+ RECOMMEND_KEYWORDS = [
1111
+ "μΆ”μ²œ", "μΆ”μ²œν•΄μ€˜", "μ·¨λ―Έ μΆ”μ²œ"
1112
+ ]
1113
 
1114
  @app.post("/chat")
1115
  def chat_api(req: ChatRequest):
 
1120
 
1121
  reply = chat_response(user_text, mode=mode)
1122
  return {"response": reply}
1123
+ from pydantic import BaseModel
1124
+ from typing import Optional
1125
+
1126
+ # κΈ°μ‘΄ ChatRequest 에 user_profile 용 ν•„λ“œλ₯Ό μΆ”κ°€ν•œ λͺ¨λΈ
1127
+ class ChatOrRecommendRequest(BaseModel):
1128
+ user_input: str # μ‚¬μš©μžμ˜ μ±„νŒ… λ©”μ‹œμ§€
1129
+ mode: str # "emotion" λ˜λŠ” "rational" (챗봇 λͺ¨λ“œ)
1130
+
1131
+ # μΆ”μ²œμš© ν”„λ‘œν•„ (μ„ νƒμ μœΌλ‘œ 받을 수 μžˆλ„λ‘ Optional)
1132
+ extroversion: Optional[str] = None
1133
+ feeling_thinking: Optional[str] = None
1134
+ hobby: Optional[str] = None
1135
+ detail_hobby: Optional[str] = None
1136
+
1137
+ @app.post("/chat_or_recommend")
1138
+ def chat_or_recommend(req: ChatOrRecommendRequest):
1139
+ user_text = req.user_input
1140
+ mode = req.mode.lower()
1141
+
1142
+ # 1) μΆ”μ²œ ν‚€μ›Œλ“œ 포함 μ—¬λΆ€ 확인
1143
+ if any(keyword in user_text for keyword in RECOMMEND_KEYWORDS):
1144
+ # μ‚¬μš©μžκ°€ μΆ”μ²œμ„ μ›ν•œλ‹€κ³  νŒλ‹¨
1145
+ # ν”„λ‘œν•„ 정보가 μ—†λŠ” 경우λ₯Ό λŒ€λΉ„ν•΄μ„œ κΈ°λ³Έκ°’ ν˜Ήμ€ μ˜ˆμ™Έμ²˜λ¦¬λ₯Ό ν•  μˆ˜λ„ 있음
1146
+ if not req.hobby:
1147
+ # λ§Œμ•½ ν”„λ‘œν•„μ΄ μ—†μœΌλ©΄ μ˜ˆμ™Έ μ²˜λ¦¬ν•˜κ±°λ‚˜,
1148
+ # "ν”„λ‘œν•„(μ·¨λ―Έ/μ„±ν–₯)을 μž…λ ₯ν•΄μ£Όμ„Έμš”" 라고 μ•ˆλ‚΄ν•  μˆ˜λ„ 있음
1149
+ raise HTTPException(
1150
+ status_code=400,
1151
+ detail="μΆ”μ²œμ„ μœ„ν•΄ hobby, detail_hobby, extroversion, feeling_thinking 정보가 ν•„μš”ν•©λ‹ˆλ‹€."
1152
+ )
1153
+
1154
+ # (A) μΆ”μ²œ API 둜직 호좜
1155
+ user_profile = {
1156
+ "extroversion": req.extroversion or "",
1157
+ "feeling_thinking": req.feeling_thinking or "",
1158
+ "hobby": req.hobby or "",
1159
+ "detail_hobby": req.detail_hobby or "",
1160
+ }
1161
+ top_items = recommend_content_based(user_profile, top_n=5)
1162
+
1163
+ results = []
1164
+ for (item, score) in top_items:
1165
+ results.append({
1166
+ "item_id": item["item_id"],
1167
+ "title": item["title"],
1168
+ "desc": item["desc"],
1169
+ "personality": item["personality"],
1170
+ "score": round(score, 4)
1171
+ })
1172
+
1173
+ return {
1174
+ "mode": "recommend",
1175
+ "recommendations": results
1176
+ }
1177
+
1178
+ else:
1179
+ # 2) μΆ”μ²œ ν‚€μ›Œλ“œκ°€ μ—†μœΌλ©΄ 챗봇 둜직
1180
+ if mode not in ["emotion", "rational"]:
1181
+ raise HTTPException(
1182
+ status_code=400,
1183
+ detail="modeλŠ” 'emotion' λ˜λŠ” 'rational'이어야 ν•©λ‹ˆλ‹€."
1184
+ )
1185
+
1186
+ # (B) 챗봇 호좜
1187
+ reply = chat_response(user_text, mode=mode)
1188
+ return {
1189
+ "mode": "chat",
1190
+ "response": reply
1191
+ }
1192
+