Spaces:
Sleeping
Sleeping
import gradio as gr | |
import torch | |
from PIL import Image | |
from torchvision import transforms | |
from transformers import ViTForImageClassification | |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
model = ViTForImageClassification.from_pretrained('umutbozdag/plant-identity', num_labels=10, ignore_mismatched_sizes=True) | |
model.load_state_dict(torch.load('model.pth', map_location=device)) | |
model.to(device) | |
model.eval() | |
# Define the prediction function | |
def predict_image(img): | |
transform = transforms.Compose([ | |
transforms.Resize((224, 224)), | |
transforms.ToTensor(), | |
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) | |
]) | |
img_t = transform(img).unsqueeze(0).to(device) | |
with torch.no_grad(): | |
outputs = model(img_t).logits | |
_, predicted = torch.max(outputs, 1) | |
class_names = ["Aloe Vera", "Areca Palm", "Boston Fern", "Chinese evergreen", "Dracaena", "Money Tree", "Peace lily", "Rubber Plant", "Snake Plant", "ZZ Plant"] | |
return class_names[predicted.item()] | |
# Create a Gradio interface | |
interface = gr.Interface(fn=predict_image, inputs=gr.Image(type="pil"), outputs="text") | |
interface.launch(share = True) |