isana25 commited on
Commit
e80e59f
·
verified ·
1 Parent(s): 9d7dde8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision.transforms as transforms
3
+ from PIL import Image
4
+ import gradio as gr
5
+
6
+ # Load model
7
+ model = torch.load("best.pt", map_location=torch.device('cpu'))
8
+ model.eval()
9
+
10
+ # Define class names (update these based on model's training)
11
+ classes = [
12
+ "Apple___Apple_scab", "Apple___Black_rot", "Apple___Cedar_apple_rust",
13
+ "Apple___healthy", "Blueberry___healthy", "Cherry_(including_sour)___Powdery_mildew",
14
+ "Cherry_(including_sour)___healthy", "Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot",
15
+ # ...continue all 39 classes...
16
+ ]
17
+
18
+ # Image transforms
19
+ transform = transforms.Compose([
20
+ transforms.Resize((224, 224)),
21
+ transforms.ToTensor(),
22
+ ])
23
+
24
+ # Prediction function
25
+ def predict(img):
26
+ img = transform(img).unsqueeze(0)
27
+ with torch.no_grad():
28
+ outputs = model(img)
29
+ _, predicted = torch.max(outputs, 1)
30
+ label = classes[predicted.item()]
31
+ return f"Prediction: {label}"
32
+
33
+ # Gradio Interface
34
+ gr.Interface(fn=predict,
35
+ inputs=gr.Image(type="pil"),
36
+ outputs="text",
37
+ title="🌿 AgroScan: Plant Disease Detector",
38
+ description="Upload a plant leaf image to detect disease."
39
+ ).launch()