Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -10,110 +10,81 @@ superkart_api = Flask("SuperKart Sales Predictor")
|
|
| 10 |
# Load the trained machine learning model
|
| 11 |
model = joblib.load("superkart_sales_prediction_model_v1_0.joblib")
|
| 12 |
|
| 13 |
-
# -------------------------------------------------------------------
|
| 14 |
-
# 🧩 Helper: Ensure All Required Columns Are Present
|
| 15 |
-
# -------------------------------------------------------------------
|
| 16 |
-
def preprocess_input(df: pd.DataFrame) -> pd.DataFrame:
|
| 17 |
-
"""
|
| 18 |
-
Ensures that all features expected by the model exist.
|
| 19 |
-
Automatically computes missing engineered columns if possible.
|
| 20 |
-
"""
|
| 21 |
-
df = df.copy()
|
| 22 |
-
|
| 23 |
-
# ✅ If Store_Age is missing but Store_Establishment_Year exists
|
| 24 |
-
if "Store_Age" not in df.columns and "Store_Establishment_Year" in df.columns:
|
| 25 |
-
current_year = datetime.datetime.now().year
|
| 26 |
-
df["Store_Age"] = current_year - df["Store_Establishment_Year"]
|
| 27 |
-
|
| 28 |
-
# ✅ Fill any completely missing numeric columns with 0 (safety net)
|
| 29 |
-
for col in ["Product_Weight", "Product_Allocated_Area", "Product_MRP", "Store_Age"]:
|
| 30 |
-
if col not in df.columns:
|
| 31 |
-
df[col] = 0.0
|
| 32 |
-
|
| 33 |
-
# ✅ For categorical fields missing in CSV, fill with a default
|
| 34 |
-
cat_defaults = {
|
| 35 |
-
"Product_Sugar_Content": "Regular",
|
| 36 |
-
"Product_Type": "Others",
|
| 37 |
-
"Store_Size": "Medium",
|
| 38 |
-
"Store_Location_City_Type": "Tier 2",
|
| 39 |
-
"Store_Type": "Supermarket Type1",
|
| 40 |
-
"Store_Id": "ST001"
|
| 41 |
-
}
|
| 42 |
-
for col, val in cat_defaults.items():
|
| 43 |
-
if col not in df.columns:
|
| 44 |
-
df[col] = val
|
| 45 |
-
|
| 46 |
-
return df
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
# 🔹 Home Route
|
| 50 |
-
# -------------------------------------------------------------------
|
| 51 |
@superkart_api.get('/')
|
| 52 |
def home():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
return "Welcome to the SuperKart Sales Prediction API!"
|
| 54 |
|
| 55 |
-
|
| 56 |
-
#
|
| 57 |
-
# -------------------------------------------------------------------
|
| 58 |
@superkart_api.post('/v1/sales')
|
| 59 |
def predict_sales():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
try:
|
|
|
|
| 61 |
data = request.get_json()
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
sample = {
|
| 64 |
-
"Product_Weight": data
|
| 65 |
-
"Product_Allocated_Area": data
|
| 66 |
-
"Product_MRP": data
|
| 67 |
-
"Store_Age": data
|
| 68 |
-
"Product_Sugar_Content": data
|
| 69 |
-
"Product_Type": data
|
| 70 |
-
"Store_Size": data
|
| 71 |
-
"Store_Location_City_Type": data
|
| 72 |
-
"Store_Type": data
|
| 73 |
-
"Store_Id": data
|
| 74 |
}
|
| 75 |
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
prediction = model.predict(input_df)[0]
|
|
|
|
|
|
|
| 78 |
predicted_sales = round(float(prediction), 2)
|
| 79 |
|
|
|
|
| 80 |
return jsonify({"Predicted_Sales_Total": predicted_sales})
|
| 81 |
|
| 82 |
except Exception as e:
|
|
|
|
| 83 |
return jsonify({"error": str(e)}), 500
|
| 84 |
|
| 85 |
-
# -------------------------------------------------------------------
|
| 86 |
-
# 🔹 Batch Prediction
|
| 87 |
-
# -------------------------------------------------------------------
|
| 88 |
-
@superkart_api.post('/v1/salesbatch')
|
| 89 |
-
def predict_sales_batch():
|
| 90 |
-
"""
|
| 91 |
-
Handles batch prediction from a CSV file.
|
| 92 |
-
Automatically computes Store_Age and validates columns.
|
| 93 |
-
"""
|
| 94 |
-
try:
|
| 95 |
-
file = request.files['file']
|
| 96 |
-
batch_data = pd.read_csv(file)
|
| 97 |
-
|
| 98 |
-
# 🧩 Preprocess to ensure consistency
|
| 99 |
-
batch_data = preprocess_input(batch_data)
|
| 100 |
-
|
| 101 |
-
# Predict
|
| 102 |
-
predictions = model.predict(batch_data)
|
| 103 |
-
batch_data["Predicted_Sales_Total"] = np.round(predictions, 2)
|
| 104 |
-
|
| 105 |
-
# Return results mapped to Product_Id or index
|
| 106 |
-
id_col = "Product_Id" if "Product_Id" in batch_data.columns else batch_data.index.astype(str)
|
| 107 |
-
result_dict = dict(zip(id_col, batch_data["Predicted_Sales_Total"]))
|
| 108 |
-
|
| 109 |
-
# Use jsonify to ensure valid JSON serialization
|
| 110 |
-
return jsonify(result_dict)
|
| 111 |
-
|
| 112 |
-
except Exception as e:
|
| 113 |
-
return jsonify({"error": str(e)}), 500
|
| 114 |
|
| 115 |
-
#
|
| 116 |
-
# 🔹 Run (Local)
|
| 117 |
-
# -------------------------------------------------------------------
|
| 118 |
if __name__ == '__main__':
|
| 119 |
superkart_api.run(debug=True)
|
|
|
|
| 10 |
# Load the trained machine learning model
|
| 11 |
model = joblib.load("superkart_sales_prediction_model_v1_0.joblib")
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# Define a route for the home page (GET request)
|
|
|
|
|
|
|
| 15 |
@superkart_api.get('/')
|
| 16 |
def home():
|
| 17 |
+
"""
|
| 18 |
+
This function handles GET requests to the root URL ('/') of the API.
|
| 19 |
+
It returns a simple welcome message.
|
| 20 |
+
"""
|
| 21 |
return "Welcome to the SuperKart Sales Prediction API!"
|
| 22 |
|
| 23 |
+
|
| 24 |
+
# Define an endpoint for single property prediction (POST request)
|
|
|
|
| 25 |
@superkart_api.post('/v1/sales')
|
| 26 |
def predict_sales():
|
| 27 |
+
"""
|
| 28 |
+
POST endpoint to predict sales for a single product-store combination.
|
| 29 |
+
Expects JSON input with product and store attributes.
|
| 30 |
+
"""
|
| 31 |
try:
|
| 32 |
+
# Get the JSON data from the request body
|
| 33 |
data = request.get_json()
|
| 34 |
+
print("Raw incoming data:", data)
|
| 35 |
+
|
| 36 |
+
# Validate expected fields
|
| 37 |
+
required_fields = [
|
| 38 |
+
"Product_Weight",
|
| 39 |
+
"Product_Allocated_Area",
|
| 40 |
+
"Product_MRP",
|
| 41 |
+
"Store_Age",
|
| 42 |
+
"Product_Sugar_Content",
|
| 43 |
+
"Product_Type",
|
| 44 |
+
"Store_Size",
|
| 45 |
+
"Store_Location_City_Type",
|
| 46 |
+
"Store_Type",
|
| 47 |
+
"Store_Id"
|
| 48 |
+
]
|
| 49 |
+
missing_fields = [f for f in required_fields if f not in data]
|
| 50 |
+
if missing_fields:
|
| 51 |
+
return jsonify({
|
| 52 |
+
"error": f"Missing required fields: {missing_fields}"
|
| 53 |
+
}), 400
|
| 54 |
+
|
| 55 |
+
# Extract relevant features from the JSON data
|
| 56 |
sample = {
|
| 57 |
+
"Product_Weight": data["Product_Weight"],
|
| 58 |
+
"Product_Allocated_Area": data["Product_Allocated_Area"],
|
| 59 |
+
"Product_MRP": data["Product_MRP"],
|
| 60 |
+
"Store_Age": data["Store_Age"],
|
| 61 |
+
"Product_Sugar_Content": data["Product_Sugar_Content"],
|
| 62 |
+
"Product_Type": data["Product_Type"],
|
| 63 |
+
"Store_Size": data["Store_Size"],
|
| 64 |
+
"Store_Location_City_Type": data["Store_Location_City_Type"],
|
| 65 |
+
"Store_Type": data["Store_Type"],
|
| 66 |
+
"Store_Id": data["Store_Id"]
|
| 67 |
}
|
| 68 |
|
| 69 |
+
# Prepare data for prediction
|
| 70 |
+
sample = {f: data[f] for f in required_fields}
|
| 71 |
+
# Convert the extracted data into a Pandas DataFrame
|
| 72 |
+
input_df = pd.DataFrame([sample])
|
| 73 |
+
|
| 74 |
+
# Make prediction
|
| 75 |
prediction = model.predict(input_df)[0]
|
| 76 |
+
|
| 77 |
+
# Convert predicted_price to Python float
|
| 78 |
predicted_sales = round(float(prediction), 2)
|
| 79 |
|
| 80 |
+
# Return the predicted sales
|
| 81 |
return jsonify({"Predicted_Sales_Total": predicted_sales})
|
| 82 |
|
| 83 |
except Exception as e:
|
| 84 |
+
# Catch all errors and return them as JSON
|
| 85 |
return jsonify({"error": str(e)}), 500
|
| 86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
# Run the Flask application in debug mode if this script is executed directly
|
|
|
|
|
|
|
| 89 |
if __name__ == '__main__':
|
| 90 |
superkart_api.run(debug=True)
|