Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import re
|
| 5 |
+
|
| 6 |
+
# Load the pre-trained pipeline
|
| 7 |
+
pipe = pipeline("image-classification", model="gianlab/swin-tiny-patch4-window7-224-finetuned-plantdisease")
|
| 8 |
+
|
| 9 |
+
def predict_disease(image):
|
| 10 |
+
"""Predicts the plant disease from an image."""
|
| 11 |
+
predictions = pipe(image)
|
| 12 |
+
|
| 13 |
+
# Extract disease name using regex and get the top prediction
|
| 14 |
+
for pred in predictions:
|
| 15 |
+
label = pred["label"]
|
| 16 |
+
score = pred["score"]
|
| 17 |
+
match = re.search(r"___(.*)", label)
|
| 18 |
+
if match:
|
| 19 |
+
disease_name = match.group(1)
|
| 20 |
+
return {disease_name: score} # Return only the top prediction as a dictionary
|
| 21 |
+
else:
|
| 22 |
+
return {label: score} # If no '___' found, return original label
|
| 23 |
+
|
| 24 |
+
# Create the Gradio interface
|
| 25 |
+
iface = gr.Interface(
|
| 26 |
+
fn=predict_disease,
|
| 27 |
+
inputs=gr.Image(type="pil"),
|
| 28 |
+
outputs="label",
|
| 29 |
+
title="Plant Disease Classifier",
|
| 30 |
+
description="Upload an image of a plant leaf to predict its disease."
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# Launch the interface
|
| 34 |
+
iface.launch()
|