File size: 1,860 Bytes
0ffa4d1
 
 
 
 
 
28ba1f3
0ffa4d1
a08c7f9
0ffa4d1
28ba1f3
 
0ffa4d1
 
 
 
a8e3248
0ffa4d1
28ba1f3
 
0ffa4d1
 
 
 
 
 
 
 
 
 
 
 
 
 
a08c7f9
0ffa4d1
2dbb2d0
a08c7f9
 
 
a8e3248
 
a08c7f9
 
 
 
 
 
a8e3248
a08c7f9
0ffa4d1
a08c7f9
0ffa4d1
a8e3248
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
from fastapi import FastAPI
from pydantic import BaseModel
from huggingface_hub import hf_hub_download
import joblib
import pandas as pd

app = FastAPI(title="Food Surplus Predictor API")

# Download model
model_path = hf_hub_download(
    repo_id="BeeBasic/food-for-all",
    filename="best_model.joblib",
    repo_type="model"
)
model = joblib.load(model_path)

# Define schema for input
class CanteenInput(BaseModel):
    canteen_id: str
    canteen_name: str
    day: int
    month: int
    year: int
    day_of_week: int

class RequestBody(BaseModel):
    data: list[CanteenInput]

@app.get("/")
def home():
    return {"message": "Food Surplus Prediction API is running!"}

@app.post("/predict")
def predict_surplus(request: RequestBody):
    # Convert input to DataFrame
    df = pd.DataFrame([canteen.dict() for canteen in request.data])

    # One-hot encode categorical columns
    df_encoded = pd.get_dummies(df, columns=["canteen_id", "canteen_name"])

    # Align columns with model features
    model_features = getattr(model, "feature_names_", None)
    if model_features:
        for col in model_features:
            if col not in df_encoded.columns:
                df_encoded[col] = 0
        df_encoded = df_encoded[model_features]

    # Predict
    predictions = model.predict(df_encoded)
    df["predicted_surplus"] = predictions

    return df.to_dict(orient="records")

@app.get("/fetch_data")
def fetch_data(date: str):
    """
    Temporary endpoint so your frontend doesn't explode.
    Replace this with an actual DB lookup later if you want real data.
    """
    # You can later connect this to your stored predictions or history table.
    sample_response = {
        "date": date,
        "canteen_id": "C002",
        "canteen_name": "Anna University Mess",
        "predicted_surplus": 24.0
    }
    return sample_response