Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
# app.py
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import pandas as pd
|
| 4 |
import joblib
|
|
@@ -6,16 +7,8 @@ import numpy as np
|
|
| 6 |
from sklearn.base import BaseEstimator, TransformerMixin
|
| 7 |
|
| 8 |
# -------------------------------
|
| 9 |
-
#
|
| 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
|
|
@@ -33,14 +26,19 @@ class FeatureFilter(BaseEstimator, TransformerMixin):
|
|
| 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
|
| 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)
|
|
@@ -50,24 +48,15 @@ def predict_desertification(**kwargs):
|
|
| 50 |
}
|
| 51 |
|
| 52 |
# -------------------------------
|
| 53 |
-
#
|
| 54 |
# -------------------------------
|
| 55 |
-
|
| 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()
|
|
|
|
| 1 |
# app.py
|
| 2 |
+
|
| 3 |
import gradio as gr
|
| 4 |
import pandas as pd
|
| 5 |
import joblib
|
|
|
|
| 7 |
from sklearn.base import BaseEstimator, TransformerMixin
|
| 8 |
|
| 9 |
# -------------------------------
|
| 10 |
+
# Define custom class BEFORE loading
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# -------------------------------
|
|
|
|
| 12 |
class FeatureFilter(BaseEstimator, TransformerMixin):
|
| 13 |
def __init__(self, expected_features):
|
| 14 |
self.expected_features = expected_features
|
|
|
|
| 26 |
X[c] = np.nan
|
| 27 |
return X[self.expected_features]
|
| 28 |
|
| 29 |
+
# -------------------------------
|
| 30 |
+
# Load artifacts
|
| 31 |
+
# -------------------------------
|
| 32 |
+
xgb_pipe = joblib.load("xgb_desertification_pipeline.pkl")
|
| 33 |
+
le = joblib.load("label_encoder.pkl")
|
| 34 |
+
expected_features = joblib.load("feature_names.pkl")
|
| 35 |
+
|
| 36 |
# -------------------------------
|
| 37 |
# Prediction function
|
| 38 |
# -------------------------------
|
| 39 |
def predict_desertification(**kwargs):
|
|
|
|
| 40 |
df = pd.DataFrame([kwargs])
|
| 41 |
+
df = df[expected_features] # reorder columns
|
|
|
|
| 42 |
pred_encoded = xgb_pipe.predict(df)
|
| 43 |
pred_label = le.inverse_transform(pred_encoded)
|
| 44 |
pred_proba = xgb_pipe.predict_proba(df).max(axis=1)
|
|
|
|
| 48 |
}
|
| 49 |
|
| 50 |
# -------------------------------
|
| 51 |
+
# Gradio interface
|
| 52 |
# -------------------------------
|
| 53 |
+
inputs = [gr.Number(label=f, value=0.0) for f in expected_features]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
|
|
|
| 55 |
demo = gr.Interface(
|
| 56 |
fn=predict_desertification,
|
| 57 |
inputs=inputs,
|
| 58 |
outputs="json",
|
| 59 |
+
title="Desertification Level Predictor"
|
|
|
|
| 60 |
)
|
| 61 |
|
|
|
|
| 62 |
demo.launch()
|