radwaamr1 commited on
Commit
a4224f5
·
verified ·
1 Parent(s): e098ee4

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +21 -0
  2. app.py +60 -0
  3. requirements.txt +7 -0
Dockerfile ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
14
+ # Install dependencies
15
+ RUN pip install --no-cache-dir -r requirements.txt
16
+
17
+ # Expose port for FastAPI
18
+ EXPOSE 8000
19
+
20
+ # Run app with uvicorn
21
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ 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
+ # Load artifacts
11
+ # =========================
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
+ app = FastAPI(title="Desertification Prediction API")
17
+
18
+ # =========================
19
+ # Input schema
20
+ # =========================
21
+ class PredictionRequest(BaseModel):
22
+ success: bool
23
+ features: Dict[str, Any]
24
+ metadata: Dict[str, Any] | None = None
25
+
26
+ # =========================
27
+ # Health check
28
+ # =========================
29
+ @app.get("/health")
30
+ def health():
31
+ return {"status": "ok", "timestamp": datetime.now().isoformat()}
32
+
33
+ # =========================
34
+ # Prediction endpoint
35
+ # =========================
36
+ @app.post("/predict")
37
+ def predict(request: PredictionRequest):
38
+
39
+ # Convert features to DataFrame
40
+ input_df = pd.DataFrame([request.features])
41
+
42
+ # Ensure correct feature order & missing features handling
43
+ input_df = input_df.reindex(columns=expected_features)
44
+
45
+ # Predict
46
+ pred_encoded = xgb_pipe.predict(input_df)[0]
47
+ pred_label = label_encoder.inverse_transform([pred_encoded])[0]
48
+ pred_proba = float(xgb_pipe.predict_proba(input_df).max())
49
+
50
+ # Build response
51
+ response = {
52
+ "success": True,
53
+ "prediction": {
54
+ "desertification_level": pred_label,
55
+ "confidence": round(pred_proba, 4)
56
+ },
57
+ "metadata": request.metadata
58
+ }
59
+
60
+ return response
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ scikit-learn
4
+ xgboost
5
+ pandas
6
+ numpy
7
+ joblib