Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +73 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import joblib
|
| 5 |
+
import numpy as np
|
| 6 |
+
from sklearn.base import BaseEstimator, TransformerMixin
|
| 7 |
+
|
| 8 |
+
# -------------------------------
|
| 9 |
+
# Load saved artifacts
|
| 10 |
+
# -------------------------------
|
| 11 |
+
xgb_pipe = joblib.load("xgb_desertification_pipeline.pkl")
|
| 12 |
+
le = joblib.load("label_encoder.pkl")
|
| 13 |
+
expected_features = joblib.load("feature_names.pkl")
|
| 14 |
+
|
| 15 |
+
# -------------------------------
|
| 16 |
+
# FeatureFilter class
|
| 17 |
+
# -------------------------------
|
| 18 |
+
# Needed in case it's part of the pipeline
|
| 19 |
+
class FeatureFilter(BaseEstimator, TransformerMixin):
|
| 20 |
+
def __init__(self, expected_features):
|
| 21 |
+
self.expected_features = expected_features
|
| 22 |
+
|
| 23 |
+
def fit(self, X, y=None):
|
| 24 |
+
return self
|
| 25 |
+
|
| 26 |
+
def transform(self, X):
|
| 27 |
+
X = X.copy()
|
| 28 |
+
# Keep only expected features
|
| 29 |
+
X = X[[c for c in self.expected_features if c in X.columns]]
|
| 30 |
+
# Add missing columns with NaN
|
| 31 |
+
for c in self.expected_features:
|
| 32 |
+
if c not in X.columns:
|
| 33 |
+
X[c] = np.nan
|
| 34 |
+
return X[self.expected_features]
|
| 35 |
+
|
| 36 |
+
# -------------------------------
|
| 37 |
+
# Prediction function
|
| 38 |
+
# -------------------------------
|
| 39 |
+
def predict_desertification(**kwargs):
|
| 40 |
+
# Convert inputs to DataFrame
|
| 41 |
+
df = pd.DataFrame([kwargs])
|
| 42 |
+
df = df[expected_features] # reorder columns to match training
|
| 43 |
+
# Predict
|
| 44 |
+
pred_encoded = xgb_pipe.predict(df)
|
| 45 |
+
pred_label = le.inverse_transform(pred_encoded)
|
| 46 |
+
pred_proba = xgb_pipe.predict_proba(df).max(axis=1)
|
| 47 |
+
return {
|
| 48 |
+
"Desertification Level": pred_label[0],
|
| 49 |
+
"Confidence": float(pred_proba[0])
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
# -------------------------------
|
| 53 |
+
# Define Gradio interface
|
| 54 |
+
# -------------------------------
|
| 55 |
+
# Separate categorical and numeric inputs if needed
|
| 56 |
+
inputs = []
|
| 57 |
+
for feat in expected_features:
|
| 58 |
+
if feat == "lc_type1":
|
| 59 |
+
inputs.append(gr.Number(label=feat, value=0))
|
| 60 |
+
else:
|
| 61 |
+
inputs.append(gr.Number(label=feat, value=0.0))
|
| 62 |
+
|
| 63 |
+
# Gradio app
|
| 64 |
+
demo = gr.Interface(
|
| 65 |
+
fn=predict_desertification,
|
| 66 |
+
inputs=inputs,
|
| 67 |
+
outputs="json",
|
| 68 |
+
title="Desertification Level Predictor",
|
| 69 |
+
description="Predict desertification level given soil, climate, and land cover features."
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
# Launch
|
| 73 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
pandas
|
| 3 |
+
numpy
|
| 4 |
+
scikit-learn
|
| 5 |
+
xgboost
|
| 6 |
+
joblib
|