Download main.py from Yashk0618/projectY_Classifier_Regressor: direct link, hf CLI and curl.
- Browser
- Download file 1.53 kB
-
https://huggingface.co/spaces/Yashk0618/projectY_Classifier_Regressor/resolve/main/main.py
- Command line
-
hf download hf://spaces/Yashk0618/projectY_Classifier_Regressor/main.py
-
curl -L -o main.py https://huggingface.co/spaces/Yashk0618/projectY_Classifier_Regressor/resolve/main/main.py
1.53 kB
| 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 -------- | |
| def root(): | |
| return { | |
| "status": "ok", | |
| "message": "ProjectY Classifier + Regressor API is running" | |
| } | |
| # -------- Stockout classifier -------- | |
| 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 -------- | |
| def predict_wastage(req: PredictRequest): | |
| X = np.array([req.features]) | |
| prediction = wastage_model.predict(X)[0] | |
| return { | |
| "prediction": float(prediction) | |
| } | |