Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,29 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
|
| 5 |
-
# Load
|
| 6 |
-
|
| 7 |
-
model =
|
| 8 |
|
|
|
|
| 9 |
def classify_leaf_disease(image):
|
|
|
|
|
|
|
|
|
|
| 10 |
# Run the model on the image
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
-
# Get the
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# Format the output
|
| 18 |
-
return
|
| 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(
|