tifischer's picture
Upload folder using huggingface_hub
0c723f6 verified
# Import necessary libraries
import numpy as np
import joblib
import pandas as pd
from flask import Flask, request, jsonify
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
# ── Load model ─────────────────────────────────────────────────────────────────
try:
model = joblib.load("smartkart_model_v1_0.joblib")
print("βœ… Model loaded successfully.")
except Exception as e:
model = None
print(f"❌ Model failed to load: {e}")
# ── Category lists (must match training data exactly) ──────────────────────────
ALL_PRODUCT_TYPES = [
"Baking Goods", "Breads", "Breakfast", "Canned", "Dairy",
"Frozen Foods", "Fruits and Vegetables", "Hard Drinks",
"Health and Hygiene", "Household", "Meat", "Others",
"Seafood", "Snack Foods", "Soft Drinks", "Starchy Foods"
]
ALL_STORE_TYPES = [
"Departmental Store", "Food Mart",
"Supermarket Type1", "Supermarket Type2"
]
@app.get("/")
def home():
return "Welcome to the SmartKart Total Sales Prediction API!"
@app.post("/v1/predict")
def predict_total_sales():
data = request.get_json(silent=True)
print(f"πŸ“₯ Received payload: {data}")
# ── Numeric + ordinal fields ───────────────────────────────────────────────
sample = {
"Product_Weight": data["Product_Weight"],
"Product_Allocated_Area": data["Product_Allocated_Area"],
"Product_MRP": data["Product_MRP"],
"Store_Age": data["Store_Age"],
"Product_Sugar_Content_Ord": data["Product_Sugar_Content_Ord"],
"Store_Size_Ord": data["Store_Size_Ord"],
"Store_Location_City_Type_Ord": data["Store_Location_City_Type_Ord"],
}
# ── One-hot encode Product_Type ────────────────────────────────────────────
for pt in ALL_PRODUCT_TYPES:
sample[f"Product_Type_{pt}"] = 1 if data["Product_Type"] == pt else 0
# ── One-hot encode Store_Type ──────────────────────────────────────────────
for st in ALL_STORE_TYPES:
sample[f"Store_Type_{st}"] = 1 if data["Store_Type"] == st else 0
input_df = pd.DataFrame([sample])
print(f"πŸ“Š Input DataFrame columns: {input_df.columns.tolist()}")
predicted_sales = round(float(model.predict(input_df)[0]), 2)
print(f"βœ… Prediction: {predicted_sales}")
return jsonify({"Predicted_Total_Sales": predicted_sales})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860)