Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import torch
|
3 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
4 |
+
from PIL import Image
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
8 |
+
|
9 |
+
def predict(image):
|
10 |
+
processor = AutoImageProcessor.from_pretrained("linkanjarad/mobilenet_v2_1.0_224-plant-disease-identification")
|
11 |
+
model = AutoModelForImageClassification.from_pretrained("linkanjarad/mobilenet_v2_1.0_224-plant-disease-identification").to(device)
|
12 |
+
model.eval()
|
13 |
+
|
14 |
+
inputs = processor(images=image, return_tensors="pt").to(device)
|
15 |
+
with torch.no_grad():
|
16 |
+
logits = model(**inputs).logits
|
17 |
+
predicted_class = logits.argmax(-1).item()
|
18 |
+
class_name = model.config.id2label.get(predicted_class, "Unknown")
|
19 |
+
return class_name
|
20 |
+
|
21 |
+
iface = gr.Interface(
|
22 |
+
fn=predict,
|
23 |
+
inputs=gr.Image(type="pil"),
|
24 |
+
outputs=gr.Label(),
|
25 |
+
title="Plant Disease Classifier",
|
26 |
+
description="Upload a leaf image to predict plant disease."
|
27 |
+
)
|
28 |
+
|
29 |
+
iface.launch()
|