Spaces:
Sleeping
Sleeping
Update breed_recommendation.py
Browse files- breed_recommendation.py +17 -28
breed_recommendation.py
CHANGED
@@ -182,7 +182,7 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
|
|
182 |
# return "Error getting recommendations"
|
183 |
|
184 |
|
185 |
-
|
186 |
try:
|
187 |
user_prefs = UserPreferences(
|
188 |
living_space=args[0],
|
@@ -204,24 +204,21 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
|
|
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 |
-
|
215 |
'breed': rec['breed'],
|
216 |
-
'rank': i,
|
217 |
'overall_score': rec['final_score'],
|
218 |
-
'base_score': rec
|
219 |
-
'bonus_score': rec
|
220 |
-
'scores': rec
|
221 |
-
'
|
222 |
-
}
|
|
|
223 |
|
224 |
-
#
|
225 |
history_component.save_search(
|
226 |
user_preferences={
|
227 |
'living_space': args[0],
|
@@ -239,25 +236,17 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
|
|
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}
|
246 |
-
<script>
|
247 |
-
document.getElementById('recommendation-output').scrollIntoView({{
|
248 |
-
behavior: 'smooth'
|
249 |
-
}});
|
250 |
-
</script>
|
251 |
-
"""
|
252 |
|
253 |
-
|
|
|
|
|
254 |
|
255 |
except Exception as e:
|
256 |
print(f"Error in find match: {str(e)}")
|
257 |
-
|
258 |
-
|
259 |
-
return gr.update(value="Error getting recommendations")
|
260 |
-
|
261 |
|
262 |
get_recommendations_btn.click(
|
263 |
fn=on_find_match_click,
|
|
|
182 |
# return "Error getting recommendations"
|
183 |
|
184 |
|
185 |
+
def on_find_match_click(*args):
|
186 |
try:
|
187 |
user_prefs = UserPreferences(
|
188 |
living_space=args[0],
|
|
|
204 |
|
205 |
recommendations = get_breed_recommendations(user_prefs, top_n=10)
|
206 |
|
207 |
+
# 格式化結果以確保所有必要的資訊都被包含
|
|
|
|
|
|
|
|
|
208 |
history_results = []
|
209 |
for i, rec in enumerate(recommendations, 1):
|
210 |
+
result_entry = {
|
211 |
'breed': rec['breed'],
|
212 |
+
'rank': i,
|
213 |
'overall_score': rec['final_score'],
|
214 |
+
'base_score': rec.get('base_score', 0),
|
215 |
+
'bonus_score': rec.get('bonus_score', 0),
|
216 |
+
'scores': rec.get('scores', {}),
|
217 |
+
'final_score': rec['final_score'] # 確保這個欄位存在
|
218 |
+
}
|
219 |
+
history_results.append(result_entry)
|
220 |
|
221 |
+
# 保存搜索歷史
|
222 |
history_component.save_search(
|
223 |
user_preferences={
|
224 |
'living_space': args[0],
|
|
|
236 |
results=history_results
|
237 |
)
|
238 |
|
239 |
+
# 生成 HTML 結果
|
240 |
html_result = format_recommendation_html(recommendations, is_description_search=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
241 |
|
242 |
+
# 強制更新顯示
|
243 |
+
gr.Info("Results updated successfully!")
|
244 |
+
return gr.update(value=html_result)
|
245 |
|
246 |
except Exception as e:
|
247 |
print(f"Error in find match: {str(e)}")
|
248 |
+
traceback.print_exc()
|
249 |
+
return gr.update(value="Error getting recommendations")
|
|
|
|
|
250 |
|
251 |
get_recommendations_btn.click(
|
252 |
fn=on_find_match_click,
|