DawnC commited on
Commit
bb53524
1 Parent(s): 0011db0

Update breed_recommendation.py

Browse files
Files changed (1) hide show
  1. breed_recommendation.py +14 -20
breed_recommendation.py CHANGED
@@ -211,24 +211,21 @@ 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
  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],
@@ -242,17 +239,14 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
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)}"
258
 
 
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],
 
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