Spaces:
Running
on
Zero
Running
on
Zero
File size: 25,605 Bytes
d26f860 6845777 e9064fd 6845777 d26f860 fe7a40d d26f860 3a0319c 0ed543a de532fe fc2b8a1 d26f860 840689b d26f860 840689b d26f860 840689b d26f860 6ceb202 d26f860 68e4cf6 d26f860 68e4cf6 fc2b8a1 d26f860 5fd68e6 d26f860 68e4cf6 d26f860 68e4cf6 fc2b8a1 68e4cf6 d26f860 fc2b8a1 68e4cf6 d26f860 0ed543a e395366 0ed543a d26f860 e395366 7f7db0c 53dba37 fc2b8a1 0ed543a 16affb8 1b1aea3 e665512 0ed543a e665512 1b1aea3 0ed543a 1b1aea3 0ed543a e665512 1b1aea3 7f7db0c fc2b8a1 7f7db0c 08dbd04 7f7db0c fc2b8a1 7f7db0c fc2b8a1 0bf9b1a fc2b8a1 de532fe 7f7db0c fc2b8a1 7f7db0c fc2b8a1 0ed543a fc2b8a1 0ed543a fc2b8a1 5fd68e6 fc2b8a1 0ed543a fc2b8a1 0ed543a e665512 fc2b8a1 d26f860 1aaf383 |
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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 |
import sqlite3
import gradio as gr
import asyncio
import itertools
from typing import Generator
from dog_database import get_dog_description, dog_data
from breed_health_info import breed_health_info
from breed_noise_info import breed_noise_info
from scoring_calculation_system import UserPreferences, calculate_compatibility_score
from recommendation_html_format import format_recommendation_html, get_breed_recommendations
from search_history import create_history_tab, create_history_component
# def create_recommendation_tab(UserPreferences, get_breed_recommendations, format_recommendation_html, history_component):
# with gr.TabItem("Breed Recommendation"):
# with gr.Tabs():
# with gr.Tab("Find by Criteria"):
# gr.HTML("""
# <div style='
# text-align: center;
# position: relative;
# padding: 20px 0;
# margin: 15px 0;
# background: linear-gradient(to right, rgba(66, 153, 225, 0.1), rgba(72, 187, 120, 0.1));
# border-radius: 10px;
# '>
# <!-- BETA 標籤 -->
# <div style='
# position: absolute;
# top: 10px;
# right: 20px;
# background: linear-gradient(90deg, #4299e1, #48bb78);
# color: white;
# padding: 4px 12px;
# border-radius: 15px;
# font-size: 0.85em;
# font-weight: 600;
# letter-spacing: 1px;
# box-shadow: 0 2px 4px rgba(0,0,0,0.1);
# '>BETA</div>
# <!-- 主標題 -->
# <p style='
# font-size: 1.2em;
# margin: 0;
# padding: 0 20px;
# line-height: 1.5;
# background: linear-gradient(90deg, #4299e1, #48bb78);
# -webkit-background-clip: text;
# -webkit-text-fill-color: transparent;
# font-weight: 600;
# '>
# Tell us about your lifestyle, and we'll recommend the perfect dog breeds for you!
# </p>
# <!-- 提示訊息 -->
# <div style='
# margin-top: 15px;
# padding: 10px 20px;
# background: linear-gradient(to right, rgba(66, 153, 225, 0.15), rgba(72, 187, 120, 0.15));
# border-radius: 8px;
# font-size: 0.9em;
# color: #2D3748;
# display: flex;
# align-items: center;
# justify-content: center;
# gap: 8px;
# '>
# <span style="font-size: 1.2em;">🔬</span>
# <span style="
# letter-spacing: 0.3px;
# line-height: 1.4;
# "><strong>Beta Feature:</strong> Our matching algorithm is continuously improving. Results are for reference only.</span>
# </div>
# </div>
# """)
# 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"
# )
# yard_access = gr.Radio(
# choices=["no_yard", "shared_yard", "private_yard"],
# label="Yard Access Type",
# info="Available outdoor space",
# value="no_yard"
# )
# exercise_time = gr.Slider(
# minimum=0,
# maximum=180,
# value=60,
# label="Daily exercise time (minutes)",
# info="Consider walks, play time, and training"
# )
# exercise_type = gr.Radio(
# choices=["light_walks", "moderate_activity", "active_training"],
# label="Exercise Style",
# info="What kind of activities do you prefer?",
# value="moderate_activity"
# )
# grooming_commitment = gr.Radio(
# choices=["low", "medium", "high"],
# label="Grooming commitment level",
# info="Low: monthly, Medium: weekly, High: daily",
# value="medium"
# )
# with gr.Column():
# size_preference = gr.Radio(
# choices=["no_preference", "small", "medium", "large", "giant"],
# label="Preference Dog Size",
# info="Select your preferred dog size - this will strongly filter the recommendations",
# value = "no_preference"
# )
# experience_level = gr.Radio(
# choices=["beginner", "intermediate", "advanced"],
# label="Dog ownership experience",
# info="Be honest - this helps find the right match",
# value="beginner"
# )
# time_availability = gr.Radio(
# choices=["limited", "moderate", "flexible"],
# label="Time Availability",
# info="Time available for dog care daily",
# value="moderate"
# )
# has_children = gr.Checkbox(
# label="Have children at home",
# info="Helps recommend child-friendly breeds"
# )
# children_age = gr.Radio(
# choices=["toddler", "school_age", "teenager"],
# label="Children's Age Group",
# info="Helps match with age-appropriate breeds",
# visible=False # 默認隱藏,只在has_children=True時顯示
# )
# noise_tolerance = gr.Radio(
# choices=["low", "medium", "high"],
# label="Noise tolerance level",
# info="Some breeds are more vocal than others",
# value="medium"
# )
# def update_children_age_visibility(has_children):
# return gr.update(visible=has_children)
# has_children.change(
# fn=update_children_age_visibility,
# inputs=has_children,
# outputs=children_age
# )
# get_recommendations_btn = gr.Button("Find My Perfect Match! 🔍", variant="primary")
# recommendation_output = gr.HTML(
# label="Breed Recommendations",
# visible=True, # 確保可見性
# elem_id="recommendation-output"
# )
# def on_find_match_click(*args):
# try:
# user_prefs = UserPreferences(
# living_space=args[0],
# yard_access=args[1],
# exercise_time=args[2],
# exercise_type=args[3],
# grooming_commitment=args[4],
# size_preference=args[5],
# experience_level=args[6],
# time_availability=args[7],
# has_children=args[8],
# children_age=args[9] if args[8] else None,
# noise_tolerance=args[10],
# space_for_play=True if args[0] != "apartment" else False,
# other_pets=False,
# climate="moderate",
# health_sensitivity="medium",
# barking_acceptance=args[10]
# )
# recommendations = get_breed_recommendations(user_prefs, top_n=15)
# 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],
# 'yard_access': args[1],
# 'exercise_time': args[2],
# 'exercise_type': args[3],
# 'grooming_commitment': args[4],
# 'size_preference': args[5],
# 'experience_level': args[6],
# 'time_availability': args[7],
# 'has_children': args[8],
# 'children_age': args[9] if args[8] else None,
# 'noise_tolerance': args[10],
# 'search_type': 'Criteria'
# },
# results=history_results
# )
# return format_recommendation_html(recommendations, is_description_search=False)
# 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,
# yard_access,
# exercise_time,
# exercise_type,
# grooming_commitment,
# size_preference,
# experience_level,
# time_availability,
# has_children,
# children_age,
# 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,
# }
def create_recommendation_tab(UserPreferences, get_breed_recommendations, format_recommendation_html, history_component):
with gr.TabItem("Breed Recommendation"):
with gr.Tabs():
with gr.Tab("Find by Criteria"):
gr.HTML("""
<div style='
text-align: center;
position: relative;
padding: 20px 0;
margin: 15px 0;
background: linear-gradient(to right, rgba(66, 153, 225, 0.1), rgba(72, 187, 120, 0.1));
border-radius: 10px;
'>
<!-- BETA 標籤 -->
<div style='
position: absolute;
top: 10px;
right: 20px;
background: linear-gradient(90deg, #4299e1, #48bb78);
color: white;
padding: 4px 12px;
border-radius: 15px;
font-size: 0.85em;
font-weight: 600;
letter-spacing: 1px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
'>BETA</div>
<!-- 主標題 -->
<p style='
font-size: 1.2em;
margin: 0;
padding: 0 20px;
line-height: 1.5;
background: linear-gradient(90deg, #4299e1, #48bb78);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
font-weight: 600;
'>
Tell us about your lifestyle, and we'll recommend the perfect dog breeds for you!
</p>
<!-- 提示訊息 -->
<div style='
margin-top: 15px;
padding: 10px 20px;
background: linear-gradient(to right, rgba(66, 153, 225, 0.15), rgba(72, 187, 120, 0.15));
border-radius: 8px;
font-size: 0.9em;
color: #2D3748;
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
'>
<span style="font-size: 1.2em;">🔬</span>
<span style="
letter-spacing: 0.3px;
line-height: 1.4;
"><strong>Beta Feature:</strong> Our matching algorithm is continuously improving. Results are for reference only.</span>
</div>
</div>
""")
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"
)
yard_access = gr.Radio(
choices=["no_yard", "shared_yard", "private_yard"],
label="Yard Access Type",
info="Available outdoor space",
value="no_yard"
)
exercise_time = gr.Slider(
minimum=0,
maximum=180,
value=60,
label="Daily exercise time (minutes)",
info="Consider walks, play time, and training"
)
exercise_type = gr.Radio(
choices=["light_walks", "moderate_activity", "active_training"],
label="Exercise Style",
info="What kind of activities do you prefer?",
value="moderate_activity"
)
grooming_commitment = gr.Radio(
choices=["low", "medium", "high"],
label="Grooming commitment level",
info="Low: monthly, Medium: weekly, High: daily",
value="medium"
)
with gr.Column():
size_preference = gr.Radio(
choices=["no_preference", "small", "medium", "large", "giant"],
label="Preference Dog Size",
info="Select your preferred dog size - this will strongly filter the recommendations",
value = "no_preference"
)
experience_level = gr.Radio(
choices=["beginner", "intermediate", "advanced"],
label="Dog ownership experience",
info="Be honest - this helps find the right match",
value="beginner"
)
time_availability = gr.Radio(
choices=["limited", "moderate", "flexible"],
label="Time Availability",
info="Time available for dog care daily",
value="moderate"
)
has_children = gr.Checkbox(
label="Have children at home",
info="Helps recommend child-friendly breeds"
)
children_age = gr.Radio(
choices=["toddler", "school_age", "teenager"],
label="Children's Age Group",
info="Helps match with age-appropriate breeds",
visible=False # 默認隱藏,只在has_children=True時顯示
)
noise_tolerance = gr.Radio(
choices=["low", "medium", "high"],
label="Noise tolerance level",
info="Some breeds are more vocal than others",
value="medium"
)
def update_children_age_visibility(has_children):
return gr.update(visible=has_children)
has_children.change(
fn=update_children_age_visibility,
inputs=has_children,
outputs=children_age
)
with gr.Column():
# 添加自定義 CSS
gr.HTML("""
<style>
.custom-match-button {
background: linear-gradient(to right, #ff8c42, #ff6b2b) !important;
border-radius: 50px !important;
padding: 12px 24px !important;
border: none !important;
color: white !important;
font-weight: 600 !important;
transition: all 0.3s ease !important;
box-shadow: 0 4px 15px rgba(255, 107, 43, 0.2) !important;
}
.custom-match-button:hover {
transform: translateY(-2px) !important;
box-shadow: 0 6px 20px rgba(255, 107, 43, 0.3) !important;
background: linear-gradient(to right, #ff6b2b, #ff5722) !important;
}
</style>
""")
# 創建新的按鈕
get_recommendations_btn = gr.Button(
"Find My Perfect Match 🐾",
elem_classes="custom-match-button",
variant="primary"
)
# 創建輸出顯示組件
recommendation_output = gr.HTML(
label="Breed Recommendations",
visible=True,
elem_id="recommendation-output"
)
async def on_find_match_click(*args):
try:
loading_messages = [
"🐕 Sniffing out your perfect match...",
"🔍 Searching through our database...",
"🎾 Playing fetch with potential matches...",
"🦴 Evaluating compatibility scores...",
"💭 Getting opinions from our experts..."
]
for message in loading_messages:
yield gr.HTML("""
<div style="
text-align: center;
padding: 15px;
background: white;
border-radius: 8px;
margin: 10px 0;
color: #4a5568;
font-size: 1.1em;
font-weight: 500;
">
{message}
</div>
""".format(message=message))
await asyncio.sleep(0.5)
# 處理用戶數據和獲取推薦
user_prefs = UserPreferences(
living_space=args[0],
yard_access=args[1],
exercise_time=args[2],
exercise_type=args[3],
grooming_commitment=args[4],
size_preference=args[5],
experience_level=args[6],
time_availability=args[7],
has_children=args[8],
children_age=args[9] if args[8] else None,
noise_tolerance=args[10],
space_for_play=True if args[0] != "apartment" else False,
other_pets=False,
climate="moderate",
health_sensitivity="medium",
barking_acceptance=args[10]
)
recommendations = get_breed_recommendations(user_prefs, top_n=15)
# 保存搜索歷史
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],
'yard_access': args[1],
'exercise_time': args[2],
'exercise_type': args[3],
'grooming_commitment': args[4],
'size_preference': args[5],
'experience_level': args[6],
'time_availability': args[7],
'has_children': args[8],
'children_age': args[9] if args[8] else None,
'noise_tolerance': args[10],
'search_type': 'Criteria'
},
results=history_results
)
# 使用 yield 而不是 return 來返回最終結果
yield gr.HTML(format_recommendation_html(recommendations, is_description_search=False))
except Exception as e:
print(f"Error in find match: {str(e)}")
yield gr.HTML("Error getting recommendations")
get_recommendations_btn.click(
fn=on_find_match_click,
inputs=[
living_space,
yard_access,
exercise_time,
exercise_type,
grooming_commitment,
size_preference,
experience_level,
time_availability,
has_children,
children_age,
noise_tolerance
],
outputs=recommendation_output,
api_name=False,
queue=True
)
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,
} |