DawnC commited on
Commit
7f7db0c
1 Parent(s): 53dba37

Update breed_recommendation.py

Browse files
Files changed (1) hide show
  1. breed_recommendation.py +31 -33
breed_recommendation.py CHANGED
@@ -449,6 +449,7 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
449
  elem_id="recommendation-output"
450
  )
451
 
 
452
  async def on_find_match_click(*args):
453
  try:
454
  loading_messages = [
@@ -459,7 +460,7 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
459
  "💭 Getting opinions from our experts..."
460
  ]
461
 
462
- # 顯示初始加載訊息
463
  for message in loading_messages:
464
  yield gr.HTML("""
465
  <div style="
@@ -483,29 +484,31 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
483
  </div>
484
  </div>
485
  """.format(message=message))
486
- await asyncio.sleep(1) # 每個訊息顯示1秒
487
-
 
488
  user_prefs = UserPreferences(
489
- living_space=args[0],
490
- yard_access=args[1],
491
- exercise_time=args[2],
492
- exercise_type=args[3],
493
- grooming_commitment=args[4],
494
- size_preference=args[5],
495
- experience_level=args[6],
496
- time_availability=args[7],
497
- has_children=args[8],
498
- children_age=args[9] if args[8] else None,
499
- noise_tolerance=args[10],
500
- space_for_play=True if args[0] != "apartment" else False,
501
- other_pets=False,
502
- climate="moderate",
503
- health_sensitivity="medium",
504
- barking_acceptance=args[10]
505
- )
506
-
507
  recommendations = get_breed_recommendations(user_prefs, top_n=15)
508
-
 
509
  history_results = [{
510
  'breed': rec['breed'],
511
  'rank': rec['rank'],
@@ -514,7 +517,7 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
514
  'bonus_score': rec['bonus_score'],
515
  'scores': rec['scores']
516
  } for rec in recommendations]
517
-
518
  history_component.save_search(
519
  user_preferences={
520
  'living_space': args[0],
@@ -532,20 +535,15 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
532
  },
533
  results=history_results
534
  )
535
-
536
- final_result = format_recommendation_html(recommendations, is_description_search=False)
537
- return final_result
538
-
539
  except Exception as e:
540
  print(f"Error in find match: {str(e)}")
541
  import traceback
542
  print(traceback.format_exc())
543
- return gr.HTML("""
544
- <div style="padding: 20px; background: #FEE2E2; border-radius: 8px; color: #991B1B;">
545
- <p style="margin: 0; font-weight: 500;">Sorry, we encountered an error while processing your request.</p>
546
- <p style="margin: 5px 0 0 0; font-size: 0.9em;">Please try again or contact support if the problem persists.</p>
547
- </div>
548
- """)
549
 
550
 
551
  get_recommendations_btn.click(
 
449
  elem_id="recommendation-output"
450
  )
451
 
452
+
453
  async def on_find_match_click(*args):
454
  try:
455
  loading_messages = [
 
460
  "💭 Getting opinions from our experts..."
461
  ]
462
 
463
+ # 顯示加載訊息
464
  for message in loading_messages:
465
  yield gr.HTML("""
466
  <div style="
 
484
  </div>
485
  </div>
486
  """.format(message=message))
487
+ await asyncio.sleep(1)
488
+
489
+ # 處理用戶數據和獲取推薦
490
  user_prefs = UserPreferences(
491
+ living_space=args[0],
492
+ yard_access=args[1],
493
+ exercise_time=args[2],
494
+ exercise_type=args[3],
495
+ grooming_commitment=args[4],
496
+ size_preference=args[5],
497
+ experience_level=args[6],
498
+ time_availability=args[7],
499
+ has_children=args[8],
500
+ children_age=args[9] if args[8] else None,
501
+ noise_tolerance=args[10],
502
+ space_for_play=True if args[0] != "apartment" else False,
503
+ other_pets=False,
504
+ climate="moderate",
505
+ health_sensitivity="medium",
506
+ barking_acceptance=args[10]
507
+ )
508
+
509
  recommendations = get_breed_recommendations(user_prefs, top_n=15)
510
+
511
+ # 保存搜索歷史
512
  history_results = [{
513
  'breed': rec['breed'],
514
  'rank': rec['rank'],
 
517
  'bonus_score': rec['bonus_score'],
518
  'scores': rec['scores']
519
  } for rec in recommendations]
520
+
521
  history_component.save_search(
522
  user_preferences={
523
  'living_space': args[0],
 
535
  },
536
  results=history_results
537
  )
538
+
539
+ # 使用 yield 而不是 return 來返回最終結果
540
+ yield gr.HTML(format_recommendation_html(recommendations, is_description_search=False))
541
+
542
  except Exception as e:
543
  print(f"Error in find match: {str(e)}")
544
  import traceback
545
  print(traceback.format_exc())
546
+ yield gr.HTML("Error getting recommendations")
 
 
 
 
 
547
 
548
 
549
  get_recommendations_btn.click(