anindya-hf-2002 commited on
Commit
735a818
·
verified ·
1 Parent(s): 8836d7e

upload 8 files

Browse files
Scaler_crop.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:421c25c8a307331cd9843155b0aea25cac383492085e69ef32cb0487a7b35452
3
+ size 1143
Scaler_fertilizer.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:11df3de14dfd37178a51213783f0011457e566834292a71dcc60dd86386a7da7
3
+ size 746
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import joblib
3
+ import numpy as np
4
+
5
+ # Load the trained models
6
+ with open('rf_crop.pkl', 'rb') as file:
7
+ crop_model = joblib.load(file)
8
+
9
+ with open('knn_fertilizer.pkl', 'rb') as file:
10
+ fertilizer_model = joblib.load(file)
11
+
12
+ with open('Scaler_fertilizer.pkl', 'rb') as file: # Assuming you saved the scaler during model training
13
+ scaler = joblib.load(file)
14
+
15
+ # Label Encoders for the models
16
+ crop_label_encoder = {
17
+ 0: "Sugarcane", 1: "Wheat", 2: "Cotton", 3: "Jowar", 4: "Rice",
18
+ 5: "Maize", 6: "Groundnut", 7: "Grapes", 8: "Tur", 9: "Ginger",
19
+ 10: "Turmeric", 11: "Urad", 12: "Gram", 13: "Moong", 14: "Soybean", 15: "Masoor"
20
+ }
21
+
22
+ fertilizer_label_encoder = {
23
+ 0: "Urea", 1: "DAP", 2: "MOP", 3: "SSP", 4: "19:19:19 NPK",
24
+ 5: "Chilated Micronutrient", 6: "50:26:26 NPK", 7: "Magnesium Sulphate",
25
+ 8: "10:26:26 NPK", 9: "Ferrous Sulphate", 10: "13:32:26 NPK",
26
+ 11: "10:10:10 NPK", 12: "Ammonium Sulphate", 13: "12:32:16 NPK",
27
+ 14: "White Potash", 15: "Hydrated Lime", 16: "20:20:20 NPK",
28
+ 17: "18:46:00 NPK", 18: "Sulphur"
29
+ }
30
+
31
+ # Prediction functions
32
+ def predict_crop(Nitrogen, Phosphorus, Potassium, pH, Rainfall, Temperature):
33
+ # Prepare the input data and scale it
34
+ crop_input = np.array([[Nitrogen, Phosphorus, Potassium, pH, Rainfall, Temperature]])
35
+ crop_input_scaled = scaler.transform(crop_input)
36
+
37
+ # Predict the crop
38
+ crop_prediction = crop_model.predict(crop_input_scaled)
39
+ return crop_prediction[0]
40
+
41
+ def predict_fertilizer(Nitrogen, Phosphorus, Potassium, pH, Rainfall, Temperature, Crop):
42
+ # Prepare the input data and scale it
43
+ crop_index = list(crop_label_encoder.keys())[list(crop_label_encoder.values()).index(Crop)]
44
+ fertilizer_input = np.array([[Nitrogen, Phosphorus, Potassium, pH, Rainfall, Temperature, crop_index]])
45
+ fertilizer_input_scaled = scaler.transform(fertilizer_input[:, :-1])
46
+
47
+ # Add crop index back to the scaled input
48
+ fertilizer_input_scaled = np.hstack([fertilizer_input_scaled, [[crop_index]]])
49
+
50
+ # Predict the fertilizer
51
+ fertilizer_prediction = fertilizer_model.predict(fertilizer_input_scaled)
52
+
53
+ return fertilizer_label_encoder[int(fertilizer_prediction[0])]
54
+
55
+ # Gradio Interface for Crop Prediction
56
+ crop_interface = gr.Interface(
57
+ fn=predict_crop,
58
+ inputs=[
59
+ gr.Number(label="Nitrogen"),
60
+ gr.Number(label="Phosphorus"),
61
+ gr.Number(label="Potassium"),
62
+ gr.Number(label="pH"),
63
+ gr.Number(label="Rainfall"),
64
+ gr.Number(label="Temperature")
65
+ ],
66
+ outputs=gr.Label(num_top_classes=1),
67
+ title="Crop Prediction",
68
+ allow_flagging='never'
69
+ )
70
+
71
+ # Gradio Interface for Fertilizer Prediction
72
+ fertilizer_interface = gr.Interface(
73
+ fn=predict_fertilizer,
74
+ inputs=[
75
+ gr.Number(label="Nitrogen"),
76
+ gr.Number(label="Phosphorus"),
77
+ gr.Number(label="Potassium"),
78
+ gr.Number(label="pH"),
79
+ gr.Number(label="Rainfall"),
80
+ gr.Number(label="Temperature"),
81
+ gr.Dropdown(label="Crop", choices=list(crop_label_encoder.values()))
82
+ ],
83
+ outputs=gr.Label(num_top_classes=1),
84
+ title="Fertilizer Prediction",
85
+ allow_flagging='never'
86
+ )
87
+
88
+ # Create a Tabbed Interface in Gradio
89
+ app = gr.TabbedInterface([crop_interface, fertilizer_interface], ["Crop Prediction", "Fertilizer Prediction"])
90
+
91
+ # Launch the app
92
+ app.launch()
knn_fertilizer.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bb9c518848083cc38347f02e7be0b665749445ff99e4504cb7e033c2b4fbc902
3
+ size 924918
knn_fertilizer.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:90e2189b4191bfeea2584aec716f00cc1640478fe9ac0f7f414594cf0bad0e8d
3
+ size 536649
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio
2
+ scikit-learn==1.3.0
3
+ numpy
4
+ joblib
5
+ pickle
rf_crop.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9185c92b9252cd95d0344849adc49aacfc8ede4ebab519876cf535b85821a458
3
+ size 17618617
rf_crop.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ea83bb405aab3c292238bda4b7a5def1bf56750e47bdecec88e0902a92fc70f8
3
+ size 17611231