isana25 commited on
Commit
2ee4dd1
·
verified ·
1 Parent(s): 55efa12

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -32
app.py CHANGED
@@ -1,39 +1,34 @@
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()
 
1
+ from ultralytics import YOLO
 
 
2
  import gradio as gr
3
+ from PIL import Image
4
+ import torch
5
 
6
+ # Load YOLOv5 model
7
+ model = YOLO("best.pt") # assumes model is in the same directory
 
8
 
9
+ # Prediction function
10
+ def predict(image):
11
+ results = model(image)
12
+ boxes = results[0].boxes
13
+ names = model.names
14
+ output = ""
 
15
 
16
+ if len(boxes) == 0:
17
+ output = "✅ Healthy: No disease detected!"
18
+ else:
19
+ for box in boxes:
20
+ class_id = int(box.cls[0])
21
+ class_name = names[class_id]
22
+ confidence = float(box.conf[0])
23
+ output += f"🚨 Detected: {class_name} ({confidence*100:.2f}%)\n"
24
 
25
+ return output
 
 
 
 
 
 
 
26
 
27
+ # Gradio UI
28
+ gr.Interface(
29
+ fn=predict,
30
+ inputs=gr.Image(type="pil"),
31
+ outputs="text",
32
+ title="🌿 AgroScan: Plant Disease Detector",
33
+ description="Upload a plant leaf image to detect disease using a YOLOv5 model.",
34
  ).launch()