radwaamr1 commited on
Commit
20d48b6
·
verified ·
1 Parent(s): 7c7e513

Upload 4 files

Browse files
Files changed (3) hide show
  1. Dockerfile +1 -8
  2. app.py +5 -3
  3. custom.py +3 -2
Dockerfile CHANGED
@@ -1,23 +1,16 @@
1
- # Base image
2
  FROM python:3.11-slim
3
 
4
- # Set working directory
5
  WORKDIR /app
6
 
7
- # Copy files
8
  COPY requirements.txt .
9
  COPY app.py .
 
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
18
 
19
- # Expose port for FastAPI
20
  EXPOSE 8000
21
 
22
- # Run app with uvicorn
23
  CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
 
 
1
  FROM python:3.11-slim
2
 
 
3
  WORKDIR /app
4
 
 
5
  COPY requirements.txt .
6
  COPY app.py .
7
+ COPY custom.py .
8
  COPY xgb_desertification_pipeline.pkl .
9
  COPY feature_names.pkl .
10
  COPY label_encoder.pkl .
 
11
 
 
 
12
  RUN pip install --no-cache-dir -r requirements.txt
13
 
 
14
  EXPOSE 8000
15
 
 
16
  CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
app.py CHANGED
@@ -5,10 +5,10 @@ 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")
@@ -16,16 +16,18 @@ expected_features = joblib.load("feature_names.pkl")
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])
 
5
  import joblib
6
  from datetime import datetime
7
 
8
+ # Import custom class before loading the pipeline
9
  from custom import FeatureFilter
10
 
11
+ # Load model 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")
 
16
  # FastAPI app
17
  app = FastAPI(title="Desertification Prediction API")
18
 
19
+ # Input schema
20
  class PredictionRequest(BaseModel):
21
  success: bool
22
  features: Dict[str, Any]
23
  metadata: Dict[str, Any] | None = None
24
 
25
+ # Health check endpoint
26
  @app.get("/health")
27
  def health():
28
  return {"status": "ok", "timestamp": datetime.now().isoformat()}
29
 
30
+ # Prediction endpoint
31
  @app.post("/predict")
32
  def predict(request: PredictionRequest):
33
  input_df = pd.DataFrame([request.features])
custom.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import numpy as np
2
  from sklearn.base import BaseEstimator, TransformerMixin
3
 
@@ -10,9 +11,9 @@ class FeatureFilter(BaseEstimator, TransformerMixin):
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
 
1
+ # custom.py
2
  import numpy as np
3
  from sklearn.base import BaseEstimator, TransformerMixin
4
 
 
11
 
12
  def transform(self, X):
13
  X = X.copy()
14
+ # Keep only expected features
15
  X = X[[c for c in self.expected_features if c in X.columns]]
16
+ # Add missing features as NaN
17
  for c in self.expected_features:
18
  if c not in X.columns:
19
  X[c] = np.nan