Spaces:
Running
on
Zero
Running
on
Zero
Update breed_recommendation.py
Browse files- breed_recommendation.py +95 -18
breed_recommendation.py
CHANGED
|
@@ -125,8 +125,67 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
|
|
| 125 |
elem_id="recommendation-output" # 添加唯一ID以便追蹤
|
| 126 |
)
|
| 127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
def on_find_match_click(*args):
|
| 129 |
try:
|
|
|
|
|
|
|
|
|
|
| 130 |
user_prefs = UserPreferences(
|
| 131 |
living_space=args[0],
|
| 132 |
yard_access=args[1],
|
|
@@ -144,19 +203,33 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
|
|
| 144 |
health_sensitivity="medium",
|
| 145 |
barking_acceptance=args[9]
|
| 146 |
)
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
recommendations = get_breed_recommendations(user_prefs, top_n=10)
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
user_preferences={
|
| 161 |
'living_space': args[0],
|
| 162 |
'yard_access': args[1],
|
|
@@ -168,18 +241,22 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
|
|
| 168 |
'has_children': args[7],
|
| 169 |
'children_age': args[8] if args[7] else None,
|
| 170 |
'noise_tolerance': args[9],
|
| 171 |
-
'search_type': 'Criteria'
|
| 172 |
},
|
| 173 |
results=history_results
|
| 174 |
)
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
except Exception as e:
|
| 179 |
-
print(f"
|
| 180 |
import traceback
|
| 181 |
print(traceback.format_exc())
|
| 182 |
-
return "Error getting recommendations"
|
| 183 |
|
| 184 |
get_recommendations_btn.click(
|
| 185 |
fn=on_find_match_click,
|
|
|
|
| 125 |
elem_id="recommendation-output" # 添加唯一ID以便追蹤
|
| 126 |
)
|
| 127 |
|
| 128 |
+
# def on_find_match_click(*args):
|
| 129 |
+
# try:
|
| 130 |
+
# user_prefs = UserPreferences(
|
| 131 |
+
# living_space=args[0],
|
| 132 |
+
# yard_access=args[1],
|
| 133 |
+
# exercise_time=args[2],
|
| 134 |
+
# exercise_type=args[3],
|
| 135 |
+
# grooming_commitment=args[4],
|
| 136 |
+
# experience_level=args[5],
|
| 137 |
+
# time_availability=args[6],
|
| 138 |
+
# has_children=args[7],
|
| 139 |
+
# children_age=args[8] if args[7] else None,
|
| 140 |
+
# noise_tolerance=args[9],
|
| 141 |
+
# space_for_play=True if args[0] != "apartment" else False,
|
| 142 |
+
# other_pets=False,
|
| 143 |
+
# climate="moderate",
|
| 144 |
+
# health_sensitivity="medium",
|
| 145 |
+
# barking_acceptance=args[9]
|
| 146 |
+
# )
|
| 147 |
+
|
| 148 |
+
# recommendations = get_breed_recommendations(user_prefs, top_n=10)
|
| 149 |
+
|
| 150 |
+
# history_results = [{
|
| 151 |
+
# 'breed': rec['breed'],
|
| 152 |
+
# 'rank': rec['rank'],
|
| 153 |
+
# 'overall_score': rec['final_score'],
|
| 154 |
+
# 'base_score': rec['base_score'],
|
| 155 |
+
# 'bonus_score': rec['bonus_score'],
|
| 156 |
+
# 'scores': rec['scores']
|
| 157 |
+
# } for rec in recommendations]
|
| 158 |
+
|
| 159 |
+
# history_component.save_search(
|
| 160 |
+
# user_preferences={
|
| 161 |
+
# 'living_space': args[0],
|
| 162 |
+
# 'yard_access': args[1],
|
| 163 |
+
# 'exercise_time': args[2],
|
| 164 |
+
# 'exercise_type': args[3],
|
| 165 |
+
# 'grooming_commitment': args[4],
|
| 166 |
+
# 'experience_level': args[5],
|
| 167 |
+
# 'time_availability': args[6],
|
| 168 |
+
# 'has_children': args[7],
|
| 169 |
+
# 'children_age': args[8] if args[7] else None,
|
| 170 |
+
# 'noise_tolerance': args[9],
|
| 171 |
+
# 'search_type': 'Criteria'
|
| 172 |
+
# },
|
| 173 |
+
# results=history_results
|
| 174 |
+
# )
|
| 175 |
+
|
| 176 |
+
# return format_recommendation_html(recommendations, is_description_search=False)
|
| 177 |
+
|
| 178 |
+
# except Exception as e:
|
| 179 |
+
# print(f"Error in find match: {str(e)}")
|
| 180 |
+
# import traceback
|
| 181 |
+
# print(traceback.format_exc())
|
| 182 |
+
# return "Error getting recommendations"
|
| 183 |
+
|
| 184 |
def on_find_match_click(*args):
|
| 185 |
try:
|
| 186 |
+
# 添加數據驗證和日誌
|
| 187 |
+
print("Received args:", args) # 檢查接收到的參數
|
| 188 |
+
|
| 189 |
user_prefs = UserPreferences(
|
| 190 |
living_space=args[0],
|
| 191 |
yard_access=args[1],
|
|
|
|
| 203 |
health_sensitivity="medium",
|
| 204 |
barking_acceptance=args[9]
|
| 205 |
)
|
| 206 |
+
|
| 207 |
+
# 添加日誌來檢查 user_prefs 是否正確建立
|
| 208 |
+
print("UserPreferences created:", vars(user_prefs))
|
| 209 |
+
|
| 210 |
+
# 檢查推薦結果
|
| 211 |
recommendations = get_breed_recommendations(user_prefs, top_n=10)
|
| 212 |
+
print("Recommendations received:", recommendations)
|
| 213 |
+
|
| 214 |
+
# 確保 recommendations 不為空
|
| 215 |
+
if not recommendations:
|
| 216 |
+
return "No matching breeds found. Please adjust your criteria."
|
| 217 |
+
|
| 218 |
+
# 驗證推薦結果的格式
|
| 219 |
+
history_results = []
|
| 220 |
+
for rec in recommendations:
|
| 221 |
+
result = {
|
| 222 |
+
'breed': rec['breed'],
|
| 223 |
+
'rank': rec.get('rank', 0), # 使用 get 方法避免 KeyError
|
| 224 |
+
'overall_score': rec['final_score'],
|
| 225 |
+
'base_score': rec.get('base_score', 0),
|
| 226 |
+
'bonus_score': rec.get('bonus_score', 0),
|
| 227 |
+
'scores': rec.get('scores', {})
|
| 228 |
+
}
|
| 229 |
+
history_results.append(result)
|
| 230 |
+
|
| 231 |
+
# 檢查歷史記錄是否成功保存
|
| 232 |
+
save_success = history_component.save_search(
|
| 233 |
user_preferences={
|
| 234 |
'living_space': args[0],
|
| 235 |
'yard_access': args[1],
|
|
|
|
| 241 |
'has_children': args[7],
|
| 242 |
'children_age': args[8] if args[7] else None,
|
| 243 |
'noise_tolerance': args[9],
|
| 244 |
+
'search_type': 'Criteria'
|
| 245 |
},
|
| 246 |
results=history_results
|
| 247 |
)
|
| 248 |
+
print("History save success:", save_success)
|
| 249 |
+
|
| 250 |
+
# 產生並返回 HTML
|
| 251 |
+
html_result = format_recommendation_html(recommendations, is_description_search=False)
|
| 252 |
+
print("HTML generation completed")
|
| 253 |
+
return html_result
|
| 254 |
+
|
| 255 |
except Exception as e:
|
| 256 |
+
print(f"Detailed error in find match: {str(e)}")
|
| 257 |
import traceback
|
| 258 |
print(traceback.format_exc())
|
| 259 |
+
return f"Error getting recommendations: {str(e)}" # 返回更具體的錯誤訊息
|
| 260 |
|
| 261 |
get_recommendations_btn.click(
|
| 262 |
fn=on_find_match_click,
|