Spaces:
Running
on
Zero
Running
on
Zero
Update breed_recommendation.py
Browse files- breed_recommendation.py +81 -39
breed_recommendation.py
CHANGED
|
@@ -125,6 +125,63 @@ 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(
|
|
@@ -144,18 +201,27 @@ 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 |
history_component.save_search(
|
| 160 |
user_preferences={
|
| 161 |
'living_space': args[0],
|
|
@@ -168,19 +234,12 @@ 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"Error in find match: {str(e)}")
|
| 180 |
-
# import traceback
|
| 181 |
-
# print(traceback.format_exc())
|
| 182 |
-
# return "Error getting recommendations"
|
| 183 |
-
|
| 184 |
html_result = format_recommendation_html(recommendations, is_description_search=False)
|
| 185 |
enhanced_html = f"""
|
| 186 |
{html_result}
|
|
@@ -192,7 +251,7 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
|
|
| 192 |
"""
|
| 193 |
|
| 194 |
return gr.update(value=enhanced_html)
|
| 195 |
-
|
| 196 |
except Exception as e:
|
| 197 |
print(f"Error in find match: {str(e)}")
|
| 198 |
import traceback
|
|
@@ -200,23 +259,6 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
|
|
| 200 |
return gr.update(value="Error getting recommendations")
|
| 201 |
|
| 202 |
|
| 203 |
-
# get_recommendations_btn.click(
|
| 204 |
-
# fn=on_find_match_click,
|
| 205 |
-
# inputs=[
|
| 206 |
-
# living_space,
|
| 207 |
-
# yard_access,
|
| 208 |
-
# exercise_time,
|
| 209 |
-
# exercise_type,
|
| 210 |
-
# grooming_commitment,
|
| 211 |
-
# experience_level,
|
| 212 |
-
# time_availability,
|
| 213 |
-
# has_children,
|
| 214 |
-
# children_age,
|
| 215 |
-
# noise_tolerance
|
| 216 |
-
# ],
|
| 217 |
-
# outputs=recommendation_output
|
| 218 |
-
# )
|
| 219 |
-
|
| 220 |
get_recommendations_btn.click(
|
| 221 |
fn=on_find_match_click,
|
| 222 |
inputs=[
|
|
|
|
| 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 |
+
|
| 185 |
def on_find_match_click(*args):
|
| 186 |
try:
|
| 187 |
user_prefs = UserPreferences(
|
|
|
|
| 201 |
health_sensitivity="medium",
|
| 202 |
barking_acceptance=args[9]
|
| 203 |
)
|
| 204 |
+
|
| 205 |
recommendations = get_breed_recommendations(user_prefs, top_n=10)
|
| 206 |
+
|
| 207 |
+
# 確保推薦結果不為空
|
| 208 |
+
if not recommendations:
|
| 209 |
+
return gr.update(value="No matching breeds found. Please try adjusting your criteria.")
|
| 210 |
+
|
| 211 |
+
# 創建完整的歷史記錄結果
|
| 212 |
+
history_results = []
|
| 213 |
+
for i, rec in enumerate(recommendations, 1):
|
| 214 |
+
history_results.append({
|
| 215 |
+
'breed': rec['breed'],
|
| 216 |
+
'rank': i, # 確保排名從1開始
|
| 217 |
+
'overall_score': rec['final_score'],
|
| 218 |
+
'base_score': rec['base_score'],
|
| 219 |
+
'bonus_score': rec['bonus_score'],
|
| 220 |
+
'scores': rec['scores'],
|
| 221 |
+
'display_score': f"{rec['final_score']*100:.1f}%" # 添加顯示用的分數
|
| 222 |
+
})
|
| 223 |
+
|
| 224 |
+
# 保存完整的搜索記錄
|
| 225 |
history_component.save_search(
|
| 226 |
user_preferences={
|
| 227 |
'living_space': args[0],
|
|
|
|
| 234 |
'has_children': args[7],
|
| 235 |
'children_age': args[8] if args[7] else None,
|
| 236 |
'noise_tolerance': args[9],
|
| 237 |
+
'search_type': 'Criteria'
|
| 238 |
},
|
| 239 |
results=history_results
|
| 240 |
)
|
| 241 |
+
|
| 242 |
+
# 生成HTML結果
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
html_result = format_recommendation_html(recommendations, is_description_search=False)
|
| 244 |
enhanced_html = f"""
|
| 245 |
{html_result}
|
|
|
|
| 251 |
"""
|
| 252 |
|
| 253 |
return gr.update(value=enhanced_html)
|
| 254 |
+
|
| 255 |
except Exception as e:
|
| 256 |
print(f"Error in find match: {str(e)}")
|
| 257 |
import traceback
|
|
|
|
| 259 |
return gr.update(value="Error getting recommendations")
|
| 260 |
|
| 261 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
get_recommendations_btn.click(
|
| 263 |
fn=on_find_match_click,
|
| 264 |
inputs=[
|