Spaces:
Running
on
Zero
Running
on
Zero
Delete breed_recommendation.py
Browse files- breed_recommendation.py +0 -134
breed_recommendation.py
DELETED
@@ -1,134 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
|
3 |
-
def create_recommendation_tab(UserPreferences, get_breed_recommendations, format_recommendation_html, history_component):
|
4 |
-
"""创建品种推荐标签页
|
5 |
-
|
6 |
-
Args:
|
7 |
-
UserPreferences: 用户偏好类
|
8 |
-
get_breed_recommendations: 获取品种推荐的函数
|
9 |
-
format_recommendation_html: 格式化推荐结果的函数
|
10 |
-
history_component: 历史记录组件
|
11 |
-
"""
|
12 |
-
with gr.TabItem("Breed Recommendation"):
|
13 |
-
gr.HTML("<p style='text-align: center;'>Tell us about your lifestyle, and we'll recommend the perfect dog breeds for you!</p>")
|
14 |
-
|
15 |
-
with gr.Row():
|
16 |
-
with gr.Column():
|
17 |
-
living_space = gr.Radio(
|
18 |
-
choices=["apartment", "house_small", "house_large"],
|
19 |
-
label="What type of living space do you have?",
|
20 |
-
info="Choose your current living situation",
|
21 |
-
value="apartment"
|
22 |
-
)
|
23 |
-
|
24 |
-
exercise_time = gr.Slider(
|
25 |
-
minimum=0,
|
26 |
-
maximum=180,
|
27 |
-
value=60,
|
28 |
-
label="Daily exercise time (minutes)",
|
29 |
-
info="Consider walks, play time, and training"
|
30 |
-
)
|
31 |
-
|
32 |
-
grooming_commitment = gr.Radio(
|
33 |
-
choices=["low", "medium", "high"],
|
34 |
-
label="Grooming commitment level",
|
35 |
-
info="Low: monthly, Medium: weekly, High: daily",
|
36 |
-
value="medium"
|
37 |
-
)
|
38 |
-
|
39 |
-
with gr.Column():
|
40 |
-
experience_level = gr.Radio(
|
41 |
-
choices=["beginner", "intermediate", "advanced"],
|
42 |
-
label="Dog ownership experience",
|
43 |
-
info="Be honest - this helps find the right match",
|
44 |
-
value="beginner"
|
45 |
-
)
|
46 |
-
|
47 |
-
has_children = gr.Checkbox(
|
48 |
-
label="Have children at home",
|
49 |
-
info="Helps recommend child-friendly breeds"
|
50 |
-
)
|
51 |
-
|
52 |
-
noise_tolerance = gr.Radio(
|
53 |
-
choices=["low", "medium", "high"],
|
54 |
-
label="Noise tolerance level",
|
55 |
-
info="Some breeds are more vocal than others",
|
56 |
-
value="medium"
|
57 |
-
)
|
58 |
-
|
59 |
-
get_recommendations_btn = gr.Button("Find My Perfect Match! 🔍", variant="primary")
|
60 |
-
recommendation_output = gr.HTML(label="Breed Recommendations")
|
61 |
-
|
62 |
-
def on_find_match_click(*args):
|
63 |
-
try:
|
64 |
-
user_prefs = UserPreferences(
|
65 |
-
living_space=args[0],
|
66 |
-
exercise_time=args[1],
|
67 |
-
grooming_commitment=args[2],
|
68 |
-
experience_level=args[3],
|
69 |
-
has_children=args[4],
|
70 |
-
noise_tolerance=args[5],
|
71 |
-
space_for_play=True if args[0] != "apartment" else False,
|
72 |
-
other_pets=False,
|
73 |
-
climate="moderate",
|
74 |
-
health_sensitivity="medium", # 新增: 默認中等敏感度
|
75 |
-
barking_acceptance=args[5] # 使用 noise_tolerance 作為 barking_acceptance
|
76 |
-
)
|
77 |
-
|
78 |
-
recommendations = get_breed_recommendations(user_prefs, top_n=10)
|
79 |
-
|
80 |
-
history_results = [{
|
81 |
-
'breed': rec['breed'],
|
82 |
-
'rank': rec['rank'],
|
83 |
-
'overall_score': rec['final_score'],
|
84 |
-
'base_score': rec['base_score'],
|
85 |
-
'bonus_score': rec['bonus_score'],
|
86 |
-
'scores': rec['scores']
|
87 |
-
} for rec in recommendations]
|
88 |
-
|
89 |
-
# 保存到歷史記錄,也需要更新保存的偏好設定
|
90 |
-
history_component.save_search(
|
91 |
-
user_preferences={
|
92 |
-
'living_space': args[0],
|
93 |
-
'exercise_time': args[1],
|
94 |
-
'grooming_commitment': args[2],
|
95 |
-
'experience_level': args[3],
|
96 |
-
'has_children': args[4],
|
97 |
-
'noise_tolerance': args[5],
|
98 |
-
'health_sensitivity': "medium",
|
99 |
-
'barking_acceptance': args[5]
|
100 |
-
},
|
101 |
-
results=history_results
|
102 |
-
)
|
103 |
-
|
104 |
-
return format_recommendation_html(recommendations)
|
105 |
-
|
106 |
-
except Exception as e:
|
107 |
-
print(f"Error in find match: {str(e)}")
|
108 |
-
import traceback
|
109 |
-
print(traceback.format_exc())
|
110 |
-
return "Error getting recommendations"
|
111 |
-
|
112 |
-
get_recommendations_btn.click(
|
113 |
-
fn=on_find_match_click,
|
114 |
-
inputs=[
|
115 |
-
living_space,
|
116 |
-
exercise_time,
|
117 |
-
grooming_commitment,
|
118 |
-
experience_level,
|
119 |
-
has_children,
|
120 |
-
noise_tolerance
|
121 |
-
],
|
122 |
-
outputs=recommendation_output
|
123 |
-
)
|
124 |
-
|
125 |
-
return {
|
126 |
-
'living_space': living_space,
|
127 |
-
'exercise_time': exercise_time,
|
128 |
-
'grooming_commitment': grooming_commitment,
|
129 |
-
'experience_level': experience_level,
|
130 |
-
'has_children': has_children,
|
131 |
-
'noise_tolerance': noise_tolerance,
|
132 |
-
'get_recommendations_btn': get_recommendations_btn,
|
133 |
-
'recommendation_output': recommendation_output
|
134 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|