CZerion commited on
Commit
f826c98
·
verified ·
1 Parent(s): f3807c7

Create Inference.py

Browse files
Files changed (1) hide show
  1. Inference.py +76 -0
Inference.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ from PIL import Image
4
+
5
+ # Load and return a dictionary of pipelines for reuse in other applications or scripts
6
+ def load_pipeline_models():
7
+ """
8
+ Returns a dict of pretrained pipelines:
9
+ - 'disease': crop disease classification pipeline
10
+ - 'nutrient': nutrient deficiency classification pipeline
11
+ """
12
+ disease_pipe = pipeline(
13
+ task="image-classification",
14
+ model="linkanjarad/mobilenet_v2_1.0_224-plant-disease-identification",
15
+ top_k=3
16
+ )
17
+ nutrient_pipe = pipeline(
18
+ task="image-classification",
19
+ model="nateraw/vit-base-beans",
20
+ top_k=3
21
+ )
22
+ return {"disease": disease_pipe, "nutrient": nutrient_pipe}
23
+
24
+
25
+ def diagnose(image: Image.Image, models=None):
26
+ """
27
+ Runs inference on a PIL Image using both pipelines.
28
+
29
+ Args:
30
+ image (PIL.Image): Input image of a crop leaf.
31
+ models (dict): Optional dict from load_pipeline_models(); if None, will be loaded.
32
+
33
+ Returns:
34
+ dict: {
35
+ 'disease': [ {'label': str, 'score': float}, ... ],
36
+ 'nutrient': [ {'label': str, 'score': float}, ... ],
37
+ 'advice': list(str)
38
+ }
39
+ """
40
+ if models is None:
41
+ models = load_pipeline_models()
42
+
43
+ disease_results = models["disease"](image)
44
+ nutrient_results = models["nutrient"](image)
45
+
46
+ # Combine predictions and generate advice
47
+ advice = []
48
+ top_disease = disease_results[0]['label'].lower()
49
+ top_nutrient = nutrient_results[0]['label'].lower()
50
+
51
+ if "healthy" in top_disease:
52
+ advice.append("No disease detected—maintain standard crop care.")
53
+ else:
54
+ advice.append(f"Disease detected: {disease_results[0]['label']}. Isolate and apply targeted treatment.")
55
+
56
+ if "healthy" in top_nutrient:
57
+ advice.append("No nutrient deficiency detected—continue regular fertilization.")
58
+ else:
59
+ advice.append(f"Nutrient issue: {nutrient_results[0]['label']}. Amend soil based on deficiency (e.g., add N, P, or K).")
60
+
61
+ return {
62
+ 'disease': [ {'label': r['label'], 'score': r['score']} for r in disease_results ],
63
+ 'nutrient': [ {'label': r['label'], 'score': r['score']} for r in nutrient_results ],
64
+ 'advice': advice
65
+ }
66
+
67
+ # Example usage
68
+ if __name__ == '__main__':
69
+ from PIL import Image
70
+ # Load sample image
71
+ img = Image.open('Plants/Unhealthy_crop_1.jpg')
72
+ models = load_pipeline_models()
73
+ result = diagnose(img, models=models)
74
+ print("Disease Predictions:", result['disease'])
75
+ print("Nutrient Predictions:", result['nutrient'])
76
+ print("Advice:", result['advice'])