Update app.py
Browse files
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 |
+
|