Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import joblib
|
| 4 |
+
|
| 5 |
+
# ==============================
|
| 6 |
+
# LOAD MODELS
|
| 7 |
+
# ==============================
|
| 8 |
+
scaler = joblib.load("scaler.pkl")
|
| 9 |
+
le_label = joblib.load("le_label.pkl")
|
| 10 |
+
le_type = joblib.load("le_type.pkl")
|
| 11 |
+
|
| 12 |
+
rf_label_model = joblib.load("rf_label_model.pkl")
|
| 13 |
+
rf_type_model = joblib.load("rf_type_model.pkl")
|
| 14 |
+
|
| 15 |
+
# ==============================
|
| 16 |
+
# PREDICTION FUNCTION
|
| 17 |
+
# ==============================
|
| 18 |
+
def predict(N, P, K, temperature, humidity, ph):
|
| 19 |
+
sample = np.array([[N, P, K, temperature, humidity, ph]])
|
| 20 |
+
sample_scaled = scaler.transform(sample)
|
| 21 |
+
|
| 22 |
+
pred_label = le_label.inverse_transform(
|
| 23 |
+
rf_label_model.predict(sample_scaled)
|
| 24 |
+
)[0]
|
| 25 |
+
|
| 26 |
+
pred_type = le_type.inverse_transform(
|
| 27 |
+
rf_type_model.predict(sample_scaled)
|
| 28 |
+
)[0]
|
| 29 |
+
|
| 30 |
+
return f"🌱 Culture: {pred_label}\n🌍 Soil Type: {pred_type}"
|
| 31 |
+
|
| 32 |
+
# ==============================
|
| 33 |
+
# GRADIO INTERFACE
|
| 34 |
+
# ==============================
|
| 35 |
+
interface = gr.Interface(
|
| 36 |
+
fn=predict,
|
| 37 |
+
inputs=[
|
| 38 |
+
gr.Number(label="Nitrogen (N)"),
|
| 39 |
+
gr.Number(label="Phosphorus (P)"),
|
| 40 |
+
gr.Number(label="Potassium (K)"),
|
| 41 |
+
gr.Number(label="Temperature (°C)"),
|
| 42 |
+
gr.Number(label="Humidity (%)"),
|
| 43 |
+
gr.Number(label="pH"),
|
| 44 |
+
],
|
| 45 |
+
outputs="text",
|
| 46 |
+
title="Crop & Soil Prediction Model",
|
| 47 |
+
description="Prediction using Random Forest model trained in Kaggle"
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
interface.launch()
|