Spaces:
Sleeping
Sleeping
SarowarSaurav
commited on
Commit
•
90d35fd
1
Parent(s):
5fbb974
Update app.py
Browse files
app.py
CHANGED
@@ -1,54 +1,32 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
from transformers import ViTFeatureExtractor, ViTForImageClassification
|
4 |
from PIL import Image
|
5 |
-
import requests
|
6 |
|
7 |
-
# Load
|
8 |
-
|
9 |
-
|
10 |
-
feature_extractor = ViTFeatureExtractor.from_pretrained(model_name)
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
1: "Tobacco Mosaic Virus",
|
16 |
-
2: "Brown Spot",
|
17 |
-
3: "Frog Eye Leaf Spot",
|
18 |
-
4: "Other"
|
19 |
-
}
|
20 |
-
|
21 |
-
# Define a function for disease detection
|
22 |
-
def detect_disease(image):
|
23 |
-
# Preprocess the image
|
24 |
-
inputs = feature_extractor(images=image, return_tensors="pt")
|
25 |
-
|
26 |
-
# Run the model
|
27 |
-
with torch.no_grad():
|
28 |
-
outputs = model(**inputs)
|
29 |
-
logits = outputs.logits
|
30 |
-
predicted_class = logits.argmax().item()
|
31 |
|
32 |
-
#
|
33 |
-
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
# Build Gradio Interface
|
38 |
-
title = "Tobacco Leaf Disease Detection"
|
39 |
-
description = """
|
40 |
-
Upload or take a real-time picture of a tobacco leaf, and the app will detect the disease (if any).
|
41 |
-
"""
|
42 |
|
43 |
-
#
|
44 |
-
|
45 |
-
fn=
|
46 |
-
inputs=gr.Image(
|
47 |
-
outputs=
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
51 |
)
|
52 |
|
53 |
-
# Launch
|
54 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline
|
|
|
3 |
from PIL import Image
|
|
|
4 |
|
5 |
+
# Load the Hugging Face image classification pipeline with EfficientNetB0
|
6 |
+
# This model is generic for plant disease, so if you have a specific tobacco disease model, replace it accordingly
|
7 |
+
classifier = pipeline("image-classification", model="nateraw/efficientnet-b0")
|
|
|
8 |
|
9 |
+
def identify_disease(image):
|
10 |
+
# Use the classifier to predict the disease
|
11 |
+
predictions = classifier(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
# Format the output to include disease name and confidence score
|
14 |
+
results = [{"Disease": pred["label"], "Confidence": f"{pred['score'] * 100:.2f}%"} for pred in predictions]
|
15 |
|
16 |
+
# Return the uploaded image along with the results
|
17 |
+
return image, results
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
# Define Gradio interface
|
20 |
+
interface = gr.Interface(
|
21 |
+
fn=identify_disease,
|
22 |
+
inputs=gr.inputs.Image(type="pil"),
|
23 |
+
outputs=[
|
24 |
+
gr.outputs.Image(type="pil", label="Uploaded Image"),
|
25 |
+
gr.outputs.Dataframe(type="pandas", label="Predictions")
|
26 |
+
],
|
27 |
+
title="Tobacco Plant Disease Identifier",
|
28 |
+
description="Upload an image of a tobacco plant, and this tool will identify the disease along with the confidence score."
|
29 |
)
|
30 |
|
31 |
+
# Launch the app
|
32 |
+
interface.launch()
|