DawnC commited on
Commit
7a0b67e
1 Parent(s): 0c605f2

Update breed_recommendation.py

Browse files
Files changed (1) hide show
  1. breed_recommendation.py +20 -89
breed_recommendation.py CHANGED
@@ -125,68 +125,8 @@ 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],
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
- from datetime import datetime
188
- import pytz
189
-
190
  user_prefs = UserPreferences(
191
  living_space=args[0],
192
  yard_access=args[1],
@@ -204,28 +144,18 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
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
- # 創建標準化的結果列表
215
- formatted_recommendations = []
216
- for i, rec in enumerate(recommendations, 1):
217
- formatted_rec = {
218
- 'breed': rec['breed'],
219
- 'rank': i,
220
- 'final_score': rec['final_score'], # 直接使用,因為這是必需的
221
- 'overall_score': rec['final_score'], # 複製 final_score
222
- 'base_score': rec.get('base_score', 0),
223
- 'bonus_score': rec.get('bonus_score', 0),
224
- 'scores': rec.get('scores', {})
225
- }
226
- formatted_recommendations.append(formatted_rec)
227
-
228
- # 使用相同的格式化數據來保存歷史記錄和生成 HTML
229
  history_component.save_search(
230
  user_preferences={
231
  'living_space': args[0],
@@ -237,18 +167,19 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
237
  'time_availability': args[6],
238
  'has_children': args[7],
239
  'children_age': args[8] if args[7] else None,
240
- 'noise_tolerance': args[9]
 
241
  },
242
- results=formatted_recommendations # 使用格式化後的結果
243
  )
244
-
245
- # 使用相同的格式化數據生成 HTML
246
- return format_recommendation_html(formatted_recommendations, is_description_search=False)
247
-
248
  except Exception as e:
249
- print(f"Error details: {str(e)}")
 
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,
 
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
  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],
 
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
  get_recommendations_btn.click(
185
  fn=on_find_match_click,