anindya-hf-2002 commited on
Commit
8e6a432
·
verified ·
1 Parent(s): e32d7c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -92
app.py CHANGED
@@ -1,92 +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()
 
1
+ import gradio as gr
2
+ import pickle
3
+ import numpy as np
4
+
5
+ # Load the trained models
6
+ with open('rf_crop.pkl', 'rb') as file:
7
+ crop_model = pickle.load(file)
8
+
9
+ with open('knn_fertilizer.pkl', 'rb') as file:
10
+ fertilizer_model = pickle.load(file)
11
+
12
+ with open('Scaler_fertilizer.pkl', 'rb') as file: # Assuming you saved the scaler during model training
13
+ scaler = pickle.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()