Spaces:
Sleeping
Sleeping
| 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 | |
| # Example inputs for the Gradio app | |
| examples = [ | |
| [90, 42, 43, 20, 82, 6.5, 202], | |
| [80, 30, 40, 25, 60, 6.8, 150], | |
| [95, 55, 50, 28, 70, 7.0, 120] | |
| ] | |
| # 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"), | |
| examples=examples, | |
| 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() |