Spaces:
Sleeping
Sleeping
import lightgbm as lgb | |
import gradio as gr | |
import numpy as np | |
# List of crops in the same order used during training | |
crop_labels = [ | |
"apple","banana", "barley", "blackgram", "chickpea", "coconut", | |
"coffee", "cotton", "grapes","groundnut", "jute", "kidneybeans", | |
"lentil", "maize", "mango", "mothbeans", "mungbean", | |
"muskmelon", "orange", "papaya", "pigeonpeas", "rice" | |
] | |
# Load the LightGBM model | |
model = lgb.Booster(model_file="crop_model_ro.txt") | |
# Prediction function | |
def recommend_crop(n, p, k, temperature, humidity, ph, rainfall): | |
input_data = np.array([[n, p, k, temperature, humidity, ph, rainfall]]) | |
prediction = model.predict(input_data) # Returns list of probabilities | |
predicted_index = np.argmax(prediction) | |
predicted_crop = crop_labels[predicted_index] | |
return f"🌾 Recommended Crop: **{predicted_crop}**" | |
# Gradio Interface | |
interface = gr.Interface( | |
fn=recommend_crop, | |
inputs=[ | |
gr.Number(label="Nitrogen (N)"), | |
gr.Number(label="Phosphorous (P)"), | |
gr.Number(label="Potassium (K)"), | |
gr.Number(label="Temperature (°C)"), | |
gr.Number(label="Humidity (%)"), | |
gr.Number(label="pH Level"), | |
gr.Number(label="Rainfall (mm)") | |
], | |
outputs="text", | |
title="🌱 AgriBot - Smart Crop Recommendation", | |
description="Enter soil and climate details to get the most suitable crop recommendation based on LightGBM ML model." | |
) | |
interface.launch() | |