riverosS's picture
Upload folder using huggingface_hub
a631685 verified
import os
import numpy as np
import joblib
import pandas as pd
from flask import Flask, request, jsonify
# Initialize Flask app
superkart_api = Flask("superkart_api")
# Build model path relative to this file's directory
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
MODEL_PATH = os.path.join(BASE_DIR, "bagging_tuned_model.joblib")
# Load the trained sales forecast model
model = joblib.load(MODEL_PATH)
@superkart_api.get("/")
def home():
return "Welcome to the SuperKart Sales Predict API!"
@superkart_api.post("/v1/predict")
def predict_sales():
# Get JSON payload
data = request.get_json()
# Build a row matching the training feature schema
sample = {
"Product_Weight": data["Product_Weight"],
"Product_Sugar_Content": data["Product_Sugar_Content"],
"Product_Allocated_Area": data["Product_Allocated_Area"],
"Product_MRP": data["Product_MRP"],
"Store_Size": data["Store_Size"],
"Store_Location_City_Type": data["Store_Location_City_Type"],
"Store_Type": data["Store_Type"],
"Product_Id_char": data["Product_Id_char"],
"Store_Age_Years": data["Store_Age_Years"],
"Product_Type_Category": data["Product_Type_Category"],
}
# Convert to DataFrame
input_df = pd.DataFrame([sample])
# Predict sales
prediction = model.predict(input_df)[0]
# Return JSON
return jsonify({"Sales": float(prediction)})
if __name__ == "__main__":
# Local debug mode
superkart_api.run(host="0.0.0.0", port=7860, debug=True)