SarowarSaurav commited on
Commit
7b15e8e
1 Parent(s): 40b061b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -9
app.py CHANGED
@@ -1,21 +1,29 @@
1
  import gradio as gr
2
- from transformers import pipeline
 
3
  from PIL import Image
4
 
5
- # Load a pre-trained model suitable for general plant disease classification
6
- # Replace with an appropriate model from Hugging Face's hub trained on PlantVillage or similar
7
- model = pipeline("image-classification", model="PlantVillage/plant-disease-model") # Replace with actual general plant disease model name if available
8
 
 
9
  def classify_leaf_disease(image):
 
 
 
10
  # Run the model on the image
11
- results = model(image)
 
12
 
13
- # Get the top prediction
14
- disease_name = results[0]['label']
15
- confidence_score = results[0]['score']
 
 
16
 
17
  # Format the output
18
- return disease_name, f"{confidence_score:.2f}", image
19
 
20
  # Create Gradio Interface
21
  interface = gr.Interface(
 
1
  import gradio as gr
2
+ from transformers import AutoImageProcessor, AutoModelForImageClassification
3
+ import torch
4
  from PIL import Image
5
 
6
+ # Load the image processor and model
7
+ processor = AutoImageProcessor.from_pretrained("ozair23/swin-tiny-patch4-window7-224-finetuned-plantdisease")
8
+ model = AutoModelForImageClassification.from_pretrained("ozair23/swin-tiny-patch4-window7-224-finetuned-plantdisease")
9
 
10
+ # Define the function to process the image and make predictions
11
  def classify_leaf_disease(image):
12
+ # Preprocess the image
13
+ inputs = processor(images=image, return_tensors="pt")
14
+
15
  # Run the model on the image
16
+ with torch.no_grad():
17
+ outputs = model(**inputs)
18
 
19
+ # Get the predicted label and confidence score
20
+ probs = torch.softmax(outputs.logits, dim=1)
21
+ predicted_class_idx = probs.argmax(dim=1).item()
22
+ predicted_label = model.config.id2label[predicted_class_idx]
23
+ confidence_score = probs[0][predicted_class_idx].item()
24
 
25
  # Format the output
26
+ return predicted_label, f"{confidence_score:.2f}", image
27
 
28
  # Create Gradio Interface
29
  interface = gr.Interface(