CZerion commited on
Commit
b4b4573
·
verified ·
1 Parent(s): 6f80c37

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -63
app.py CHANGED
@@ -1,74 +1,19 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
- from PIL import Image
4
 
5
- # Initialize two classification pipelines with authentication
6
- plant_disease_classifier = pipeline(
7
- task="image-classification",
8
- model="linkanjarad/mobilenet_v2_1.0_224-plant-disease-identification",
9
- top_k=3
10
- )
11
- nutrient_deficiency_classifier = pipeline(
12
- task="image-classification",
13
- model="nateraw/vit-base-beans",
14
- top_k=3
15
- )
16
-
17
- def diagnose_crop_health(image: Image.Image):
18
- """
19
- Takes a PIL Image of a crop leaf and returns:
20
- - Top 3 disease predictions
21
- - Top 3 nutrient deficiency predictions
22
- - Combined care advice based on both
23
- """
24
- disease_results = plant_disease_classifier(image)
25
- nutrient_results = nutrient_deficiency_classifier(image)
26
-
27
- disease_preds = [f"{res['label']} ({res['score']*100:.1f}%)" for res in disease_results]
28
- nutrient_preds = [f"{res['label']} ({res['score']*100:.1f}%)" for res in nutrient_results]
29
-
30
- # Generate advice based on predictions
31
- advices = []
32
- top_disease = disease_results[0]['label'].lower()
33
- top_nutrient = nutrient_results[0]['label'].lower()
34
 
35
- if "healthy" in top_disease:
36
- advices.append("No disease detected—maintain standard crop care.")
37
- else:
38
- advices.append(f"Disease detected: {disease_results[0]['label']}. Isolate and apply targeted treatment.")
39
-
40
- if "healthy" in top_nutrient:
41
- advices.append("No nutrient deficiency detected—continue regular fertilization.")
42
- else:
43
- advices.append(f"Nutrient issue: {nutrient_results[0]['label']}. Amend soil based on deficiency (e.g., add N, P, or K).")
44
-
45
- return "\n".join(disease_preds), "\n".join(nutrient_preds), "\n".join(advices)
46
-
47
- # Build the Gradio interface
48
  iface = gr.Interface(
49
- fn=diagnose_crop_health,
50
- inputs=gr.Image(type="pil", label="Upload Crop Leaf Image"),
51
  outputs=[
52
  gr.Textbox(label="Disease Predictions (Top 3)"),
53
- gr.Textbox(label="Nutrient Predictions (Top 3)"),
54
  gr.Textbox(label="Care Advice")
55
  ],
56
- title="Crop Health Monitor",
57
- description=(
58
- "Upload a photo of your crop's leaf to diagnose diseases and nutrient deficiencies, "
59
- "and receive combined care recommendations using two complementary models."
60
- ),
61
- examples=[
62
- ["Plants/Unhealthy_crop_1.jpg"],
63
- ["Plants/Unhealthy_crop_2.jpg"],
64
- ["Plants/Unhealthy_crop_3.jpg"],
65
- ["Plants/Unhealthy_crop_4.jpg"],
66
- ["Plants/Unhealthy_crop_5.jpg"],
67
- ["Plants/Healthy_crop_1.jpg"],
68
- ["Plants/Healthy_crop_2.jpg"]
69
- ],
70
- allow_flagging="never"
71
  )
72
 
73
  if __name__ == "__main__":
74
- iface.launch(server_name="0.0.0.0", server_port=7860)
 
1
  import gradio as gr
2
+ from crop_disease_monitor.inference.inference import load_disease_pipeline, diagnose
 
3
 
4
+ # load your published model or local checkpoint
5
+ pipe = load_disease_pipeline("CZerion/COMP-019_Assignment-Crop_Health_Monitor")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  iface = gr.Interface(
8
+ fn=lambda img: diagnose(img, pipe),
9
+ inputs=gr.Image(type="pil", label="Upload Leaf Image"),
10
  outputs=[
11
  gr.Textbox(label="Disease Predictions (Top 3)"),
 
12
  gr.Textbox(label="Care Advice")
13
  ],
14
+ title="Plant Disease Monitor",
15
+ description="Upload a crop leaf photo to detect diseases using a fine-tuned model."
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  )
17
 
18
  if __name__ == "__main__":
19
+ iface.launch()