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()