Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +15 -0
- SuperKart_clf_model_v1_0.joblib +3 -0
- SuperKart_reg_model_v1_0.joblib +3 -0
- app.py +106 -0
- requirements.txt +9 -0
- util.py +8 -0
Dockerfile
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
FROM python:3.12.10-slim-bookworm
|
| 3 |
+
|
| 4 |
+
WORKDIR /app
|
| 5 |
+
|
| 6 |
+
COPY app.py .
|
| 7 |
+
COPY requirements.txt .
|
| 8 |
+
COPY util.py ./backend/
|
| 9 |
+
COPY SuperKart_clf_model_v1_0.joblib .
|
| 10 |
+
COPY SuperKart_reg_model_v1_0.joblib .
|
| 11 |
+
|
| 12 |
+
RUN python -m pip install --upgrade pip
|
| 13 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 14 |
+
|
| 15 |
+
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app:app"]
|
SuperKart_clf_model_v1_0.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ec11f58ed68ae52a7fda7fdd111f719e720be9b7b777974cb727c477a63068fc
|
| 3 |
+
size 479126
|
SuperKart_reg_model_v1_0.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3da42fd9b3a16c5fde1f63acc730d8b961e8538de420cf3188b3472f362eacf3
|
| 3 |
+
size 9613354
|
app.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import os
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from typing import Any
|
| 5 |
+
from flask import Flask, request, jsonify
|
| 6 |
+
from pydantic import BaseModel, Field, ValidationError, model_validator
|
| 7 |
+
from backend.util import num_features_selector, cat_features_selector, store_age
|
| 8 |
+
|
| 9 |
+
import joblib
|
| 10 |
+
clf = joblib.load("SuperKart_clf_model_v1_0.joblib")
|
| 11 |
+
reg = joblib.load("SuperKart_reg_model_v1_0.joblib")
|
| 12 |
+
|
| 13 |
+
app = Flask("SuperKart: Revenue Forecasting")
|
| 14 |
+
|
| 15 |
+
@app.get("/")
|
| 16 |
+
def liveProbe():
|
| 17 |
+
return app.name
|
| 18 |
+
|
| 19 |
+
class ReqSchema(BaseModel):
|
| 20 |
+
Store_Size: str = Field(..., description="Size of the store")
|
| 21 |
+
Store_Type: str = Field(..., description="Type of the store")
|
| 22 |
+
Product_Type: str = Field(..., description="Type of the product")
|
| 23 |
+
Product_Weight: float = Field(..., description="Weight of the product", gt=0)
|
| 24 |
+
Product_MRP: float = Field(..., description="Maximum Retail Price of the product")
|
| 25 |
+
Store_Establishment_Year: int = Field(..., description="Store Establishment year")
|
| 26 |
+
Product_Allocated_Area: float = Field(..., description="Allocated area for the product")
|
| 27 |
+
Store_Location_City_Type: str = Field(..., description="Location city type of the store")
|
| 28 |
+
|
| 29 |
+
Store_Age: int = Field(..., description="Age of the store", exclude=True)
|
| 30 |
+
|
| 31 |
+
@model_validator(mode="before")
|
| 32 |
+
def set_store_age(cls, values: dict[str, Any]) -> dict[str, Any]:
|
| 33 |
+
establishment_year = values.get('Store_Establishment_Year')
|
| 34 |
+
values['Store_Age'] = store_age(establishment_year)
|
| 35 |
+
return values
|
| 36 |
+
|
| 37 |
+
@app.post("/v1/predict")
|
| 38 |
+
def predict():
|
| 39 |
+
auth = request.headers.get('Authorization')
|
| 40 |
+
if auth != os.environ['auth_key']:
|
| 41 |
+
return jsonify({"error": "Unauthorized"}), 401
|
| 42 |
+
elif clf is None or reg is None:
|
| 43 |
+
return jsonify({"error": "Prediction service is unavailable. Model(s) failed to load."}), 503
|
| 44 |
+
|
| 45 |
+
try:
|
| 46 |
+
payload = request.get_json()
|
| 47 |
+
reqData = ReqSchema(**payload)
|
| 48 |
+
except ValidationError as e:
|
| 49 |
+
return jsonify({"error": e.errors()}), 400
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return jsonify({"error": f"Invalid request payload: {e}"}), 400
|
| 52 |
+
|
| 53 |
+
try:
|
| 54 |
+
data = pd.DataFrame([{
|
| 55 |
+
'Product_Weight': reqData.Product_Weight,
|
| 56 |
+
'Product_Allocated_Area': reqData.Product_Allocated_Area,
|
| 57 |
+
'Product_Type': reqData.Product_Type,
|
| 58 |
+
'Product_MRP': reqData.Product_MRP,
|
| 59 |
+
'Store_Size': reqData.Store_Size,
|
| 60 |
+
'Store_Type': reqData.Store_Type,
|
| 61 |
+
'Store_Location_City_Type': reqData.Store_Location_City_Type,
|
| 62 |
+
'Store_Age': reqData.Store_Age,
|
| 63 |
+
}])
|
| 64 |
+
|
| 65 |
+
pred_label = clf.predict(data).tolist()[0]
|
| 66 |
+
pred_value = reg.predict(data).tolist()[0]
|
| 67 |
+
print(pred_label, pred_value)
|
| 68 |
+
return jsonify({'performance': pred_label, 'revenue': round(pred_value, 2)})
|
| 69 |
+
except Exception as e:
|
| 70 |
+
return jsonify({"error": f"Prediction failed: {e}"}), 500
|
| 71 |
+
|
| 72 |
+
@app.post("/v1/predict/bulk")
|
| 73 |
+
def predictBulk():
|
| 74 |
+
auth = request.headers.get('Authorization')
|
| 75 |
+
if auth != os.environ['auth_key']:
|
| 76 |
+
return jsonify({"error": "Unauthorized"}), 401
|
| 77 |
+
elif clf is None or reg is None:
|
| 78 |
+
return jsonify({"error": "Prediction service is unavailable. Model(s) failed to load."}), 503
|
| 79 |
+
elif 'file' not in request.files:
|
| 80 |
+
return jsonify({"error": "No file uploaded"}), 400
|
| 81 |
+
|
| 82 |
+
file = request.files['file']
|
| 83 |
+
try:
|
| 84 |
+
if file.filename.endswith(".csv"):
|
| 85 |
+
df = pd.read_csv(file)
|
| 86 |
+
elif file.filename.endswith((".xls", ".xlsx")):
|
| 87 |
+
df = pd.read_excel(file)
|
| 88 |
+
else:
|
| 89 |
+
return jsonify({"error": "Unsupported file format! Upload CSV or Excel"}), 400
|
| 90 |
+
|
| 91 |
+
reqCols = ['Product_Weight', 'Product_Allocated_Area', 'Product_Type', 'Product_MRP', 'Store_Size', 'Store_Location_City_Type', 'Store_Type', 'Store_Establishment_Year']
|
| 92 |
+
missingCols = [col for col in reqCols if col not in df.columns]
|
| 93 |
+
if missingCols:
|
| 94 |
+
return jsonify({"error": f"Missing columns: {missingCols}"}), 400
|
| 95 |
+
|
| 96 |
+
X = df[reqCols].copy()
|
| 97 |
+
X["Store_Age"] = X["Store_Establishment_Year"].apply(store_age)
|
| 98 |
+
df["Sales_Performance"] = clf.predict(X)
|
| 99 |
+
df["Sales_Revenue"] = reg.predict(X)
|
| 100 |
+
|
| 101 |
+
return jsonify(df.to_dict(orient="records"))
|
| 102 |
+
except Exception as e:
|
| 103 |
+
return jsonify({"error": f"Bulk prediction failed: {e}"}), 500
|
| 104 |
+
|
| 105 |
+
if __name__ == '__main__':
|
| 106 |
+
app.run(debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy==2.1.2
|
| 2 |
+
pandas==2.3.1
|
| 3 |
+
joblib==1.5.1
|
| 4 |
+
flask==3.1.2
|
| 5 |
+
Werkzeug==3.1.3
|
| 6 |
+
gunicorn==23.0.0
|
| 7 |
+
pydantic==2.11.7
|
| 8 |
+
uvicorn[standard]
|
| 9 |
+
scikit-learn==1.7.1
|
util.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
def num_features_selector(X):
|
| 4 |
+
return X.select_dtypes(include=np.number).columns.to_list()
|
| 5 |
+
def cat_features_selector(X):
|
| 6 |
+
return X.select_dtypes(include=['object', 'category']).columns.to_list()
|
| 7 |
+
def store_age(establishmentYear):
|
| 8 |
+
return datetime.now().year - establishmentYear
|