Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- dockerfile +16 -0
- main.py +49 -0
- requirements.txt +4 -0
- shipment_delay_model.pkl +3 -0
dockerfile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
| 2 |
+
# you will also find guides on how best to write your Dockerfile
|
| 3 |
+
|
| 4 |
+
FROM python:3.9
|
| 5 |
+
|
| 6 |
+
RUN useradd -m -u 1000 user
|
| 7 |
+
USER user
|
| 8 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 9 |
+
|
| 10 |
+
WORKDIR /app
|
| 11 |
+
|
| 12 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
| 13 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 14 |
+
|
| 15 |
+
COPY --chown=user . /app
|
| 16 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
import pickle
|
| 3 |
+
import numpy as np
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
app = FastAPI(title="Shipment Delay Prediction API")
|
| 7 |
+
|
| 8 |
+
# -------- Load ML model --------
|
| 9 |
+
MODEL_PATH = "shipment_delay_model.pkl"
|
| 10 |
+
model = None
|
| 11 |
+
if os.path.exists(MODEL_PATH):
|
| 12 |
+
with open(MODEL_PATH, "rb") as f:
|
| 13 |
+
model = pickle.load(f)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
# -------- ML predictor --------
|
| 17 |
+
def ml_score(features: dict) -> float:
|
| 18 |
+
arr = np.array([[
|
| 19 |
+
features.get("distance_km", 0.0),
|
| 20 |
+
features.get("hours_to_deadline", 0.0),
|
| 21 |
+
features.get("origin_rain_mm", 0.0),
|
| 22 |
+
features.get("origin_storm", 0),
|
| 23 |
+
features.get("congestion_index", 0.0),
|
| 24 |
+
features.get("carrier_reliability", 0.7),
|
| 25 |
+
]])
|
| 26 |
+
|
| 27 |
+
if hasattr(model, "predict_proba"): # classifier
|
| 28 |
+
return float(model.predict_proba(arr)[0][1])
|
| 29 |
+
return float(model.predict(arr)[0]) # regression
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
# -------- API endpoints --------
|
| 33 |
+
@app.get("/health")
|
| 34 |
+
def health():
|
| 35 |
+
return {"status": "alive", "model_loaded": model is not None}
|
| 36 |
+
|
| 37 |
+
@app.post("/predict")
|
| 38 |
+
async def predict_endpoint(request: Request):
|
| 39 |
+
shipment = await request.json()
|
| 40 |
+
features = shipment.get("features", {})
|
| 41 |
+
|
| 42 |
+
if model is None:
|
| 43 |
+
return {"error": "Model not loaded on server."}
|
| 44 |
+
|
| 45 |
+
delay_prob = ml_score(features)
|
| 46 |
+
return {
|
| 47 |
+
"delay_prob": round(delay_prob, 3),
|
| 48 |
+
"risk_level": "HIGH" if delay_prob >= 0.6 else "MEDIUM" if delay_prob >= 0.3 else "LOW"
|
| 49 |
+
}
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
numpy
|
| 4 |
+
scikit-learn==1.3.2
|
shipment_delay_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1428981ca03239b41646bcd68096a61996bfbc33fabc307386c6cb0b627013c2
|
| 3 |
+
size 979878
|