from typing import Dict, Any import traceback import gradio as gr from recommendation_html_format import ( format_recommendation_html, get_breed_recommendations ) def create_recommendation_tab(UserPreferences, get_breed_recommendations, format_recommendation_html, history_component): with gr.TabItem("Breed Recommendation"): gr.HTML("
Tell us about your lifestyle, and we'll recommend the perfect dog breeds for you!
") 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" ) exercise_time = gr.Slider( minimum=0, maximum=180, value=60, label="Daily exercise time (minutes)", info="Consider walks, play time, and training" ) grooming_commitment = gr.Radio( choices=["low", "medium", "high"], label="Grooming commitment level", info="Low: monthly, Medium: weekly, High: daily", value="medium" ) with gr.Column(): experience_level = gr.Radio( choices=["beginner", "intermediate", "advanced"], label="Dog ownership experience", info="Be honest - this helps find the right match", value="beginner" ) has_children = gr.Checkbox( label="Have children at home", info="Helps recommend child-friendly breeds" ) noise_tolerance = gr.Radio( choices=["low", "medium", "high"], label="Noise tolerance level", info="Some breeds are more vocal than others", value="medium" ) get_recommendations_btn = gr.Button("Find My Perfect Match! 🔍", variant="primary") recommendation_output = gr.HTML(label="Breed Recommendations") def on_find_match_click(*args): try: user_prefs = UserPreferences( living_space=args[0], exercise_time=args[1], grooming_commitment=args[2], experience_level=args[3], has_children=args[4], noise_tolerance=args[5], space_for_play=True if args[0] != "apartment" else False, other_pets=False, climate="moderate", health_sensitivity="medium", # 新增: 默認中等敏感度 barking_acceptance=args[5] # 使用 noise_tolerance 作為 barking_acceptance ) 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], 'exercise_time': args[1], 'grooming_commitment': args[2], 'experience_level': args[3], 'has_children': args[4], 'noise_tolerance': args[5], 'health_sensitivity': "medium", 'barking_acceptance': args[5] }, results=history_results ) return format_recommendation_html(recommendations) 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, exercise_time, grooming_commitment, experience_level, has_children, 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 }