Spaces:
Running
on
Zero
Running
on
Zero
Update breed_recommendation.py
Browse files- breed_recommendation.py +327 -11
breed_recommendation.py
CHANGED
@@ -10,6 +10,255 @@ from recommendation_html_format import format_recommendation_html, get_breed_rec
|
|
10 |
from search_history import create_history_tab, create_history_component
|
11 |
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
def create_recommendation_tab(UserPreferences, get_breed_recommendations, format_recommendation_html, history_component):
|
14 |
|
15 |
with gr.TabItem("Breed Recommendation"):
|
@@ -163,16 +412,82 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
|
|
163 |
outputs=children_age
|
164 |
)
|
165 |
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
|
174 |
def on_find_match_click(*args):
|
175 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
user_prefs = UserPreferences(
|
177 |
living_space=args[0],
|
178 |
yard_access=args[1],
|
@@ -234,18 +549,19 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
|
|
234 |
fn=on_find_match_click,
|
235 |
inputs=[
|
236 |
living_space,
|
237 |
-
yard_access,
|
238 |
exercise_time,
|
239 |
-
exercise_type,
|
240 |
grooming_commitment,
|
241 |
size_preference,
|
242 |
experience_level,
|
243 |
-
time_availability,
|
244 |
has_children,
|
245 |
children_age,
|
246 |
noise_tolerance
|
247 |
],
|
248 |
-
outputs=recommendation_output
|
|
|
249 |
)
|
250 |
|
251 |
return {
|
|
|
10 |
from search_history import create_history_tab, create_history_component
|
11 |
|
12 |
|
13 |
+
# def create_recommendation_tab(UserPreferences, get_breed_recommendations, format_recommendation_html, history_component):
|
14 |
+
|
15 |
+
# with gr.TabItem("Breed Recommendation"):
|
16 |
+
# with gr.Tabs():
|
17 |
+
# with gr.Tab("Find by Criteria"):
|
18 |
+
# gr.HTML("""
|
19 |
+
# <div style='
|
20 |
+
# text-align: center;
|
21 |
+
# position: relative;
|
22 |
+
# padding: 20px 0;
|
23 |
+
# margin: 15px 0;
|
24 |
+
# background: linear-gradient(to right, rgba(66, 153, 225, 0.1), rgba(72, 187, 120, 0.1));
|
25 |
+
# border-radius: 10px;
|
26 |
+
# '>
|
27 |
+
# <!-- BETA 標籤 -->
|
28 |
+
# <div style='
|
29 |
+
# position: absolute;
|
30 |
+
# top: 10px;
|
31 |
+
# right: 20px;
|
32 |
+
# background: linear-gradient(90deg, #4299e1, #48bb78);
|
33 |
+
# color: white;
|
34 |
+
# padding: 4px 12px;
|
35 |
+
# border-radius: 15px;
|
36 |
+
# font-size: 0.85em;
|
37 |
+
# font-weight: 600;
|
38 |
+
# letter-spacing: 1px;
|
39 |
+
# box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
40 |
+
# '>BETA</div>
|
41 |
+
|
42 |
+
# <!-- 主標題 -->
|
43 |
+
# <p style='
|
44 |
+
# font-size: 1.2em;
|
45 |
+
# margin: 0;
|
46 |
+
# padding: 0 20px;
|
47 |
+
# line-height: 1.5;
|
48 |
+
# background: linear-gradient(90deg, #4299e1, #48bb78);
|
49 |
+
# -webkit-background-clip: text;
|
50 |
+
# -webkit-text-fill-color: transparent;
|
51 |
+
# font-weight: 600;
|
52 |
+
# '>
|
53 |
+
# Tell us about your lifestyle, and we'll recommend the perfect dog breeds for you!
|
54 |
+
# </p>
|
55 |
+
|
56 |
+
# <!-- 提示訊息 -->
|
57 |
+
# <div style='
|
58 |
+
# margin-top: 15px;
|
59 |
+
# padding: 10px 20px;
|
60 |
+
# background: linear-gradient(to right, rgba(66, 153, 225, 0.15), rgba(72, 187, 120, 0.15));
|
61 |
+
# border-radius: 8px;
|
62 |
+
# font-size: 0.9em;
|
63 |
+
# color: #2D3748;
|
64 |
+
# display: flex;
|
65 |
+
# align-items: center;
|
66 |
+
# justify-content: center;
|
67 |
+
# gap: 8px;
|
68 |
+
# '>
|
69 |
+
# <span style="font-size: 1.2em;">🔬</span>
|
70 |
+
# <span style="
|
71 |
+
# letter-spacing: 0.3px;
|
72 |
+
# line-height: 1.4;
|
73 |
+
# "><strong>Beta Feature:</strong> Our matching algorithm is continuously improving. Results are for reference only.</span>
|
74 |
+
# </div>
|
75 |
+
# </div>
|
76 |
+
# """)
|
77 |
+
|
78 |
+
# with gr.Row():
|
79 |
+
# with gr.Column():
|
80 |
+
# living_space = gr.Radio(
|
81 |
+
# choices=["apartment", "house_small", "house_large"],
|
82 |
+
# label="What type of living space do you have?",
|
83 |
+
# info="Choose your current living situation",
|
84 |
+
# value="apartment"
|
85 |
+
# )
|
86 |
+
|
87 |
+
# yard_access = gr.Radio(
|
88 |
+
# choices=["no_yard", "shared_yard", "private_yard"],
|
89 |
+
# label="Yard Access Type",
|
90 |
+
# info="Available outdoor space",
|
91 |
+
# value="no_yard"
|
92 |
+
# )
|
93 |
+
|
94 |
+
# exercise_time = gr.Slider(
|
95 |
+
# minimum=0,
|
96 |
+
# maximum=180,
|
97 |
+
# value=60,
|
98 |
+
# label="Daily exercise time (minutes)",
|
99 |
+
# info="Consider walks, play time, and training"
|
100 |
+
# )
|
101 |
+
|
102 |
+
# exercise_type = gr.Radio(
|
103 |
+
# choices=["light_walks", "moderate_activity", "active_training"],
|
104 |
+
# label="Exercise Style",
|
105 |
+
# info="What kind of activities do you prefer?",
|
106 |
+
# value="moderate_activity"
|
107 |
+
# )
|
108 |
+
|
109 |
+
|
110 |
+
# grooming_commitment = gr.Radio(
|
111 |
+
# choices=["low", "medium", "high"],
|
112 |
+
# label="Grooming commitment level",
|
113 |
+
# info="Low: monthly, Medium: weekly, High: daily",
|
114 |
+
# value="medium"
|
115 |
+
# )
|
116 |
+
|
117 |
+
# with gr.Column():
|
118 |
+
# size_preference = gr.Radio(
|
119 |
+
# choices=["no_preference", "small", "medium", "large", "giant"],
|
120 |
+
# label="Preference Dog Size",
|
121 |
+
# info="Select your preferred dog size - this will strongly filter the recommendations",
|
122 |
+
# value = "no_preference"
|
123 |
+
# )
|
124 |
+
# experience_level = gr.Radio(
|
125 |
+
# choices=["beginner", "intermediate", "advanced"],
|
126 |
+
# label="Dog ownership experience",
|
127 |
+
# info="Be honest - this helps find the right match",
|
128 |
+
# value="beginner"
|
129 |
+
# )
|
130 |
+
|
131 |
+
# time_availability = gr.Radio(
|
132 |
+
# choices=["limited", "moderate", "flexible"],
|
133 |
+
# label="Time Availability",
|
134 |
+
# info="Time available for dog care daily",
|
135 |
+
# value="moderate"
|
136 |
+
# )
|
137 |
+
|
138 |
+
# has_children = gr.Checkbox(
|
139 |
+
# label="Have children at home",
|
140 |
+
# info="Helps recommend child-friendly breeds"
|
141 |
+
# )
|
142 |
+
|
143 |
+
# children_age = gr.Radio(
|
144 |
+
# choices=["toddler", "school_age", "teenager"],
|
145 |
+
# label="Children's Age Group",
|
146 |
+
# info="Helps match with age-appropriate breeds",
|
147 |
+
# visible=False # 默認隱藏,只在has_children=True時顯示
|
148 |
+
# )
|
149 |
+
|
150 |
+
# noise_tolerance = gr.Radio(
|
151 |
+
# choices=["low", "medium", "high"],
|
152 |
+
# label="Noise tolerance level",
|
153 |
+
# info="Some breeds are more vocal than others",
|
154 |
+
# value="medium"
|
155 |
+
# )
|
156 |
+
|
157 |
+
# def update_children_age_visibility(has_children):
|
158 |
+
# return gr.update(visible=has_children)
|
159 |
+
|
160 |
+
# has_children.change(
|
161 |
+
# fn=update_children_age_visibility,
|
162 |
+
# inputs=has_children,
|
163 |
+
# outputs=children_age
|
164 |
+
# )
|
165 |
+
|
166 |
+
# get_recommendations_btn = gr.Button("Find My Perfect Match! 🔍", variant="primary")
|
167 |
+
|
168 |
+
# recommendation_output = gr.HTML(
|
169 |
+
# label="Breed Recommendations",
|
170 |
+
# visible=True, # 確保可見性
|
171 |
+
# elem_id="recommendation-output"
|
172 |
+
# )
|
173 |
+
|
174 |
+
# def on_find_match_click(*args):
|
175 |
+
# try:
|
176 |
+
# user_prefs = UserPreferences(
|
177 |
+
# living_space=args[0],
|
178 |
+
# yard_access=args[1],
|
179 |
+
# exercise_time=args[2],
|
180 |
+
# exercise_type=args[3],
|
181 |
+
# grooming_commitment=args[4],
|
182 |
+
# size_preference=args[5],
|
183 |
+
# experience_level=args[6],
|
184 |
+
# time_availability=args[7],
|
185 |
+
# has_children=args[8],
|
186 |
+
# children_age=args[9] if args[8] else None,
|
187 |
+
# noise_tolerance=args[10],
|
188 |
+
# space_for_play=True if args[0] != "apartment" else False,
|
189 |
+
# other_pets=False,
|
190 |
+
# climate="moderate",
|
191 |
+
# health_sensitivity="medium",
|
192 |
+
# barking_acceptance=args[10]
|
193 |
+
# )
|
194 |
+
|
195 |
+
# recommendations = get_breed_recommendations(user_prefs, top_n=15)
|
196 |
+
|
197 |
+
# history_results = [{
|
198 |
+
# 'breed': rec['breed'],
|
199 |
+
# 'rank': rec['rank'],
|
200 |
+
# 'overall_score': rec['final_score'],
|
201 |
+
# 'base_score': rec['base_score'],
|
202 |
+
# 'bonus_score': rec['bonus_score'],
|
203 |
+
# 'scores': rec['scores']
|
204 |
+
# } for rec in recommendations]
|
205 |
+
|
206 |
+
# history_component.save_search(
|
207 |
+
# user_preferences={
|
208 |
+
# 'living_space': args[0],
|
209 |
+
# 'yard_access': args[1],
|
210 |
+
# 'exercise_time': args[2],
|
211 |
+
# 'exercise_type': args[3],
|
212 |
+
# 'grooming_commitment': args[4],
|
213 |
+
# 'size_preference': args[5],
|
214 |
+
# 'experience_level': args[6],
|
215 |
+
# 'time_availability': args[7],
|
216 |
+
# 'has_children': args[8],
|
217 |
+
# 'children_age': args[9] if args[8] else None,
|
218 |
+
# 'noise_tolerance': args[10],
|
219 |
+
# 'search_type': 'Criteria'
|
220 |
+
# },
|
221 |
+
# results=history_results
|
222 |
+
# )
|
223 |
+
|
224 |
+
# return format_recommendation_html(recommendations, is_description_search=False)
|
225 |
+
|
226 |
+
# except Exception as e:
|
227 |
+
# print(f"Error in find match: {str(e)}")
|
228 |
+
# import traceback
|
229 |
+
# print(traceback.format_exc())
|
230 |
+
# return "Error getting recommendations"
|
231 |
+
|
232 |
+
|
233 |
+
# get_recommendations_btn.click(
|
234 |
+
# fn=on_find_match_click,
|
235 |
+
# inputs=[
|
236 |
+
# living_space,
|
237 |
+
# yard_access,
|
238 |
+
# exercise_time,
|
239 |
+
# exercise_type,
|
240 |
+
# grooming_commitment,
|
241 |
+
# size_preference,
|
242 |
+
# experience_level,
|
243 |
+
# time_availability,
|
244 |
+
# has_children,
|
245 |
+
# children_age,
|
246 |
+
# noise_tolerance
|
247 |
+
# ],
|
248 |
+
# outputs=recommendation_output
|
249 |
+
# )
|
250 |
+
|
251 |
+
# return {
|
252 |
+
# 'living_space': living_space,
|
253 |
+
# 'exercise_time': exercise_time,
|
254 |
+
# 'grooming_commitment': grooming_commitment,
|
255 |
+
# 'experience_level': experience_level,
|
256 |
+
# 'has_children': has_children,
|
257 |
+
# 'noise_tolerance': noise_tolerance,
|
258 |
+
# 'get_recommendations_btn': get_recommendations_btn,
|
259 |
+
# 'recommendation_output': recommendation_output,
|
260 |
+
# }
|
261 |
+
|
262 |
def create_recommendation_tab(UserPreferences, get_breed_recommendations, format_recommendation_html, history_component):
|
263 |
|
264 |
with gr.TabItem("Breed Recommendation"):
|
|
|
412 |
outputs=children_age
|
413 |
)
|
414 |
|
415 |
+
with gr.Column():
|
416 |
+
# 添加自定義 CSS
|
417 |
+
gr.HTML("""
|
418 |
+
<style>
|
419 |
+
.custom-match-button {
|
420 |
+
background: linear-gradient(to right, #ff8c42, #ff6b2b) !important;
|
421 |
+
border-radius: 50px !important;
|
422 |
+
padding: 12px 24px !important;
|
423 |
+
border: none !important;
|
424 |
+
color: white !important;
|
425 |
+
font-weight: 600 !important;
|
426 |
+
transition: all 0.3s ease !important;
|
427 |
+
box-shadow: 0 4px 15px rgba(255, 107, 43, 0.2) !important;
|
428 |
+
}
|
429 |
+
|
430 |
+
.custom-match-button:hover {
|
431 |
+
transform: translateY(-2px) !important;
|
432 |
+
box-shadow: 0 6px 20px rgba(255, 107, 43, 0.3) !important;
|
433 |
+
background: linear-gradient(to right, #ff6b2b, #ff5722) !important;
|
434 |
+
}
|
435 |
+
</style>
|
436 |
+
""")
|
437 |
+
|
438 |
+
# 創建新的按鈕
|
439 |
+
get_recommendations_btn = gr.Button(
|
440 |
+
"Find My Perfect Match 🐾",
|
441 |
+
elem_classes="custom-match-button",
|
442 |
+
variant="primary"
|
443 |
+
)
|
444 |
|
445 |
def on_find_match_click(*args):
|
446 |
try:
|
447 |
+
loading_messages = [
|
448 |
+
"🐕 Sniffing out your perfect match...",
|
449 |
+
"🔍 Searching through our database...",
|
450 |
+
"🎾 Playing fetch with potential matches...",
|
451 |
+
"🦴 Evaluating compatibility scores...",
|
452 |
+
"💭 Getting opinions from our experts..."
|
453 |
+
]
|
454 |
+
|
455 |
+
# 顯示初始加載訊息
|
456 |
+
yield gr.HTML("""
|
457 |
+
<div style="
|
458 |
+
text-align: center;
|
459 |
+
padding: 20px;
|
460 |
+
background: rgba(255, 255, 255, 0.9);
|
461 |
+
border-radius: 12px;
|
462 |
+
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
|
463 |
+
margin: 20px 0;
|
464 |
+
">
|
465 |
+
<div style="font-size: 1.1em; color: #4a5568; margin-bottom: 10px;">
|
466 |
+
{}
|
467 |
+
</div>
|
468 |
+
<div style="
|
469 |
+
width: 100%;
|
470 |
+
height: 4px;
|
471 |
+
background: #f0f0f0;
|
472 |
+
border-radius: 2px;
|
473 |
+
overflow: hidden;
|
474 |
+
">
|
475 |
+
<div style="
|
476 |
+
width: 30%;
|
477 |
+
height: 100%;
|
478 |
+
background: linear-gradient(to right, #ff8c42, #ff6b2b);
|
479 |
+
animation: progress 2s infinite linear;
|
480 |
+
"></div>
|
481 |
+
</div>
|
482 |
+
</div>
|
483 |
+
<style>
|
484 |
+
@keyframes progress {{
|
485 |
+
0% {{ transform: translateX(-100%); }}
|
486 |
+
100% {{ transform: translateX(400%); }}
|
487 |
+
}}
|
488 |
+
</style>
|
489 |
+
""".format(loading_messages[0]))
|
490 |
+
|
491 |
user_prefs = UserPreferences(
|
492 |
living_space=args[0],
|
493 |
yard_access=args[1],
|
|
|
549 |
fn=on_find_match_click,
|
550 |
inputs=[
|
551 |
living_space,
|
552 |
+
yard_access,
|
553 |
exercise_time,
|
554 |
+
exercise_type,
|
555 |
grooming_commitment,
|
556 |
size_preference,
|
557 |
experience_level,
|
558 |
+
time_availability,
|
559 |
has_children,
|
560 |
children_age,
|
561 |
noise_tolerance
|
562 |
],
|
563 |
+
outputs=recommendation_output,
|
564 |
+
api_name=False # 支持生成器功能
|
565 |
)
|
566 |
|
567 |
return {
|