BeeBasic commited on
Commit
0ffa4d1
·
verified ·
1 Parent(s): e0e8fcf

updated app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -24
app.py CHANGED
@@ -1,24 +1,37 @@
1
- from fastapi import FastAPI, Request
2
- import joblib
3
-
4
- app = FastAPI(title="Food Surplus Predictor")
5
-
6
- # Load model from Hugging Face model repo
7
- model = joblib.load("https://huggingface.co/BeeBasic/food-for-all/resolve/main/best_model.joblib")
8
-
9
- @app.post("/predict")
10
- async def predict(request: Request):
11
- data = await request.json()
12
-
13
- canteens = data.get("canteens", [])
14
- ngos = data.get("ngos", [])
15
-
16
- total_surplus = sum(item["surplus"] for item in canteens)
17
- total_need = sum(item["requirement"] for item in ngos)
18
- balance = total_surplus - total_need
19
-
20
- return {
21
- "total_surplus": total_surplus,
22
- "total_need": total_need,
23
- "balance": balance
24
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
+ from huggingface_hub import hf_hub_download
4
+ import joblib
5
+ import pandas as pd
6
+
7
+ app = FastAPI(title="Food Surplus Predictor API")
8
+
9
+ # Download model from your Hugging Face model repo
10
+ model_path = hf_hub_download(
11
+ repo_id="BeeBasic/food-for-all",
12
+ filename="best_model.joblib",
13
+ repo_type="model"
14
+ )
15
+ model = joblib.load(model_path)
16
+
17
+ # Define input data schema
18
+ class CanteenInput(BaseModel):
19
+ canteen_id: int
20
+ day: int
21
+ month: int
22
+ year: int
23
+ day_of_week: int
24
+
25
+ class RequestBody(BaseModel):
26
+ data: list[CanteenInput]
27
+
28
+ @app.get("/")
29
+ def home():
30
+ return {"message": "Food Surplus Prediction API is running!"}
31
+
32
+ @app.post("/predict")
33
+ def predict_surplus(request: RequestBody):
34
+ df = pd.DataFrame([canteen.dict() for canteen in request.data])
35
+ predictions = model.predict(df)
36
+ df["predicted_surplus"] = predictions
37
+ return df.to_dict(orient="records")