Spaces:
Sleeping
Sleeping
File size: 22,453 Bytes
ee184bc 9c51072 ee184bc 9c51072 ee184bc 9c51072 ee184bc 9c51072 ee184bc 9c51072 ee184bc 2ff8199 ee184bc bba5cac ee184bc 12b9dec ee184bc 2ff8199 ee184bc |
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 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 |
import gradio as gr
import sqlite3
from dog_database import get_dog_description
from breed_health_info import breed_health_info
from breed_noise_info import breed_noise_info
def create_comparison_tab(dog_breeds, get_dog_description, breed_noise_info, breed_health_info):
# 每個選項是一個元組:(顯示值, 實際值)
# 對整個列表進行排序,基於顯示值(即沒有底線的品種名稱)
breed_choices = [(breed.replace('_', ' '), breed) for breed in sorted(dog_breeds)]
with gr.TabItem("Breed Comparison"):
gr.HTML("""
<div style='
text-align: center;
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;
'>
<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;
'>
Select two dog breeds to compare their characteristics and care requirements.
</p>
</div>
""")
with gr.Row():
breed1_dropdown = gr.Dropdown(
choices=breed_choices,
label="Select First Breed",
value=breed_choices[0][1] if breed_choices else None # 使用第一個品種的實際值作為預設值
)
breed2_dropdown = gr.Dropdown(
choices=breed_choices,
label="Select Second Breed",
value=breed_choices[1][1] if len(breed_choices) > 1 else None # 使用第二個品種的實際值作為預設值
)
compare_btn = gr.Button("Compare Breeds", elem_classes="custom-compare-button")
comparison_output = gr.HTML(label="Comparison Results")
def format_noise_data(notes):
characteristics = []
triggers = []
noise_level = "Moderate" # 預設值
if isinstance(notes, str):
lines = notes.strip().split('\n')
section = ""
for line in lines:
line = line.strip()
if "Typical noise characteristics:" in line:
section = "characteristics"
elif "Barking triggers:" in line:
section = "triggers"
elif "Noise level:" in line:
noise_level = line.split(':')[1].strip()
elif line.startswith('•'):
if section == "characteristics":
characteristics.append(line[1:].strip())
elif section == "triggers":
triggers.append(line[1:].strip())
return {
'characteristics': characteristics,
'triggers': triggers,
'noise_level': noise_level
}
def format_health_data(notes):
considerations = []
screenings = []
if isinstance(notes, str):
lines = notes.strip().split('\n')
current_section = None
for line in lines:
line = line.strip()
# 修正字串比對
if "Common breed-specific health considerations" in line:
current_section = "considerations"
elif "Recommended health screenings:" in line:
current_section = "screenings"
elif line.startswith('•'):
item = line[1:].strip()
if current_section == "considerations":
considerations.append(item)
elif current_section == "screenings":
screenings.append(item)
# 只有當真的沒有資料時才返回 "Information not available"
if not considerations and not screenings:
return {
'considerations': ["Information not available"],
'screenings': ["Information not available"]
}
return {
'considerations': considerations,
'screenings': screenings
}
def get_comparison_styles():
return """
.comparison-container {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 24px;
padding: 20px;
}
.breed-column {
background: white;
border-radius: 10px;
padding: 24px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.section-title {
font-size: 24px;
color: #2D3748;
margin-bottom: 20px;
padding-bottom: 10px;
border-bottom: 2px solid #E2E8F0;
}
.info-section {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 16px;
margin-bottom: 24px;
}
.info-item {
position: relative;
background: #F8FAFC;
padding: 16px;
border-radius: 8px;
border: 1px solid #E2E8F0;
}
.info-label {
display: flex;
align-items: center;
gap: 8px;
color: #4A5568;
font-size: 0.9em;
margin-bottom: 4px;
}
.info-icon {
cursor: help;
background: #E2E8F0;
width: 18px;
height: 18px;
border-radius: 50%;
display: inline-flex;
align-items: center;
justify-content: center;
font-size: 12px;
color: #4A5568;
margin-left: 4px;
}
.info-icon:hover + .tooltip-content {
display: block;
}
.tooltip-content {
display: none;
position: absolute;
background: #2D3748;
color: #FFFFFF;
padding: 8px 12px;
border-radius: 6px;
font-size: 14px;
line-height: 1.3;
width: max-content;
max-width: 280px;
z-index: 1000;
top: 0; /* 修改位置 */
left: 100%;
margin-left: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
white-space: normal; /* 允許文字換行 */
}
.tooltip-content,
.tooltip-content *,
.tooltip-content strong,
.tooltip-content li,
.tooltip-content ul,
.tooltip-content p,
.tooltip-content span,
.tooltip-content div {
color: #FFFFFF !important;
}
.tooltip-content::before {
content: '';
position: absolute;
left: -6px;
top: 14px; /* 配合上方位置調整 */
border-width: 6px;
border-style: solid;
border-color: transparent #2D3748 transparent transparent;
}
.tooltip-content strong {
color: #FFFFFF;
display: block;
margin-bottom: 4px;
font-weight: 600;
}
.tooltip-content ul {
margin: 0;
padding-left: 16px;
color: #FFFFFF;
}
.tooltip-content * {
color: #FFFFFF;
}
.tooltip-content li {
margin-bottom: 2px;
color: #FFFFFF;
}
.tooltip-content li::before {
color: #FFFFFF !important;
}
.tooltip-content br {
display: block;
margin: 2px 0;
}
.info-value {
color: #2D3748;
font-weight: 500;
}
.characteristic-section {
background: #F8FAFC;
padding: 20px;
border-radius: 8px;
margin-bottom: 20px;
}
.subsection-title {
font-size: 18px;
color: #2D3748;
margin-bottom: 16px;
display: flex;
align-items: center;
gap: 8px;
}
.noise-level {
background: #EDF2F7;
padding: 16px;
border-radius: 6px;
margin: 16px 0;
border: 1px solid #CBD5E0;
}
.level-label {
color: #4A5568;
font-size: 1.1em;
font-weight: 500;
margin-bottom: 8px;
}
.level-value {
color: #2D3748;
font-size: 1.2em;
font-weight: 600;
}
.characteristics-grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 12px;
margin-top: 12px;
}
.characteristic-item {
background: white;
padding: 12px;
border-radius: 6px;
border: 1px solid #E2E8F0;
color: #4A5568;
}
.health-insights {
margin-top: 24px;
}
.health-grid {
display: grid;
grid-template-columns: 1fr;
gap: 8px;
}
.health-item {
background: white;
padding: 12px;
border-radius: 6px;
border: 1px solid #E2E8F0;
color: #E53E3E;
}
.screening-item {
background: white;
padding: 12px;
border-radius: 6px;
border: 1px solid #E2E8F0;
color: #38A169;
}
.learn-more-btn {
display: inline-block;
margin-top: 20px;
padding: 12px 24px;
background: linear-gradient(90deg, #4299e1, #48bb78);
color: white;
text-decoration: none;
border-radius: 6px;
transition: all 0.3s ease;
text-align: center;
width: 100%;
font-weight: 500;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.learn-more-btn:hover {
background: linear-gradient(135deg, #2C5282, #2B6CB0);
transform: translateY(-2px);
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
.info-disclaimer {
margin-top: 24px;
padding: 16px;
background: #F7FAFC;
border-radius: 8px;
font-size: 0.9em;
color: #4A5568;
line-height: 1.5;
border-left: 4px solid #4299E1;
}
@media (max-width: 768px) {
.comparison-container {
grid-template-columns: 1fr;
}
.info-section {
grid-template-columns: 1fr;
}
.characteristics-grid {
grid-template-columns: 1fr;
}
}
.custom-compare-button {
background: linear-gradient(135deg, #4299e1, #48bb78) !important;
border: none !important;
padding: 12px 30px !important;
border-radius: 8px !important;
font-size: 1.1em !important;
font-weight: 600 !important;
color: white !important;
cursor: pointer !important;
transition: all 0.3s ease !important;
box-shadow: 0 4px 6px rgba(66, 153, 225, 0.2) !important;
margin: 20px auto !important;
display: block !important;
width: auto !important;
min-width: 200px !important;
text-transform: uppercase !important;
letter-spacing: 0.5px !important;
}
.custom-compare-button:hover {
transform: translateY(-2px) !important;
box-shadow: 0 6px 12px rgba(66, 153, 225, 0.3) !important;
background: linear-gradient(135deg, #48bb78, #4299e1) !important;
}
.custom-compare-button:active {
transform: translateY(1px) !important;
box-shadow: 0 2px 4px rgba(66, 153, 225, 0.2) !important;
}
"""
gr.HTML(f"<style>{get_comparison_styles()}</style>")
def show_comparison(breed1, breed2):
if not breed1 or not breed2:
return "Please select two breeds to compare"
breed1_info = get_dog_description(breed1)
breed2_info = get_dog_description(breed2)
breed1_noise = breed_noise_info.get(breed1, {})
breed2_noise = breed_noise_info.get(breed2, {})
breed1_health = breed_health_info.get(breed1, {})
breed2_health = breed_health_info.get(breed2, {})
def create_info_item(label, value, tooltip_text=""):
tooltip = f"""
<div class="info-label">
<span>{label}</span>
<div class="tooltip">
<span class="info-icon">i</span>
<div class="tooltip-content">
{tooltip_text}
</div>
</div>
</div>
""" if tooltip_text else f'<div class="info-label">{label}</div>'
return f"""
<div class="info-item" style="position: relative;">
{tooltip}
<div class="info-value">{value}</div>
</div>
"""
def create_breed_section(breed, info, noise_info, health_info):
# 建立提示文字
section_tooltips = {
'Size': """
<strong>Size Categories:</strong><br>
• Small: Under 20 pounds<br>
• Medium: 20-60 pounds<br>
• Large: Over 60 pounds
""",
'Exercise': """
<strong>Exercise Needs:</strong><br>
• Low: Short walks suffice<br>
• Moderate: 1-2 hours daily<br>
• High: 2+ hours daily activity<br>
• Very High: Intensive daily exercise
""",
'Grooming': """
<strong>Grooming Requirements:</strong><br>
• Low: Occasional brushing<br>
• Moderate: Weekly grooming<br>
• High: Daily maintenance needed
""",
'Children': """
<strong>Compatibility with Children:</strong><br>
• Yes: Excellent with kids<br>
• Moderate: Good with supervision<br>
• No: Better with older children
""",
'Lifespan': """
<strong>Average Lifespan Range:</strong><br>
Typical lifespan for this breed with proper care and genetics
""",
'noise': """
<strong>Noise Behavior Information:</strong><br>
• Noise Level indicates typical vocalization intensity<br>
• Characteristics describe common vocal behaviors<br>
• Triggers list common causes of barking or vocalization
""",
'health': """
<strong>Health Information:</strong><br>
• Health considerations are breed-specific concerns<br>
• Screenings are recommended preventive tests<br>
• Always consult with veterinary professionals
"""
}
noise_data = format_noise_data(noise_info.get('noise_notes', ''))
health_data = format_health_data(health_info.get('health_notes', ''))
def create_section_header(title, icon, tooltip_text):
return f"""
<div class="section-header">
<span>{icon}</span>
<span>{title}</span>
<span class="tooltip">
<span class="tooltip-icon">ⓘ</span>
<span class="tooltip-text">{tooltip_text}</span>
</span>
</div>
"""
return f"""
<div class="breed-column">
<h2 class="section-title">🐕 {breed.replace('_', ' ')}</h2>
<div class="info-section">
{create_info_item('Size', info['Size'], section_tooltips['Size'])}
{create_info_item('Exercise', info['Exercise Needs'], section_tooltips['Exercise'])}
{create_info_item('Grooming', info['Grooming Needs'], section_tooltips['Grooming'])}
{create_info_item('With Children', info['Good with Children'], section_tooltips['Children'])}
{create_info_item('Lifespan', info['Lifespan'], section_tooltips['Lifespan'])}
</div>
<div class="characteristic-section">
{create_section_header('Noise Behavior', '🔊', section_tooltips['noise'])}
<div class="noise-level">
<div class="level-label">Noise Level</div>
<div class="level-value">{noise_data['noise_level'].upper()}</div>
</div>
<div class="subsection">
<h4>Typical Characteristics</h4>
<div class="characteristics-grid">
{' '.join([f'<div class="characteristic-item">{char}</div>'
for char in noise_data['characteristics']])}
</div>
</div>
<div class="subsection">
<h4>Barking Triggers</h4>
<div class="characteristics-grid">
{' '.join([f'<div class="characteristic-item">{trigger}</div>'
for trigger in noise_data['triggers']])}
</div>
</div>
</div>
<div class="characteristic-section health-insights">
{create_section_header('Health Insights', '🏥', section_tooltips['health'])}
<div class="subsection">
<h4>Health Considerations</h4>
<div class="health-grid">
{' '.join([f'<div class="health-item">{item}</div>'
for item in health_data['considerations']])}
</div>
</div>
<div class="subsection">
<h4>Recommended Screenings</h4>
<div class="health-grid">
{' '.join([f'<div class="screening-item">{item}</div>'
for item in health_data['screenings']])}
</div>
</div>
</div>
<a href="https://www.akc.org/dog-breeds/{breed.lower().replace('_', '-')}/"
class="learn-more-btn"
target="_blank">
🌐 Learn more about {breed.replace('_', ' ')} on AKC
</a>
</div>
"""
html_output = f"""
<div class="comparison-container">
{create_breed_section(breed1, breed1_info, breed1_noise, breed1_health)}
{create_breed_section(breed2, breed2_info, breed2_noise, breed2_health)}
</div>
<div class="info-disclaimer">
<strong>Note:</strong> The health and behavioral information provided is for general reference only.
Individual dogs may vary, and characteristics can be influenced by training, socialization, and genetics.
Always consult with veterinary professionals for specific health advice and professional trainers for
behavioral guidance.
</div>
<style>{get_comparison_styles()}</style>
"""
return html_output
compare_btn.click(
show_comparison,
inputs=[breed1_dropdown, breed2_dropdown],
outputs=comparison_output
)
return {
'breed1_dropdown': breed1_dropdown,
'breed2_dropdown': breed2_dropdown,
'compare_btn': compare_btn,
'comparison_output': comparison_output
}
|