Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,60 +1,65 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
from PIL import Image
|
4 |
|
5 |
-
#
|
6 |
-
|
|
|
7 |
plant_disease_classifier = pipeline(
|
|
|
|
|
|
|
|
|
|
|
8 |
task="image-classification",
|
9 |
-
model="
|
10 |
top_k=3
|
11 |
)
|
12 |
|
13 |
-
|
14 |
-
def diagnose_plant_health(image: Image.Image):
|
15 |
"""
|
16 |
-
Takes a PIL Image of a
|
17 |
-
- Top
|
18 |
-
-
|
19 |
-
-
|
20 |
"""
|
21 |
-
|
22 |
-
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
if "healthy" in top_label:
|
34 |
-
advice = "Your plant looks healthy! Maintain regular watering and adequate sunlight."
|
35 |
else:
|
36 |
-
|
37 |
-
f"Detected symptom: {results[0]['label']}. "
|
38 |
-
"Consider the following care steps:\n"
|
39 |
-
"1. Isolate the plant to prevent spread.\n"
|
40 |
-
"2. Prune affected areas with sterilized tools.\n"
|
41 |
-
"3. Apply an appropriate fungicide or treatment."
|
42 |
-
)
|
43 |
|
44 |
-
return "\n".join(
|
45 |
|
46 |
-
#
|
47 |
iface = gr.Interface(
|
48 |
-
fn=
|
49 |
-
inputs=gr.Image(type="pil", label="Upload
|
50 |
outputs=[
|
51 |
-
gr.Textbox(label="
|
|
|
52 |
gr.Textbox(label="Care Advice")
|
53 |
],
|
54 |
-
title="
|
55 |
description=(
|
56 |
-
"Upload a photo of your
|
57 |
-
"
|
58 |
),
|
59 |
examples=[
|
60 |
["Plants/Unhealthy_crop_1.jpg"],
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
from transformers import pipeline
|
4 |
from PIL import Image
|
5 |
|
6 |
+
# Retrieve Hugging Face token from Secrets
|
7 |
+
hf_token = os.getenv("HF_TOKEN")
|
8 |
+
|
9 |
plant_disease_classifier = pipeline(
|
10 |
+
task="image-classification",
|
11 |
+
model="linkanjarad/mobilenet_v2_1.0_224-plant-disease-identification",
|
12 |
+
top_k=3
|
13 |
+
)
|
14 |
+
nutrient_deficiency_classifier = pipeline(
|
15 |
task="image-classification",
|
16 |
+
model="yanyu/efficientformer_l1",
|
17 |
top_k=3
|
18 |
)
|
19 |
|
20 |
+
def diagnose_crop_health(image: Image.Image):
|
|
|
21 |
"""
|
22 |
+
Takes a PIL Image of a crop leaf and returns:
|
23 |
+
- Top 3 disease predictions
|
24 |
+
- Top 3 nutrient deficiency predictions
|
25 |
+
- Combined care advice based on both
|
26 |
"""
|
27 |
+
disease_results = plant_disease_classifier(image)
|
28 |
+
nutrient_results = nutrient_deficiency_classifier(image)
|
29 |
|
30 |
+
disease_preds = [f"{res['label']} ({res['score']*100:.1f}%)" for res in disease_results]
|
31 |
+
nutrient_preds = [f"{res['label']} ({res['score']*100:.1f}%)" for res in nutrient_results]
|
32 |
+
|
33 |
+
# Generate advice based on predictions
|
34 |
+
advices = []
|
35 |
+
top_disease = disease_results[0]['label'].lower()
|
36 |
+
top_nutrient = nutrient_results[0]['label'].lower()
|
37 |
+
|
38 |
+
if "healthy" in top_disease:
|
39 |
+
advices.append("No disease detected—maintain standard crop care.")
|
40 |
+
else:
|
41 |
+
advices.append(f"Disease detected: {disease_results[0]['label']}. Isolate and apply targeted treatment.")
|
42 |
|
43 |
+
if "healthy" in top_nutrient:
|
44 |
+
advices.append("No nutrient deficiency detected—continue regular fertilization.")
|
|
|
|
|
45 |
else:
|
46 |
+
advices.append(f"Nutrient issue: {nutrient_results[0]['label']}. Amend soil based on deficiency (e.g., add N, P, or K).")
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
+
return "\n".join(disease_preds), "\n".join(nutrient_preds), "\n".join(advices)
|
49 |
|
50 |
+
# Build the Gradio interface
|
51 |
iface = gr.Interface(
|
52 |
+
fn=diagnose_crop_health,
|
53 |
+
inputs=gr.Image(type="pil", label="Upload Crop Leaf Image"),
|
54 |
outputs=[
|
55 |
+
gr.Textbox(label="Disease Predictions (Top 3)"),
|
56 |
+
gr.Textbox(label="Nutrient Predictions (Top 3)"),
|
57 |
gr.Textbox(label="Care Advice")
|
58 |
],
|
59 |
+
title="Crop Health Monitor",
|
60 |
description=(
|
61 |
+
"Upload a photo of your crop's leaf to diagnose diseases and nutrient deficiencies, "
|
62 |
+
"and receive combined care recommendations using two complementary models."
|
63 |
),
|
64 |
examples=[
|
65 |
["Plants/Unhealthy_crop_1.jpg"],
|