CZerion commited on
Commit
176b45c
·
verified ·
1 Parent(s): e656379

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -36
app.py CHANGED
@@ -1,60 +1,65 @@
 
1
  import gradio as gr
2
  from transformers import pipeline
3
  from PIL import Image
4
 
5
- # Initialize the plant disease classification pipeline
6
- # You can replace the model with any fine-tuned plant disease model hosted on Hugging Face
 
7
  plant_disease_classifier = pipeline(
 
 
 
 
 
8
  task="image-classification",
9
- model="wambugu71/crop_leaf_diseases_vit",
10
  top_k=3
11
  )
12
 
13
-
14
- def diagnose_plant_health(image: Image.Image):
15
  """
16
- Takes a PIL Image of a plant leaf and returns:
17
- - Top predicted disease label
18
- - Confidence score
19
- - Care advice based on the label
20
  """
21
- # Run the image through the classification pipeline
22
- results = plant_disease_classifier(image)
23
 
24
- # Format top-3 predictions
25
- predictions = []
26
- for res in results:
27
- label = res['label']
28
- score = res['score']
29
- predictions.append(f"{label} ({score*100:.1f}%)")
 
 
 
 
 
 
30
 
31
- # Determine advice based on the top prediction
32
- top_label = results[0]['label'].lower()
33
- if "healthy" in top_label:
34
- advice = "Your plant looks healthy! Maintain regular watering and adequate sunlight."
35
  else:
36
- advice = (
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(predictions), advice
45
 
46
- # Building the Gradio interface
47
  iface = gr.Interface(
48
- fn=diagnose_plant_health,
49
- inputs=gr.Image(type="pil", label="Upload Plant Leaf Image"),
50
  outputs=[
51
- gr.Textbox(label="Predicted Diseases (Top 3)"),
 
52
  gr.Textbox(label="Care Advice")
53
  ],
54
- title="Home Plant Health Monitor",
55
  description=(
56
- "Upload a photo of your plant's leaf to diagnose diseases and receive care recommendations. "
57
- "This app uses a fine-tuned image-classification model on common plant diseases."
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"],