Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,25 +4,39 @@ from sklearn.preprocessing import LabelEncoder
|
|
4 |
from xgboost import XGBClassifier
|
5 |
import pickle
|
6 |
|
7 |
-
model
|
8 |
-
|
|
|
9 |
|
10 |
def recommend_crop(nitrogen, phosphorus, potassium, temperature, humidity, ph, rainfall):
|
11 |
-
|
|
|
12 |
|
13 |
# Predict crop recommendations
|
14 |
y_pred_sample = model.predict(X_sample)
|
15 |
|
16 |
-
# Decode the
|
17 |
crops_pred = le.inverse_transform(y_pred_sample)
|
18 |
|
19 |
-
return crops_pred
|
20 |
|
21 |
# Create the Gradio interface
|
22 |
interface = gr.Interface(
|
23 |
-
fn=
|
24 |
-
inputs=[
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
from xgboost import XGBClassifier
|
5 |
import pickle
|
6 |
|
7 |
+
# Load the trained model and label encoder
|
8 |
+
model = pickle.load(open('crop_recommendation_model.pkl', 'rb'))
|
9 |
+
le = pickle.load(open('label_encoder.pkl', 'rb'))
|
10 |
|
11 |
def recommend_crop(nitrogen, phosphorus, potassium, temperature, humidity, ph, rainfall):
|
12 |
+
# Prepare the input sample as a 2D array
|
13 |
+
X_sample = np.array([[nitrogen, phosphorus, potassium, temperature, humidity, ph, rainfall]])
|
14 |
|
15 |
# Predict crop recommendations
|
16 |
y_pred_sample = model.predict(X_sample)
|
17 |
|
18 |
+
# Decode the prediction back to crop name
|
19 |
crops_pred = le.inverse_transform(y_pred_sample)
|
20 |
|
21 |
+
return crops_pred[0] # Return the predicted crop name
|
22 |
|
23 |
# Create the Gradio interface
|
24 |
interface = gr.Interface(
|
25 |
+
fn=recommend_crop,
|
26 |
+
inputs=[
|
27 |
+
gr.Number(label="Nitrogen - Ratio of Nitrogen in the soil"),
|
28 |
+
gr.Number(label="Phosphorus - Ratio of Phosphorus in the soil"),
|
29 |
+
gr.Number(label="Potassium - Ratio of Potassium in the soil"),
|
30 |
+
gr.Number(label="Temperature - In degrees Celsius"),
|
31 |
+
gr.Number(label="Humidity - Relative humidity in %"),
|
32 |
+
gr.Number(label="pH Value - pH value of the soil"),
|
33 |
+
gr.Number(label="Rainfall - Rainfall in mm")
|
34 |
+
],
|
35 |
+
outputs=gr.Textbox(label="Recommended Crop"),
|
36 |
+
title="Acres - CR",
|
37 |
+
description="Acres Crop Recommendation recommends the best crop to plant based on soil and climate conditions."
|
38 |
+
)
|
39 |
+
|
40 |
+
# Launch the app
|
41 |
+
if __name__ == "__main__":
|
42 |
+
interface.launch()
|