Spaces:
Running
on
Zero
Running
on
Zero
File size: 5,054 Bytes
505d2dd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
import gradio as gr
def create_recommendation_tab(UserPreferences, get_breed_recommendations, format_recommendation_html, history_component):
"""创建品种推荐标签页
Args:
UserPreferences: 用户偏好类
get_breed_recommendations: 获取品种推荐的函数
format_recommendation_html: 格式化推荐结果的函数
history_component: 历史记录组件
"""
with gr.TabItem("Breed Recommendation"):
gr.HTML("<p style='text-align: center;'>Tell us about your lifestyle, and we'll recommend the perfect dog breeds for you!</p>")
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"
)
recommendations = get_breed_recommendations(user_prefs)
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]
},
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
}
|