from typing import Dict, List, Union, Any, Optional, Callable from urllib.parse import quote from breed_health_info import breed_health_info from breed_noise_info import breed_noise_info def get_akc_breeds_link(breed: str) -> str: """Generate AKC breed page URL with intelligent name handling.""" breed_name = breed.lower() breed_name = breed_name.replace('_', '-') breed_name = breed_name.replace("'", '') breed_name = breed_name.replace(" ", '-') special_cases = { 'mexican-hairless': 'xoloitzcuintli', 'brabancon-griffon': 'brussels-griffon', 'bull-mastiff': 'bullmastiff', 'walker-hound': 'treeing-walker-coonhound' } breed_name = special_cases.get(breed_name, breed_name) return f"https://www.akc.org/dog-breeds/{breed_name}/" def get_color_scheme(is_single_dog: bool) -> Union[str, List[str]]: """Get color scheme for dog detection visualization.""" single_dog_color = '#34C759' # 清爽的綠色作為單狗顏色 color_list = [ '#FF5733', # 珊瑚紅 '#28A745', # 深綠色 '#3357FF', # 寶藍色 '#FF33F5', # 粉紫色 '#FFB733', # 橙黃色 '#33FFF5', # 青藍色 '#A233FF', # 紫色 '#FF3333', # 紅色 '#33FFB7', # 青綠色 '#FFE033' # 金黃色 ] return single_dog_color if is_single_dog else color_list def format_warning_html(message: str) -> str: """Format warning messages in a consistent style.""" return f'''

⚠️ {message}

''' def format_error_message(color: str, index: int) -> str: """Format error message when confidence is too low.""" return f'''
Dog {index}
⚠️ The image is unclear or the breed is not in the dataset. Please upload a clearer image.
''' def format_unknown_breed_message(color: str, index: int) -> str: """ 當狗的品種無法被辨識時(信心度低於0.2)使用此函數格式化錯誤訊息。 這種情況通常發生在: 1. 圖片品質不佳 2. 狗的品種不在我們的資料集中 3. 拍攝角度不理想 """ return f'''
Dog {index}
⚠️ Unable to identify the dog breed. This breed might not be included in the dataset.
''' def format_not_dog_message(color: str, index: int) -> str: """ 當YOLO模型檢測到物體不是狗時使用此函數格式化錯誤訊息。 這是第一層的過濾機制,在進行品種分類之前就先確認是否為狗。 """ return f'''
Object {index}
This does not appear to be a dog. Please upload an image containing a dog.
''' def format_description_html(description: Dict[str, Any], breed: str) -> str: """Format basic breed description with tooltips.""" if not isinstance(description, dict): return f"

{description}

" fields_order = [ "Size", "Lifespan", "Temperament", "Exercise Needs", "Grooming Needs", "Care Level", "Good with Children", "Description" ] html_parts = [] for field in fields_order: if field in description: value = description[field] tooltip_html = format_tooltip(field, value) html_parts.append(f'
  • {tooltip_html}
  • ') # Add any remaining fields for key, value in description.items(): if key not in fields_order and key != "Breed": html_parts.append(f'
  • {key}: {value}
  • ') return f'' def format_tooltip(key: str, value: str) -> str: """Format tooltip with content for each field.""" tooltip_contents = { "Size": { "title": "Size Categories", "items": [ "Small: Under 20 pounds", "Medium: 20-60 pounds", "Large: Over 60 pounds", "Giant: Over 100 pounds", "Varies: Depends on variety" ] }, "Exercise Needs": { "title": "Exercise Needs", "items": [ "Low: Short walks and play sessions", "Moderate: 1-2 hours of daily activity", "High: Extensive exercise (2+ hours/day)", "Very High: Constant activity and mental stimulation needed" ] }, "Grooming Needs": { "title": "Grooming Requirements", "items": [ "Low: Basic brushing, occasional baths", "Moderate: Weekly brushing, occasional grooming", "High: Daily brushing, frequent professional grooming needed", "Professional care recommended for all levels" ] }, "Care Level": { "title": "Care Level Explained", "items": [ "Low: Basic care and attention needed", "Moderate: Regular care and routine needed", "High: Significant time and attention needed", "Very High: Extensive care, training and attention required" ] }, "Good with Children": { "title": "Child Compatibility", "items": [ "Yes: Excellent with kids, patient and gentle", "Moderate: Good with older children", "No: Better suited for adult households" ] }, "Lifespan": { "title": "Average Lifespan", "items": [ "Short: 6-8 years", "Average: 10-15 years", "Long: 12-20 years", "Varies by size: Larger breeds typically have shorter lifespans" ] }, "Temperament": { "title": "Temperament Guide", "items": [ "Describes the dog's natural behavior and personality", "Important for matching with owner's lifestyle", "Can be influenced by training and socialization" ] } } tooltip = tooltip_contents.get(key, {"title": key, "items": []}) tooltip_content = "
    ".join([f"• {item}" for item in tooltip["items"]]) return f''' {key}: {tooltip["title"]}:
    {tooltip_content}
    {value} ''' def format_single_dog_result(breed: str, description: Dict[str, Any], color: str = "#34C759") -> str: """Format single dog detection result into HTML.""" # 獲取noise和health資訊 noise_info = breed_noise_info.get(breed, {}) health_info = breed_health_info.get(breed, {}) # 處理噪音資訊 noise_notes = noise_info.get('noise_notes', '').split('\n') noise_characteristics = [] barking_triggers = [] noise_level = noise_info.get('noise_level', 'Information not available') in_section = None for line in noise_notes: line = line.strip() if 'Typical noise characteristics:' in line: in_section = 'characteristics' elif 'Barking triggers:' in line: in_section = 'triggers' elif line.startswith('•'): if in_section == 'characteristics': noise_characteristics.append(line[1:].strip()) elif in_section == 'triggers': barking_triggers.append(line[1:].strip()) # 處理健康資訊 health_notes = health_info.get('health_notes', '').split('\n') health_considerations = [] health_screenings = [] in_section = None for line in health_notes: line = line.strip() if 'Common breed-specific health considerations' in line: in_section = 'considerations' elif 'Recommended health screenings:' in line: in_section = 'screenings' elif line.startswith('•'): if in_section == 'considerations': health_considerations.append(line[1:].strip()) elif in_section == 'screenings': health_screenings.append(line[1:].strip()) display_breeds_name = breed.replace('_', ' ') return f'''
    🐾

    {display_breeds_name}

    📋 BASIC INFORMATION

    📏 Size Size Categories:
    • Small: Under 20 pounds
    • Medium: 20-60 pounds
    • Large: Over 60 pounds
    • Giant: Over 100 pounds
    {description['Size']}
    Lifespan Lifespan Categories:
    • Short: 6-8 years
    • Average: 10-15 years
    • Long: 12-20 years
    {description['Lifespan']}

    💪 CARE REQUIREMENTS

    🏃 Exercise Exercise Needs:
    • Low: Short walks
    • Moderate: 1-2 hours daily
    • High: 2+ hours daily
    {description['Exercise Needs']}
    ✂️ Grooming Grooming Requirements:
    • Low: Basic brushing
    • Moderate: Weekly grooming
    • High: Daily maintenance
    {description['Grooming Needs']}
    Care Level Care Level:
    • Low: Basic care
    • Moderate: Regular care
    • High: Extensive care
    {description['Care Level']}

    🔊 NOISE BEHAVIOR Noise Behavior:
    • Typical vocalization patterns
    • Common triggers and frequency
    • Based on breed characteristics

    Noise Level: {noise_level}
    {format_noise_items(noise_characteristics[:2])}
    Show Complete Noise Information

    All Characteristics:

    {format_noise_items(noise_characteristics)}

    Barking Triggers:

    {format_noise_items(barking_triggers)}

    Source: Compiled from various breed behavior resources, 2024

    Individual dogs may vary in their vocalization patterns.

    Training can significantly influence barking behavior.

    Environmental factors may affect noise levels.

    🏥 HEALTH INSIGHTS Health Information:
    • Common breed-specific conditions
    • Recommended health screenings
    • General health considerations

    {format_health_items(health_considerations[:2])}
    Show Complete Health Information

    All Health Considerations:

    {format_health_items(health_considerations)}

    Recommended Screenings:

    {format_health_items(health_screenings)}

    Source: Compiled from various veterinary and breed information resources, 2024

    This information is for reference only and based on breed tendencies.

    Each dog is unique and may not develop any or all of these conditions.

    Always consult with qualified veterinarians for professional advice.

    📝 DESCRIPTION

    {description.get('Description', '')}

    ''' def format_noise_items(items: List[str]) -> str: """Format noise-related items into HTML list items.""" if not items: return "
    Information not available
    " return "\n".join([f"
    • {item}
    " for item in items]) def format_health_items(items: List[str]) -> str: """Format health-related items into HTML list items.""" if not items: return "
    Information not available
    " return "\n".join([f"
    • {item}
    " for item in items]) def format_multiple_breeds_result( topk_breeds: List[str], relative_probs: List[str], color: str, index: int, get_dog_description: Callable ) -> str: """Format multiple breed predictions into HTML with complete information.""" display_breeds = [breed.replace('_', ' ') for breed in topk_breeds] result = f'''
    🐾 Dog {index}
    ℹ️ Note: The model is showing some uncertainty in its predictions. Here are the most likely breeds based on the available visual features.
    ''' for j, (breed, display_name, prob) in enumerate(zip(topk_breeds, display_breeds, relative_probs)): description = get_dog_description(breed) noise_info = breed_noise_info.get(breed, {}) health_info = breed_health_info.get(breed, {}) # 處理噪音資訊 noise_notes = noise_info.get('noise_notes', '').split('\n') noise_characteristics = [] barking_triggers = [] noise_level = noise_info.get('noise_level', 'Information not available') in_section = None for line in noise_notes: line = line.strip() if 'Typical noise characteristics:' in line: in_section = 'characteristics' elif 'Barking triggers:' in line: in_section = 'triggers' elif line.startswith('•'): if in_section == 'characteristics': noise_characteristics.append(line[1:].strip()) elif in_section == 'triggers': barking_triggers.append(line[1:].strip()) # 處理健康資訊 health_notes = health_info.get('health_notes', '').split('\n') health_considerations = [] health_screenings = [] in_section = None for line in health_notes: line = line.strip() if 'Common breed-specific health considerations' in line: in_section = 'considerations' elif 'Recommended health screenings:' in line: in_section = 'screenings' elif line.startswith('•'): if in_section == 'considerations': health_considerations.append(line[1:].strip()) elif in_section == 'screenings': health_screenings.append(line[1:].strip()) result += f'''
    🐾

    {'Option ' + str(j+1) + ': ' if prob else ''}{display_name}

    {f'Confidence: {prob}' if prob else ''}

    📋 BASIC INFORMATION

    📏 Size
    Size Categories: • Small: Under 20 pounds
    • Medium: 20-60 pounds
    • Large: Over 60 pounds
    • Giant: Over 100 pounds
    {description['Size']}
    Lifespan
    Lifespan Categories: • Short: 6-8 years
    • Average: 10-15 years
    • Long: 12-20 years
    {description['Lifespan']}

    💪 CARE REQUIREMENTS

    🏃 Exercise
    Exercise Needs: • Low: Short walks
    • Moderate: 1-2 hours daily
    • High: 2+ hours daily
    {description['Exercise Needs']}
    ✂️ Grooming
    Grooming Requirements: • Low: Basic brushing
    • Moderate: Weekly grooming
    • High: Daily maintenance
    {description['Grooming Needs']}
    Care Level
    Care Level: • Low: Basic care
    • Moderate: Regular care
    • High: Extensive care
    {description['Care Level']}

    🔊 NOISE BEHAVIOR
    Noise Behavior: • Typical vocalization patterns
    • Common triggers and frequency
    • Based on breed characteristics

    Noise Level: {noise_level}
    {format_noise_items(noise_characteristics[:2])}
    Show Complete Noise Information

    All Characteristics

    {format_noise_items(noise_characteristics)}

    Barking Triggers

    {format_noise_items(barking_triggers)}

    Source: Compiled from various breed behavior resources, 2024

    Individual dogs may vary in their vocalization patterns.

    Training can significantly influence barking behavior.

    Environmental factors may affect noise levels.

    🏥 HEALTH INSIGHTS
    Health Information: • Common breed-specific conditions
    • Recommended health screenings
    • General health considerations

    {format_health_items(health_considerations[:2])}
    Show Complete Health Information

    All Health Considerations

    {format_health_items(health_considerations)}

    Recommended Screenings

    {format_health_items(health_screenings)}

    Source: Compiled from veterinary resources and breed health studies, 2024

    Regular vet check-ups are essential for all breeds.

    Early detection and prevention are key to managing health issues.

    Not all dogs will develop these conditions.

    📝 DESCRIPTION

    {description.get('Description', '')}

    ''' return result def format_multi_dog_container(dogs_info: str) -> str: """Wrap multiple dog detection results in a container.""" return f"""
    {dogs_info}
    """ def format_breed_details_html(description: Dict[str, Any], breed: str) -> str: """Format breed details for the show_details_html function.""" return f"""

    {breed}

    {format_description_html(description, breed)}
    """ def format_comparison_result(breed1: str, breed2: str, comparison_data: Dict) -> str: """Format breed comparison results into HTML.""" return f"""

    Comparison: {breed1} vs {breed2}

    {breed1}

    {format_comparison_details(comparison_data[breed1])}

    {breed2}

    {format_comparison_details(comparison_data[breed2])}
    """ def format_comparison_details(breed_data: Dict) -> str: """Format individual breed details for comparison.""" original_data = breed_data.get('Original_Data', {}) return f"""

    Size: {original_data.get('Size', 'N/A')}

    Exercise Needs: {original_data.get('Exercise Needs', 'N/A')}

    Care Level: {original_data.get('Care Level', 'N/A')}

    Grooming Needs: {original_data.get('Grooming Needs', 'N/A')}

    Good with Children: {original_data.get('Good with Children', 'N/A')}

    Temperament: {original_data.get('Temperament', 'N/A')}

    """