updated the deployment code in the backend
Browse files- .dockerignore +9 -0
- Dockerfile +19 -0
- README.md +36 -0
- app/__pycache__/main.cpython-310.pyc +0 -0
- app/main.py +113 -0
- requirements.txt +7 -0
- xg_bost.pkl +3 -0
.dockerignore
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.git
|
| 2 |
+
.gitignore
|
| 3 |
+
__pycache__/
|
| 4 |
+
*.pyc
|
| 5 |
+
*.pyo
|
| 6 |
+
*.pyd
|
| 7 |
+
.pytest_cache/
|
| 8 |
+
.mypy_cache/
|
| 9 |
+
notebooks/
|
Dockerfile
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9
|
| 2 |
+
|
| 3 |
+
RUN useradd -m -u 1000 user
|
| 4 |
+
USER user
|
| 5 |
+
ENV PATH="/home/user/.local/bin:$PATH" \
|
| 6 |
+
PYTHONDONTWRITEBYTECODE=1 \
|
| 7 |
+
PYTHONUNBUFFERED=1
|
| 8 |
+
|
| 9 |
+
WORKDIR /app
|
| 10 |
+
|
| 11 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
| 12 |
+
RUN pip install --no-cache-dir --upgrade pip && \
|
| 13 |
+
pip install --no-cache-dir --upgrade -r requirements.txt
|
| 14 |
+
|
| 15 |
+
COPY --chown=user . /app
|
| 16 |
+
|
| 17 |
+
EXPOSE 7860
|
| 18 |
+
|
| 19 |
+
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
README.md
CHANGED
|
@@ -10,3 +10,39 @@ short_description: This is an ml inference space
|
|
| 10 |
---
|
| 11 |
|
| 12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
| 13 |
+
|
| 14 |
+
## Inference API
|
| 15 |
+
|
| 16 |
+
This Space runs a FastAPI app with an XGBoost model loaded from `xg_bost.pkl`.
|
| 17 |
+
|
| 18 |
+
- Health check: `GET /health`
|
| 19 |
+
- Prediction endpoint: `POST /predit`
|
| 20 |
+
|
| 21 |
+
### Sample request
|
| 22 |
+
|
| 23 |
+
```json
|
| 24 |
+
{
|
| 25 |
+
"distance_km": 7.93,
|
| 26 |
+
"weather_condition": "Windy",
|
| 27 |
+
"traffic_level": "Low",
|
| 28 |
+
"vehicle_type": "scooter",
|
| 29 |
+
"temperature_c": 23.0,
|
| 30 |
+
"humidity_pct": 55.0,
|
| 31 |
+
"precipitation_mm": 0.0,
|
| 32 |
+
"preparation_time_min": 12.0,
|
| 33 |
+
"courier_experience_yrs": 1.0,
|
| 34 |
+
"worker_age": 29.0,
|
| 35 |
+
"worker_rating": 4.7,
|
| 36 |
+
"order_type": "Unknown",
|
| 37 |
+
"weather_risk": 7.0,
|
| 38 |
+
"traffic_risk": 25.0
|
| 39 |
+
}
|
| 40 |
+
```
|
| 41 |
+
|
| 42 |
+
### Sample response
|
| 43 |
+
|
| 44 |
+
```json
|
| 45 |
+
{
|
| 46 |
+
"prediction": 1.5953301191329956
|
| 47 |
+
}
|
| 48 |
+
```
|
app/__pycache__/main.cpython-310.pyc
ADDED
|
Binary file (3.46 kB). View file
|
|
|
app/main.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
from typing import Any, Union
|
| 5 |
+
|
| 6 |
+
import joblib
|
| 7 |
+
import pandas as pd
|
| 8 |
+
from fastapi import FastAPI, HTTPException
|
| 9 |
+
from pydantic import BaseModel, Field
|
| 10 |
+
|
| 11 |
+
MODEL_PATH = Path(__file__).resolve().parent.parent / "xg_bost.pkl"
|
| 12 |
+
|
| 13 |
+
# LabelEncoder-style mappings (alphabetical order) for categorical features.
|
| 14 |
+
CATEGORICAL_MAPPINGS = {
|
| 15 |
+
"weather_condition": {
|
| 16 |
+
"Clear": 0.0,
|
| 17 |
+
"Cloudy": 1.0,
|
| 18 |
+
"Fog": 2.0,
|
| 19 |
+
"Rain": 3.0,
|
| 20 |
+
"Snow": 4.0,
|
| 21 |
+
"Windy": 5.0,
|
| 22 |
+
},
|
| 23 |
+
"traffic_level": {
|
| 24 |
+
"High": 0.0,
|
| 25 |
+
"Low": 1.0,
|
| 26 |
+
"Medium": 2.0,
|
| 27 |
+
"Very High": 3.0,
|
| 28 |
+
"Very Low": 4.0,
|
| 29 |
+
},
|
| 30 |
+
"vehicle_type": {
|
| 31 |
+
"car": 0.0,
|
| 32 |
+
"cycle": 1.0,
|
| 33 |
+
"ev": 2.0,
|
| 34 |
+
"motorcycle": 3.0,
|
| 35 |
+
"scooter": 4.0,
|
| 36 |
+
},
|
| 37 |
+
"order_type": {
|
| 38 |
+
"Buffet": 0.0,
|
| 39 |
+
"Drinks": 1.0,
|
| 40 |
+
"Meal": 2.0,
|
| 41 |
+
"Snack": 3.0,
|
| 42 |
+
"Unknown": 4.0,
|
| 43 |
+
},
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
model = joblib.load(MODEL_PATH)
|
| 47 |
+
FEATURE_ORDER = list(model.feature_names_in_)
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
class PredictInput(BaseModel):
|
| 51 |
+
distance_km: float = Field(..., example=7.93)
|
| 52 |
+
weather_condition: Union[str, float, int] = Field(..., example="Windy")
|
| 53 |
+
traffic_level: Union[str, float, int] = Field(..., example="Low")
|
| 54 |
+
vehicle_type: Union[str, float, int] = Field(..., example="scooter")
|
| 55 |
+
temperature_c: float = Field(..., example=23.0)
|
| 56 |
+
humidity_pct: float = Field(..., example=55.0)
|
| 57 |
+
precipitation_mm: float = Field(..., example=0.0)
|
| 58 |
+
preparation_time_min: float = Field(..., example=12.0)
|
| 59 |
+
courier_experience_yrs: float = Field(..., example=1.0)
|
| 60 |
+
worker_age: float = Field(..., example=29.0)
|
| 61 |
+
worker_rating: float = Field(..., example=4.7)
|
| 62 |
+
order_type: Union[str, float, int] = Field(..., example="Unknown")
|
| 63 |
+
weather_risk: float = Field(..., example=7.0)
|
| 64 |
+
traffic_risk: float = Field(..., example=25.0)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def _encode_categorical(feature_name: str, value: Any) -> float:
|
| 68 |
+
if isinstance(value, (int, float)) and not isinstance(value, bool):
|
| 69 |
+
return float(value)
|
| 70 |
+
|
| 71 |
+
mapping = CATEGORICAL_MAPPINGS[feature_name]
|
| 72 |
+
raw = str(value).strip()
|
| 73 |
+
if raw in mapping:
|
| 74 |
+
return mapping[raw]
|
| 75 |
+
|
| 76 |
+
lower_map = {k.lower(): v for k, v in mapping.items()}
|
| 77 |
+
if raw.lower() in lower_map:
|
| 78 |
+
return lower_map[raw.lower()]
|
| 79 |
+
|
| 80 |
+
allowed = ", ".join(mapping.keys())
|
| 81 |
+
raise HTTPException(
|
| 82 |
+
status_code=422,
|
| 83 |
+
detail=f"Unknown value '{value}' for '{feature_name}'. Allowed values: {allowed}",
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def _prepare_features(payload: PredictInput) -> pd.DataFrame:
|
| 88 |
+
values = payload.model_dump()
|
| 89 |
+
row: dict[str, float] = {}
|
| 90 |
+
|
| 91 |
+
for feature in FEATURE_ORDER:
|
| 92 |
+
value = values[feature]
|
| 93 |
+
if feature in CATEGORICAL_MAPPINGS:
|
| 94 |
+
row[feature] = _encode_categorical(feature, value)
|
| 95 |
+
else:
|
| 96 |
+
row[feature] = float(value)
|
| 97 |
+
|
| 98 |
+
return pd.DataFrame([row], columns=FEATURE_ORDER)
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
app = FastAPI(title="Gitwire XGBoost Inference API", version="1.0.0")
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
@app.get("/health")
|
| 105 |
+
def health() -> dict[str, str]:
|
| 106 |
+
return {"status": "ok"}
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
@app.post("/predit")
|
| 110 |
+
def predit(payload: PredictInput) -> dict[str, float]:
|
| 111 |
+
features = _prepare_features(payload)
|
| 112 |
+
prediction = float(model.predict(features)[0])
|
| 113 |
+
return {"prediction": prediction}
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn[standard]
|
| 3 |
+
pandas
|
| 4 |
+
numpy
|
| 5 |
+
scikit-learn
|
| 6 |
+
xgboost
|
| 7 |
+
joblib
|
xg_bost.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:cc8263f811015caf03f53ddc1e8fbc2d96a214c3ae3fce18bf52735749b2a463
|
| 3 |
+
size 1292025
|