Spaces:
Running
on
Zero
Running
on
Zero
| import os | |
| import numpy as np | |
| import torch | |
| import torch.nn as nn | |
| import gradio as gr | |
| from torchvision.models import efficientnet_v2_m, EfficientNet_V2_M_Weights | |
| from torchvision.ops import nms, box_iou | |
| import torch.nn.functional as F | |
| from torchvision import transforms | |
| from PIL import Image, ImageDraw, ImageFont, ImageFilter | |
| from data_manager import get_dog_description | |
| from urllib.parse import quote | |
| from ultralytics import YOLO | |
| import asyncio | |
| import traceback | |
| model_yolo = YOLO('yolov8l.pt') | |
| dog_breeds = ["Afghan_Hound", "African_Hunting_Dog", "Airedale", "American_Staffordshire_Terrier", | |
| "Appenzeller", "Australian_Terrier", "Bedlington_Terrier", "Bernese_Mountain_Dog", | |
| "Blenheim_Spaniel", "Border_Collie", "Border_Terrier", "Boston_Bull", "Bouvier_Des_Flandres", | |
| "Brabancon_Griffon", "Brittany_Spaniel", "Cardigan", "Chesapeake_Bay_Retriever", | |
| "Chihuahua", "Dandie_Dinmont", "Doberman", "English_Foxhound", "English_Setter", | |
| "English_Springer", "EntleBucher", "Eskimo_Dog", "French_Bulldog", "German_Shepherd", | |
| "German_Short-Haired_Pointer", "Gordon_Setter", "Great_Dane", "Great_Pyrenees", | |
| "Greater_Swiss_Mountain_Dog", "Ibizan_Hound", "Irish_Setter", "Irish_Terrier", | |
| "Irish_Water_Spaniel", "Irish_Wolfhound", "Italian_Greyhound", "Japanese_Spaniel", | |
| "Kerry_Blue_Terrier", "Labrador_Retriever", "Lakeland_Terrier", "Leonberg", "Lhasa", | |
| "Maltese_Dog", "Mexican_Hairless", "Newfoundland", "Norfolk_Terrier", "Norwegian_Elkhound", | |
| "Norwich_Terrier", "Old_English_Sheepdog", "Pekinese", "Pembroke", "Pomeranian", | |
| "Rhodesian_Ridgeback", "Rottweiler", "Saint_Bernard", "Saluki", "Samoyed", | |
| "Scotch_Terrier", "Scottish_Deerhound", "Sealyham_Terrier", "Shetland_Sheepdog", | |
| "Shih-Tzu", "Siberian_Husky", "Staffordshire_Bullterrier", "Sussex_Spaniel", | |
| "Tibetan_Mastiff", "Tibetan_Terrier", "Walker_Hound", "Weimaraner", | |
| "Welsh_Springer_Spaniel", "West_Highland_White_Terrier", "Yorkshire_Terrier", | |
| "Affenpinscher", "Basenji", "Basset", "Beagle", "Black-and-Tan_Coonhound", "Bloodhound", | |
| "Bluetick", "Borzoi", "Boxer", "Briard", "Bull_Mastiff", "Cairn", "Chow", "Clumber", | |
| "Cocker_Spaniel", "Collie", "Curly-Coated_Retriever", "Dhole", "Dingo", | |
| "Flat-Coated_Retriever", "Giant_Schnauzer", "Golden_Retriever", "Groenendael", "Keeshond", | |
| "Kelpie", "Komondor", "Kuvasz", "Malamute", "Malinois", "Miniature_Pinscher", | |
| "Miniature_Poodle", "Miniature_Schnauzer", "Otterhound", "Papillon", "Pug", "Redbone", | |
| "Schipperke", "Silky_Terrier", "Soft-Coated_Wheaten_Terrier", "Standard_Poodle", | |
| "Standard_Schnauzer", "Toy_Poodle", "Toy_Terrier", "Vizsla", "Whippet", | |
| "Wire-Haired_Fox_Terrier"] | |
| class MultiHeadAttention(nn.Module): | |
| def __init__(self, in_dim, num_heads=8): | |
| super().__init__() | |
| self.num_heads = num_heads | |
| self.head_dim = max(1, in_dim // num_heads) | |
| self.scaled_dim = self.head_dim * num_heads | |
| self.fc_in = nn.Linear(in_dim, self.scaled_dim) | |
| self.query = nn.Linear(self.scaled_dim, self.scaled_dim) | |
| self.key = nn.Linear(self.scaled_dim, self.scaled_dim) | |
| self.value = nn.Linear(self.scaled_dim, self.scaled_dim) | |
| self.fc_out = nn.Linear(self.scaled_dim, in_dim) | |
| def forward(self, x): | |
| N = x.shape[0] | |
| x = self.fc_in(x) | |
| q = self.query(x).view(N, self.num_heads, self.head_dim) | |
| k = self.key(x).view(N, self.num_heads, self.head_dim) | |
| v = self.value(x).view(N, self.num_heads, self.head_dim) | |
| energy = torch.einsum("nqd,nkd->nqk", [q, k]) | |
| attention = F.softmax(energy / (self.head_dim ** 0.5), dim=2) | |
| out = torch.einsum("nqk,nvd->nqd", [attention, v]) | |
| out = out.reshape(N, self.scaled_dim) | |
| out = self.fc_out(out) | |
| return out | |
| class BaseModel(nn.Module): | |
| def __init__(self, num_classes, device='cuda' if torch.cuda.is_available() else 'cpu'): | |
| super().__init__() | |
| self.device = device | |
| self.backbone = efficientnet_v2_m(weights=EfficientNet_V2_M_Weights.IMAGENET1K_V1) | |
| self.feature_dim = self.backbone.classifier[1].in_features | |
| self.backbone.classifier = nn.Identity() | |
| self.num_heads = max(1, min(8, self.feature_dim // 64)) | |
| self.attention = MultiHeadAttention(self.feature_dim, num_heads=self.num_heads) | |
| self.classifier = nn.Sequential( | |
| nn.LayerNorm(self.feature_dim), | |
| nn.Dropout(0.3), | |
| nn.Linear(self.feature_dim, num_classes) | |
| ) | |
| self.to(device) | |
| def forward(self, x): | |
| x = x.to(self.device) | |
| features = self.backbone(x) | |
| attended_features = self.attention(features) | |
| logits = self.classifier(attended_features) | |
| return logits, attended_features | |
| num_classes = 120 | |
| device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') | |
| model = BaseModel(num_classes=num_classes, device=device) | |
| checkpoint = torch.load('best_model_81_dog.pth', map_location=torch.device('cpu')) | |
| model.load_state_dict(checkpoint['model_state_dict']) | |
| # evaluation mode | |
| model.eval() | |
| # Image preprocessing function | |
| def preprocess_image(image): | |
| # If the image is numpy.ndarray turn into PIL.Image | |
| if isinstance(image, np.ndarray): | |
| image = Image.fromarray(image) | |
| # Use torchvision.transforms to process images | |
| transform = transforms.Compose([ | |
| transforms.Resize((224, 224)), | |
| transforms.ToTensor(), | |
| transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), | |
| ]) | |
| return transform(image).unsqueeze(0) | |
| def get_akc_breeds_link(): | |
| return "https://www.akc.org/dog-breeds/" | |
| async def predict_single_dog(image): | |
| image_tensor = preprocess_image(image) | |
| with torch.no_grad(): | |
| output = model(image_tensor) | |
| logits = output[0] if isinstance(output, tuple) else output | |
| probabilities = F.softmax(logits, dim=1) | |
| topk_probs, topk_indices = torch.topk(probabilities, k=3) | |
| top1_prob = topk_probs[0][0].item() | |
| topk_breeds = [dog_breeds[idx.item()] for idx in topk_indices[0]] | |
| topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]] | |
| return top1_prob, topk_breeds, topk_probs_percent | |
| async def detect_multiple_dogs(image, conf_threshold=0.35, iou_threshold=0.55): | |
| results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0] | |
| dogs = [] | |
| boxes = [] | |
| for box in results.boxes: | |
| if box.cls == 16: # COCO dataset class for dog is 16 | |
| xyxy = box.xyxy[0].tolist() | |
| confidence = box.conf.item() | |
| boxes.append((xyxy, confidence)) | |
| if not boxes: | |
| dogs.append((image, 1.0, [0, 0, image.width, image.height])) | |
| else: | |
| nms_boxes = non_max_suppression(boxes, iou_threshold) | |
| for box, confidence in nms_boxes: | |
| x1, y1, x2, y2 = box | |
| w, h = x2 - x1, y2 - y1 | |
| x1 = max(0, x1 - w * 0.05) | |
| y1 = max(0, y1 - h * 0.05) | |
| x2 = min(image.width, x2 + w * 0.05) | |
| y2 = min(image.height, y2 + h * 0.05) | |
| cropped_image = image.crop((x1, y1, x2, y2)) | |
| dogs.append((cropped_image, confidence, [x1, y1, x2, y2])) | |
| return dogs | |
| def non_max_suppression(boxes, iou_threshold): | |
| keep = [] | |
| boxes = sorted(boxes, key=lambda x: x[1], reverse=True) | |
| while boxes: | |
| current = boxes.pop(0) | |
| keep.append(current) | |
| boxes = [box for box in boxes if calculate_iou(current[0], box[0]) < iou_threshold] | |
| return keep | |
| def calculate_iou(box1, box2): | |
| x1 = max(box1[0], box2[0]) | |
| y1 = max(box1[1], box2[1]) | |
| x2 = min(box1[2], box2[2]) | |
| y2 = min(box1[3], box2[3]) | |
| intersection = max(0, x2 - x1) * max(0, y2 - y1) | |
| area1 = (box1[2] - box1[0]) * (box1[3] - box1[1]) | |
| area2 = (box2[2] - box2[0]) * (box2[3] - box2[1]) | |
| iou = intersection / float(area1 + area2 - intersection) | |
| return iou | |
| async def process_single_dog(image): | |
| top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image) | |
| if top1_prob < 0.15: | |
| initial_state = { | |
| "explanation": "The image is unclear or the breed is not in the dataset. Please upload a clearer image of a dog.", | |
| "buttons": [], | |
| "show_back": False, | |
| "image": None, | |
| "is_multi_dog": False | |
| } | |
| return initial_state["explanation"], None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), initial_state | |
| breed = topk_breeds[0] | |
| description = get_dog_description(breed) | |
| if top1_prob >= 0.45: | |
| formatted_description = format_description(description, breed) | |
| initial_state = { | |
| "explanation": formatted_description, | |
| "buttons": [], | |
| "show_back": False, | |
| "image": image, | |
| "is_multi_dog": False | |
| } | |
| return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), initial_state | |
| else: | |
| explanation = ( | |
| f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n" | |
| f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n" | |
| f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n" | |
| f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n" | |
| "Click on a button to view more information about the breed." | |
| ) | |
| buttons = [ | |
| gr.update(visible=True, value=f"More about {topk_breeds[0]}"), | |
| gr.update(visible=True, value=f"More about {topk_breeds[1]}"), | |
| gr.update(visible=True, value=f"More about {topk_breeds[2]}") | |
| ] | |
| initial_state = { | |
| "explanation": explanation, | |
| "buttons": buttons, | |
| "show_back": True, | |
| "image": image, | |
| "is_multi_dog": False | |
| } | |
| return explanation, image, buttons[0], buttons[1], buttons[2], gr.update(visible=True), initial_state | |
| # async def predict(image): | |
| # if image is None: | |
| # return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None | |
| # try: | |
| # if isinstance(image, np.ndarray): | |
| # image = Image.fromarray(image) | |
| # dogs = await detect_multiple_dogs(image) | |
| # color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500'] | |
| # buttons = [] | |
| # annotated_image = image.copy() | |
| # draw = ImageDraw.Draw(annotated_image) | |
| # font = ImageFont.load_default() | |
| # dogs_info = "" | |
| # for i, (cropped_image, detection_confidence, box) in enumerate(dogs): | |
| # buttons_html = "" | |
| # top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image) | |
| # color = color_list[i % len(color_list)] | |
| # draw.rectangle(box, outline=color, width=3) | |
| # draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font) | |
| # combined_confidence = detection_confidence * top1_prob | |
| # dogs_info += f'<div class="dog-info" style="border-left: 5px solid {color}; margin-bottom: 20px; padding: 15px;">' | |
| # dogs_info += f'<h2>Dog {i+1}</h2>' | |
| # if top1_prob >= 0.45: | |
| # breed = topk_breeds[0] | |
| # description = get_dog_description(breed) | |
| # dogs_info += format_description_html(description, breed) | |
| # elif combined_confidence >= 0.15: | |
| # dogs_info += f"<p>Top 3 possible breeds:</p><ul>" | |
| # for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])): | |
| # prob = float(prob.replace('%', '')) | |
| # dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>" | |
| # dogs_info += "</ul>" | |
| # for breed in topk_breeds[:3]: | |
| # button_id = f"Dog {i+1}: More about {breed}" | |
| # buttons_html += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>' | |
| # buttons.append(button_id) | |
| # else: | |
| # dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>" | |
| # dogs_info += '</div>' | |
| # buttons_html = "" | |
| # html_output = f""" | |
| # <style> | |
| # .dog-info {{ border: 1px solid #ddd; margin-bottom: 20px; padding: 15px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); }} | |
| # .dog-info h2 {{ background-color: #f0f0f0; padding: 10px; margin: -15px -15px 15px -15px; border-radius: 5px 5px 0 0; }} | |
| # .breed-buttons {{ margin-top: 10px; }} | |
| # .breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }} | |
| # </style> | |
| # {dogs_info} | |
| # """ | |
| # if buttons: | |
| # html_output += """ | |
| # <script> | |
| # function handle_button_click(button_id) { | |
| # const radio = document.querySelector('input[type=radio][value="' + button_id + '"]'); | |
| # if (radio) { | |
| # radio.click(); | |
| # } else { | |
| # console.error("Radio button not found:", button_id); | |
| # } | |
| # } | |
| # </script> | |
| # """ | |
| # initial_state = { | |
| # "dogs_info": dogs_info, | |
| # "buttons": buttons, | |
| # "show_back": True, | |
| # "image": annotated_image, | |
| # "is_multi_dog": len(dogs) > 1, | |
| # "html_output": html_output | |
| # } | |
| # return html_output, annotated_image, gr.update(visible=True, choices=buttons), initial_state | |
| # else: | |
| # initial_state = { | |
| # "dogs_info": dogs_info, | |
| # "buttons": [], | |
| # "show_back": False, | |
| # "image": annotated_image, | |
| # "is_multi_dog": len(dogs) > 1, | |
| # "html_output": html_output | |
| # } | |
| # return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state | |
| # except Exception as e: | |
| # error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}" | |
| # print(error_msg) | |
| # return error_msg, None, gr.update(visible=False, choices=[]), None | |
| async def predict(image): | |
| if image is None: | |
| return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None | |
| try: | |
| if isinstance(image, np.ndarray): | |
| image = Image.fromarray(image) | |
| dogs = await detect_multiple_dogs(image) | |
| color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500'] | |
| buttons = [] | |
| annotated_image = image.copy() | |
| draw = ImageDraw.Draw(annotated_image) | |
| font = ImageFont.load_default() | |
| dogs_info = "" | |
| for i, (cropped_image, detection_confidence, box) in enumerate(dogs): | |
| # ๆฏๅๅพช็ฐๅๅงๅ buttons_html๏ผ็ขบไฟๆ้ๅๅ็จฎไธ้่ค | |
| buttons_html = "" | |
| top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image) | |
| color = color_list[i % len(color_list)] | |
| draw.rectangle(box, outline=color, width=3) | |
| draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font) | |
| combined_confidence = detection_confidence * top1_prob | |
| dogs_info += f'<div class="dog-info" style="border-left: 5px solid {color}; margin-bottom: 20px; padding: 15px;">' | |
| dogs_info += f'<h2>Dog {i+1}</h2>' | |
| if top1_prob >= 0.45: | |
| breed = topk_breeds[0] | |
| description = get_dog_description(breed) | |
| dogs_info += format_description_html(description, breed) | |
| # ๆพๅ ฅๆผๆฏๅๅ็จฎๆ่ฟฐๅๅกไธญ๏ผ็ดๆฅ้ๅ buttons_html | |
| elif combined_confidence >= 0.15: | |
| dogs_info += f"<p>Top 3 possible breeds:</p><ul>" | |
| for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])): | |
| prob = float(prob.replace('%', '')) | |
| dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>" | |
| dogs_info += "</ul>" | |
| # Append buttons directly into this dog's section | |
| buttons_html = "" | |
| for breed in topk_breeds[:3]: | |
| button_id = f"Dog {i+1}: More about {breed}" | |
| buttons_html += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>' | |
| buttons.append(button_id) | |
| # ๆ buttons_html ๆพๅ ฅ้ๅ็นๅฎ็็ๅๅก | |
| dogs_info += buttons_html | |
| # ๅจๅ็จฎ็ๆ่ฟฐไธๆๅ ฅๅฐๆๆ้ | |
| for breed in topk_breeds[:3]: | |
| button_id = f"Dog {i+1}: More about {breed}" | |
| buttons_html += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>' | |
| buttons.append(button_id) | |
| dogs_info += buttons_html # ๅฐๆ้็ดๆฅๆๅ ฅ็ถๅๅๅก | |
| else: | |
| dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>" | |
| dogs_info += '</div>' | |
| html_output = f""" | |
| <style> | |
| .dog-info {{ border: 1px solid #ddd; margin-bottom: 20px; padding: 15px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); }} | |
| .dog-info h2 {{ background-color: #f0f0f0; padding: 10px; margin: -15px -15px 15px -15px; border-radius: 5px 5px 0 0; }} | |
| .breed-buttons {{ margin-top: 10px; }} | |
| .breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }} | |
| </style> | |
| {dogs_info} | |
| """ | |
| if buttons: | |
| html_output += """ | |
| <script> | |
| function handle_button_click(button_id) { | |
| const radio = document.querySelector('input[type=radio][value="' + button_id + '"]'); | |
| if (radio) { | |
| radio.click(); | |
| } else { | |
| console.error("Radio button not found:", button_id); | |
| } | |
| } | |
| </script> | |
| """ | |
| initial_state = { | |
| "dogs_info": dogs_info, | |
| "buttons": buttons, | |
| "show_back": True, | |
| "image": annotated_image, | |
| "is_multi_dog": len(dogs) > 1, | |
| "html_output": html_output | |
| } | |
| return html_output, annotated_image, gr.update(visible=True, choices=buttons), initial_state | |
| else: | |
| initial_state = { | |
| "dogs_info": dogs_info, | |
| "buttons": [], | |
| "show_back": False, | |
| "image": annotated_image, | |
| "is_multi_dog": len(dogs) > 1, | |
| "html_output": html_output | |
| } | |
| return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state | |
| except Exception as e: | |
| error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}" | |
| print(error_msg) | |
| return error_msg, None, gr.update(visible=False, choices=[]), None | |
| def show_details_html(choice, previous_output, initial_state): | |
| if not choice: | |
| return previous_output, gr.update(visible=True), initial_state | |
| try: | |
| breed = choice.split("More about ")[-1] | |
| description = get_dog_description(breed) | |
| formatted_description = format_description_html(description, breed) | |
| html_output = f""" | |
| <div class="dog-info"> | |
| <h2>{breed}</h2> | |
| {formatted_description} | |
| </div> | |
| """ | |
| initial_state["current_description"] = html_output | |
| initial_state["original_buttons"] = initial_state.get("buttons", []) | |
| return html_output, gr.update(visible=True), initial_state | |
| except Exception as e: | |
| error_msg = f"An error occurred while showing details: {e}" | |
| print(error_msg) | |
| return f"<p style='color: red;'>{error_msg}</p>", gr.update(visible=True), initial_state | |
| def format_description_html(description, breed): | |
| html = "<ul style='list-style-type: none; padding-left: 0;'>" | |
| if isinstance(description, dict): | |
| for key, value in description.items(): | |
| html += f"<li style='margin-bottom: 10px;'><strong>{key}:</strong> {value}</li>" | |
| elif isinstance(description, str): | |
| html += f"<li>{description}</li>" | |
| else: | |
| html += f"<li>No description available for {breed}</li>" | |
| html += "</ul>" | |
| akc_link = get_akc_breeds_link() | |
| html += f'<p><a href="{akc_link}" target="_blank">Learn more about {breed} on the AKC website</a></p>' | |
| return html | |
| def go_back(state): | |
| buttons = state.get("buttons", []) | |
| return ( | |
| state["html_output"], | |
| state["image"], | |
| gr.update(visible=True, choices=buttons), | |
| gr.update(visible=False), | |
| state | |
| ) | |
| with gr.Blocks() as iface: | |
| gr.HTML("<h1 style='text-align: center;'>๐ถ Dog Breed Classifier ๐</h1>") | |
| gr.HTML("<p style='text-align: center;'>Upload a picture of a dog, and the model will predict its breed, provide detailed information, and include an extra information link!</p>") | |
| with gr.Row(): | |
| input_image = gr.Image(label="Upload a dog image", type="pil") | |
| output_image = gr.Image(label="Annotated Image") | |
| output = gr.HTML(label="Prediction Results") | |
| breed_buttons = gr.Radio(choices=[], label="More Information", visible=False) | |
| back_button = gr.Button("Back", visible=False) | |
| initial_state = gr.State() | |
| input_image.change( | |
| predict, | |
| inputs=input_image, | |
| outputs=[output, output_image, breed_buttons, initial_state] | |
| ) | |
| breed_buttons.change( | |
| show_details_html, | |
| inputs=[breed_buttons, output, initial_state], | |
| outputs=[output, back_button, initial_state] | |
| ) | |
| back_button.click( | |
| go_back, | |
| inputs=[initial_state], | |
| outputs=[output, output_image, breed_buttons, back_button, initial_state] | |
| ) | |
| gr.Examples( | |
| examples=['Border_Collie.jpg', 'Golden_Retriever.jpeg', 'Saint_Bernard.jpeg', 'French_Bulldog.jpeg', 'Samoyed.jpg'], | |
| inputs=input_image | |
| ) | |
| gr.HTML('For more details on this project and other work, feel free to visit my GitHub <a href="https://github.com/Eric-Chung-0511/Learning-Record/tree/main/Data%20Science%20Projects/Dog_Breed_Classifier">Dog Breed Classifier</a>') | |
| if __name__ == "__main__": | |
| iface.launch() |