| from flask import Flask, request, jsonify |
| import pandas as pd |
| import joblib |
| import datetime |
|
|
| |
| model = joblib.load("superkart_sales_predictor.joblib") |
|
|
| app = Flask(__name__) |
|
|
| |
| final_columns = [ |
| 'Product_Id', |
| 'Store_Id', |
| 'Product_NumId', |
| 'Product_Code', |
| 'Store_Establishment_Year', |
| 'Product_Weight', |
| 'Product_MRP', |
| 'Product_Allocated_Area', |
| 'Product_Type', |
| 'Store_Size', |
| 'Store_Location_City_Type', |
| 'Store_Type', |
| 'Product_Sugar_Content', |
| 'Store_Age', |
| 'Is_No_Sugar' |
| ] |
|
|
| |
| store_size_map = {"Small": 0, "Medium": 1, "High": 2} |
| store_location_map = {"Tier 1": 0, "Tier 2": 1, "Tier 3": 2} |
| store_type_map = { |
| "Grocery Store": 0, |
| "Supermarket Type1": 1, |
| "Supermarket Type2": 2, |
| "Supermarket Type3": 3 |
| } |
| product_type_map = { |
| "Dairy": 0, |
| "Soft Drinks": 1, |
| "Meat": 2, |
| "Fruits and Vegetables": 3, |
| "Household": 4, |
| "Baking Goods": 5, |
| "Snack Foods": 6, |
| "Frozen Foods": 7, |
| "Breakfast": 8, |
| "Health and Hygiene": 9, |
| "Hard Drinks": 10, |
| "Canned": 11, |
| "Breads": 12, |
| "Starchy Foods": 13, |
| "Others": 14 |
| } |
| sugar_content_map = {"No Sugar": 0, "Low": 1, "Regular": 2} |
|
|
| @app.route("/") |
| def home(): |
| return jsonify({"message": "SuperKart Sales Predictor API is running!"}) |
|
|
| @app.route("/predict", methods=["POST"]) |
| def predict(): |
| try: |
| |
| data = request.json |
| df = pd.DataFrame([data]) |
|
|
| |
| df["Product_Id"] = 1 |
| df["Store_Id"] = 1 |
| df["Product_NumId"] = df.index + 1 |
| df["Product_Code"] = df.index + 100 |
|
|
| |
| current_year = datetime.datetime.now().year |
| df["Store_Age"] = current_year - df["Store_Establishment_Year"] |
|
|
| |
| df["Store_Size"] = df["Store_Size"].map(store_size_map) |
| df["Store_Location_City_Type"] = df["Store_Location_City_Type"].map(store_location_map) |
| df["Store_Type"] = df["Store_Type"].map(store_type_map) |
| df["Product_Type"] = df["Product_Type"].map(product_type_map) |
| df["Product_Sugar_Content"] = df["Product_Sugar_Content"].map(sugar_content_map) |
|
|
| |
| df.fillna(-1, inplace=True) |
|
|
| |
| df["Is_No_Sugar"] = df["Product_Sugar_Content"].apply(lambda x: 1 if x == 0 else 0) |
|
|
| |
| df = df[final_columns] |
|
|
| |
| prediction = model.predict(df)[0] |
|
|
| return jsonify({"prediction": float(prediction)}) |
|
|
| except Exception as e: |
| return jsonify({"error": str(e)}) |
|
|
|
|
| if __name__ == "__main__": |
| app.run(host="0.0.0.0", port=7860) |
|
|