prithivMLmods commited on
Commit
f5aceff
·
verified ·
1 Parent(s): 01c5bc1

Create rice_leaf_disease.py

Browse files
Files changed (1) hide show
  1. rice_leaf_disease.py +46 -0
rice_leaf_disease.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import spaces
3
+ from transformers import AutoImageProcessor, SiglipForImageClassification
4
+ from transformers.image_utils import load_image
5
+ from PIL import Image
6
+ import torch
7
+
8
+ # Load model and processor
9
+ model_name = "prithivMLmods/Rice-Leaf-Disease"
10
+ model = SiglipForImageClassification.from_pretrained(model_name)
11
+ processor = AutoImageProcessor.from_pretrained(model_name)
12
+
13
+ @spaces.GPU
14
+ def classify_leaf_disease(image):
15
+ """Predicts the disease type in a rice leaf image."""
16
+ image = Image.fromarray(image).convert("RGB")
17
+ inputs = processor(images=image, return_tensors="pt")
18
+
19
+ with torch.no_grad():
20
+ outputs = model(**inputs)
21
+ logits = outputs.logits
22
+ probs = torch.nn.functional.softmax(logits, dim=1).squeeze().tolist()
23
+
24
+ labels = {
25
+ "0": "Bacterial Blight",
26
+ "1": "Blast",
27
+ "2": "Brown Spot",
28
+ "3": "Healthy",
29
+ "4": "Tungro"
30
+ }
31
+ predictions = {labels[str(i)]: round(probs[i], 3) for i in range(len(probs))}
32
+
33
+ return predictions
34
+
35
+ # Create Gradio interface
36
+ iface = gr.Interface(
37
+ fn=classify_leaf_disease,
38
+ inputs=gr.Image(type="numpy"),
39
+ outputs=gr.Label(label="Prediction Scores"),
40
+ title="Rice Leaf Disease Classification 🌾",
41
+ description="Upload an image of a rice leaf to identify if it is healthy or affected by diseases like Bacterial Blight, Blast, Brown Spot, or Tungro."
42
+ )
43
+
44
+ # Launch the app
45
+ if __name__ == "__main__":
46
+ iface.launch()