File size: 1,361 Bytes
3614ee8
 
 
 
fbf6023
3614ee8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fbf6023
3614ee8
 
 
fbf6023
 
 
 
 
 
 
3614ee8
fbf6023
3614ee8
 
 
 
 
 
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
import gradio as gr
import numpy as np
import tensorflow as tf
import joblib
import sklearn  # Required for unpickling label_encoder

# Load the saved model and label encoder
model = tf.keras.models.load_model("crop_recommendation_model.h5")
label_encoder = joblib.load("label_encoder.pkl")

def predict_crop(N, P, K, temperature, humidity, ph, rainfall):
    # Prepare the input sample as a 2D numpy array
    sample = np.array([[N, P, K, temperature, humidity, ph, rainfall]])
    # Get model prediction
    pred_probs = model.predict(sample)
    pred_class = np.argmax(pred_probs, axis=1)[0]
    # Convert numeric prediction back to crop label
    predicted_crop = label_encoder.inverse_transform([pred_class])[0]
    return predicted_crop

# Define Gradio Interface using the updated API
iface = gr.Interface(
    fn=predict_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"),
        gr.Number(label="Rainfall (mm)")
    ],
    outputs=gr.Textbox(label="Recommended Crop"),
    title="Crop Recommendation System",
    description="Enter soil and climate parameters to get the recommended crop."
)

if __name__ == "__main__":
    iface.launch()