File size: 1,773 Bytes
e4cf7c3
 
 
 
 
 
bdf0fae
 
 
e4cf7c3
ee2253a
bdf0fae
 
e4cf7c3
 
 
 
bdf0fae
e4cf7c3
 
bdf0fae
e4cf7c3
2d47d31
 
 
 
 
 
 
e4cf7c3
 
bdf0fae
 
 
 
 
 
 
 
 
 
 
2d47d31
bdf0fae
 
 
 
 
 
 
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
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()