Spaces:
Runtime error
Runtime error
File size: 1,350 Bytes
4c200b6 |
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
import spaces
import gradio as gr
from transformers import AutoImageProcessor, AutoModelForImageClassification
import torch
from PIL import Image
# Load the fine-tuned model
model = AutoModelForImageClassification.from_pretrained("Pavarissy/ConvNextV2-large-DogBreed")
# Initialize the image processor
preprocessor = AutoImageProcessor.from_pretrained("Pavarissy/ConvNextV2-large-DogBreed")
def classify_image(image):
# Preprocess the image
inputs = preprocessor(images=image, return_tensors="pt")
# Model prediction
with torch.no_grad():
logits = model(**inputs).logits
# Convert logits to probabilities
probs = logits.softmax(dim=-1)
# Extract top 5 predictions
top_5_probs, top_5_labels = torch.topk(probs, 5)
top_5_probs = top_5_probs.squeeze().tolist()
top_5_labels = top_5_labels.squeeze().tolist()
# Map labels to their names
labels = model.config.id2label
predicted_labels = [labels[label] for label in top_5_labels]
return dict(zip(predicted_labels, top_5_probs))
# Create a Gradio interface
iface = gr.Interface(
fn=classify_image,
inputs=gr.Image(type="pil"),
outputs=gr.Label(num_top_classes=5),
title="Dog Breed Classifier",
description="Upload an image of a dog, and the model will predict the breed."
)
# Launch the interface
iface.launch()
|