Spaces:
Sleeping
Sleeping
Update breed_recommendation.py
Browse files- breed_recommendation.py +87 -50
breed_recommendation.py
CHANGED
|
@@ -530,64 +530,101 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
|
|
| 530 |
|
| 531 |
def on_find_match_click(*args):
|
| 532 |
try:
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 552 |
|
| 553 |
-
|
| 554 |
-
|
| 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 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 591 |
|
| 592 |
# 設置按鈕點擊事件
|
| 593 |
get_recommendations_btn.click(
|
|
|
|
| 530 |
|
| 531 |
def on_find_match_click(*args):
|
| 532 |
try:
|
| 533 |
+
yield gr.HTML("""
|
| 534 |
+
<div style="
|
| 535 |
+
text-align: center;
|
| 536 |
+
padding: 20px;
|
| 537 |
+
background: linear-gradient(to right, rgba(66, 153, 225, 0.1), rgba(72, 187, 120, 0.1));
|
| 538 |
+
border-radius: 10px;
|
| 539 |
+
margin: 20px 0;
|
| 540 |
+
">
|
| 541 |
+
<div style="font-size: 40px; margin-bottom: 10px;">🐕</div>
|
| 542 |
+
<p style="
|
| 543 |
+
color: #2D3748;
|
| 544 |
+
font-size: 1.2em;
|
| 545 |
+
margin: 0;
|
| 546 |
+
">
|
| 547 |
+
Finding your perfect match...
|
| 548 |
+
<br>
|
| 549 |
+
<span style="font-size: 0.9em; color: #4A5568;">
|
| 550 |
+
This may take a few moments
|
| 551 |
+
</span>
|
| 552 |
+
</p>
|
| 553 |
+
</div>
|
| 554 |
+
""")
|
| 555 |
+
# 創建用戶偏好對象
|
| 556 |
+
user_prefs = UserPreferences(
|
| 557 |
+
living_space=args[0],
|
| 558 |
+
yard_access=args[1],
|
| 559 |
+
exercise_time=args[2],
|
| 560 |
+
exercise_type=args[3],
|
| 561 |
+
grooming_commitment=args[4],
|
| 562 |
+
experience_level=args[5],
|
| 563 |
+
time_availability=args[6],
|
| 564 |
+
has_children=args[7],
|
| 565 |
+
children_age=args[8] if args[7] else None,
|
| 566 |
+
noise_tolerance=args[9],
|
| 567 |
+
space_for_play=True if args[0] != "apartment" else False,
|
| 568 |
+
other_pets=False,
|
| 569 |
+
climate="moderate",
|
| 570 |
+
health_sensitivity="medium",
|
| 571 |
+
barking_acceptance=args[9]
|
| 572 |
+
)
|
| 573 |
|
| 574 |
+
# 獲取推薦結果
|
| 575 |
+
recommendations = get_breed_recommendations(user_prefs, top_n=10)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 576 |
|
| 577 |
+
# 儲存搜尋歷史
|
| 578 |
+
history_results = [{
|
| 579 |
+
'breed': rec['breed'],
|
| 580 |
+
'rank': rec['rank'],
|
| 581 |
+
'overall_score': rec['final_score'],
|
| 582 |
+
'base_score': rec['base_score'],
|
| 583 |
+
'bonus_score': rec['bonus_score'],
|
| 584 |
+
'scores': rec['scores']
|
| 585 |
+
} for rec in recommendations]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 586 |
|
| 587 |
+
history_component.save_search(
|
| 588 |
+
user_preferences={
|
| 589 |
+
'living_space': args[0],
|
| 590 |
+
'yard_access': args[1],
|
| 591 |
+
'exercise_time': args[2],
|
| 592 |
+
'exercise_type': args[3],
|
| 593 |
+
'grooming_commitment': args[4],
|
| 594 |
+
'experience_level': args[5],
|
| 595 |
+
'time_availability': args[6],
|
| 596 |
+
'has_children': args[7],
|
| 597 |
+
'children_age': args[8] if args[7] else None,
|
| 598 |
+
'noise_tolerance': args[9],
|
| 599 |
+
'search_type': 'Criteria'
|
| 600 |
+
},
|
| 601 |
+
results=history_results
|
| 602 |
+
)
|
| 603 |
+
|
| 604 |
+
return format_recommendation_html(recommendations, is_description_search=False)
|
| 605 |
|
| 606 |
except Exception as e:
|
| 607 |
print(f"Error in find match: {str(e)}")
|
| 608 |
import traceback
|
| 609 |
print(traceback.format_exc())
|
| 610 |
+
return gr.HTML("""
|
| 611 |
+
<div style="
|
| 612 |
+
text-align: center;
|
| 613 |
+
padding: 20px;
|
| 614 |
+
background: rgba(255, 0, 0, 0.1);
|
| 615 |
+
border-radius: 10px;
|
| 616 |
+
margin: 20px 0;
|
| 617 |
+
">
|
| 618 |
+
<p style="
|
| 619 |
+
color: #d32f2f;
|
| 620 |
+
font-size: 1.1em;
|
| 621 |
+
margin: 0;
|
| 622 |
+
">
|
| 623 |
+
🐾 Oops! Something went wrong while finding your perfect match.
|
| 624 |
+
<br>Please try again!
|
| 625 |
+
</p>
|
| 626 |
+
</div>
|
| 627 |
+
""")
|
| 628 |
|
| 629 |
# 設置按鈕點擊事件
|
| 630 |
get_recommendations_btn.click(
|