Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pandas as pd | |
| import pickle | |
| # Load model | |
| with open("random_forest_model.pkl", "rb") as f: | |
| loaded_model = pickle.load(f) | |
| # Load scaler | |
| with open("standard_scaler.pkl", "rb") as f: | |
| loaded_scaler = pickle.load(f) | |
| def predict_crop(N, P, K, temperature, humidity, ph, rainfall): | |
| input_data = pd.DataFrame( | |
| [[N, P, K, temperature, humidity, ph, rainfall]], | |
| columns=[ | |
| "N", | |
| "P", | |
| "K", | |
| "temperature", | |
| "humidity", | |
| "ph", | |
| "rainfall", | |
| ], | |
| ) | |
| scaled_input = loaded_scaler.transform(input_data) | |
| prediction = loaded_model.predict(scaled_input)[0] | |
| return f"🌱 Recommended Crop: {prediction}" | |
| iface = gr.Interface( | |
| fn=predict_crop, | |
| inputs=[ | |
| gr.Number(label="Nitrogen (N)", minimum=0, maximum=140, value=50), | |
| gr.Number(label="Phosphorus (P)", minimum=5, maximum=145, value=50), | |
| gr.Number(label="Potassium (K)", minimum=5, maximum=205, value=50), | |
| gr.Number(label="Temperature (°C)", minimum=8.8, maximum=43.7, value=25), | |
| gr.Number(label="Humidity (%)", minimum=14.2, maximum=100, value=70), | |
| gr.Number(label="pH", minimum=3.5, maximum=9.9, value=6.5), | |
| gr.Number(label="Rainfall (mm)", minimum=20.2, maximum=298.6, value=100), | |
| ], | |
| outputs=gr.Textbox(label="Prediction"), | |
| title="🌾 Crop Recommendation System", | |
| description="Enter soil nutrients and environmental parameters to receive a recommended crop.", | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch() |