DawnC commited on
Commit
af6b2e9
1 Parent(s): 10b1aec

Update breed_recommendation.py

Browse files
Files changed (1) hide show
  1. breed_recommendation.py +58 -67
breed_recommendation.py CHANGED
@@ -528,92 +528,83 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
528
  elem_id="recommendation-output"
529
  )
530
 
531
- # 處理推薦請求的函數
532
  def on_find_match_click(*args):
533
  try:
534
- # 顯示 loading 狀態
535
- yield loading_html.update(visible=True)
536
-
537
- # 創建用戶偏好對象
538
- user_prefs = UserPreferences(
539
- living_space=args[0],
540
- yard_access=args[1],
541
- exercise_time=args[2],
542
- exercise_type=args[3],
543
- grooming_commitment=args[4],
544
- experience_level=args[5],
545
- time_availability=args[6],
546
- has_children=args[7],
547
- children_age=args[8] if args[7] else None,
548
- noise_tolerance=args[9],
549
- space_for_play=True if args[0] != "apartment" else False,
550
- other_pets=False,
551
- climate="moderate",
552
- health_sensitivity="medium",
553
- barking_acceptance=args[9]
554
- )
555
-
556
- # 獲取推薦結果
557
- recommendations = get_breed_recommendations(user_prefs, top_n=10)
558
-
559
- # 儲存搜尋歷史
560
- history_results = [{
561
- 'breed': rec['breed'],
562
- 'rank': rec['rank'],
563
- 'overall_score': rec['final_score'],
564
- 'base_score': rec['base_score'],
565
- 'bonus_score': rec['bonus_score'],
566
- 'scores': rec['scores']
567
- } for rec in recommendations]
568
-
569
- history_component.save_search(
570
- user_preferences={
571
- 'living_space': args[0],
572
- 'yard_access': args[1],
573
- 'exercise_time': args[2],
574
- 'exercise_type': args[3],
575
- 'grooming_commitment': args[4],
576
- 'experience_level': args[5],
577
- 'time_availability': args[6],
578
- 'has_children': args[7],
579
- 'children_age': args[8] if args[7] else None,
580
- 'noise_tolerance': args[9],
581
- 'search_type': 'Criteria'
582
- },
583
- results=history_results
584
- )
585
-
586
- # 隱藏 loading 狀態並返回結果
587
- return [
588
- loading_html.update(visible=False),
589
- format_recommendation_html(recommendations, is_description_search=False)
590
- ]
591
-
592
  except Exception as e:
593
  print(f"Error in find match: {str(e)}")
594
  import traceback
595
  print(traceback.format_exc())
596
- return [
597
- loading_html.update(visible=False),
598
- "Oops! Something went wrong while finding your perfect match. Please try again!"
599
- ]
600
 
601
  # 設置按鈕點擊事件
602
  get_recommendations_btn.click(
603
  fn=on_find_match_click,
604
  inputs=[
605
  living_space,
606
- yard_access,
607
  exercise_time,
608
- exercise_type,
609
  grooming_commitment,
610
  experience_level,
611
- time_availability,
612
  has_children,
613
  children_age,
614
  noise_tolerance
615
  ],
616
- outputs=[loading_html, recommendation_output]
617
  )
618
 
619
  # 返回頁面組件
 
528
  elem_id="recommendation-output"
529
  )
530
 
 
531
  def on_find_match_click(*args):
532
  try:
533
+ with gr.Progress() as progress:
534
+ # 創建用戶偏好對象
535
+ user_prefs = UserPreferences(
536
+ living_space=args[0],
537
+ yard_access=args[1],
538
+ exercise_time=args[2],
539
+ exercise_type=args[3],
540
+ grooming_commitment=args[4],
541
+ experience_level=args[5],
542
+ time_availability=args[6],
543
+ has_children=args[7],
544
+ children_age=args[8] if args[7] else None,
545
+ noise_tolerance=args[9],
546
+ space_for_play=True if args[0] != "apartment" else False,
547
+ other_pets=False,
548
+ climate="moderate",
549
+ health_sensitivity="medium",
550
+ barking_acceptance=args[9]
551
+ )
552
+
553
+ progress(0.3, desc="Finding your perfect match...")
554
+ recommendations = get_breed_recommendations(user_prefs, top_n=10)
555
+
556
+ progress(0.6, desc="Analyzing compatibility...")
557
+ history_results = [{
558
+ 'breed': rec['breed'],
559
+ 'rank': rec['rank'],
560
+ 'overall_score': rec['final_score'],
561
+ 'base_score': rec['base_score'],
562
+ 'bonus_score': rec['bonus_score'],
563
+ 'scores': rec['scores']
564
+ } for rec in recommendations]
565
+
566
+ progress(0.8, desc="Preparing results...")
567
+ history_component.save_search(
568
+ user_preferences={
569
+ 'living_space': args[0],
570
+ 'yard_access': args[1],
571
+ 'exercise_time': args[2],
572
+ 'exercise_type': args[3],
573
+ 'grooming_commitment': args[4],
574
+ 'experience_level': args[5],
575
+ 'time_availability': args[6],
576
+ 'has_children': args[7],
577
+ 'children_age': args[8] if args[7] else None,
578
+ 'noise_tolerance': args[9],
579
+ 'search_type': 'Criteria'
580
+ },
581
+ results=history_results
582
+ )
583
+
584
+ return format_recommendation_html(recommendations, is_description_search=False)
585
+
 
 
 
 
 
586
  except Exception as e:
587
  print(f"Error in find match: {str(e)}")
588
  import traceback
589
  print(traceback.format_exc())
590
+ return "Oops! Something went wrong while finding your perfect match. Please try again!"
 
 
 
591
 
592
  # 設置按鈕點擊事件
593
  get_recommendations_btn.click(
594
  fn=on_find_match_click,
595
  inputs=[
596
  living_space,
597
+ yard_access,
598
  exercise_time,
599
+ exercise_type,
600
  grooming_commitment,
601
  experience_level,
602
+ time_availability,
603
  has_children,
604
  children_age,
605
  noise_tolerance
606
  ],
607
+ outputs=recommendation_output
608
  )
609
 
610
  # 返回頁面組件