import sqlite3
import gradio as gr
import asyncio
from typing import Generator
from dog_database import get_dog_description, dog_data
from breed_health_info import breed_health_info
from breed_noise_info import breed_noise_info
from scoring_calculation_system import UserPreferences, calculate_compatibility_score
from recommendation_html_format import format_recommendation_html, get_breed_recommendations
from search_history import create_history_tab, create_history_component
def filter_breed_matches(user_prefs: UserPreferences, top_n: int = 10) -> List[Dict]:
"""
根據使用者偏好篩選並推薦狗狗品種。
Parameters:
user_prefs: 使用者偏好設定
top_n: 要返回的推薦數量
Returns:
List[Dict]: 排序後的推薦品種列表
"""
all_breeds = []
for breed_info in breed_database:
score = calculate_compatibility_score(breed_info, user_prefs)
if score is not None: # 只添加未被過濾的品種
all_breeds.append({
'breed': breed_info['Breed'],
'final_score': score['overall'],
'base_score': score.get('base_score', 0),
'bonus_score': score.get('bonus_score', 0),
'size': breed_info['Size'],
'scores': score
})
# 根據體型偏好過濾
if user_prefs.size_preference != "no_preference":
filtered_breeds = [b for b in all_breeds if b['size'].lower() == user_prefs.size_preference.lower()]
# 如果符合體型的品種太少,調整返回數量
if len(filtered_breeds) < 5: # 設定最少要有5種品種
top_n = len(filtered_breeds)
else:
filtered_breeds = all_breeds
# 為每個品種添加排名
sorted_breeds = sorted(filtered_breeds, key=lambda x: x['final_score'], reverse=True)
for i, breed in enumerate(sorted_breeds, 1):
breed['rank'] = i
return sorted_breeds[:top_n]
def create_recommendation_tab(UserPreferences, get_breed_recommendations, format_recommendation_html, history_component):
with gr.TabItem("Breed Recommendation"):
with gr.Tabs():
with gr.Tab("Find by Criteria"):
gr.HTML("""
BETA
Tell us about your lifestyle, and we'll recommend the perfect dog breeds for you!
🔬
Beta Feature: Our matching algorithm is continuously improving. Results are for reference only.
""")
with gr.Row():
with gr.Column():
living_space = gr.Radio(
choices=["apartment", "house_small", "house_large"],
label="What type of living space do you have?",
info="Choose your current living situation",
value="apartment"
)
yard_access = gr.Radio(
choices=["no_yard", "shared_yard", "private_yard"],
label="Yard Access Type",
info="Available outdoor space",
value="no_yard"
)
exercise_time = gr.Slider(
minimum=0,
maximum=180,
value=60,
label="Daily exercise time (minutes)",
info="Consider walks, play time, and training"
)
exercise_type = gr.Radio(
choices=["light_walks", "moderate_activity", "active_training"],
label="Exercise Style",
info="What kind of activities do you prefer?",
value="moderate_activity"
)
grooming_commitment = gr.Radio(
choices=["low", "medium", "high"],
label="Grooming commitment level",
info="Low: monthly, Medium: weekly, High: daily",
value="medium"
)
with gr.Column():
size_preference = gr.Radio(
choices=["no_preference", "small", "medium", "large", "giant"],
label="Preference Dog Size",
info="Select your preferred dog size - this will strongly filter the recommendations",
value = "no_preference"
)
experience_level = gr.Radio(
choices=["beginner", "intermediate", "advanced"],
label="Dog ownership experience",
info="Be honest - this helps find the right match",
value="beginner"
)
time_availability = gr.Radio(
choices=["limited", "moderate", "flexible"],
label="Time Availability",
info="Time available for dog care daily",
value="moderate"
)
has_children = gr.Checkbox(
label="Have children at home",
info="Helps recommend child-friendly breeds"
)
children_age = gr.Radio(
choices=["toddler", "school_age", "teenager"],
label="Children's Age Group",
info="Helps match with age-appropriate breeds",
visible=False # 默認隱藏,只在has_children=True時顯示
)
noise_tolerance = gr.Radio(
choices=["low", "medium", "high"],
label="Noise tolerance level",
info="Some breeds are more vocal than others",
value="medium"
)
def update_children_age_visibility(has_children):
return gr.update(visible=has_children)
has_children.change(
fn=update_children_age_visibility,
inputs=has_children,
outputs=children_age
)
get_recommendations_btn = gr.Button("Find My Perfect Match! 🔍", variant="primary")
recommendation_output = gr.HTML(
label="Breed Recommendations",
visible=True, # 確保可見性
elem_id="recommendation-output"
)
def on_find_match_click(*args):
try:
user_prefs = UserPreferences(
living_space=args[0],
yard_access=args[1],
exercise_time=args[2],
exercise_type=args[3],
grooming_commitment=args[4],
size_preference=args[5],
experience_level=args[6],
time_availability=args[7],
has_children=args[8],
children_age=args[9] if args[8] else None,
noise_tolerance=args[10],
space_for_play=True if args[0] != "apartment" else False,
other_pets=False,
climate="moderate",
health_sensitivity="medium",
barking_acceptance=args[10]
)
recommendations = get_breed_recommendations(user_prefs, top_n=10)
history_results = [{
'breed': rec['breed'],
'rank': rec['rank'],
'overall_score': rec['final_score'],
'base_score': rec['base_score'],
'bonus_score': rec['bonus_score'],
'scores': rec['scores']
} for rec in recommendations]
history_component.save_search(
user_preferences={
'living_space': args[0],
'yard_access': args[1],
'exercise_time': args[2],
'exercise_type': args[3],
'grooming_commitment': args[4],
'experience_level': args[5],
'time_availability': args[6],
'has_children': args[7],
'children_age': args[8] if args[7] else None,
'noise_tolerance': args[9],
'search_type': 'Criteria'
},
results=history_results
)
return format_recommendation_html(recommendations, is_description_search=False)
except Exception as e:
print(f"Error in find match: {str(e)}")
import traceback
print(traceback.format_exc())
return "Error getting recommendations"
get_recommendations_btn.click(
fn=on_find_match_click,
inputs=[
living_space,
yard_access,
exercise_time,
exercise_type,
grooming_commitment,
size_preference,
experience_level,
time_availability,
has_children,
children_age,
noise_tolerance
],
outputs=recommendation_output
)
return {
'living_space': living_space,
'exercise_time': exercise_time,
'grooming_commitment': grooming_commitment,
'experience_level': experience_level,
'has_children': has_children,
'noise_tolerance': noise_tolerance,
'get_recommendations_btn': get_recommendations_btn,
'recommendation_output': recommendation_output,
}