Spaces:
Sleeping
Sleeping
File size: 1,637 Bytes
229359b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | 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() |