Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
import joblib
|
| 5 |
+
|
| 6 |
+
# Load the saved model and label encoder
|
| 7 |
+
model = tf.keras.models.load_model("crop_recommendation_model.h5")
|
| 8 |
+
label_encoder = joblib.load("label_encoder.pkl")
|
| 9 |
+
|
| 10 |
+
def predict_crop(N, P, K, temperature, humidity, ph, rainfall):
|
| 11 |
+
# Prepare the input sample as a 2D numpy array
|
| 12 |
+
sample = np.array([[N, P, K, temperature, humidity, ph, rainfall]])
|
| 13 |
+
# Get model prediction
|
| 14 |
+
pred_probs = model.predict(sample)
|
| 15 |
+
pred_class = np.argmax(pred_probs, axis=1)[0]
|
| 16 |
+
# Convert numeric prediction back to crop label
|
| 17 |
+
predicted_crop = label_encoder.inverse_transform([pred_class])[0]
|
| 18 |
+
return predicted_crop
|
| 19 |
+
|
| 20 |
+
# Define Gradio Interface
|
| 21 |
+
iface = gr.Interface(
|
| 22 |
+
fn=predict_crop,
|
| 23 |
+
inputs=[
|
| 24 |
+
gr.inputs.Number(label="Nitrogen (N)"),
|
| 25 |
+
gr.inputs.Number(label="Phosphorous (P)"),
|
| 26 |
+
gr.inputs.Number(label="Potassium (K)"),
|
| 27 |
+
gr.inputs.Number(label="Temperature (°C)"),
|
| 28 |
+
gr.inputs.Number(label="Humidity (%)"),
|
| 29 |
+
gr.inputs.Number(label="pH"),
|
| 30 |
+
gr.inputs.Number(label="Rainfall (mm)")
|
| 31 |
+
],
|
| 32 |
+
outputs=gr.outputs.Textbox(label="Recommended Crop"),
|
| 33 |
+
title="Crop Recommendation System",
|
| 34 |
+
description="Enter soil and climate parameters to get the recommended crop."
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
if __name__ == "__main__":
|
| 38 |
+
iface.launch()
|