Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,99 +1,119 @@
|
|
| 1 |
-
#
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
import pandas as pd # For data manipulation
|
| 5 |
-
from flask import Flask, request, jsonify # For creating the Flask API
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
superkart_sales_api = Flask("SuperKart Sales Predictor")
|
| 11 |
-
print("--- app.py: Flask app initialized ---")
|
| 12 |
|
| 13 |
-
#
|
|
|
|
|
|
|
|
|
|
| 14 |
try:
|
| 15 |
model = joblib.load("superkart_sales_model.pkl")
|
| 16 |
-
print("
|
| 17 |
except Exception as e:
|
| 18 |
-
print(
|
| 19 |
-
raise
|
| 20 |
-
|
| 21 |
-
#
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
def home():
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
#
|
| 32 |
-
@
|
| 33 |
def predict_sales():
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
# Define an endpoint for batch prediction (POST request)
|
| 72 |
-
@superkart_sales_api.post('/v1/salesbatch')
|
| 73 |
def predict_sales_batch():
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ===============================
|
| 2 |
+
# SuperKart Sales Prediction API
|
| 3 |
+
# ===============================
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
import joblib
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from flask import Flask, request, jsonify
|
| 8 |
|
| 9 |
+
print("Starting SuperKart Sales Prediction API...")
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Initialize Flask app
|
| 12 |
+
app = Flask(__name__)
|
| 13 |
+
|
| 14 |
+
# Load trained model
|
| 15 |
try:
|
| 16 |
model = joblib.load("superkart_sales_model.pkl")
|
| 17 |
+
print("Model loaded successfully")
|
| 18 |
except Exception as e:
|
| 19 |
+
print("ERROR loading model:", e)
|
| 20 |
+
raise
|
| 21 |
+
|
| 22 |
+
# Required columns expected by the trained pipeline
|
| 23 |
+
REQUIRED_COLUMNS = [
|
| 24 |
+
"Product_Weight",
|
| 25 |
+
"Product_Allocated_Area",
|
| 26 |
+
"Product_MRP",
|
| 27 |
+
"Store_Current_Age",
|
| 28 |
+
"Product_Sugar_Content",
|
| 29 |
+
"Store_Id",
|
| 30 |
+
"Store_Size",
|
| 31 |
+
"Store_Location_City_Type",
|
| 32 |
+
"Store_Type",
|
| 33 |
+
"Product_Id",
|
| 34 |
+
"Product_Type"
|
| 35 |
+
]
|
| 36 |
+
|
| 37 |
+
# -------------------------------
|
| 38 |
+
# Health Check / Home Route
|
| 39 |
+
# -------------------------------
|
| 40 |
+
@app.get("/")
|
| 41 |
def home():
|
| 42 |
+
return jsonify({
|
| 43 |
+
"message": "SuperKart Sales Prediction API is running",
|
| 44 |
+
"status": "OK"
|
| 45 |
+
})
|
| 46 |
+
|
| 47 |
+
# -------------------------------
|
| 48 |
+
# Single Prediction Endpoint
|
| 49 |
+
# -------------------------------
|
| 50 |
+
@app.post("/v1/sales")
|
| 51 |
def predict_sales():
|
| 52 |
+
try:
|
| 53 |
+
input_data = request.get_json()
|
| 54 |
+
|
| 55 |
+
if not input_data:
|
| 56 |
+
return jsonify({"error": "Invalid or empty JSON payload"}), 400
|
| 57 |
+
|
| 58 |
+
# Convert to DataFrame
|
| 59 |
+
df = pd.DataFrame([input_data])
|
| 60 |
+
|
| 61 |
+
# Ensure all required columns exist
|
| 62 |
+
for col in REQUIRED_COLUMNS:
|
| 63 |
+
if col not in df.columns:
|
| 64 |
+
df[col] = None # handled by imputers
|
| 65 |
+
|
| 66 |
+
# Reorder columns to match training
|
| 67 |
+
df = df[REQUIRED_COLUMNS]
|
| 68 |
+
|
| 69 |
+
# Make prediction
|
| 70 |
+
prediction = model.predict(df)[0]
|
| 71 |
+
|
| 72 |
+
return jsonify({
|
| 73 |
+
"Predicted Sales": round(float(prediction), 2)
|
| 74 |
+
})
|
| 75 |
+
|
| 76 |
+
except Exception as e:
|
| 77 |
+
print("Prediction error:", str(e))
|
| 78 |
+
return jsonify({
|
| 79 |
+
"error": "Prediction failed",
|
| 80 |
+
"details": str(e)
|
| 81 |
+
}), 500
|
| 82 |
+
|
| 83 |
+
# -------------------------------
|
| 84 |
+
# Batch Prediction Endpoint
|
| 85 |
+
# -------------------------------
|
| 86 |
+
@app.post("/v1/salesbatch")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
def predict_sales_batch():
|
| 88 |
+
try:
|
| 89 |
+
if "file" not in request.files:
|
| 90 |
+
return jsonify({"error": "CSV file not provided"}), 400
|
| 91 |
+
|
| 92 |
+
file = request.files["file"]
|
| 93 |
+
df = pd.read_csv(file)
|
| 94 |
+
|
| 95 |
+
# Ensure required columns
|
| 96 |
+
for col in REQUIRED_COLUMNS:
|
| 97 |
+
if col not in df.columns:
|
| 98 |
+
df[col] = None
|
| 99 |
+
|
| 100 |
+
df = df[REQUIRED_COLUMNS]
|
| 101 |
+
|
| 102 |
+
predictions = model.predict(df)
|
| 103 |
+
|
| 104 |
+
return jsonify({
|
| 105 |
+
"Predicted Sales": [round(float(p), 2) for p in predictions]
|
| 106 |
+
})
|
| 107 |
+
|
| 108 |
+
except Exception as e:
|
| 109 |
+
print("Batch prediction error:", str(e))
|
| 110 |
+
return jsonify({
|
| 111 |
+
"error": "Batch prediction failed",
|
| 112 |
+
"details": str(e)
|
| 113 |
+
}), 500
|
| 114 |
+
|
| 115 |
+
# -------------------------------
|
| 116 |
+
# Local Run (Not used on HF)
|
| 117 |
+
# -------------------------------
|
| 118 |
+
if __name__ == "__main__":
|
| 119 |
+
app.run(dedug=True)
|