Spaces:
Sleeping
Sleeping
Update breed_recommendation.py
Browse files- 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 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
| 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=
|
| 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 |
# 返回頁面組件
|