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"""