Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +18 -0
- SuperKart_prediction_model_v1_0.joblib +3 -0
- app.py +92 -0
- requirements.txt +7 -0
Dockerfile
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Dockerfile — FastAPI backend for HF Spaces (Docker SDK)
|
| 2 |
+
FROM python:3.10-slim
|
| 3 |
+
|
| 4 |
+
# (Optional) system deps helpful for pandas/numpy wheels
|
| 5 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 6 |
+
build-essential gcc g++ && \
|
| 7 |
+
rm -rf /var/lib/apt/lists/*
|
| 8 |
+
|
| 9 |
+
WORKDIR /app
|
| 10 |
+
|
| 11 |
+
# Install Python dependencies first (better layer caching)
|
| 12 |
+
COPY requirements.txt /app/requirements.txt
|
| 13 |
+
RUN pip install --no-cache-dir --upgrade pip && \
|
| 14 |
+
pip install --no-cache-dir -r requirements.txt
|
| 15 |
+
|
| 16 |
+
# Copy app + model
|
| 17 |
+
COPY app.py /app/app.py
|
| 18 |
+
COPY SuperKart_prediction_model_v1_0.joblib /app/SuperKart_prediction_model_v1_0.joblib
|
SuperKart_prediction_model_v1_0.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:33928b47148fc610cca2d689a1bef609af0dcc231f552e86342f73508e1b3c4a
|
| 3 |
+
size 559087
|
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import io
|
| 2 |
+
import json
|
| 3 |
+
import joblib
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 6 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
+
from pydantic import BaseModel
|
| 8 |
+
|
| 9 |
+
MODEL_PATH = "SuperKart_prediction_model_v1_0.joblib" # <-- update if your filename differs
|
| 10 |
+
|
| 11 |
+
# Expected feature order/names (must match training)
|
| 12 |
+
EXPECTED_COLS = [
|
| 13 |
+
"Product_Weight",
|
| 14 |
+
"Product_Allocated_Area",
|
| 15 |
+
"Product_MRP",
|
| 16 |
+
"Store_Age", # change to Store_Age_Years or Store_Establishment_Year if that's what you trained on
|
| 17 |
+
"Product_Sugar_Content",
|
| 18 |
+
"Product_Type",
|
| 19 |
+
"Store_Type",
|
| 20 |
+
"Store_Size",
|
| 21 |
+
"Store_Location_City_Type",
|
| 22 |
+
]
|
| 23 |
+
|
| 24 |
+
# ---------- App ----------
|
| 25 |
+
app = FastAPI(title="SuperKart Backend", version="1.0")
|
| 26 |
+
|
| 27 |
+
# CORS (allow any frontend, incl. your Streamlit Space)
|
| 28 |
+
app.add_middleware(
|
| 29 |
+
CORSMiddleware,
|
| 30 |
+
allow_origins=["*"], # tighten for prod
|
| 31 |
+
allow_credentials=True,
|
| 32 |
+
allow_methods=["*"],
|
| 33 |
+
allow_headers=["*"],
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# ---------- Load model at startup ----------
|
| 37 |
+
model = None
|
| 38 |
+
@app.on_event("startup")
|
| 39 |
+
def load_model():
|
| 40 |
+
global model
|
| 41 |
+
model = joblib.load(MODEL_PATH)
|
| 42 |
+
|
| 43 |
+
# ---------- Schemas ----------
|
| 44 |
+
class Payload(BaseModel):
|
| 45 |
+
Product_Weight: float
|
| 46 |
+
Product_Allocated_Area: float
|
| 47 |
+
Product_MRP: float
|
| 48 |
+
Store_Age: int # adjust type/name if needed
|
| 49 |
+
Product_Sugar_Content: str
|
| 50 |
+
Product_Type: str
|
| 51 |
+
Store_Type: str
|
| 52 |
+
Store_Size: int # or str, if you trained as categorical strings
|
| 53 |
+
Store_Location_City_Type: int # or str, adjust to your training
|
| 54 |
+
|
| 55 |
+
# ---------- Helpers ----------
|
| 56 |
+
def validate_and_order(df: pd.DataFrame) -> pd.DataFrame:
|
| 57 |
+
missing = [c for c in EXPECTED_COLS if c not in df.columns]
|
| 58 |
+
if missing:
|
| 59 |
+
raise HTTPException(status_code=422, detail=f"Missing columns: {missing}")
|
| 60 |
+
return df[EXPECTED_COLS].copy()
|
| 61 |
+
|
| 62 |
+
# ---------- Endpoints ----------
|
| 63 |
+
@app.get("/health")
|
| 64 |
+
def health():
|
| 65 |
+
return {"status": "ok"}
|
| 66 |
+
|
| 67 |
+
@app.post("/v1/salesprice")
|
| 68 |
+
def predict_single(payload: Payload):
|
| 69 |
+
try:
|
| 70 |
+
df = pd.DataFrame([payload.dict()])
|
| 71 |
+
X = validate_and_order(df)
|
| 72 |
+
y = model.predict(X)
|
| 73 |
+
return {"Predicted Price": float(y[0])}
|
| 74 |
+
except Exception as e:
|
| 75 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 76 |
+
|
| 77 |
+
@app.post("/v1/salespricebatch")
|
| 78 |
+
def predict_batch(file: UploadFile = File(...)):
|
| 79 |
+
try:
|
| 80 |
+
# read CSV into DataFrame
|
| 81 |
+
content = file.file.read()
|
| 82 |
+
df = pd.read_csv(io.BytesIO(content))
|
| 83 |
+
X = validate_and_order(df)
|
| 84 |
+
y = model.predict(X)
|
| 85 |
+
out = df.copy()
|
| 86 |
+
out["Predicted Price"] = y
|
| 87 |
+
# return as records (JSON)
|
| 88 |
+
return json.loads(out.to_json(orient="records"))
|
| 89 |
+
except HTTPException:
|
| 90 |
+
raise
|
| 91 |
+
except Exception as e:
|
| 92 |
+
raise HTTPException(status_code=500, detail=str(e))
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.111.0
|
| 2 |
+
uvicorn==0.30.3
|
| 3 |
+
pandas==2.2.2
|
| 4 |
+
numpy==2.0.2
|
| 5 |
+
scikit-learn==1.6.1
|
| 6 |
+
xgboost==2.1.4
|
| 7 |
+
joblib==1.4.2
|