Spaces:
Running
on
Zero
Running
on
Zero
Update breed_recommendation.py
Browse files- breed_recommendation.py +307 -7
breed_recommendation.py
CHANGED
@@ -10,6 +10,37 @@ from scoring_calculation_system import UserPreferences, calculate_compatibility_
|
|
10 |
from recommendation_html_format import format_recommendation_html, get_breed_recommendations
|
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 |
|
@@ -163,9 +194,18 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
|
|
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, # 確保可見性
|
@@ -174,6 +214,7 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
|
|
174 |
|
175 |
def on_find_match_click(*args):
|
176 |
try:
|
|
|
177 |
user_prefs = UserPreferences(
|
178 |
living_space=args[0],
|
179 |
yard_access=args[1],
|
@@ -222,15 +263,23 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
|
|
222 |
results=history_results
|
223 |
)
|
224 |
|
225 |
-
return
|
|
|
|
|
|
|
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=[
|
@@ -246,7 +295,7 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
|
|
246 |
children_age,
|
247 |
noise_tolerance
|
248 |
],
|
249 |
-
outputs=recommendation_output
|
250 |
)
|
251 |
|
252 |
return {
|
@@ -258,4 +307,255 @@ def create_recommendation_tab(UserPreferences, get_breed_recommendations, format
|
|
258 |
'noise_tolerance': noise_tolerance,
|
259 |
'get_recommendations_btn': get_recommendations_btn,
|
260 |
'recommendation_output': recommendation_output,
|
261 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
from recommendation_html_format import format_recommendation_html, get_breed_recommendations
|
11 |
from search_history import create_history_tab, create_history_component
|
12 |
|
13 |
+
def create_custom_button_style():
|
14 |
+
return """
|
15 |
+
<style>
|
16 |
+
/* 自定義按鈕樣式 */
|
17 |
+
#find-match-btn {
|
18 |
+
background: linear-gradient(90deg, #ff7e5f 0%, #feb47b 100%);
|
19 |
+
border: none;
|
20 |
+
border-radius: 30px;
|
21 |
+
padding: 12px 24px;
|
22 |
+
color: white;
|
23 |
+
font-weight: bold;
|
24 |
+
cursor: pointer;
|
25 |
+
transition: all 0.3s ease;
|
26 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
27 |
+
}
|
28 |
+
|
29 |
+
#find-match-btn:hover {
|
30 |
+
box-shadow: 0 6px 8px rgba(0, 0, 0, 0.15);
|
31 |
+
opacity: 0.9;
|
32 |
+
}
|
33 |
+
|
34 |
+
/* 搜尋中提示樣式 */
|
35 |
+
#search-status {
|
36 |
+
text-align: center;
|
37 |
+
padding: 10px;
|
38 |
+
font-size: 1.1em;
|
39 |
+
color: #666;
|
40 |
+
display: none;
|
41 |
+
}
|
42 |
+
</style>
|
43 |
+
"""
|
44 |
|
45 |
def create_recommendation_tab(UserPreferences, get_breed_recommendations, format_recommendation_html, history_component):
|
46 |
|
|
|
194 |
inputs=has_children,
|
195 |
outputs=children_age
|
196 |
)
|
197 |
+
gr.HTML(create_custom_button_style())
|
198 |
+
|
199 |
+
search_status = gr.HTML(
|
200 |
+
'<div id="search-status">🔍 Sniffing out your perfect furry companion...</div>',
|
201 |
+
visible=False
|
202 |
+
)
|
203 |
+
|
204 |
+
get_recommendations_btn = gr.Button(
|
205 |
+
"Find My Perfect Match! 🔍",
|
206 |
+
elem_id="find-match-btn" # 添加自定義ID
|
207 |
+
)
|
208 |
|
|
|
|
|
209 |
recommendation_output = gr.HTML(
|
210 |
label="Breed Recommendations",
|
211 |
visible=True, # 確保可見性
|
|
|
214 |
|
215 |
def on_find_match_click(*args):
|
216 |
try:
|
217 |
+
print("Starting breed matching process...")
|
218 |
user_prefs = UserPreferences(
|
219 |
living_space=args[0],
|
220 |
yard_access=args[1],
|
|
|
263 |
results=history_results
|
264 |
)
|
265 |
|
266 |
+
return [
|
267 |
+
format_recommendation_html(recommendations, is_description_search=False),
|
268 |
+
gr.HTML.update(visible=False) # 隱藏搜尋狀態
|
269 |
+
]
|
270 |
|
271 |
except Exception as e:
|
272 |
print(f"Error in find match: {str(e)}")
|
273 |
import traceback
|
274 |
print(traceback.format_exc())
|
275 |
+
return ["Error getting recommendations", gr.HTML.update(visible=False)]
|
276 |
+
|
277 |
+
get_recommendations_btn.click(
|
278 |
+
fn=lambda: gr.HTML.update(visible=True),
|
279 |
+
outputs=search_status,
|
280 |
+
queue=False # 確保立即執行
|
281 |
+
)
|
282 |
|
|
|
283 |
get_recommendations_btn.click(
|
284 |
fn=on_find_match_click,
|
285 |
inputs=[
|
|
|
295 |
children_age,
|
296 |
noise_tolerance
|
297 |
],
|
298 |
+
outputs=[recommendation_output, search_status]
|
299 |
)
|
300 |
|
301 |
return {
|
|
|
307 |
'noise_tolerance': noise_tolerance,
|
308 |
'get_recommendations_btn': get_recommendations_btn,
|
309 |
'recommendation_output': recommendation_output,
|
310 |
+
}
|
311 |
+
|
312 |
+
|
313 |
+
|
314 |
+
# def create_recommendation_tab(UserPreferences, get_breed_recommendations, format_recommendation_html, history_component):
|
315 |
+
|
316 |
+
# with gr.TabItem("Breed Recommendation"):
|
317 |
+
# with gr.Tabs():
|
318 |
+
# with gr.Tab("Find by Criteria"):
|
319 |
+
# gr.HTML("""
|
320 |
+
# <div style='
|
321 |
+
# text-align: center;
|
322 |
+
# position: relative;
|
323 |
+
# padding: 20px 0;
|
324 |
+
# margin: 15px 0;
|
325 |
+
# background: linear-gradient(to right, rgba(66, 153, 225, 0.1), rgba(72, 187, 120, 0.1));
|
326 |
+
# border-radius: 10px;
|
327 |
+
# '>
|
328 |
+
# <!-- BETA 標籤 -->
|
329 |
+
# <div style='
|
330 |
+
# position: absolute;
|
331 |
+
# top: 10px;
|
332 |
+
# right: 20px;
|
333 |
+
# background: linear-gradient(90deg, #4299e1, #48bb78);
|
334 |
+
# color: white;
|
335 |
+
# padding: 4px 12px;
|
336 |
+
# border-radius: 15px;
|
337 |
+
# font-size: 0.85em;
|
338 |
+
# font-weight: 600;
|
339 |
+
# letter-spacing: 1px;
|
340 |
+
# box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
341 |
+
# '>BETA</div>
|
342 |
+
|
343 |
+
# <!-- 主標題 -->
|
344 |
+
# <p style='
|
345 |
+
# font-size: 1.2em;
|
346 |
+
# margin: 0;
|
347 |
+
# padding: 0 20px;
|
348 |
+
# line-height: 1.5;
|
349 |
+
# background: linear-gradient(90deg, #4299e1, #48bb78);
|
350 |
+
# -webkit-background-clip: text;
|
351 |
+
# -webkit-text-fill-color: transparent;
|
352 |
+
# font-weight: 600;
|
353 |
+
# '>
|
354 |
+
# Tell us about your lifestyle, and we'll recommend the perfect dog breeds for you!
|
355 |
+
# </p>
|
356 |
+
|
357 |
+
# <!-- 提示訊息 -->
|
358 |
+
# <div style='
|
359 |
+
# margin-top: 15px;
|
360 |
+
# padding: 10px 20px;
|
361 |
+
# background: linear-gradient(to right, rgba(66, 153, 225, 0.15), rgba(72, 187, 120, 0.15));
|
362 |
+
# border-radius: 8px;
|
363 |
+
# font-size: 0.9em;
|
364 |
+
# color: #2D3748;
|
365 |
+
# display: flex;
|
366 |
+
# align-items: center;
|
367 |
+
# justify-content: center;
|
368 |
+
# gap: 8px;
|
369 |
+
# '>
|
370 |
+
# <span style="font-size: 1.2em;">🔬</span>
|
371 |
+
# <span style="
|
372 |
+
# letter-spacing: 0.3px;
|
373 |
+
# line-height: 1.4;
|
374 |
+
# "><strong>Beta Feature:</strong> Our matching algorithm is continuously improving. Results are for reference only.</span>
|
375 |
+
# </div>
|
376 |
+
# </div>
|
377 |
+
# """)
|
378 |
+
|
379 |
+
# with gr.Row():
|
380 |
+
# with gr.Column():
|
381 |
+
# living_space = gr.Radio(
|
382 |
+
# choices=["apartment", "house_small", "house_large"],
|
383 |
+
# label="What type of living space do you have?",
|
384 |
+
# info="Choose your current living situation",
|
385 |
+
# value="apartment"
|
386 |
+
# )
|
387 |
+
|
388 |
+
# yard_access = gr.Radio(
|
389 |
+
# choices=["no_yard", "shared_yard", "private_yard"],
|
390 |
+
# label="Yard Access Type",
|
391 |
+
# info="Available outdoor space",
|
392 |
+
# value="no_yard"
|
393 |
+
# )
|
394 |
+
|
395 |
+
# exercise_time = gr.Slider(
|
396 |
+
# minimum=0,
|
397 |
+
# maximum=180,
|
398 |
+
# value=60,
|
399 |
+
# label="Daily exercise time (minutes)",
|
400 |
+
# info="Consider walks, play time, and training"
|
401 |
+
# )
|
402 |
+
|
403 |
+
# exercise_type = gr.Radio(
|
404 |
+
# choices=["light_walks", "moderate_activity", "active_training"],
|
405 |
+
# label="Exercise Style",
|
406 |
+
# info="What kind of activities do you prefer?",
|
407 |
+
# value="moderate_activity"
|
408 |
+
# )
|
409 |
+
|
410 |
+
|
411 |
+
# grooming_commitment = gr.Radio(
|
412 |
+
# choices=["low", "medium", "high"],
|
413 |
+
# label="Grooming commitment level",
|
414 |
+
# info="Low: monthly, Medium: weekly, High: daily",
|
415 |
+
# value="medium"
|
416 |
+
# )
|
417 |
+
|
418 |
+
# with gr.Column():
|
419 |
+
# size_preference = gr.Radio(
|
420 |
+
# choices=["no_preference", "small", "medium", "large", "giant"],
|
421 |
+
# label="Preference Dog Size",
|
422 |
+
# info="Select your preferred dog size - this will strongly filter the recommendations",
|
423 |
+
# value = "no_preference"
|
424 |
+
# )
|
425 |
+
# experience_level = gr.Radio(
|
426 |
+
# choices=["beginner", "intermediate", "advanced"],
|
427 |
+
# label="Dog ownership experience",
|
428 |
+
# info="Be honest - this helps find the right match",
|
429 |
+
# value="beginner"
|
430 |
+
# )
|
431 |
+
|
432 |
+
# time_availability = gr.Radio(
|
433 |
+
# choices=["limited", "moderate", "flexible"],
|
434 |
+
# label="Time Availability",
|
435 |
+
# info="Time available for dog care daily",
|
436 |
+
# value="moderate"
|
437 |
+
# )
|
438 |
+
|
439 |
+
# has_children = gr.Checkbox(
|
440 |
+
# label="Have children at home",
|
441 |
+
# info="Helps recommend child-friendly breeds"
|
442 |
+
# )
|
443 |
+
|
444 |
+
# children_age = gr.Radio(
|
445 |
+
# choices=["toddler", "school_age", "teenager"],
|
446 |
+
# label="Children's Age Group",
|
447 |
+
# info="Helps match with age-appropriate breeds",
|
448 |
+
# visible=False # 默認隱藏,只在has_children=True時顯示
|
449 |
+
# )
|
450 |
+
|
451 |
+
# noise_tolerance = gr.Radio(
|
452 |
+
# choices=["low", "medium", "high"],
|
453 |
+
# label="Noise tolerance level",
|
454 |
+
# info="Some breeds are more vocal than others",
|
455 |
+
# value="medium"
|
456 |
+
# )
|
457 |
+
|
458 |
+
# def update_children_age_visibility(has_children):
|
459 |
+
# return gr.update(visible=has_children)
|
460 |
+
|
461 |
+
# has_children.change(
|
462 |
+
# fn=update_children_age_visibility,
|
463 |
+
# inputs=has_children,
|
464 |
+
# outputs=children_age
|
465 |
+
# )
|
466 |
+
|
467 |
+
# get_recommendations_btn = gr.Button("Find My Perfect Match! 🔍", variant="primary")
|
468 |
+
|
469 |
+
# recommendation_output = gr.HTML(
|
470 |
+
# label="Breed Recommendations",
|
471 |
+
# visible=True, # 確保可見性
|
472 |
+
# elem_id="recommendation-output"
|
473 |
+
# )
|
474 |
+
|
475 |
+
# def on_find_match_click(*args):
|
476 |
+
# try:
|
477 |
+
# user_prefs = UserPreferences(
|
478 |
+
# living_space=args[0],
|
479 |
+
# yard_access=args[1],
|
480 |
+
# exercise_time=args[2],
|
481 |
+
# exercise_type=args[3],
|
482 |
+
# grooming_commitment=args[4],
|
483 |
+
# size_preference=args[5],
|
484 |
+
# experience_level=args[6],
|
485 |
+
# time_availability=args[7],
|
486 |
+
# has_children=args[8],
|
487 |
+
# children_age=args[9] if args[8] else None,
|
488 |
+
# noise_tolerance=args[10],
|
489 |
+
# space_for_play=True if args[0] != "apartment" else False,
|
490 |
+
# other_pets=False,
|
491 |
+
# climate="moderate",
|
492 |
+
# health_sensitivity="medium",
|
493 |
+
# barking_acceptance=args[10]
|
494 |
+
# )
|
495 |
+
|
496 |
+
# recommendations = get_breed_recommendations(user_prefs, top_n=15)
|
497 |
+
|
498 |
+
# history_results = [{
|
499 |
+
# 'breed': rec['breed'],
|
500 |
+
# 'rank': rec['rank'],
|
501 |
+
# 'overall_score': rec['final_score'],
|
502 |
+
# 'base_score': rec['base_score'],
|
503 |
+
# 'bonus_score': rec['bonus_score'],
|
504 |
+
# 'scores': rec['scores']
|
505 |
+
# } for rec in recommendations]
|
506 |
+
|
507 |
+
# history_component.save_search(
|
508 |
+
# user_preferences={
|
509 |
+
# 'living_space': args[0],
|
510 |
+
# 'yard_access': args[1],
|
511 |
+
# 'exercise_time': args[2],
|
512 |
+
# 'exercise_type': args[3],
|
513 |
+
# 'grooming_commitment': args[4],
|
514 |
+
# 'size_preference': args[5],
|
515 |
+
# 'experience_level': args[6],
|
516 |
+
# 'time_availability': args[7],
|
517 |
+
# 'has_children': args[8],
|
518 |
+
# 'children_age': args[9] if args[8] else None,
|
519 |
+
# 'noise_tolerance': args[10],
|
520 |
+
# 'search_type': 'Criteria'
|
521 |
+
# },
|
522 |
+
# results=history_results
|
523 |
+
# )
|
524 |
+
|
525 |
+
# return format_recommendation_html(recommendations, is_description_search=False)
|
526 |
+
|
527 |
+
# except Exception as e:
|
528 |
+
# print(f"Error in find match: {str(e)}")
|
529 |
+
# import traceback
|
530 |
+
# print(traceback.format_exc())
|
531 |
+
# return "Error getting recommendations"
|
532 |
+
|
533 |
+
|
534 |
+
# get_recommendations_btn.click(
|
535 |
+
# fn=on_find_match_click,
|
536 |
+
# inputs=[
|
537 |
+
# living_space,
|
538 |
+
# yard_access,
|
539 |
+
# exercise_time,
|
540 |
+
# exercise_type,
|
541 |
+
# grooming_commitment,
|
542 |
+
# size_preference,
|
543 |
+
# experience_level,
|
544 |
+
# time_availability,
|
545 |
+
# has_children,
|
546 |
+
# children_age,
|
547 |
+
# noise_tolerance
|
548 |
+
# ],
|
549 |
+
# outputs=recommendation_output
|
550 |
+
# )
|
551 |
+
|
552 |
+
# return {
|
553 |
+
# 'living_space': living_space,
|
554 |
+
# 'exercise_time': exercise_time,
|
555 |
+
# 'grooming_commitment': grooming_commitment,
|
556 |
+
# 'experience_level': experience_level,
|
557 |
+
# 'has_children': has_children,
|
558 |
+
# 'noise_tolerance': noise_tolerance,
|
559 |
+
# 'get_recommendations_btn': get_recommendations_btn,
|
560 |
+
# 'recommendation_output': recommendation_output,
|
561 |
+
# }
|