File size: 1,154 Bytes
6e488f9
 
 
 
 
7c46844
 
 
6e488f9
7c46844
 
 
6e488f9
7c46844
 
 
 
 
6e488f9
7c46844
 
 
6e488f9
 
7c46844
 
6e488f9
 
7c46844
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
from transformers import AutoImageProcessor, AutoModelForImageClassification
from PIL import Image
import torch
import gradio as gr

# Load the image processor and model from Hugging Face
processor = AutoImageProcessor.from_pretrained("wesleyacheng/dog-breeds-multiclass-image-classification-with-vit")
breed_model = AutoModelForImageClassification.from_pretrained("wesleyacheng/dog-breeds-multiclass-image-classification-with-vit")

# This function takes an uploaded image and returns the predicted dog breed
def detect_breed(img):
    inputs = processor(images=img, return_tensors="pt")
    with torch.no_grad():
        result = breed_model(**inputs)
    predictions = result.logits
    top_prediction = predictions.argmax(dim=1).item()
    breed_name = breed_model.config.id2label[top_prediction]
    return f"This looks like a {breed_name}!"

# Set up the Gradio web interface
app = gr.Interface(
    fn=detect_breed,
    inputs=gr.Image(type="pil"),
    outputs="text",
    title="Dog Breed Identifier 🐶",
    description="Upload a photo of a dog and find out what breed it is! The model can recognize 120 different dog breeds."
)

app.launch()