Chanjeans commited on
Commit
835c90b
ยท
verified ยท
1 Parent(s): 0c0a5d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -10
app.py CHANGED
@@ -1037,6 +1037,8 @@ def recommend_content_based(user_profile, top_n=5):
1037
  user_extroversion = user_profile.get("extroversion", "")
1038
  user_feeling_thinking = user_profile.get("feeling_thinking", "")
1039
 
 
 
1040
  for item in items:
1041
  item_id = item["item_id"]
1042
  item_emb = item_embedding_dict[item_id]
@@ -1045,21 +1047,19 @@ def recommend_content_based(user_profile, top_n=5):
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.3
1060
 
1061
- # (2) ์„ฑํ–ฅ
1062
- # ์„ฑํ–ฅ 2๊ฐœ(์™ธํ–ฅํ˜•/๋‚ดํ–ฅํ˜•, ๊ฐ์ •ํ˜•/์ด์„ฑํ˜•) ์ค‘ ๋ช‡ ๊ฐœ๊ฐ€ ๋งค์นญ๋˜๋Š”์ง€
1063
  personality_match_count = sum(
1064
  trait in item["personality"]
1065
  for trait in [user_extroversion, user_feeling_thinking]
@@ -1074,7 +1074,26 @@ def recommend_content_based(user_profile, top_n=5):
1074
 
1075
  # ์ ์ˆ˜๊ฐ€ ๋†’์€ ์ˆœ์œผ๋กœ ์ •๋ ฌ
1076
  scored.sort(key=lambda x: x[1], reverse=True)
1077
- return scored[:top_n]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1078
 
1079
 
1080
  #####################################
@@ -1215,8 +1234,8 @@ def chat_or_recommend(req: ChatOrRecommendRequest):
1215
  response_text = "๋‹น์‹ ์„ ์œ„ํ•œ ๋งž์ถค ์ถ”์ฒœ์„ ๊ฐ€์ ธ์™”์–ด์š”! โ˜บ๏ธ"
1216
  for i, (item, score) in enumerate(top_items, start=1):
1217
  response_text += (
1218
- f"{i}. **{item['title']}**\n"
1219
- f" - {item['desc']}\n"
1220
  )
1221
  response_text += "์ด ์ค‘์—์„œ ์–ด๋–ค ํ™œ๋™์ด ๊ฐ€์žฅ ๋Œ๋ฆฌ์‹œ๋‚˜์š”? ๐ŸŒŸ"
1222
  return {
 
1037
  user_extroversion = user_profile.get("extroversion", "")
1038
  user_feeling_thinking = user_profile.get("feeling_thinking", "")
1039
 
1040
+ hobby_count = {hobby: 0 for hobby in user_hobbies} # ์ทจ๋ฏธ๋ณ„ ์ถ”์ฒœ ๊ฐœ์ˆ˜ ์ œํ•œ
1041
+
1042
  for item in items:
1043
  item_id = item["item_id"]
1044
  item_emb = item_embedding_dict[item_id]
 
1047
  # ๊ธฐ๋ณธ ๊ฐ€์ค‘์น˜
1048
  weight = 1.0
1049
 
1050
+ # (1) ์ทจ๋ฏธ ๊ฐ€์ค‘์น˜ (๋…์„œ ์ ๋ฆผ ๋ฐฉ์ง€)
1051
  desc_hobby = extract_hobby(item["desc"]) # ์˜ˆ: "(์šด๋™, ํ—ฌ์Šค)"
1052
 
 
1053
  for h in user_hobbies:
1054
  if h in desc_hobby:
1055
+ weight *= 1.1 # ๊ธฐ๋ณธ ๊ฐ€์ค‘์น˜ 1.1๋ฐฐ
1056
+ hobby_count[h] += 1
1057
 
 
1058
  for dh in user_details:
1059
  if dh in desc_hobby:
1060
+ weight *= 1.3 # ์„ธ๋ถ€ ์ทจ๋ฏธ๋Š” 1.3๋ฐฐ ๊ฐ€์ค‘์น˜
1061
 
1062
+ # (2) ์„ฑํ–ฅ (์™ธํ–ฅ/๋‚ดํ–ฅ, ๊ฐ์ •/์ด์„ฑ)
 
1063
  personality_match_count = sum(
1064
  trait in item["personality"]
1065
  for trait in [user_extroversion, user_feeling_thinking]
 
1074
 
1075
  # ์ ์ˆ˜๊ฐ€ ๋†’์€ ์ˆœ์œผ๋กœ ์ •๋ ฌ
1076
  scored.sort(key=lambda x: x[1], reverse=True)
1077
+
1078
+ # ๐ŸŸข ํŠน์ • ์ทจ๋ฏธ(์˜ˆ: ๋…์„œ)๊ฐ€ ๊ณผํ•˜๊ฒŒ ๋‚˜์˜ค๋Š” ๋ฌธ์ œ ํ•ด๊ฒฐ (์ตœ๋Œ€ 2๊ฐœ ์ œํ•œ)
1079
+ balanced_recommendations = []
1080
+ hobby_limits = {hobby: 2 for hobby in user_hobbies} # ๊ฐ ์ทจ๋ฏธ๋ณ„ ์ตœ๋Œ€ ์ถ”์ฒœ ๊ฐœ์ˆ˜ ์ œํ•œ
1081
+
1082
+ for item, score in scored:
1083
+ item_hobby = extract_hobby(item["desc"]) # "(๋…์„œ, ์†Œ์„ค)" โ†’ "๋…์„œ, ์†Œ์„ค"
1084
+ item_main_hobby = item_hobby.split(", ")[0] if item_hobby else None
1085
+
1086
+ # ํŠน์ • ์ทจ๋ฏธ๊ฐ€ ์ด๋ฏธ ๋„ˆ๋ฌด ๋งŽ๋‹ค๋ฉด ์Šคํ‚ต
1087
+ if item_main_hobby and hobby_limits.get(item_main_hobby, 0) > 0:
1088
+ balanced_recommendations.append((item, score))
1089
+ hobby_limits[item_main_hobby] -= 1
1090
+
1091
+ # ์ถ”์ฒœ ๊ฐœ์ˆ˜๊ฐ€ ์ถฉ๋ถ„ํ•˜๋ฉด ์ข…๋ฃŒ
1092
+ if len(balanced_recommendations) >= top_n:
1093
+ break
1094
+
1095
+ return balanced_recommendations
1096
+
1097
 
1098
 
1099
  #####################################
 
1234
  response_text = "๋‹น์‹ ์„ ์œ„ํ•œ ๋งž์ถค ์ถ”์ฒœ์„ ๊ฐ€์ ธ์™”์–ด์š”! โ˜บ๏ธ"
1235
  for i, (item, score) in enumerate(top_items, start=1):
1236
  response_text += (
1237
+ f"{i}. **{item['title']}**"
1238
+ f" - {item['desc']}"
1239
  )
1240
  response_text += "์ด ์ค‘์—์„œ ์–ด๋–ค ํ™œ๋™์ด ๊ฐ€์žฅ ๋Œ๋ฆฌ์‹œ๋‚˜์š”? ๐ŸŒŸ"
1241
  return {