Update app.py
Browse files
app.py
CHANGED
@@ -968,13 +968,54 @@ def make_user_profile_text(user_profile: dict) -> str:
|
|
968 |
|
969 |
|
970 |
#####################################
|
971 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
972 |
#####################################
|
973 |
def get_user_embedding(user_profile: dict):
|
974 |
user_text = make_user_profile_text(user_profile)
|
975 |
return model_bert.encode(user_text, convert_to_numpy=True)
|
976 |
|
977 |
def extract_hobby(desc):
|
|
|
978 |
match = re.search(r"\((.*?)\)", desc)
|
979 |
return match.group(1) if match else ""
|
980 |
|
@@ -982,15 +1023,17 @@ def cosine_similarity(vec1, vec2):
|
|
982 |
norm1 = np.linalg.norm(vec1)
|
983 |
norm2 = np.linalg.norm(vec2)
|
984 |
if norm1 == 0 or norm2 == 0:
|
985 |
-
return 0
|
986 |
-
return np.dot(vec1, vec2) / (norm1 * norm2)
|
987 |
|
988 |
def recommend_content_based(user_profile, top_n=5):
|
989 |
user_emb = get_user_embedding(user_profile)
|
990 |
scored = []
|
991 |
|
992 |
-
|
993 |
-
|
|
|
|
|
994 |
user_extroversion = user_profile.get("extroversion", "")
|
995 |
user_feeling_thinking = user_profile.get("feeling_thinking", "")
|
996 |
|
@@ -999,18 +1042,28 @@ def recommend_content_based(user_profile, top_n=5):
|
|
999 |
item_emb = item_embedding_dict[item_id]
|
1000 |
sim = cosine_similarity(user_emb, item_emb)
|
1001 |
|
1002 |
-
# ๊ฐ์ค์น
|
1003 |
weight = 1.0
|
1004 |
|
1005 |
-
# (1) ์ทจ๋ฏธ
|
1006 |
-
desc_hobby = extract_hobby(item["desc"]) # ์: (์ด๋, ํฌ์ค)
|
1007 |
-
|
1008 |
-
|
1009 |
-
|
1010 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
1011 |
|
1012 |
# (2) ์ฑํฅ
|
1013 |
-
|
|
|
|
|
|
|
|
|
1014 |
if personality_match_count == 1:
|
1015 |
weight *= 1.15
|
1016 |
elif personality_match_count == 2:
|
@@ -1019,6 +1072,7 @@ def recommend_content_based(user_profile, top_n=5):
|
|
1019 |
final_score = sim * weight
|
1020 |
scored.append((item, final_score))
|
1021 |
|
|
|
1022 |
scored.sort(key=lambda x: x[1], reverse=True)
|
1023 |
return scored[:top_n]
|
1024 |
|
@@ -1081,7 +1135,8 @@ def chat_response(user_input, mode="emotion", max_retries=5):
|
|
1081 |
def home():
|
1082 |
return {"message": "์๋
ํ์ธ์! ์ถ์ฒ & ์ฑ๋ด FastAPI ์๋ฒ์
๋๋ค."}
|
1083 |
|
1084 |
-
|
|
|
1085 |
class UserProfile(BaseModel):
|
1086 |
extroversion: str
|
1087 |
feeling_thinking: str
|
@@ -1090,7 +1145,6 @@ class UserProfile(BaseModel):
|
|
1090 |
|
1091 |
@app.post("/recommend")
|
1092 |
def recommend_api(profile: UserProfile):
|
1093 |
-
# "์๋ฃจ์
์ ๊ณต" ๋ก์ง๊ณผ ๋์ผ
|
1094 |
top_items = recommend_content_based(profile.dict(), top_n=5)
|
1095 |
results = []
|
1096 |
for (item, score) in top_items:
|
@@ -1103,63 +1157,51 @@ def recommend_api(profile: UserProfile):
|
|
1103 |
})
|
1104 |
return {"recommendations": results}
|
1105 |
|
1106 |
-
|
|
|
1107 |
class ChatRequest(BaseModel):
|
1108 |
user_input: str
|
1109 |
-
mode: str #
|
1110 |
-
RECOMMEND_KEYWORDS = [
|
1111 |
-
"์ถ์ฒ", "์ถ์ฒํด์ค", "์ทจ๋ฏธ ์ถ์ฒ"
|
1112 |
-
]
|
1113 |
|
1114 |
-
|
1115 |
-
def chat_api(req: ChatRequest):
|
1116 |
-
user_text = req.user_input
|
1117 |
-
mode = req.mode.lower() # ์
๋ ฅ์ ์๋ฌธ์๋ก ๋ณํํด ํต์ผ์ฑ ์ ์ง
|
1118 |
-
if mode not in ["emotion", "rational"]:
|
1119 |
-
raise HTTPException(status_code=400, detail="mode๋ 'emotion' ๋๋ 'rational'์ด์ด์ผ ํฉ๋๋ค.")
|
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 |
-
#
|
1127 |
class ChatOrRecommendRequest(BaseModel):
|
1128 |
user_input: str # ์ฌ์ฉ์์ ์ฑํ
๋ฉ์์ง
|
1129 |
-
mode: str # "emotion" ๋๋ "rational"
|
1130 |
|
1131 |
-
#
|
1132 |
extroversion: Optional[str] = None
|
1133 |
feeling_thinking: Optional[str] = None
|
1134 |
-
hobby: Optional[List[str]] =
|
1135 |
-
detail_hobby: Optional[List[str]] =
|
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) ์ถ์ฒ
|
1155 |
user_profile = {
|
1156 |
"extroversion": req.extroversion or "",
|
1157 |
"feeling_thinking": req.feeling_thinking or "",
|
1158 |
-
"hobby": req.hobby
|
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({
|
@@ -1169,24 +1211,15 @@ def chat_or_recommend(req: ChatOrRecommendRequest):
|
|
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 |
-
|
|
|
968 |
|
969 |
|
970 |
#####################################
|
971 |
+
# 2) ์์ดํ
์๋ฒ ๋ฉ ๋ก์ง
|
972 |
+
#####################################
|
973 |
+
def make_item_embedding_dict(items, model):
|
974 |
+
item_embedding_dict = {}
|
975 |
+
for item in items:
|
976 |
+
text = f"{item['title']} {item['desc']}"
|
977 |
+
emb = model.encode(text, convert_to_numpy=True)
|
978 |
+
item_embedding_dict[item['item_id']] = emb
|
979 |
+
return item_embedding_dict
|
980 |
+
|
981 |
+
item_embedding_dict = make_item_embedding_dict(items, model_bert)
|
982 |
+
|
983 |
+
|
984 |
+
#####################################
|
985 |
+
# 3) ์ฌ์ฉ์ ํ๋กํ -> ๋ฌธ์ฅํ (๋ค์ค ์ทจ๋ฏธ ๋์)
|
986 |
+
#####################################
|
987 |
+
def make_user_profile_text(user_profile: dict) -> str:
|
988 |
+
ext = user_profile.get("extroversion", "")
|
989 |
+
ft = user_profile.get("feeling_thinking", "")
|
990 |
+
|
991 |
+
# ๐ข hobby, detail_hobby๋ฅผ ๋ฆฌ์คํธ๋ก ๋ฐ๋๋ค๊ณ ๊ฐ์
|
992 |
+
hobby_list = user_profile.get("hobby", [])
|
993 |
+
detail_list = user_profile.get("detail_hobby", [])
|
994 |
+
|
995 |
+
# ๋ฆฌ์คํธ๋ฅผ ๋ฌธ์์ด๋ก ํฉ์น๊ธฐ
|
996 |
+
# ์: hobby_list=["์ด๋","๋
์"] โ "์ด๋, ๋
์"
|
997 |
+
hobby_str = ", ".join(hobby_list) if hobby_list else ""
|
998 |
+
detail_str = ", ".join(detail_list) if detail_list else ""
|
999 |
+
|
1000 |
+
# ๊ฐ๋จํ ํ๋์ ๋ฌธ์ฅ์ผ๋ก ๊ตฌ์ฑ
|
1001 |
+
# ํ์ํ๋ค๋ฉด ์ทจ๋ฏธ๋ง๋ค ๋ณ๋ ๋ฌธ์ฅ ๋ฑ์ ์ถ๊ฐ ๊ฐ๋ฅ
|
1002 |
+
text = (
|
1003 |
+
f"์ ๋ {ext}์ด๋ฉฐ {ft}ํ ์ฑํฅ์ ๊ฐ์ง ์ฌ๋์
๋๋ค. "
|
1004 |
+
f"์ฃผ์ ์ทจ๋ฏธ๋ก๋ {hobby_str}๋ฅผ ์ฆ๊ธฐ๊ณ , "
|
1005 |
+
f"ํนํ {detail_str} ๋ถ์ผ์ ๊ด์ฌ์ด ๋ง์ต๋๋ค."
|
1006 |
+
)
|
1007 |
+
return text
|
1008 |
+
|
1009 |
+
|
1010 |
+
#####################################
|
1011 |
+
# 4) ์ฌ์ฉ์ ์๋ฒ ๋ฉ + ์ถ์ฒ ๋ก์ง (๋ค์ค ์ทจ๋ฏธ ๋์)
|
1012 |
#####################################
|
1013 |
def get_user_embedding(user_profile: dict):
|
1014 |
user_text = make_user_profile_text(user_profile)
|
1015 |
return model_bert.encode(user_text, convert_to_numpy=True)
|
1016 |
|
1017 |
def extract_hobby(desc):
|
1018 |
+
""" ์: "PT ์ ๋ฌธ ์ฝ์น... (์ด๋, ํฌ์ค)" -> "์ด๋, ํฌ์ค" """
|
1019 |
match = re.search(r"\((.*?)\)", desc)
|
1020 |
return match.group(1) if match else ""
|
1021 |
|
|
|
1023 |
norm1 = np.linalg.norm(vec1)
|
1024 |
norm2 = np.linalg.norm(vec2)
|
1025 |
if norm1 == 0 or norm2 == 0:
|
1026 |
+
return 0.0
|
1027 |
+
return float(np.dot(vec1, vec2) / (norm1 * norm2))
|
1028 |
|
1029 |
def recommend_content_based(user_profile, top_n=5):
|
1030 |
user_emb = get_user_embedding(user_profile)
|
1031 |
scored = []
|
1032 |
|
1033 |
+
# ๐ข ๋ค์ค ์ทจ๋ฏธ/์ธ๋ถ์ทจ๋ฏธ๋ฅผ ๋ฆฌ์คํธ๋ก ๊ฐ์
|
1034 |
+
user_hobbies = user_profile.get("hobby", []) or []
|
1035 |
+
user_details = user_profile.get("detail_hobby", []) or []
|
1036 |
+
|
1037 |
user_extroversion = user_profile.get("extroversion", "")
|
1038 |
user_feeling_thinking = user_profile.get("feeling_thinking", "")
|
1039 |
|
|
|
1042 |
item_emb = item_embedding_dict[item_id]
|
1043 |
sim = cosine_similarity(user_emb, item_emb)
|
1044 |
|
1045 |
+
# ๊ธฐ๋ณธ ๊ฐ์ค์น
|
1046 |
weight = 1.0
|
1047 |
|
1048 |
+
# (1) ์ทจ๋ฏธ ๊ฐ์ค์น
|
1049 |
+
desc_hobby = extract_hobby(item["desc"]) # ์: "(์ด๋, ํฌ์ค)"
|
1050 |
+
|
1051 |
+
# [hobby] ๊ฐ ์์๊ฐ desc_hobby์ ์์ผ๋ฉด ๊ฐ์ค์น ๋ถ์ฌ
|
1052 |
+
for h in user_hobbies:
|
1053 |
+
if h in desc_hobby:
|
1054 |
+
weight *= 1.05
|
1055 |
+
|
1056 |
+
# [detail_hobby] ๊ฐ ์์๊ฐ desc_hobby์ ์์ผ๋ฉด ๊ฐ์ค์น ๋ถ์ฌ
|
1057 |
+
for dh in user_details:
|
1058 |
+
if dh in desc_hobby:
|
1059 |
+
weight *= 1.2
|
1060 |
|
1061 |
# (2) ์ฑํฅ
|
1062 |
+
# ์ฑํฅ 2๊ฐ(์ธํฅํ/๋ดํฅํ, ๊ฐ์ ํ/์ด์ฑํ) ์ค ๋ช ๊ฐ๊ฐ ๋งค์นญ๋๋์ง
|
1063 |
+
personality_match_count = sum(
|
1064 |
+
trait in item["personality"]
|
1065 |
+
for trait in [user_extroversion, user_feeling_thinking]
|
1066 |
+
)
|
1067 |
if personality_match_count == 1:
|
1068 |
weight *= 1.15
|
1069 |
elif personality_match_count == 2:
|
|
|
1072 |
final_score = sim * weight
|
1073 |
scored.append((item, final_score))
|
1074 |
|
1075 |
+
# ์ ์๊ฐ ๋์ ์์ผ๋ก ์ ๋ ฌ
|
1076 |
scored.sort(key=lambda x: x[1], reverse=True)
|
1077 |
return scored[:top_n]
|
1078 |
|
|
|
1135 |
def home():
|
1136 |
return {"message": "์๋
ํ์ธ์! ์ถ์ฒ & ์ฑ๋ด FastAPI ์๋ฒ์
๋๋ค."}
|
1137 |
|
1138 |
+
|
1139 |
+
# (2) ์ถ์ฒ์ฉ ๋ชจ๋ธ (๋จ์ผ hobby / detail_hobby) - ๊ธฐ์กด ํธํ์ฉ
|
1140 |
class UserProfile(BaseModel):
|
1141 |
extroversion: str
|
1142 |
feeling_thinking: str
|
|
|
1145 |
|
1146 |
@app.post("/recommend")
|
1147 |
def recommend_api(profile: UserProfile):
|
|
|
1148 |
top_items = recommend_content_based(profile.dict(), top_n=5)
|
1149 |
results = []
|
1150 |
for (item, score) in top_items:
|
|
|
1157 |
})
|
1158 |
return {"recommendations": results}
|
1159 |
|
1160 |
+
|
1161 |
+
# (3) ์ฑ๋ด์ฉ ๋ชจ๋ธ + ๋ค์ค ์ทจ๋ฏธ/์ธ๋ถ์ทจ๋ฏธ ๋์
|
1162 |
class ChatRequest(BaseModel):
|
1163 |
user_input: str
|
1164 |
+
mode: str # "emotion" or "rational"
|
|
|
|
|
|
|
1165 |
|
1166 |
+
RECOMMEND_KEYWORDS = ["์ถ์ฒ", "์ถ์ฒํด์ค", "์ทจ๋ฏธ ์ถ์ฒ"]
|
|
|
|
|
|
|
|
|
|
|
1167 |
|
|
|
|
|
|
|
|
|
1168 |
|
1169 |
+
# (4) ์ฑ๋ด + ์ถ์ฒ ์๋ ๋ถ๊ธฐ์ฉ ๋ชจ๋ธ
|
1170 |
class ChatOrRecommendRequest(BaseModel):
|
1171 |
user_input: str # ์ฌ์ฉ์์ ์ฑํ
๋ฉ์์ง
|
1172 |
+
mode: str # "emotion" ๋๋ "rational"
|
1173 |
|
1174 |
+
# ๐ข ๋ค์ค ์ ํ ๊ฐ๋ฅํ๋๋ก List[str]๋ก ๋ณ๊ฒฝ
|
1175 |
extroversion: Optional[str] = None
|
1176 |
feeling_thinking: Optional[str] = None
|
1177 |
+
hobby: Optional[List[str]] = None
|
1178 |
+
detail_hobby: Optional[List[str]] = None
|
1179 |
|
1180 |
+
|
1181 |
+
# (5) ์๋ ๋ถ๊ธฐ ์๋ํฌ์ธํธ
|
1182 |
@app.post("/chat_or_recommend")
|
1183 |
def chat_or_recommend(req: ChatOrRecommendRequest):
|
1184 |
user_text = req.user_input
|
1185 |
mode = req.mode.lower()
|
1186 |
|
1187 |
+
# โถ 1) "์ถ์ฒ" ํค์๋ ํฌํจ ์ โ ์ถ์ฒ
|
1188 |
if any(keyword in user_text for keyword in RECOMMEND_KEYWORDS):
|
1189 |
+
# ํ๋กํ ๋ฏธ์
๋ ฅ ์ ์์ธ ์ฒ๋ฆฌ
|
1190 |
+
# (์ฌ๊ธฐ์๋ hobby / detail_hobby๊ฐ ์ต์ํ 1๊ฐ ์ด์ ์๋ค๊ณ ๊ฐ์ )
|
1191 |
if not req.hobby:
|
|
|
|
|
1192 |
raise HTTPException(
|
1193 |
status_code=400,
|
1194 |
+
detail="์ถ์ฒ์ ์ํด [hobby, detail_hobby, extroversion, feeling_thinking] ์ ๋ณด๊ฐ ํ์ํฉ๋๋ค."
|
1195 |
)
|
1196 |
+
|
1197 |
+
# (A) ์ถ์ฒ ๋ก์ง
|
1198 |
user_profile = {
|
1199 |
"extroversion": req.extroversion or "",
|
1200 |
"feeling_thinking": req.feeling_thinking or "",
|
1201 |
+
"hobby": req.hobby, # list
|
1202 |
+
"detail_hobby": req.detail_hobby or [],
|
1203 |
}
|
1204 |
top_items = recommend_content_based(user_profile, top_n=5)
|
|
|
1205 |
results = []
|
1206 |
for (item, score) in top_items:
|
1207 |
results.append({
|
|
|
1211 |
"personality": item["personality"],
|
1212 |
"score": round(score, 4)
|
1213 |
})
|
1214 |
+
return {"mode": "recommend", "recommendations": results}
|
|
|
|
|
|
|
|
|
1215 |
|
1216 |
else:
|
1217 |
+
# โถ 2) ์ฑ๋ด ๋ก์ง
|
1218 |
if mode not in ["emotion", "rational"]:
|
1219 |
raise HTTPException(
|
1220 |
status_code=400,
|
1221 |
detail="mode๋ 'emotion' ๋๋ 'rational'์ด์ด์ผ ํฉ๋๋ค."
|
1222 |
)
|
1223 |
|
|
|
1224 |
reply = chat_response(user_text, mode=mode)
|
1225 |
+
return {"mode": "chat", "response": reply}
|
|
|
|
|
|
|
|