Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,7 +14,7 @@ model_path = hf_hub_download(
|
|
| 14 |
)
|
| 15 |
model = joblib.load(model_path)
|
| 16 |
|
| 17 |
-
# Define schema
|
| 18 |
class CanteenInput(BaseModel):
|
| 19 |
canteen_id: str
|
| 20 |
canteen_name: str
|
|
@@ -38,16 +38,31 @@ def predict_surplus(request: RequestBody):
|
|
| 38 |
# One-hot encode categorical columns
|
| 39 |
df_encoded = pd.get_dummies(df, columns=["canteen_id", "canteen_name"])
|
| 40 |
|
| 41 |
-
# Align columns with model
|
| 42 |
-
model_features =
|
| 43 |
if model_features:
|
| 44 |
for col in model_features:
|
| 45 |
if col not in df_encoded.columns:
|
| 46 |
df_encoded[col] = 0
|
| 47 |
df_encoded = df_encoded[model_features]
|
| 48 |
|
| 49 |
-
#
|
| 50 |
predictions = model.predict(df_encoded)
|
| 51 |
df["predicted_surplus"] = predictions
|
| 52 |
|
| 53 |
return df.to_dict(orient="records")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
)
|
| 15 |
model = joblib.load(model_path)
|
| 16 |
|
| 17 |
+
# Define schema for input
|
| 18 |
class CanteenInput(BaseModel):
|
| 19 |
canteen_id: str
|
| 20 |
canteen_name: str
|
|
|
|
| 38 |
# One-hot encode categorical columns
|
| 39 |
df_encoded = pd.get_dummies(df, columns=["canteen_id", "canteen_name"])
|
| 40 |
|
| 41 |
+
# Align columns with model features
|
| 42 |
+
model_features = getattr(model, "feature_names_", None)
|
| 43 |
if model_features:
|
| 44 |
for col in model_features:
|
| 45 |
if col not in df_encoded.columns:
|
| 46 |
df_encoded[col] = 0
|
| 47 |
df_encoded = df_encoded[model_features]
|
| 48 |
|
| 49 |
+
# Predict
|
| 50 |
predictions = model.predict(df_encoded)
|
| 51 |
df["predicted_surplus"] = predictions
|
| 52 |
|
| 53 |
return df.to_dict(orient="records")
|
| 54 |
+
|
| 55 |
+
@app.get("/fetch_data")
|
| 56 |
+
def fetch_data(date: str):
|
| 57 |
+
"""
|
| 58 |
+
Temporary endpoint so your frontend doesn't explode.
|
| 59 |
+
Replace this with an actual DB lookup later if you want real data.
|
| 60 |
+
"""
|
| 61 |
+
# You can later connect this to your stored predictions or history table.
|
| 62 |
+
sample_response = {
|
| 63 |
+
"date": date,
|
| 64 |
+
"canteen_id": "C002",
|
| 65 |
+
"canteen_name": "Anna University Mess",
|
| 66 |
+
"predicted_surplus": 24.0
|
| 67 |
+
}
|
| 68 |
+
return sample_response
|