File size: 1,202 Bytes
94e8a13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)