File size: 1,528 Bytes
d434e45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from fastapi import FastAPI
from pydantic import BaseModel
import joblib
import numpy as np
import os

app = FastAPI()

# -------- Load models once at startup --------
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
MODEL_DIR = os.path.join(BASE_DIR, "models")

stockout_model = joblib.load(
    os.path.join(MODEL_DIR, "restaurant_stockout_classifier.joblib")
)

wastage_model = joblib.load(
    os.path.join(MODEL_DIR, "restaurant_wastage_regressor.joblib")
)

# -------- Request schema --------
class PredictRequest(BaseModel):
    features: list[float]

# -------- Health check --------
@app.get("/")
def root():
    return {
        "status": "ok",
        "message": "ProjectY Classifier + Regressor API is running"
    }

# -------- Stockout classifier --------
@app.post("/predict/stockout")
def predict_stockout(req: PredictRequest):
    X = np.array([req.features])  # shape: (1, n_features)
    prediction = stockout_model.predict(X)[0]

    response = {
        "prediction": int(prediction) if isinstance(prediction, (int, np.integer)) else float(prediction)
    }

    # Optional probabilities (if supported)
    if hasattr(stockout_model, "predict_proba"):
        response["probabilities"] = stockout_model.predict_proba(X)[0].tolist()

    return response

# -------- Wastage regressor --------
@app.post("/predict/wastage")
def predict_wastage(req: PredictRequest):
    X = np.array([req.features])
    prediction = wastage_model.predict(X)[0]

    return {
        "prediction": float(prediction)
    }