DawnC commited on
Commit
0011db0
·
verified ·
1 Parent(s): 04b305f

Update breed_recommendation.py

Browse files
Files changed (1) hide show
  1. breed_recommendation.py +23 -17
breed_recommendation.py CHANGED
@@ -211,19 +211,24 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
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],
@@ -235,17 +240,18 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
235
  'time_availability': args[6],
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)}"
 
211
  if not recommendations:
212
  return "No matching breeds found. Please adjust your criteria."
213
 
214
+ history_results = []
215
+ for i, rec in enumerate(recommendations, 1):
216
+ # 確保每個結果都有完整的數據結構
217
+ result = {
218
+ 'breed': rec['breed'],
219
+ 'rank': i, # 使用循環索引確保排名連續
220
+ 'overall_score': rec.get('final_score', 0), # 使用 get 方法提供默認值
221
+ 'final_score': rec.get('final_score', 0), # 確保有 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
+ history_results.append(result)
227
 
228
+ # 添加調試輸出
229
+ print("History results to be saved:", history_results)
 
230
 
231
+ # 保存搜索歷史時確保數據完整性
232
  history_component.save_search(
233
  user_preferences={
234
  'living_space': args[0],
 
240
  'time_availability': args[6],
241
  'has_children': args[7],
242
  'children_age': args[8] if args[7] else None,
243
+ 'noise_tolerance': args[9]
 
 
244
  },
245
+ results=history_results # 使用處理過的結果
246
  )
247
 
248
+ # 返回推薦結果的 HTML 格式
249
+ html_result = format_recommendation_html(recommendations, is_description_search=False)
250
+
251
+ return html_result
252
 
253
  except Exception as e:
254
+ print(f"詳細錯誤: {str(e)}")
255
  import traceback
256
  print(traceback.format_exc())
257
  return f"Error getting recommendations: {str(e)}"