Spaces:
Running
on
Zero
Running
on
Zero
Update breed_recommendation.py
Browse files- breed_recommendation.py +20 -28
breed_recommendation.py
CHANGED
@@ -183,8 +183,9 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
|
|
183 |
|
184 |
def on_find_match_click(*args):
|
185 |
try:
|
186 |
-
#
|
187 |
-
|
|
|
188 |
|
189 |
user_prefs = UserPreferences(
|
190 |
living_space=args[0],
|
@@ -203,33 +204,27 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
|
|
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 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
|
231 |
-
|
232 |
-
save_success = history_component.save_search(
|
233 |
user_preferences={
|
234 |
'living_space': args[0],
|
235 |
'yard_access': args[1],
|
@@ -241,22 +236,19 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
|
|
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 |
-
|
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,
|
|
|
183 |
|
184 |
def on_find_match_click(*args):
|
185 |
try:
|
186 |
+
# 時間處理
|
187 |
+
from datetime import datetime
|
188 |
+
import pytz
|
189 |
|
190 |
user_prefs = UserPreferences(
|
191 |
living_space=args[0],
|
|
|
204 |
health_sensitivity="medium",
|
205 |
barking_acceptance=args[9]
|
206 |
)
|
|
|
|
|
|
|
207 |
|
|
|
208 |
recommendations = get_breed_recommendations(user_prefs, top_n=10)
|
|
|
209 |
|
210 |
# 確保 recommendations 不為空
|
211 |
if not recommendations:
|
212 |
return "No matching breeds found. Please adjust your criteria."
|
213 |
|
214 |
+
history_results = [{
|
215 |
+
'breed': rec['breed'],
|
216 |
+
'rank': rec.get('rank', 0),
|
217 |
+
'overall_score': rec['final_score'],
|
218 |
+
'base_score': rec.get('base_score', 0),
|
219 |
+
'bonus_score': rec.get('bonus_score', 0),
|
220 |
+
'scores': rec.get('scores', {})
|
221 |
+
} for rec in recommendations]
|
222 |
+
|
223 |
+
# 添加時間戳
|
224 |
+
taipei_tz = pytz.timezone('Asia/Taipei')
|
225 |
+
current_time = datetime.now(taipei_tz)
|
226 |
|
227 |
+
history_component.save_search(
|
|
|
228 |
user_preferences={
|
229 |
'living_space': args[0],
|
230 |
'yard_access': args[1],
|
|
|
236 |
'has_children': args[7],
|
237 |
'children_age': args[8] if args[7] else None,
|
238 |
'noise_tolerance': args[9],
|
239 |
+
'search_type': 'Criteria',
|
240 |
+
'timestamp': current_time.strftime("%Y-%m-%d %H:%M:%S")
|
241 |
},
|
242 |
results=history_results
|
243 |
)
|
|
|
244 |
|
245 |
+
return format_recommendation_html(recommendations, is_description_search=False)
|
|
|
|
|
|
|
246 |
|
247 |
except Exception as e:
|
248 |
print(f"Detailed error in find match: {str(e)}")
|
249 |
import traceback
|
250 |
print(traceback.format_exc())
|
251 |
+
return f"Error getting recommendations: {str(e)}"
|
252 |
|
253 |
get_recommendations_btn.click(
|
254 |
fn=on_find_match_click,
|