File size: 1,465 Bytes
f50cb4b
 
 
 
b0d4c90
 
c8cf505
 
b0d4c90
 
 
 
 
fdc20c3
f50cb4b
 
 
 
b0d4c90
 
 
 
7a0a525
b0d4c90
f50cb4b
5359d94
f50cb4b
 
 
 
 
 
 
 
5359d94
f50cb4b
 
 
5359d94
 
f50cb4b
 
 
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
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()