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