Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,17 +4,18 @@ from huggingface_hub import hf_hub_download
|
|
| 4 |
import joblib
|
| 5 |
import pandas as pd
|
| 6 |
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
# Download model from your Hugging Face model
|
| 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 |
-
#
|
| 18 |
class CanteenInput(BaseModel):
|
| 19 |
canteen_id: int
|
| 20 |
day: int
|
|
@@ -22,16 +23,29 @@ class CanteenInput(BaseModel):
|
|
| 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")
|
|
|
|
| 4 |
import joblib
|
| 5 |
import pandas as pd
|
| 6 |
|
| 7 |
+
# Initialize the app
|
| 8 |
+
app = FastAPI(title="Food Surplus Prediction API")
|
| 9 |
|
| 10 |
+
# Download the trained model from your Hugging Face model repository
|
| 11 |
model_path = hf_hub_download(
|
| 12 |
+
repo_id="BeeBasic/food-for-all", # your model repo
|
| 13 |
+
filename="best_model.joblib", # model file name
|
| 14 |
repo_type="model"
|
| 15 |
)
|
| 16 |
model = joblib.load(model_path)
|
| 17 |
|
| 18 |
+
# Input schema for each record
|
| 19 |
class CanteenInput(BaseModel):
|
| 20 |
canteen_id: int
|
| 21 |
day: int
|
|
|
|
| 23 |
year: int
|
| 24 |
day_of_week: int
|
| 25 |
|
| 26 |
+
# Request body schema
|
| 27 |
class RequestBody(BaseModel):
|
| 28 |
data: list[CanteenInput]
|
| 29 |
|
| 30 |
+
# Root route for quick health check
|
| 31 |
@app.get("/")
|
| 32 |
def home():
|
| 33 |
return {"message": "Food Surplus Prediction API is running!"}
|
| 34 |
|
| 35 |
+
# Prediction route
|
| 36 |
@app.post("/predict")
|
| 37 |
def predict_surplus(request: RequestBody):
|
| 38 |
+
# Convert request data into DataFrame
|
| 39 |
df = pd.DataFrame([canteen.dict() for canteen in request.data])
|
| 40 |
+
|
| 41 |
+
# Reorder columns to match the order used during model training
|
| 42 |
+
df = df[['day', 'month', 'year', 'day_of_week', 'canteen_id']]
|
| 43 |
+
|
| 44 |
+
# Make predictions
|
| 45 |
predictions = model.predict(df)
|
| 46 |
+
|
| 47 |
+
# Append results to DataFrame
|
| 48 |
df["predicted_surplus"] = predictions
|
| 49 |
+
|
| 50 |
+
# Return as JSON
|
| 51 |
return df.to_dict(orient="records")
|