import gradio as gr from dog_database import get_dog_description def create_comparison_tab(dog_breeds, get_dog_description): """创建品种比较标签页 Args: dog_breeds: 狗品种列表 get_dog_description: 获取品种描述的函数 """ with gr.TabItem("Breed Comparison"): gr.HTML("

Select two dog breeds to compare their characteristics and care requirements.

") with gr.Row(): breed1_dropdown = gr.Dropdown( choices=dog_breeds, label="Select First Breed", value="Golden_Retriever" ) breed2_dropdown = gr.Dropdown( choices=dog_breeds, label="Select Second Breed", value="Border_Collie" ) compare_btn = gr.Button("Compare Breeds") comparison_output = gr.HTML(label="Comparison Results") def get_comparison_styles(): return """ /* Comparison specific styles */ .comparison-grid { display: flex; flex-direction: column; gap: 0; position: relative; } .breed-column { padding: 24px; position: relative; } .breed-column:first-child { margin-bottom: 60px; padding-bottom: 40px; } .breed-column:first-child::after { content: ''; position: absolute; bottom: -30px; left: 0; right: 0; height: 2px; background: linear-gradient( to right, transparent, #cbd5e0 10%, #cbd5e0 90%, transparent ); box-shadow: 0 1px 2px rgba(0,0,0,0.1); } .breed-column:first-child::before { content: '•••'; position: absolute; bottom: -38px; left: 50%; transform: translateX(-50%); font-size: 24px; letter-spacing: 8px; color: #94a3b8; text-align: center; background: white; padding: 0 20px; z-index: 1; } .breed-column:first-child .action-section { margin-bottom: 0; padding-bottom: 0; } @media (max-width: 768px) { .breed-column:first-child { margin-bottom: 50px; padding-bottom: 30px; } .breed-column:first-child::after { bottom: -25px; } .breed-column:first-child::before { bottom: -33px; font-size: 20px; } } .dog-info-card { background: white; position: relative; z-index: 0; } .breed-column:nth-child(2) { position: relative; margin-top: 20px; } """ 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, {}).get('noise_notes', '').strip().split('\n') breed2_noise = breed_noise_info.get(breed2, {}).get('noise_notes', '').strip().split('\n') breed1_health = breed_health_info.get(breed1, {}).get('health_notes', '').strip().split('\n') breed2_health = breed_health_info.get(breed2, {}).get('health_notes', '').strip().split('\n') def format_noise_info(noise_data): characteristics = [] triggers = [] noise_level = "Moderate" # 默认值 in_characteristics = False in_triggers = False for line in noise_data: line = line.strip() if "Typical noise characteristics:" in line: in_characteristics = True continue elif "Barking triggers:" in line: in_triggers = True in_characteristics = False continue elif "Noise level:" in line: noise_level = line.split(':')[1].strip() continue if line.startswith('•'): if in_characteristics: characteristics.append(line[1:].strip()) elif in_triggers: triggers.append(line[1:].strip()) return { 'characteristics': characteristics, 'triggers': triggers, 'noise_level': noise_level } def format_health_info(health_data): considerations = [] screenings = [] in_considerations = False in_screenings = False for line in health_data: line = line.strip() if "Common breed-specific health considerations" in line: in_considerations = True in_screenings = False continue elif "Recommended health screenings:" in line: in_screenings = True in_considerations = False continue if line.startswith('•'): if in_considerations: considerations.append(line[1:].strip()) elif in_screenings: screenings.append(line[1:].strip()) return { 'considerations': considerations, 'screenings': screenings } def create_breed_column(breed, info, noise_data, health_data): noise_info = format_noise_info(noise_data) health_info = format_health_info(health_data) basic_info = f"""

🐕 {breed.replace('_', ' ')}

📏 Size: Size Categories:
• Small: Under 20 pounds
• Medium: 20-60 pounds
• Large: Over 60 pounds
{info.get('Size', 'Not available')}
🏃 Exercise: Exercise Needs:
• Low: Short walks
• Moderate: 1-2 hours daily
• High: 2+ hours daily
{info.get('Exercise Needs', 'Not available')}
✂️ Grooming: Grooming Requirements:
• Low: Occasional brushing
• Moderate: Weekly grooming
• High: Daily maintenance
{info.get('Grooming Needs', 'Not available')}
👨‍👩‍👧‍👦 With Children: Child Compatibility:
• Yes: Excellent with kids
• Moderate: Good with older children
• No: Better for adult households
{info.get('Good with Children', 'Not available')}
Lifespan: Average Lifespan:
• Short: 6-8 years
• Average: 10-15 years
• Long: 12-20 years
{info.get('Lifespan', 'Not available')}
""" # Noise Section noise_section = f"""

🔊 Noise Behavior Information about typical barking patterns and noise levels

Typical noise characteristics:

{' '.join([f'
{item}
' for item in noise_info['characteristics']]) or '
Information not available
'}

Noise level:

{noise_info['noise_level']}

Barking triggers:

{' '.join([f'
{item}
' for item in noise_info['triggers']]) or '
Information not available
'}
""" # Health Section health_section = f"""

🏥 Health Insights Health information is compiled from multiple sources including veterinary resources, breed guides, and international canine health databases.

Common health considerations:

{' '.join([f'
{item}
' for item in health_info['considerations']]) or '
Information not available
'}

Recommended screenings:

{' '.join([f'
{item}
' for item in health_info['screenings']]) or '
Information not available
'}
🌐 Learn More about {breed.replace('_', ' ')} on AKC
""" return basic_info + noise_section + health_section html_output = f"""
{create_breed_column(breed1, breed1_info, breed1_noise, breed1_health)} {create_breed_column(breed2, breed2_info, breed2_noise, breed2_health)}
""" 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 }