|
|
| |
| 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) |
|
|
| |
| 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}") |
|
|
| |
| 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}") |
|
|
| |
| 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"], |
| } |
|
|
| |
| for pt in ALL_PRODUCT_TYPES: |
| sample[f"Product_Type_{pt}"] = 1 if data["Product_Type"] == pt else 0 |
|
|
| |
| 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) |
|
|