Spaces:
Running
on
Zero
Running
on
Zero
Update breed_recommendation.py
Browse files- breed_recommendation.py +95 -26
breed_recommendation.py
CHANGED
|
@@ -146,63 +146,132 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
|
|
| 146 |
|
| 147 |
# 計算基礎相容性分數
|
| 148 |
compatibility_scores = calculate_compatibility_score(breed_info, user_prefs)
|
| 149 |
-
|
| 150 |
-
|
|
|
|
| 151 |
is_preferred = smart_rec.get('is_preferred', False)
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
# 根據是否為偏好品種調整分數
|
| 156 |
if is_preferred:
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
final_recommendations.append({
|
| 163 |
-
'rank': 0,
|
| 164 |
'breed': breed_name,
|
| 165 |
'base_score': round(base_score, 4),
|
| 166 |
-
'
|
| 167 |
'final_score': round(final_score, 4),
|
| 168 |
'scores': compatibility_scores,
|
| 169 |
-
'match_reason':
|
| 170 |
'info': breed_info,
|
| 171 |
'noise_info': breed_noise_info.get(breed_name, {}),
|
| 172 |
'health_info': breed_health_info.get(breed_name, {})
|
| 173 |
})
|
| 174 |
|
| 175 |
-
#
|
| 176 |
final_recommendations.sort(key=lambda x: (-x['final_score'], x['breed']))
|
| 177 |
|
| 178 |
# 更新排名
|
| 179 |
for i, rec in enumerate(final_recommendations, 1):
|
| 180 |
rec['rank'] = i
|
| 181 |
-
|
| 182 |
# 驗證排序
|
| 183 |
print("\nFinal Rankings:")
|
| 184 |
for rec in final_recommendations:
|
| 185 |
print(f"#{rec['rank']} {rec['breed']}")
|
| 186 |
print(f"Base Score: {rec['base_score']:.4f}")
|
| 187 |
-
print(f"
|
| 188 |
print(f"Final Score: {rec['final_score']:.4f}")
|
| 189 |
print(f"Reason: {rec['match_reason']}\n")
|
| 190 |
-
|
| 191 |
-
# 確保分數按降序排列
|
| 192 |
-
if rec['rank'] > 1:
|
| 193 |
-
prev_score = final_recommendations[rec['rank']-2]['final_score']
|
| 194 |
-
if rec['final_score'] > prev_score:
|
| 195 |
-
print(f"Warning: Ranking inconsistency detected!")
|
| 196 |
-
print(f"#{rec['rank']-1} score: {prev_score:.4f}")
|
| 197 |
-
print(f"#{rec['rank']} score: {rec['final_score']:.4f}")
|
| 198 |
-
|
| 199 |
result = format_recommendation_html(final_recommendations)
|
| 200 |
return [gr.update(value=result), gr.update(visible=False)]
|
| 201 |
|
| 202 |
-
|
| 203 |
except Exception as e:
|
| 204 |
error_msg = f"Error processing your description. Details: {str(e)}"
|
| 205 |
return [gr.update(value=error_msg), gr.update(visible=False)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
def show_loading():
|
| 208 |
return [gr.update(value=""), gr.update(visible=True)]
|
|
|
|
| 146 |
|
| 147 |
# 計算基礎相容性分數
|
| 148 |
compatibility_scores = calculate_compatibility_score(breed_info, user_prefs)
|
| 149 |
+
|
| 150 |
+
bonus_reasons = []
|
| 151 |
+
bonus_score = 0
|
| 152 |
is_preferred = smart_rec.get('is_preferred', False)
|
| 153 |
+
similarity = smart_rec.get('similarity', 0)
|
| 154 |
+
|
| 155 |
+
# 用戶直接提到的品種
|
|
|
|
| 156 |
if is_preferred:
|
| 157 |
+
bonus_score = 0.15 # 15% bonus
|
| 158 |
+
bonus_reasons.append("Directly mentioned breed (+15%)")
|
| 159 |
+
# 高相似度品種
|
| 160 |
+
elif similarity > 0.8:
|
| 161 |
+
bonus_score = 0.10 # 10% bonus
|
| 162 |
+
bonus_reasons.append("Very similar to preferred breed (+10%)")
|
| 163 |
+
# 中等相似度品種
|
| 164 |
+
elif similarity > 0.6:
|
| 165 |
+
bonus_score = 0.05 # 5% bonus
|
| 166 |
+
bonus_reasons.append("Similar to preferred breed (+5%)")
|
| 167 |
+
|
| 168 |
+
# 基於品種特性的額外加分
|
| 169 |
+
temperament = breed_info.get('Temperament', '').lower()
|
| 170 |
+
if any(trait in temperament for trait in ['friendly', 'gentle', 'affectionate']):
|
| 171 |
+
bonus_score += 0.02 # 2% bonus
|
| 172 |
+
bonus_reasons.append("Positive temperament traits (+2%)")
|
| 173 |
+
|
| 174 |
+
if breed_info.get('Good with Children') == 'Yes' and user_prefs.has_children:
|
| 175 |
+
bonus_score += 0.03 # 3% bonus
|
| 176 |
+
bonus_reasons.append("Excellent with children (+3%)")
|
| 177 |
+
|
| 178 |
+
# 基礎分數和最終分數計算
|
| 179 |
+
base_score = compatibility_scores.get('overall', 0.7)
|
| 180 |
+
final_score = min(0.95, base_score + bonus_score) # 確保不超過95%
|
| 181 |
|
| 182 |
final_recommendations.append({
|
| 183 |
+
'rank': 0,
|
| 184 |
'breed': breed_name,
|
| 185 |
'base_score': round(base_score, 4),
|
| 186 |
+
'bonus_score': round(bonus_score, 4),
|
| 187 |
'final_score': round(final_score, 4),
|
| 188 |
'scores': compatibility_scores,
|
| 189 |
+
'match_reason': ' • '.join(bonus_reasons) if bonus_reasons else "Standard match",
|
| 190 |
'info': breed_info,
|
| 191 |
'noise_info': breed_noise_info.get(breed_name, {}),
|
| 192 |
'health_info': breed_health_info.get(breed_name, {})
|
| 193 |
})
|
| 194 |
|
| 195 |
+
# 根據最終分數排序
|
| 196 |
final_recommendations.sort(key=lambda x: (-x['final_score'], x['breed']))
|
| 197 |
|
| 198 |
# 更新排名
|
| 199 |
for i, rec in enumerate(final_recommendations, 1):
|
| 200 |
rec['rank'] = i
|
| 201 |
+
|
| 202 |
# 驗證排序
|
| 203 |
print("\nFinal Rankings:")
|
| 204 |
for rec in final_recommendations:
|
| 205 |
print(f"#{rec['rank']} {rec['breed']}")
|
| 206 |
print(f"Base Score: {rec['base_score']:.4f}")
|
| 207 |
+
print(f"Bonus Score: {rec['bonus_score']:.4f}")
|
| 208 |
print(f"Final Score: {rec['final_score']:.4f}")
|
| 209 |
print(f"Reason: {rec['match_reason']}\n")
|
| 210 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
result = format_recommendation_html(final_recommendations)
|
| 212 |
return [gr.update(value=result), gr.update(visible=False)]
|
| 213 |
|
|
|
|
| 214 |
except Exception as e:
|
| 215 |
error_msg = f"Error processing your description. Details: {str(e)}"
|
| 216 |
return [gr.update(value=error_msg), gr.update(visible=False)]
|
| 217 |
+
|
| 218 |
+
# # 最終分數計算
|
| 219 |
+
# is_preferred = smart_rec.get('is_preferred', False)
|
| 220 |
+
# base_score = compatibility_scores.get('overall', 0.7)
|
| 221 |
+
# smart_score = smart_rec['score']
|
| 222 |
+
|
| 223 |
+
# # 根據是否為偏好品種調整分數
|
| 224 |
+
# if is_preferred:
|
| 225 |
+
# final_score = 0.95 # 確保最高分
|
| 226 |
+
# else:
|
| 227 |
+
# # 相似品種的分數計算
|
| 228 |
+
# final_score = min(0.90, (base_score * 0.6 + smart_score * 0.4))
|
| 229 |
+
|
| 230 |
+
# final_recommendations.append({
|
| 231 |
+
# 'rank': 0, # 稍後更新
|
| 232 |
+
# 'breed': breed_name,
|
| 233 |
+
# 'base_score': round(base_score, 4),
|
| 234 |
+
# 'smart_match_score': round(smart_score, 4),
|
| 235 |
+
# 'final_score': round(final_score, 4),
|
| 236 |
+
# 'scores': compatibility_scores,
|
| 237 |
+
# 'match_reason': smart_rec['reason'],
|
| 238 |
+
# 'info': breed_info,
|
| 239 |
+
# 'noise_info': breed_noise_info.get(breed_name, {}),
|
| 240 |
+
# 'health_info': breed_health_info.get(breed_name, {})
|
| 241 |
+
# })
|
| 242 |
+
|
| 243 |
+
# # 根據final_score重新排序
|
| 244 |
+
# final_recommendations.sort(key=lambda x: (-x['final_score'], x['breed']))
|
| 245 |
+
|
| 246 |
+
# # 更新排名
|
| 247 |
+
# for i, rec in enumerate(final_recommendations, 1):
|
| 248 |
+
# rec['rank'] = i
|
| 249 |
+
|
| 250 |
+
# # 驗證排序
|
| 251 |
+
# print("\nFinal Rankings:")
|
| 252 |
+
# for rec in final_recommendations:
|
| 253 |
+
# print(f"#{rec['rank']} {rec['breed']}")
|
| 254 |
+
# print(f"Base Score: {rec['base_score']:.4f}")
|
| 255 |
+
# print(f"Smart Match Score: {rec['smart_match_score']:.4f}")
|
| 256 |
+
# print(f"Final Score: {rec['final_score']:.4f}")
|
| 257 |
+
# print(f"Reason: {rec['match_reason']}\n")
|
| 258 |
+
|
| 259 |
+
# # 確保分數按降序排列
|
| 260 |
+
# if rec['rank'] > 1:
|
| 261 |
+
# prev_score = final_recommendations[rec['rank']-2]['final_score']
|
| 262 |
+
# if rec['final_score'] > prev_score:
|
| 263 |
+
# print(f"Warning: Ranking inconsistency detected!")
|
| 264 |
+
# print(f"#{rec['rank']-1} score: {prev_score:.4f}")
|
| 265 |
+
# print(f"#{rec['rank']} score: {rec['final_score']:.4f}")
|
| 266 |
+
|
| 267 |
+
# result = format_recommendation_html(final_recommendations)
|
| 268 |
+
# return [gr.update(value=result), gr.update(visible=False)]
|
| 269 |
+
|
| 270 |
+
|
| 271 |
+
# except Exception as e:
|
| 272 |
+
# error_msg = f"Error processing your description. Details: {str(e)}"
|
| 273 |
+
# return [gr.update(value=error_msg), gr.update(visible=False)]
|
| 274 |
+
|
| 275 |
|
| 276 |
def show_loading():
|
| 277 |
return [gr.update(value=""), gr.update(visible=True)]
|