Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- Dockerfile +2 -0
- app.py +5 -47
- custom.py +19 -0
Dockerfile
CHANGED
|
@@ -10,6 +10,8 @@ COPY app.py .
|
|
| 10 |
COPY xgb_desertification_pipeline.pkl .
|
| 11 |
COPY feature_names.pkl .
|
| 12 |
COPY label_encoder.pkl .
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# Install dependencies
|
| 15 |
RUN pip install --no-cache-dir -r requirements.txt
|
|
|
|
| 10 |
COPY xgb_desertification_pipeline.pkl .
|
| 11 |
COPY feature_names.pkl .
|
| 12 |
COPY label_encoder.pkl .
|
| 13 |
+
COPY custom.py ./
|
| 14 |
+
|
| 15 |
|
| 16 |
# Install dependencies
|
| 17 |
RUN pip install --no-cache-dir -r requirements.txt
|
app.py
CHANGED
|
@@ -3,77 +3,37 @@ from pydantic import BaseModel
|
|
| 3 |
from typing import Dict, Any
|
| 4 |
import pandas as pd
|
| 5 |
import joblib
|
| 6 |
-
import numpy as np
|
| 7 |
from datetime import datetime
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
# =========================
|
| 12 |
-
from sklearn.base import BaseEstimator, TransformerMixin
|
| 13 |
|
| 14 |
-
|
| 15 |
-
def __init__(self, expected_features):
|
| 16 |
-
self.expected_features = expected_features
|
| 17 |
-
|
| 18 |
-
def fit(self, X, y=None):
|
| 19 |
-
return self
|
| 20 |
-
|
| 21 |
-
def transform(self, X):
|
| 22 |
-
X = X.copy()
|
| 23 |
-
# keep only expected features
|
| 24 |
-
X = X[[c for c in self.expected_features if c in X.columns]]
|
| 25 |
-
# add missing features as NaN
|
| 26 |
-
for c in self.expected_features:
|
| 27 |
-
if c not in X.columns:
|
| 28 |
-
X[c] = np.nan
|
| 29 |
-
return X[self.expected_features]
|
| 30 |
-
|
| 31 |
-
# =========================
|
| 32 |
-
# Load model artifacts
|
| 33 |
-
# =========================
|
| 34 |
xgb_pipe = joblib.load("xgb_desertification_pipeline.pkl")
|
| 35 |
label_encoder = joblib.load("label_encoder.pkl")
|
| 36 |
expected_features = joblib.load("feature_names.pkl")
|
| 37 |
|
| 38 |
-
# =========================
|
| 39 |
# FastAPI app
|
| 40 |
-
# =========================
|
| 41 |
app = FastAPI(title="Desertification Prediction API")
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
# Input schema
|
| 45 |
-
# =========================
|
| 46 |
class PredictionRequest(BaseModel):
|
| 47 |
success: bool
|
| 48 |
features: Dict[str, Any]
|
| 49 |
metadata: Dict[str, Any] | None = None
|
| 50 |
|
| 51 |
-
# =========================
|
| 52 |
-
# Health check endpoint
|
| 53 |
-
# =========================
|
| 54 |
@app.get("/health")
|
| 55 |
def health():
|
| 56 |
return {"status": "ok", "timestamp": datetime.now().isoformat()}
|
| 57 |
|
| 58 |
-
# =========================
|
| 59 |
-
# Prediction endpoint
|
| 60 |
-
# =========================
|
| 61 |
@app.post("/predict")
|
| 62 |
def predict(request: PredictionRequest):
|
| 63 |
-
|
| 64 |
-
# Convert features to DataFrame
|
| 65 |
input_df = pd.DataFrame([request.features])
|
| 66 |
-
|
| 67 |
-
# Ensure correct feature order & missing features handling
|
| 68 |
input_df = input_df.reindex(columns=expected_features)
|
| 69 |
-
|
| 70 |
-
# Predict
|
| 71 |
pred_encoded = xgb_pipe.predict(input_df)[0]
|
| 72 |
pred_label = label_encoder.inverse_transform([pred_encoded])[0]
|
| 73 |
pred_proba = float(xgb_pipe.predict_proba(input_df).max())
|
| 74 |
-
|
| 75 |
-
# Build response
|
| 76 |
-
response = {
|
| 77 |
"success": True,
|
| 78 |
"prediction": {
|
| 79 |
"desertification_level": pred_label,
|
|
@@ -81,5 +41,3 @@ def predict(request: PredictionRequest):
|
|
| 81 |
},
|
| 82 |
"metadata": request.metadata
|
| 83 |
}
|
| 84 |
-
|
| 85 |
-
return response
|
|
|
|
| 3 |
from typing import Dict, Any
|
| 4 |
import pandas as pd
|
| 5 |
import joblib
|
|
|
|
| 6 |
from datetime import datetime
|
| 7 |
|
| 8 |
+
# Import custom class BEFORE loading pipeline
|
| 9 |
+
from custom import FeatureFilter
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Load artifacts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
xgb_pipe = joblib.load("xgb_desertification_pipeline.pkl")
|
| 13 |
label_encoder = joblib.load("label_encoder.pkl")
|
| 14 |
expected_features = joblib.load("feature_names.pkl")
|
| 15 |
|
|
|
|
| 16 |
# FastAPI app
|
|
|
|
| 17 |
app = FastAPI(title="Desertification Prediction API")
|
| 18 |
|
| 19 |
+
# Schema
|
|
|
|
|
|
|
| 20 |
class PredictionRequest(BaseModel):
|
| 21 |
success: bool
|
| 22 |
features: Dict[str, Any]
|
| 23 |
metadata: Dict[str, Any] | None = None
|
| 24 |
|
|
|
|
|
|
|
|
|
|
| 25 |
@app.get("/health")
|
| 26 |
def health():
|
| 27 |
return {"status": "ok", "timestamp": datetime.now().isoformat()}
|
| 28 |
|
|
|
|
|
|
|
|
|
|
| 29 |
@app.post("/predict")
|
| 30 |
def predict(request: PredictionRequest):
|
|
|
|
|
|
|
| 31 |
input_df = pd.DataFrame([request.features])
|
|
|
|
|
|
|
| 32 |
input_df = input_df.reindex(columns=expected_features)
|
|
|
|
|
|
|
| 33 |
pred_encoded = xgb_pipe.predict(input_df)[0]
|
| 34 |
pred_label = label_encoder.inverse_transform([pred_encoded])[0]
|
| 35 |
pred_proba = float(xgb_pipe.predict_proba(input_df).max())
|
| 36 |
+
return {
|
|
|
|
|
|
|
| 37 |
"success": True,
|
| 38 |
"prediction": {
|
| 39 |
"desertification_level": pred_label,
|
|
|
|
| 41 |
},
|
| 42 |
"metadata": request.metadata
|
| 43 |
}
|
|
|
|
|
|
custom.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from sklearn.base import BaseEstimator, TransformerMixin
|
| 3 |
+
|
| 4 |
+
class FeatureFilter(BaseEstimator, TransformerMixin):
|
| 5 |
+
def __init__(self, expected_features):
|
| 6 |
+
self.expected_features = expected_features
|
| 7 |
+
|
| 8 |
+
def fit(self, X, y=None):
|
| 9 |
+
return self
|
| 10 |
+
|
| 11 |
+
def transform(self, X):
|
| 12 |
+
X = X.copy()
|
| 13 |
+
# keep only expected features
|
| 14 |
+
X = X[[c for c in self.expected_features if c in X.columns]]
|
| 15 |
+
# add missing features as NaN
|
| 16 |
+
for c in self.expected_features:
|
| 17 |
+
if c not in X.columns:
|
| 18 |
+
X[c] = np.nan
|
| 19 |
+
return X[self.expected_features]
|