Acres-CR / app.py
JohanBeytell's picture
Update app.py
bdf0fae verified
raw
history blame
1.59 kB
import gradio as gr
import numpy as np
from sklearn.preprocessing import LabelEncoder
from xgboost import XGBClassifier
import pickle
# Load the trained model and label encoder
model = pickle.load(open('crop_recommendation_model.pkl', 'rb'))
le = pickle.load(open('label_encoder.pkl', 'rb'))
def recommend_crop(nitrogen, phosphorus, potassium, temperature, humidity, ph, rainfall):
# Prepare the input sample as a 2D array
X_sample = np.array([[nitrogen, phosphorus, potassium, temperature, humidity, ph, rainfall]])
# Predict crop recommendations
y_pred_sample = model.predict(X_sample)
# Decode the prediction back to crop name
crops_pred = le.inverse_transform(y_pred_sample)
return crops_pred[0] # Return the predicted crop name
# Create the Gradio interface
interface = gr.Interface(
fn=recommend_crop,
inputs=[
gr.Number(label="Nitrogen - Ratio of Nitrogen in the soil"),
gr.Number(label="Phosphorus - Ratio of Phosphorus in the soil"),
gr.Number(label="Potassium - Ratio of Potassium in the soil"),
gr.Number(label="Temperature - In degrees Celsius"),
gr.Number(label="Humidity - Relative humidity in %"),
gr.Number(label="pH Value - pH value of the soil"),
gr.Number(label="Rainfall - Rainfall in mm")
],
outputs=gr.Textbox(label="Recommended Crop"),
title="Acres - CR",
description="Acres Crop Recommendation recommends the best crop to plant based on soil and climate conditions."
)
# Launch the app
if __name__ == "__main__":
interface.launch()